-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.sh
executable file
·64 lines (53 loc) · 2.14 KB
/
setup.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
TARGET="$1"
if [[ "n$TARGET" = "n" ]]; then
echo "Usage: $0 PATH_TO_USB_MOUNT_POINT"
exit 1
fi
DEVICE=$(grep "$TARGET " /proc/mounts | cut -d' ' -f1) # "$TARGET " incl. space to avoid false positives
if [[ "n$DEVICE" = "n" ]]; then
echo "Not a valid mount point: \"$TARGET\""
echo "Usage: $0 PATH_TO_USB_MOUNT_POINT"
exit 1
fi
lsblk $DEVICE > /dev/null
if [[ $? -ne 0 ]]; then
echo "Directory must be mounted to a block device"
exit 1
fi
DEVICE_NAME=$(basename "$DEVICE")
DEVICE_PATH=$(dirname "$DEVICE")
echo "" | jq .
if [[ $? -ne 0 ]]; then
echo "The jq json parser seems not to be installed"
exit 1
fi
PARENT_DEVICE="$DEVICE_PATH/"$(lsblk --tree --output NAME --json | jq -r '{"children": .["blockdevices"]} | recurse(.children[]; .children != null) | if (.children | any(.["name"] == "'$DEVICE_NAME'")) then .["name"] else null end' | grep -v null)
if [[ "n$PARENT_DEVICE" = "n" ]]; then
PARENT_DEVICE="$DEVICE" # use device itself if it has no parent
fi
LABEL=$(lsblk "$DEVICE" -n --output LABEL)
if [[ "n$DEVICE" = "n" ]]; then
echo "Device \"$DEVICE\" doesn't have a label, please give it one first."
exit 1
fi
# Sanity check before performing any action
echo "Install config file in device \"$DEVICE\" with label \"$LABEL\" mounted at \"$TARGET\""
echo "Install grub-i386 to device \"$PARENT_DEVICE\" and grub-x86_64 to \"$TARGET/EFI\""
read -p "Do you want to continue? [yN]" -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo "Copy files"
cp -avr boot "$TARGET/"
mkdir -p "$TARGET/boot/iso"
echo "Patch files"
sed -e"s/__FS_LABEL__/$LABEL/g" "$TARGET/boot/grub/grub.cfg.in" > "$TARGET/boot/grub/grub.cfg"
echo "Install GRUB"
sudo grub-install --target=x86_64-efi --boot-directory="$TARGET/boot" --efi-directory="$TARGET" --no-floppy --recheck --removable
sudo grub-install --target=i386-pc --boot-directory="$TARGET/boot" --no-floppy --recheck "$PARENT_DEVICE"
echo "Done"
echo ""
# TODO remove edit instruction when grub config discovers images itself
echo "Now put your ISOs in \"$TARGET/boot/iso\" and then edit \"$TARGET/boot/grub/grub.cfg\" accordingly"
echo "Have fun booting :)"
fi