Skip to content

Instantly share code, notes, and snippets.

@Cynesiz
Forked from mruediger/USB Bootstick.md
Last active March 13, 2020 01:45
Show Gist options
  • Save Cynesiz/4baf7932bdabd8ae532265a10dad0286 to your computer and use it in GitHub Desktop.
Save Cynesiz/4baf7932bdabd8ae532265a10dad0286 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/sdc
  • create a new empty partition table by pressing o
  • if it asks for a confirmation press y
  • create a new partition by pressing n
  • use the default partition number by pressing enter
  • use the default first sector by 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
  • use the default next partition number by pressing enter
  • use the default first sector 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
  • verify the partitions are created by pressing p

Number Start (sector) End (sector) Size Code Name 1 2048 6143 2.0 MiB EF02 BIOS boot partition 2 6144 30529502 14.6 GiB 0700 Microsoft basic data

  • write the changes by pressing w
  • answer yes to the confirmation by pressing y
  • now unplug the usb stick for 10 seconds and plug it back in

Next we have to format the data partition

  • ensure that the partition table has been updated:

$ fdisk -l /dev/sdc Disk /dev/sdc: 14.6 GiB, 15631122432 bytes, 30529536 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: gpt Disk identifier: F2A83318-983E-4547-86C9-7C1301FFFE0B

Device Start End Sectors Size Type /dev/sdc1 2048 6143 4096 2M BIOS boot /dev/sdc2 6144 30529502 30523359 14.6G Microsoft basic data

  • in this case, /dev/sdc2 is the data partition
  • next run sudo mkfs.vfat /dev/sdc2

Now lets install grub on the stick

  • first mount the stick: sudo mount /dev/sdc2 /mnt
  • next install grub for non-uefi systems use sudo grub2-install --target i386-pc --removable --boot-directory=/mnt
  • then install grub for uefi systems use sudo 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