]> code.delx.au - monosys/blob - hacks/multiboot-setup
notes: fix raspi install notes, also @home -> @username
[monosys] / hacks / multiboot-setup
1 #!/bin/bash
2
3 set -eu
4
5 PARTITION_LABEL_ESP="multibt-esp"
6 PARTITION_LABEL_DATA="multiboot"
7 MULTIBOOT_MNT="${MULTIBOOT_MNT:-/mnt/multiboot}"
8
9 function cmd_format {
10 if [ ! -b "${1:-}" ]; then
11 echo "Usage: $0 format /dev/sdX"
12 exit 1
13 fi
14 set -x
15
16 sudo -k
17 DISK_DEVICE="$1"
18 print_sfdisk_command | sudo sfdisk --wipe always --wipe-partitions always "$DISK_DEVICE"
19 udevadm settle
20 sudo mkfs.vfat -n "${PARTITION_LABEL_ESP}" "/dev/disk/by-partlabel/${PARTITION_LABEL_ESP}"
21 sudo mkfs.ext4 -L "${PARTITION_LABEL_DATA}" -E "root_owner=$(id -u):$(id -g)" "/dev/disk/by-partlabel/${PARTITION_LABEL_DATA}"
22 }
23
24 function print_sfdisk_command {
25 cat <<EOT
26 label: gpt
27 unit: sectors
28
29 size=10M, type=uefi, name="$PARTITION_LABEL_ESP"
30 size=1M, type=21686148-6449-6E6F-744E-656564454649
31 type=linux, name="$PARTITION_LABEL_DATA"
32 EOT
33 }
34
35 function cmd_grub {
36 DISK_DEVICE="$(findmnt -n -o source "$MULTIBOOT_MNT" | sed 's/[0-9]*$//')"
37 if [ ! -b "$DISK_DEVICE" ]; then
38 echo "ERROR! Could not find disk to install bootloader. Try using mount."
39 exit 1
40 fi
41 set -x
42
43 sudo -k
44 install_grub_bios
45 install_grub_efi
46 install_grub_cfg
47 }
48
49 function cmd_grubcfg {
50 set -x
51 install_grub_cfg
52 }
53
54 function install_grub_bios {
55 sudo grub-install \
56 --target=i386-pc \
57 --boot-directory="$MULTIBOOT_MNT" \
58 "$DISK_DEVICE"
59 }
60
61 function install_grub_efi {
62 for arch in i386-efi x86_64-efi; do
63 sudo grub-install \
64 --target="$arch" \
65 --no-nvram \
66 --removable \
67 --efi-directory="${MULTIBOOT_MNT}/EFI" \
68 --boot-directory="$MULTIBOOT_MNT" \
69 "$DISK_DEVICE"
70 done
71 }
72
73 function install_grub_cfg {
74 if [[ -w "${MULTIBOOT_MNT}/grub/" ]]; then
75 # We already have write access, no need to use sudo
76 print_grub_cfg > "${MULTIBOOT_MNT}/grub/grub.cfg"
77 else
78 print_grub_cfg | sudo tee "${MULTIBOOT_MNT}/grub/grub.cfg" > /dev/null
79 fi
80 }
81
82 function cmd_mount {
83 set -x
84
85 while sudo umount "/dev/disk/by-partlabel/${PARTITION_LABEL_ESP}" &> /dev/null; do true; done
86 while sudo umount "/dev/disk/by-partlabel/${PARTITION_LABEL_DATA}" &> /dev/null; do true; done
87 sudo mkdir -p "$MULTIBOOT_MNT"
88 sudo mount "/dev/disk/by-partlabel/${PARTITION_LABEL_DATA}" "$MULTIBOOT_MNT"
89 mkdir -p "${MULTIBOOT_MNT}/EFI"
90 sudo mount "/dev/disk/by-partlabel/${PARTITION_LABEL_ESP}" "${MULTIBOOT_MNT}/EFI"
91 }
92
93 function cmd_umount {
94 set -x
95
96 sudo umount "${MULTIBOOT_MNT}/EFI" || true
97 sudo umount "$MULTIBOOT_MNT" || true
98 sudo rmdir "$MULTIBOOT_MNT"
99 }
100
101 function cmd_freedos {
102 set -x
103
104 local SYSLINUX_VERSION="6.03"
105 local SYSLINUX_URL="https://www.kernel.org/pub/linux/utils/boot/syslinux/syslinux-${SYSLINUX_VERSION}.tar.gz"
106 local FREEDOS_URL="https://www.ibiblio.org/pub/micro/pc-stuff/freedos/files/distributions/1.2/official/FD12LITE.zip"
107
108 curl -fL "$SYSLINUX_URL" | \
109 tar xz --no-same-owner --strip-components=3 -C "$MULTIBOOT_MNT" \
110 "syslinux-${SYSLINUX_VERSION}/bios/memdisk/memdisk"
111
112 curl -fL "$FREEDOS_URL" > "${MULTIBOOT_MNT}/FD12LITE.zip"
113 }
114
115 function cmd_memtest {
116 curl -fL -o "${MULTIBOOT_MNT}/memtest.tmp.zip" "https://memtest.org/download/v6.20/mt86plus_6.20_64.grub.iso.zip"
117 unzip -d "$MULTIBOOT_MNT" "${MULTIBOOT_MNT}/memtest.tmp.zip"
118 rm "${MULTIBOOT_MNT}/memtest.tmp.zip"
119 }
120
121 function print_grub_cfg {
122 cat <<EOT
123 search --set=root --label $PARTITION_LABEL_DATA
124
125 insmod all_video
126 insmod gfxterm
127 loadfont unicode
128 set gfxmode=1024x768
129 terminal_output gfxterm
130
131 insmod part_msdos
132 insmod progress
133 insmod regexp
134
135 set maybe_quiet='quiet splash'
136 set maybe_to_ram=''
137
138 menuentry "! Copy ISO image to ram before booting" {
139 # copytoram is used by arch
140 # toram is used by casper based images (tails, Ubuntu, etc)
141 set maybe_to_ram="copytoram toram"
142 }
143
144 menuentry "! Verbose" {
145 set maybe_quiet=''
146 }
147
148 function setup_arch {
149 menuentry "\$1" {
150 loopback loop \$1
151 echo "Loading kernel..."
152 linux (loop)/arch/boot/x86_64/vmlinuz-* img_label=${PARTITION_LABEL_DATA} img_loop=\$1 archisobasedir=arch earlymodules=loop \$maybe_to_ram \$maybe_quiet
153 echo "Loading initrd (and microcode if they exist)..."
154 initrd (loop)/arch/boot/*.img (loop)/arch/boot/x86_64/initramfs-*.img
155 }
156 }
157 for iso in /archlinux-*.iso /isos/archlinux-*.iso; do
158 if [ -f "\$iso" ]; then
159 setup_arch \$iso
160 fi
161 done
162
163 function setup_debian {
164 menuentry "\$1" {
165 loopback loop \$1
166 echo "Loading kernel..."
167 linux (loop)/live/vmlinuz* boot=live components findiso=\$1 \$maybe_to_ram \$maybe_quiet
168 echo "Loading initrd..."
169 initrd (loop)/live/initrd*
170 }
171 }
172 for iso in /debian-live-*.iso /isos/debian-live-*.iso; do
173 if [ -f "\$iso" ]; then
174 setup_debian \$iso
175 fi
176 done
177
178 if [ -f /memdisk -a -f /FD12LITE.zip ]; then
179 menuentry /FD12LITE.zip {
180 if [ \${grub_platform} = pc ]; then
181 linux16 /memdisk raw
182 initrd16 /FD12LITE.zip
183 else
184 echo "FreeDOS only works with BIOS boot."
185 sleep 3
186 fi
187 }
188 fi
189
190
191 function setup_memtest {
192 menuentry "\$1" {
193 loopback loop \$1
194 if [ \${grub_platform} = pc ]; then
195 linux (loop)/boot/memtest
196 else
197 linux (loop)/EFI/BOOT/memtest
198 fi
199 }
200 }
201 for iso in /mt86plus*.grub.iso /isos/mt86plus*.grub.iso; do
202 if [ -f "\$iso" ]; then
203 setup_memtest \$iso
204 fi
205 done
206
207
208 function setup_fedora {
209 menuentry "\$1" {
210 loopback loop \$1
211 probe -s iso_label -l (loop)
212 echo "Loading kernel..."
213 linux (loop)/images/pxeboot/vmlinuz root=live:CDLABEL=\$iso_label rd.live.image iso-scan/filename=\$1 \$maybe_quiet
214 echo "Loading initrd..."
215 initrd (loop)/images/pxeboot/initrd.img
216 }
217 }
218 for iso in /Fedora-Workstation-Live-*.iso /isos/Fedora-Workstation-Live-*.iso; do
219 if [ -f "\$iso" ]; then
220 setup_fedora \$iso
221 fi
222 done
223
224 function setup_ubuntu {
225 menuentry "\$1" {
226 loopback loop \$1
227 set maybe_layerfs_path=''
228 for f in minimal.standard.live.squashfs; do
229 if [ -f "(loop)/casper/\$f" ]; then
230 echo " \$f"
231 set maybe_layerfs_path="layerfs-path=\$f"
232 echo "Setting \$maybe_layerfs_path"
233 fi
234 done
235 echo "Loading kernel..."
236 linux (loop)/casper/vmlinuz* \$maybe_layerfs_path boot=casper iso-scan/filename=\$1 \$maybe_to_ram \$maybe_quiet
237 echo "Loading initrd..."
238 initrd (loop)/casper/initrd*
239 }
240 }
241 for iso in /ubuntu-*-desktop-*.iso /isos/ubuntu-*-desktop-*.iso; do
242 if [ -f "\$iso" ]; then
243 setup_ubuntu \$iso
244 fi
245 done
246
247 function setup_tails {
248 menuentry "\$1" {
249 loopback loop \$1
250 echo "Loading kernel..."
251 linux (loop)/live/vmlinuz* initrd=/live/initrd.img boot=live config iso-scan/filename=\$1 findiso=\$1 nopersistence noprompt timezone=Etc/UTC noautologin module=Tails slab_nomerge slub_debug=FZP mce=0 vsyscall=none page_poison=1 init_on_free=1 mds=full,nosmt \$maybe_to_ram \$maybe_quiet
252 echo "Loading initrd..."
253 initrd (loop)/live/initrd*
254 }
255 }
256 for iso in /tails-*.iso /isos/tails-*.iso; do
257 if [ -f "\$iso" ]; then
258 setup_tails \$iso
259 fi
260 done
261
262 EOT
263 }
264
265 CMD="cmd_${1:-}"
266 shift || true
267
268 if [ "$(type -t -- "$CMD")" = "function" ]; then
269 "${CMD}" "$@"
270 else
271 echo "Usage: $0 [format|mount|grub|grubcfg|freedos|memtest|umount]"
272 exit 1
273 fi