]> code.delx.au - monosys/blob - multiboot-setup
816e602a42d51e0c60c3449f979e0ccf6c42fba5
[monosys] / multiboot-setup
1 #!/bin/bash
2
3 set -eu
4
5 PARTITION_LABEL="multiboot"
6 MULTIBOOT_MNT="/mnt/multiboot"
7
8 function cmd_format {
9 if [ ! -b "${1:-}" ]; then
10 echo "Usage: $0 format /dev/sdX"
11 exit 1
12 fi
13 set -x
14
15 sudo -k
16 DISK_DEVICE="$1"
17 PARTITION_DEVICE="${DISK_DEVICE}1"
18 echo -ne 'label: dos\ntype=c, bootable\n' | sudo sfdisk "$DISK_DEVICE"
19 sudo mkfs.vfat -n "$PARTITION_LABEL" "$PARTITION_DEVICE"
20 }
21
22 function cmd_grub {
23 DISK_DEVICE="$(findmnt -n -o source "$MULTIBOOT_MNT" | sed 's/[0-9]*$//')"
24 if [ ! -b "$DISK_DEVICE" ]; then
25 echo "ERROR! Could not find disk to install bootloader. Try using mount."
26 exit 1
27 fi
28 set -x
29
30 sudo -k
31 install_grub_bios
32 install_grub_efi
33 install_grub_cfg
34 }
35
36 function cmd_grubcfg {
37 set -x
38 install_grub_cfg
39 }
40
41 function install_grub_bios {
42 sudo grub-install \
43 --target=i386-pc \
44 --boot-directory="$MULTIBOOT_MNT" \
45 "$DISK_DEVICE"
46 }
47
48 function install_grub_efi {
49 for arch in i386-efi x86_64-efi; do
50 sudo grub-install \
51 --target="$arch" \
52 --no-nvram \
53 --removable \
54 --efi-directory="$MULTIBOOT_MNT" \
55 --boot-directory="$MULTIBOOT_MNT" \
56 "$DISK_DEVICE"
57 done
58 }
59
60 function install_grub_cfg {
61 print_grub_cfg | sudo tee "${MULTIBOOT_MNT}/grub/grub.cfg" > /dev/null
62 }
63
64 function cmd_mount {
65 set -x
66
67 PARTITION_DEVICE="$(readlink -f "/dev/disk/by-label/${PARTITION_LABEL}")"
68 sudo mkdir -p "$MULTIBOOT_MNT"
69 while sudo umount "$PARTITION_DEVICE" &> /dev/null; do true; done
70 sudo mount "$PARTITION_DEVICE" "$MULTIBOOT_MNT" -o "uid=$(whoami)"
71 }
72
73 function cmd_umount {
74 set -x
75
76 sudo umount "$MULTIBOOT_MNT"
77 sudo rmdir "$MULTIBOOT_MNT"
78 }
79
80 function cmd_freedos {
81 set -x
82
83 local SYSLINUX_VERSION="6.03"
84 local SYSLINUX_URL="https://www.kernel.org/pub/linux/utils/boot/syslinux/syslinux-${SYSLINUX_VERSION}.tar.gz"
85 local FREEDOS_URL="http://www.freedos.org/download/download/FD12LITE.zip"
86
87 curl -fL "$SYSLINUX_URL" | \
88 tar xz --no-same-owner --strip-components=3 -C "$MULTIBOOT_MNT" \
89 "syslinux-${SYSLINUX_VERSION}/bios/memdisk/memdisk"
90
91 curl -fL "$FREEDOS_URL" > "${MULTIBOOT_MNT}/FD12LITE.zip"
92 }
93
94 function print_grub_cfg {
95 cat <<EOT
96 insmod all_video
97 insmod part_msdos
98 insmod progress
99 insmod regexp
100 search --set=root --label $PARTITION_LABEL
101
102 function setup_arch {
103 menuentry "\$1" {
104 loopback loop \$1
105 linux (loop)/arch/boot/x86_64/vmlinuz img_label=${PARTITION_LABEL} img_loop=\$1 archisobasedir=arch earlymodules=loop
106 initrd (loop)/arch/boot/x86_64/archiso.img
107 }
108 }
109 for iso in /archlinux-*.iso; do
110 if [ -f "\$iso" ]; then
111 setup_arch \$iso
112 fi
113 done
114
115 function setup_debian {
116 menuentry "\$1" {
117 linux \$1/vmlinuz
118 initrd \$1/initrd.gz
119 }
120 }
121 for d in /debian-*-hd-media; do
122 if [ -d "\$d" ]; then
123 setup_debian \$d
124 fi
125 done
126
127 if [ -f /memdisk -a -f /FD12LITE.zip ]; then
128 menuentry /FD12LITE.zip {
129 if [ \${grub_platform} = pc ]; then
130 linux16 /memdisk raw
131 initrd16 /FD12LITE.zip
132 else
133 echo "FreeDOS only works with BIOS boot."
134 sleep 3
135 fi
136 }
137 fi
138
139 function setup_fedora {
140 menuentry "\$1" {
141 loopback loop \$1
142 probe -s iso_label -l (loop)
143 linux (loop)/isolinux/vmlinuz root=live:CDLABEL=\$iso_label rd.live.image quiet iso-scan/filename=\$1
144 initrd (loop)/isolinux/initrd.img
145 }
146 }
147 for iso in /Fedora-Workstation-Live-*.iso; do
148 if [ -f "\$iso" ]; then
149 setup_fedora \$iso
150 fi
151 done
152
153 function setup_ubuntu {
154 menuentry "\$1" {
155 loopback loop \$1
156 linux (loop)/casper/vmlinuz boot=casper quiet iso-scan/filename=\$1
157 initrd (loop)/casper/initrd*
158 }
159 }
160 for iso in /ubuntu-*-desktop-*.iso; do
161 if [ -f "\$iso" ]; then
162 setup_ubuntu \$iso
163 fi
164 done
165
166 EOT
167 }
168
169 CMD="cmd_${1:-}"
170 shift || true
171
172 if [ "$(type -t -- "$CMD")" = "function" ]; then
173 "${CMD}" "$@"
174 else
175 echo "Usage: $0 [format|mount|grub|grubcfg|freedos|umount]"
176 exit 1
177 fi