]> code.delx.au - monosys/blob - scripts/multiboot-setup.sh
ssh-screen-wrapper doesn't use tput
[monosys] / scripts / multiboot-setup.sh
1 #!/bin/bash
2
3 set -e
4
5 SYSLINUX_CFG="syslinux/syslinux.cfg"
6 REFIND_CFG="System/Library/CoreServices/refind.conf"
7
8 function setup_bootloader {
9 install_syslinux
10 install_refind
11 rm -rf distros
12 }
13
14 function install_syslinux {
15 rm -rf syslinux
16 mkdir syslinux
17 cp -R /usr/lib/syslinux/bios/*.c32 syslinux/
18
19 echo "UI menu.c32" >> "$SYSLINUX_CFG"
20
21 echo "Run these commands to configure set up your bootable device:"
22 echo " # dosfslabel /dev/sdX multiboot"
23 echo " # dd bs=440 count=1 if=/usr/lib/syslinux/bios/mbr.bin of=/dev/sdX"
24 echo " # extlinux -i -d /<mountpoint>/syslinux"
25 }
26
27 function install_refind {
28 rm -rf System
29 mkdir -p System/Library/CoreServices
30 cp /usr/share/refind/refind_x64.efi System/Library/CoreServices/boot.efi
31
32 echo "timeout 0" >> "$REFIND_CFG"
33 echo "textonly true" >> "$REFIND_CFG"
34 echo "textmode 1024" >> "$REFIND_CFG"
35 echo "scanfor manual" >> "$REFIND_CFG"
36 }
37
38 function setup_iso {
39 mount_iso
40 set_boot_vars
41 copy_boot_files
42 configure_syslinux
43 configure_refind
44 umount_iso
45 }
46
47 function mount_iso {
48 umount_iso
49 sudo mount "$ISOFILE" /mnt
50 }
51
52 function umount_iso {
53 sudo umount /mnt || true
54 }
55
56 function set_boot_vars {
57 if [[ "$ISOFILE" == *ubuntu*.iso ]]; then
58 set_ubuntu_boot_vars
59 elif [[ "$ISOFILE" == *fedora*.iso ]]; then
60 set_fedora_boot_vars
61 elif [[ "$ISOFILE" == *archlinux*.iso ]]; then
62 set_archlinux_boot_vars
63 else
64 echo "Unsupported ISO! $ISOFILE"
65 fi
66 }
67
68 function set_ubuntu_boot_vars {
69 version="$(basename "$ISOFILE" | cut -d- -f2)"
70 menulabel="Ubuntu $version"
71 unpackdir="distros/ubuntu_$(generate_safe_filename "$version")"
72 kernel="/mnt/casper/vmlinuz.efi"
73 initrd="/mnt/casper/initrd.lz"
74 bootparams="boot=casper iso-scan/filename=/${unpackdir}/$(basename "$ISOFILE")"
75 }
76
77 function set_fedora_boot_vars {
78 version="$(basename "$ISOFILE" .iso | sed 's/.*x86_64-//')"
79 menulabel="Fedora $version"
80 unpackdir="distros/fedora_$(generate_safe_filename "$version")"
81 kernel="/mnt/isolinux/vmlinuz0"
82 initrd="/mnt/isolinux/initrd0.img"
83 bootparams="root=live:CDLABEL=$(basename "$ISOFILE" .iso) rootfstype=auto iso-scan/filename=/${unpackdir}/$(basename "$ISOFILE")"
84 }
85
86 function set_archlinux_boot_vars {
87 version="$(basename "$ISOFILE" .iso | sed -e 's/archlinux-//' -e 's/-dual//')"
88 menulabel="ArchLinux $version"
89 unpackdir="distros/archlinux_$(generate_safe_filename "$version")"
90 kernel="/mnt/arch/boot/x86_64/vmlinuz"
91 initrd="/mnt/arch/boot/x86_64/archiso.img"
92 bootparams="img_label=multiboot img_loop=${unpackdir}/$(basename "$ISOFILE") archisobasedir=arch earlymodules=loop"
93 }
94
95 function generate_safe_filename {
96 echo -n "$1" | tr -c '[:alnum:]\n' '_'
97 }
98
99 function copy_boot_files {
100 mkdir -p "${unpackdir}"
101 ln "$ISOFILE" "${unpackdir}/"
102 cp "$kernel" "$initrd" "${unpackdir}/"
103 }
104
105 function configure_syslinux {
106 cat <<EOT >> "$SYSLINUX_CFG"
107
108 LABEL $(basename $unpackdir)
109 MENU LABEL ${menulabel}
110 LINUX /${unpackdir}/$(basename ${kernel})
111 INITRD /${unpackdir}/$(basename ${initrd})
112 APPEND ${bootparams}
113 EOT
114 }
115
116 function configure_refind {
117 cat <<EOT >> "$REFIND_CFG"
118
119 menuentry "${menulabel}"
120 loader /${unpackdir}/$(basename ${kernel})
121 initrd /${unpackdir}/$(basename ${initrd})
122 options "${bootparams}"
123 }
124 EOT
125 }
126
127 if [ "$1" = "reset" ]; then
128 setup_bootloader
129 elif [[ "$1" == *.iso ]]; then
130 ISOFILE="$1"
131 setup_iso
132 else
133 echo "Usage: $0 [reset|/path/to/ubuntu.iso]"
134 fi