Skip to content

Instantly share code, notes, and snippets.

@mruediger
Last active December 8, 2020 16:09
Show Gist options
  • Save mruediger/de58c4ce49c0bfbeac6a to your computer and use it in GitHub Desktop.
Save mruediger/de58c4ce49c0bfbeac6a to your computer and use it in GitHub Desktop.
USB Bootstick for multiple isos using UEFI and BIOS

This small Howto is about setting up a USB stick to boot different iso files using grub's loopback capability. I only tested this on Fedora, but it should also work on other Linux distributions.

First, lets find the USB Stick

lsblk

This should give you a list of all your block devices. My stick is found as /dev/sdc

Then we need to partition the USB stick using GPT.

  • we will use gdisk sudo gdisk /dev/USBDEV
  • create a new empty partition table by pressing o
  • create a new partition by pressing n
  • use the default start point by just pressing enter
  • make it 2 megabytes big by entering +2M
  • enter ef02 as the type of the partition to make it a Bios boot partition
  • create another partition by pressing n
  • let it start at the default by pressing enter
  • let it use the maximum available size by pressing enter again
  • enter 0700 as its type to make it a Microsoft basic data partition

Next we have to format the data partition

  • use partprobe to update your partition tables without rebooting
  • next run mkfs.vfat /dev/USBDEV2

Now lets install grub on the stick

  • first mount the stick: mount /dev/USBDEV2 /mnt
  • first install grub for non-uefi systems use grub2-install --target i386-pc --removable --boot-directory=/mnt
  • then install grub for uefi systems use grub2-install --target x86_64-efi --efi-directory /mnt --removable --boot-directory /mnt

Configure Grub

  • create the file /mnt/grub2/grub.cfg with the following contents:
insmod font
if loadfont ${prefix}/fonts/unicode.pf2 ; then
	# Use shift key to avoid loading gfxterm
	if keystatus --shift ; then true ; else
		insmod gfxterm
		insmod vbe
		insmod vga
		set gfxmode=auto
		set gfxpayload=auto
		terminal_output gfxterm 
		if terminal_output gfxterm ; then true ; else
			terminal gfxterm
		fi
	fi
fi

set color_normal=white/black
set color_highlight=white/light-blue
export color_normal
export color_highlight

echo "Generating entries from *.cfg files"

insmod regexp
for cfg_file in /isos/*.cfg /isos/*.CFG; do
    echo "sourced $cfg_file"
    source $cfg_file
done

menuentry "Reboot" {
	  reboot
}

menuentry "Poweroff" {
	  halt
}

Add the iso files and a config for each

  • create the folder /mnt/isos
  • put all the iso files you want to be able to boot in it
  • create a config for each in the same folder, named <something>.cnf

A sample config file for Fedora 21

menuentry "Fedora 21" {
	  set iso_path="/isos/Fedora-Live-Workstation-x86_64-21_Beta-4.iso"  
	  loopback loop $iso_path
	  linux (loop)/isolinux/vmlinuz0 iso-scan/filename=$iso_path root=live:CDLABEL=Fedora-Live-Workstation-x86_64-2 rootfstype=auto ro rd.live.image quiet  rhgb rd.luks=0 rd.md=0 rd.dm=0 nomodeset 
	  initrd (loop)/isolinux/initrd0.img
}

menuentry "Fedora 21 (uefi)" {
	  set iso_path="/isos/Fedora-Live-Workstation-x86_64-21_Beta-4.iso"  
	  loopback loop $iso_path
	  linuxefi (loop)/isolinux/vmlinuz0 iso-scan/filename=$iso_path root=live:CDLABEL=Fedora-Live-Workstation-x86_64-2 rootfstype=auto ro rd.live.image quiet  rhgb rd.luks=0 rd.md=0 rd.dm=0 nomodeset 
	  initrdefi (loop)/isolinux/initrd0.img
}

And you are done. To test your stick you can use qemu to boot the stick

  • for UEFI: qemu-system-x86_64 -L /usr/share/qemu-ovmf/bios -hda /dev/USBDEV
  • for BIOS: qemu-system-x86_64 -hda /dev/USBDEV
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment