]> code.delx.au - monosys/blob - hacks/multiboot-setup
0ea027c0379a9890033d63f6be57648014b6ea1a
[monosys] / hacks / multiboot-setup
1 #!/bin/bash
2
3 set -eu
4
5 PARTITION_LABEL="multiboot"
6 MULTIBOOT_MNT="${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 if [[ -w "${MULTIBOOT_MNT}/grub/" ]]; then
62 # We already have write access, no need to use sudo
63 print_grub_cfg > "${MULTIBOOT_MNT}/grub/grub.cfg"
64 else
65 print_grub_cfg | sudo tee "${MULTIBOOT_MNT}/grub/grub.cfg" > /dev/null
66 fi
67 }
68
69 function cmd_mount {
70 set -x
71
72 PARTITION_DEVICE="$(readlink -f "/dev/disk/by-label/${PARTITION_LABEL}")"
73 sudo mkdir -p "$MULTIBOOT_MNT"
74 while sudo umount "$PARTITION_DEVICE" &> /dev/null; do true; done
75 sudo mount "$PARTITION_DEVICE" "$MULTIBOOT_MNT" -o "uid=$(whoami)"
76 }
77
78 function cmd_umount {
79 set -x
80
81 sudo umount "$MULTIBOOT_MNT"
82 sudo rmdir "$MULTIBOOT_MNT"
83 }
84
85 function cmd_freedos {
86 set -x
87
88 local SYSLINUX_VERSION="6.03"
89 local SYSLINUX_URL="https://www.kernel.org/pub/linux/utils/boot/syslinux/syslinux-${SYSLINUX_VERSION}.tar.gz"
90 local FREEDOS_URL="http://www.freedos.org/download/download/FD12LITE.zip"
91
92 curl -fL "$SYSLINUX_URL" | \
93 tar xz --no-same-owner --strip-components=3 -C "$MULTIBOOT_MNT" \
94 "syslinux-${SYSLINUX_VERSION}/bios/memdisk/memdisk"
95
96 curl -fL "$FREEDOS_URL" > "${MULTIBOOT_MNT}/FD12LITE.zip"
97 }
98
99 function cmd_memtest {
100 curl -fL "https://www.memtest.org/download/5.01/memtest86+-5.01.bin.gz" | \
101 zcat - > "${MULTIBOOT_MNT}/memtest.bin"
102 }
103
104 function print_grub_cfg {
105 cat <<EOT
106 insmod all_video
107 insmod part_msdos
108 insmod progress
109 insmod regexp
110 search --set=root --label $PARTITION_LABEL
111
112 function setup_arch {
113 menuentry "\$1" {
114 loopback loop \$1
115 linux (loop)/arch/boot/x86_64/vmlinuz-linux img_label=${PARTITION_LABEL} img_loop=\$1 archisobasedir=arch earlymodules=loop
116 initrd (loop)/arch/boot/x86_64/initramfs-linux.img
117 }
118 }
119 for iso in /archlinux-*.iso; do
120 if [ -f "\$iso" ]; then
121 setup_arch \$iso
122 fi
123 done
124
125 function setup_debian {
126 menuentry "\$1" {
127 linux \$1/vmlinuz
128 initrd \$1/initrd.gz
129 }
130 }
131 for d in /debian-*-hd-media; do
132 if [ -d "\$d" ]; then
133 setup_debian \$d
134 fi
135 done
136
137 if [ -f /memdisk -a -f /FD12LITE.zip ]; then
138 menuentry /FD12LITE.zip {
139 if [ \${grub_platform} = pc ]; then
140 linux16 /memdisk raw
141 initrd16 /FD12LITE.zip
142 else
143 echo "FreeDOS only works with BIOS boot."
144 sleep 3
145 fi
146 }
147 fi
148
149 if [ -f /memtest.bin ]; then
150 menuentry "/memtest" {
151 linux16 /memtest.bin
152 }
153 fi
154
155 function setup_fedora {
156 menuentry "\$1" {
157 loopback loop \$1
158 probe -s iso_label -l (loop)
159 linux (loop)/isolinux/vmlinuz root=live:CDLABEL=\$iso_label rd.live.image quiet iso-scan/filename=\$1
160 initrd (loop)/isolinux/initrd.img
161 }
162 }
163 for iso in /Fedora-Workstation-Live-*.iso; do
164 if [ -f "\$iso" ]; then
165 setup_fedora \$iso
166 fi
167 done
168
169 function setup_ubuntu {
170 menuentry "\$1" {
171 loopback loop \$1
172 linux (loop)/casper/vmlinuz* boot=casper quiet iso-scan/filename=\$1
173 initrd (loop)/casper/initrd*
174 }
175 }
176 for iso in /ubuntu-*-desktop-*.iso; do
177 if [ -f "\$iso" ]; then
178 setup_ubuntu \$iso
179 fi
180 done
181
182 EOT
183 }
184
185 CMD="cmd_${1:-}"
186 shift || true
187
188 if [ "$(type -t -- "$CMD")" = "function" ]; then
189 "${CMD}" "$@"
190 else
191 echo "Usage: $0 [format|mount|grub|grubcfg|freedos|memtest|umount]"
192 exit 1
193 fi