-
Notifications
You must be signed in to change notification settings - Fork 0
/
OS-CHOOSER.sh
95 lines (83 loc) · 3.06 KB
/
OS-CHOOSER.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/bash
#set -x
#set -e
#mount -t proc proc /proc
#sleep 10
#mount -t sysfs sys /sys
#mount -t tmpfs tmp /tmp
#mount /boot
#mount / -o remount,ro
USB_BLOCK_DEVICE=/dev/sda
SACRIFICIAL_BOOTPARTITION_NUMBER=2
SACRIFICIAL_BOOTPARTITION="${USB_BLOCK_DEVICE}${SACRIFICIAL_BOOTPARTITION_NUMBER}"
TEMP_DIR=/mnt
SACRIFICIAL_MOUNT_DIR="${TEMP_DIR}/boot"
TARGET_MOUNT_DIR="${TEMP_DIR}/targetboot"
KERNEL_NAME="kernel7l.img"
KERNEL64_NAME="kernel8.img"
CONFIG_NAME="config.txt"
declare -A oslist
options=()
fat_partitions=$(sfdisk -l "$USB_BLOCK_DEVICE" | grep -i microsoft | sed -n '3,$p' | awk '{print $1}' | sed -r 's/[^0-9]*([0-9]+)/\1/')
#fat_partitions=$(parted -l $USB_BLOCK_DEVICE|grep fat|sed -n '3,$p'|awk '{print $1}')
for i in $fat_partitions
do
mount "${USB_BLOCK_DEVICE}$i" $TEMP_DIR
if [ -f "${TEMP_DIR}/NAME" ];
then
os=$(cat "${TEMP_DIR}/NAME")
oslist["$os"]=$i
options+=("$os" "$os")
fi
umount $TEMP_DIR
done
#echo "${!oslist[@]}"
CHOICE=$(whiptail --title "Choose OS" --menu " " --nocancel --noitem 20 70 5 "${options[@]}" 3>&1 1>&2 2>&3)
#echo $CHOICE
#echo ${oslist[$CHOICE]}
bootpart="${oslist[$CHOICE]}"
echo $bootpart
set -x
linuxpart=$(( bootpart + 1 ))
mkdir -p "$SACRIFICIAL_MOUNT_DIR"
mkdir -p "$TARGET_MOUNT_DIR"
mount -o rw "$SACRIFICIAL_BOOTPARTITION" $SACRIFICIAL_MOUNT_DIR
mount -o rw "${USB_BLOCK_DEVICE}${bootpart}" $TARGET_MOUNT_DIR
ESCAPED_USB_BLOCK_DEVICE=$(sed 's/\//\\\//g' <<< $USB_BLOCK_DEVICE)
sed -i -r "s/root=([^ ]*) /root=${ESCAPED_USB_BLOCK_DEVICE}${linuxpart} /" "${SACRIFICIAL_MOUNT_DIR}/cmdline.txt"
cp "${TARGET_MOUNT_DIR}/${CONFIG_NAME}" "$SACRIFICIAL_MOUNT_DIR"
mkdir -p "$SACRIFICIAL_MOUNT_DIR/$bootpart"
chmod +w "${SACRIFICIAL_MOUNT_DIR}/${CONFIG_NAME}"
echo -e 'dtparam=sd_poll_once=on\n' >> "${SACRIFICIAL_MOUNT_DIR}/${CONFIG_NAME}"
echo -e "os_prefix=${bootpart}/\n" >> "${SACRIFICIAL_MOUNT_DIR}/${CONFIG_NAME}"
cp $TARGET_MOUNT_DIR/*.dtb "$SACRIFICIAL_MOUNT_DIR/$bootpart"
if [ -f "$TARGET_MOUNT_DIR/initrd.img" ];
then
cp "$TARGET_MOUNT_DIR/initrd.img" "$SACRIFICIAL_MOUNT_DIR/$bootpart"
fi
if [ -f "$TARGET_MOUNT_DIR/vmlinuz" ];
then
gzip -t "$TARGET_MOUNT_DIR/vmlinuz" 2>/dev/null
if [ $? -eq 0 ];
then
cp "$TARGET_MOUNT_DIR/vmlinuz" "$SACRIFICIAL_MOUNT_DIR/$bootpart"
zcat "$TARGET_MOUNT_DIR/vmlinuz" > "$TARGET_MOUNT_DIR/vmlinux"
fi
cp "$TARGET_MOUNT_DIR/vmlinuz" "$SACRIFICIAL_MOUNT_DIR/$bootpart"
fi
cp $TARGET_MOUNT_DIR/kernel*.img "$SACRIFICIAL_MOUNT_DIR/$bootpart"
cp -r $TARGET_MOUNT_DIR/overlays "$SACRIFICIAL_MOUNT_DIR/$bootpart"
sync
#grep arm_64bit=1 "$TARGET_MOUNT_DIR/config.txt" >/dev/null
#if [ $? -eq 0 ];
#then
# cp "${TARGET_MOUNT_DIR}/${KERNEL64_NAME}" "$SACRIFICIAL_MOUNT_DIR"
# echo -e 'kernel=kernel8.img\n' >> "$SACRIFICIAL_MOUNT_DIR/$CONFIG_NAME"
#else
# cp "${TARGET_MOUNT_DIR}/${KERNEL_NAME}" "$SACRIFICIAL_MOUNT_DIR"
# echo -e 'kernel=kernel7l.img\n' >> "$SACRIFICIAL_MOUNT_DIR/$CONFIG_NAME"
#fi
#sed -i -r "s/kernel=(.*)$/kernel=${bootpart}.img/" "${SACRIFICIAL_MOUNT_DIR}/config.txt"
umount "$SACRIFICIAL_MOUNT_DIR"
umount "$TARGET_MOUNT_DIR"
sudo reboot $SACRIFICIAL_BOOTPARTITION_NUMBER