Skip to content

Instantly share code, notes, and snippets.

@gregjhogan
Last active March 16, 2022 16:27
Show Gist options
  • Save gregjhogan/5d89cebf6596bf53f5da217af7741a27 to your computer and use it in GitHub Desktop.
Save gregjhogan/5d89cebf6596bf53f5da217af7741a27 to your computer and use it in GitHub Desktop.
grub2 USB boot drive that chain loads another grub config (for example on a NVME drive)
USB_DEV=/dev/sdX # replace X with actual device name
USB_MNT=/mnt/usb
ROOT_LABEL="Ubuntu" # replace with label of root drive you installed ubuntu on
# format
sudo wipefs -a $USB_DEV
sudo dd if=/dev/zero of=${USB_DEV} bs=1M count=128 conv=notrunc
(
echo n # new partition
echo p # primary
echo 1 # partition number 1
echo 2048 # first sector 2048
echo +100M # last sector end of disk (default)
echo w # write
) | sudo fdisk $USB_DEV
sudo mkfs.vfat -F 32 -n bootloader ${USB_DEV}1
# mount
sudo mkdir -p $USB_MNT
sudo mount ${USB_DEV}1 $USB_MNT
# install grub
sudo grub-install --force --removable --no-floppy --target=i386-pc --boot-directory=${USB_MNT}/boot ${USB_DEV}
sudo grub-install --force --removable --no-floppy --target=x86_64-efi --boot-directory=${USB_MNT}/boot --efi-directory=${USB_MNT}
# generate config file that loads existing grub config file
cat << EOF | sudo tee ${USB_MNT}/boot/grub/grub.cfg
search --no-floppy --label --set=root ${ROOT_LABEL}
configfile /boot/grub/grub.cfg
EOF
sudo umount ${USB_DEV}1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment