Skip to content

Instantly share code, notes, and snippets.

@gdamjan
Last active October 1, 2022 06:01
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save gdamjan/05b799162e4b3e97a30d37a89fed0fa8 to your computer and use it in GitHub Desktop.
universal bootable usb/disk - UEFI and BIOS - Arch, Fedora, Mint, Solus and Ubuntu

tools needed: fdisk, mkfs, qemu

Make a GPT partition table:

$ sudo fdisk -l /dev/sdb
Disk /dev/sdb: 7.5 GiB, 8010194944 bytes, 15644912 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: XXXXX-YYYYY-CCCCC-BBBBB-AAAAA

Device       Start      End  Sectors  Size Type
/dev/sdb1     2048     4095     2048    1M BIOS boot
/dev/sdb2     4096   528383   524288  256M EFI System
/dev/sdb3   528384  4722687  4194304    2G Microsoft basic data
/dev/sdb4  4722688 15644878 10922191  5.2G Linux filesystem
  • 1M bootloader space for legacy bios
  • 128M (or 256) for ESP
  • rest for data, one vfat for legacy os, one ext4 for normal os

Make filesystems:

$ sudo mkfs.vfat /dev/sdb2
$ sudo mkfs.ext4 -O ^has_journal -L LINUX_BOOT /dev/sdb4
  • vfat for the ESP
  • ext4 without journal for data (note the UUID of the ext4 fs, will be needed below)

Install grub for bios and uefi:

$ sudo mkdir /tmp/espboot
$ sudo mount /dev/sdb2 /tmp/espboot
$ sudo grub-install --removable --recheck --target=i386-pc --boot-directory=/tmp/espboot /dev/sdb
$ sudo grub-install --removable --recheck --target=x86_64-efi --boot-directory=/tmp/espboot --efi-directory=/tmp/espboot --bootloader-id=grub

Setup grub.cfg:

/tmp/espboot/grub.cfg

if loadfont /grub/fonts/unicode.pf2 ; then
        set gfxmode=auto
        insmod efi_gop
        insmod efi_uga
        insmod gfxterm
        terminal_output gfxterm
fi

set menu_color_normal=white/black
set menu_color_highlight=black/light-gray

search --set=root --no-floppy --fs-uuid "<UUID HERE>"

menuentry "Boot Arch Linux" {
    linux   /arch/x86_64/vmlinuz archisobasedir=arch archisolabel=LINUX_BOOT
    initrd  /arch/x86_64/archiso.img
}
menuentry "Try Ubuntu without installing" {
    set gfxpayload=keep
    linux   /ubuntu/vmlinuz.efi file=/cdrom/ubuntu/preseed/ubuntu.seed boot=casper quiet splash live-media-path=ubuntu uuid=<UUID HERE> --
    initrd  /ubuntu/initrd.lz
}
menuentry 'Mint 18.2 Cinnamon' {
    set gfxpayload=keep
    linux   /mint/vmlinuz file=/cdrom/mint/preseed/linuxmint.seed boot=casper quiet splash live-media-path=mint uuid=<UUID HERE> --
    initrd  /mint/initrd.lz
}
menuentry 'Start Fedora-Workstation-Live 26' {
        linux /Fedora/vmlinuz root=live:CDLABEL=LINUX_BOOT rd.live.image rd.live.dir=Fedora quiet
        initrd /Fedora/initrd.img
}
menuentry 'Solus 3' {
    linux     /Solus/kernel root=live:CDLABEL=LINUX_BOOT ro rd.luks=0 rd.md=0 quiet splash rd.live.dir=Solus
    initrd    /Solus/initrd.img
}

note the UUID requirement

OSes

the contents of official .iso of distros are copied almost verbatim on the ext4 partition, this is an example how files are layed out:

├── arch/
│   └── x86_64/
│       ├── airootfs.md5
│       ├── airootfs.sfs
│       ├── airootfs.sfs.sig
│       ├── archiso.img
│       └── vmlinuz
├── Fedora/
│   ├── initrd.img
│   ├── squashfs.img
│   └── vmlinuz*
├── Solus/
│   ├── initrd.img
│   ├── kernel*
│   └── squashfs.img
├── mint/
│   ├── dists/
│   ├── filesystem.manifest
│   ├── filesystem.manifest-remove
│   ├── filesystem.size
│   ├── filesystem.squashfs
│   ├── initrd.lz
│   ├── pool/
│   ├── preseed/
│   ├── README.diskdefines
│   └── vmlinuz
└── ubuntu/
    ├── dists/
    ├── filesystem.manifest
    ├── filesystem.manifest-remove
    ├── filesystem.size
    ├── filesystem.squashfs
    ├── filesystem.squashfs.gpg
    ├── initrd.lz
    ├── md5sum.txt
    ├── pics/
    ├── pool/
    ├── preseed/
    ├── README.diskdefines
    └── vmlinuz.efi

Test with QEMU

UEFI:

sudo qemu-system-x86_64 -enable-kvm -m 2048 -bios /usr/share/ovmf/ovmf_code_x64.bin -hda /dev/sdb

Bios:

sudo qemu-system-x86_64 -enable-kvm -m 2048 -hda /dev/sdb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment