Skip to content

Instantly share code, notes, and snippets.

@bahamat
Forked from oetiker/make-bootable-image.sh
Created March 8, 2023 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bahamat/029629aa7aa1cabb096cd28bde164a74 to your computer and use it in GitHub Desktop.
Save bahamat/029629aa7aa1cabb096cd28bde164a74 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment