Rather small cookbook recipe for creating Linux bootable drives using your hands some commands and some magic.
you might do this because you’re either:
- Paranoic and you want to keep your trust chain small, or making your own rootfs.
- Trying to use an ISO image which doesn’t boot even if you use
dd
and write it to the USB drive. - God knows why, maybe you want some documentation.
Well be using syslinux
to accomplish this, you may want to download it using
your package manager, downloading a random .exe
if you’re on Windows as we
all do, or - coughs in paranoic - compile it yourself from the source code.
Also, this is for the MBR partition scheme, GPT is too advanced for my 2011 computer.
Photo of my small USB with some Rust
2020 (C) Me The Owner
Erasing parition table
- Warning!! All your data will be gone after this and I don’t care too much, so
please do a backup, also, be sure you don’t write to the wrong
/dev/sdX
device like your system drive for example.
- Find your device with
lsblk
- Umount it
sudo umount /dev/sdX
- Erase partitions
dd if=/dev/zero of=/dev/sdX bs=512 count=1 conv=notrunc
You now shouldn’t see any partitions with lsblk
on your drive.
Remember to change sdX with your device please
Partitioning
Ironically we need partitions again, let’s do this job with our old friend fdisk
.
- Go with
sudo fdisk /dev/sdX
. - Make sure there aren’t partitions by using the
p
command.
Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xe74168a6.
Command (m for help): p
Disk /dev/sdb: 119,5 GiB, 128320801792 bytes, 250626566 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: dos
Disk identifier: 0xe74168a6
- Make new partition with
n
, thenp
for primary partition, at this point you can use the defaults (all of the drive) or create your own complex partitions. Just accept the defaults and go on.
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-250626565, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-250626565, default 250626565):
Created a new partition 1 of type 'Linux' and of size 119,5 GiB.
- Now make it a fat32 partition by writing
t
to change partition type and then usingb
(which is the value for fat32).
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): b
Changed type of partition 'Linux' to 'W95 FAT32'.
- Make it active (bootable) with
a
.
Command (m for help): a
Selected partition 1
The bootable flag on partition 1 is enabled now.
- Write with
w
.
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
- Make it fat (in the good sense)
sudo mkfs.vfat /dev/sdX1
Setting up syslinux
- Okay, mount your drive to your preferred folder, maybe
/mnt/usb
.sudo mkdir -p /mnt/usb
sudo mount /dev/sdb1 /mnt/usb
- Copy syslinux files for BIOS.
sudo mkdir -p /mnt/usb/boot/syslinux
sudo sudo cp -v -a /usr/lib/syslinux/modules/bios/*.c32 boot/syslinux
- Install syslinux.
sudo umount /mnt/usb
sudo syslinux --install /dev/sdb1 --directory boot/syslinux
- Copy MBR.
dd bs=440 count=1 conv=notrunc if=/usr/lib/syslinux/mbr/mbr.bin of=/dev/sda
Et voila! you now have your syslinux running on your usb, now you can setup the rootfs and add the syslinux configuration files.