]> code.delx.au - monosys/blob - multiboot-setup
smart-stats
[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="$(mount|grep /mnt/multiboot|cut -d' ' -f1|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/multiboot)"
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 setup_arch \$iso
111 done
112
113 function setup_debian {
114 menuentry "\$1" {
115 loopback loop \$1
116 linux (loop)/live/vmlinuz* boot=live components findiso=\$1
117 initrd (loop)/live/initrd.img*
118 }
119 }
120 for iso in /debian-live-*.iso; do
121 setup_debian \$iso
122 done
123
124 if [ -f /memdisk -a -f /FD12LITE.zip ]; then
125 menuentry /FD12LITE.zip {
126 if [ \${grub_platform} = pc ]; then
127 linux16 /memdisk raw
128 initrd16 /FD12LITE.zip
129 else
130 echo "FreeDOS only works with BIOS boot."
131 sleep 3
132 fi
133 }
134 fi
135
136 function setup_fedora {
137 menuentry "\$1" {
138 loopback loop \$1
139 probe -s iso_label -l (loop)
140 linux (loop)/isolinux/vmlinuz root=live:CDLABEL=\$iso_label rd.live.image quiet iso-scan/filename=\$1
141 initrd (loop)/isolinux/initrd.img
142 }
143 }
144 for iso in /Fedora-Workstation-Live-*.iso; do
145 setup_fedora \$iso
146 done
147
148 function setup_ubuntu {
149 menuentry "\$1" {
150 loopback loop \$1
151 linux (loop)/casper/vmlinuz boot=casper quiet iso-scan/filename=\$1
152 initrd (loop)/casper/initrd*
153 }
154 }
155 for iso in /ubuntu-*-desktop-*.iso; do
156 setup_ubuntu \$iso
157 done
158
159 EOT
160 }
161
162 CMD="cmd_${1:-}"
163 shift || true
164
165 if [ "$(type -t -- "$CMD")" = "function" ]; then
166 "${CMD}" "$@"
167 else
168 echo "Usage: $0 [format|mount|grub|grubcfg|freedos|umount]"
169 exit 1
170 fi