Skip to content

Instantly share code, notes, and snippets.

@oetiker
Last active June 19, 2024 06:58
Show Gist options
  • Save oetiker/62ab467b1d6749ee33f4d3dfee404506 to your computer and use it in GitHub Desktop.
Save oetiker/62ab467b1d6749ee33f4d3dfee404506 to your computer and use it in GitHub Desktop.
How to create a bootable disk image that works with bios and uefi
#!/bin/sh
# apt install gdisk syslinux syslinux-efi mtools
IMG=boot-image.img
SIZE=300
KERNEL=vmlinz
INITRAMFS=initrd.img
MD="mmd -i ${IMG}@@1M"
CP="mcopy -i ${IMG}@@1M"
RN="mren -i ${IMG}@@1M"
dd if=/dev/zero bs=1M count=$SIZE of=$IMG
sgdisk -n 1::+$(($SIZE-2))M -c 1:EFI-SYSTEM -t 1:ef00 --hybrid=1 --attributes=1:set:2 ${IMG}
mkfs.fat -S 512 --offset 2048 ${IMG} -n EFI-SYSTEM
$MD ::efi
$MD ::efi/boot
$CP /usr/lib/SYSLINUX.EFI/efi64/syslinux.efi ::efi/boot/bootx64.efi
$CP /usr/lib/syslinux/modules/efi64/ldlinux.e64 ::efi/boot/ldlinux.e64
$MD ::system
$CP $INITRAMFS $KERNEL ::system
dd bs=440 count=1 conv=notrunc if=/usr/lib/syslinux/mbr/gptmbr.bin of=${IMG}
syslinux --offset $((1024*1024)) --install ${IMG}
$CP syslinux.cfg ::syslinux.cfg
DEFAULT linux
LABEL linux
SAY Now booting the kernel from SYSLINUX...
KERNEL /system/vmlinuz
APPEND initrd=/system/initrd.img
@oetiker
Copy link
Author

oetiker commented Mar 8, 2023

After quite some tinkering I came up with this little script for creating bootable disk images, that work both in MBR BIOS and in UEFI setups. The cool thing about this script is that it does NOT require any privileged access. No loop back mounting or such.

If you don't know where to get the KERNEL and INITRAMFS files, look in your /boot directory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment