#!/bin/bash set -eu PARTITION_LABEL="multiboot" MULTIBOOT_MNT="/mnt/multiboot" function cmd_format { if [ ! -b "${1:-}" ]; then echo "Usage: $0 format /dev/sdX" exit 1 fi set -x sudo -k DISK_DEVICE="$1" PARTITION_DEVICE="${DISK_DEVICE}1" echo -ne 'label: dos\ntype=c, bootable\n' | sudo sfdisk "$DISK_DEVICE" sudo mkfs.vfat -n "$PARTITION_LABEL" "$PARTITION_DEVICE" } function cmd_grub { DISK_DEVICE="$(findmnt -n -o source "$MULTIBOOT_MNT" | sed 's/[0-9]*$//')" if [ ! -b "$DISK_DEVICE" ]; then echo "ERROR! Could not find disk to install bootloader. Try using mount." exit 1 fi set -x sudo -k install_grub_bios install_grub_efi install_grub_cfg } function cmd_grubcfg { set -x install_grub_cfg } function install_grub_bios { sudo grub-install \ --target=i386-pc \ --boot-directory="$MULTIBOOT_MNT" \ "$DISK_DEVICE" } function install_grub_efi { for arch in i386-efi x86_64-efi; do sudo grub-install \ --target="$arch" \ --no-nvram \ --removable \ --efi-directory="$MULTIBOOT_MNT" \ --boot-directory="$MULTIBOOT_MNT" \ "$DISK_DEVICE" done } function install_grub_cfg { print_grub_cfg | sudo tee "${MULTIBOOT_MNT}/grub/grub.cfg" > /dev/null } function cmd_mount { set -x PARTITION_DEVICE="$(readlink -f "/dev/disk/by-label/${PARTITION_LABEL}")" sudo mkdir -p "$MULTIBOOT_MNT" while sudo umount "$PARTITION_DEVICE" &> /dev/null; do true; done sudo mount "$PARTITION_DEVICE" "$MULTIBOOT_MNT" -o "uid=$(whoami)" } function cmd_umount { set -x sudo umount "$MULTIBOOT_MNT" sudo rmdir "$MULTIBOOT_MNT" } function cmd_freedos { set -x local SYSLINUX_VERSION="6.03" local SYSLINUX_URL="https://www.kernel.org/pub/linux/utils/boot/syslinux/syslinux-${SYSLINUX_VERSION}.tar.gz" local FREEDOS_URL="http://www.freedos.org/download/download/FD12LITE.zip" curl -fL "$SYSLINUX_URL" | \ tar xz --no-same-owner --strip-components=3 -C "$MULTIBOOT_MNT" \ "syslinux-${SYSLINUX_VERSION}/bios/memdisk/memdisk" curl -fL "$FREEDOS_URL" > "${MULTIBOOT_MNT}/FD12LITE.zip" } function print_grub_cfg { cat <