Bootable Disk

From OSDev Wiki
Jump to navigation Jump to search

This brief tutorial is about how to create your own bootable disk image. For information about partitioning see GPT and FAT.

Difficulty level
Difficulty 1.png
Beginner

BIOS Disk

Bochs can be a powerful tool to debug, especially early in the operating system load sequence. However, BOCHS can only boot with BIOS and is somewhat legacy in what it emulates. In order to create a basic bootable disk you need to calculate the size based on Cylinders, Heads (max 16) & Sectors (max 63). Then, you need to create the disk and install grub and your files on it.

The following uses a disk layout of 80*16*63, which needs to be explicitly configured in BOCHS.

$ dd if=/dev/zero of=disk.img bs=512 count=80640 # create an empty null-filled disk. You can use a random device to get a facsimile of a real disk if desired
$ LOOPBACK_DEVICE=$(losetup --find --show disk.img ) # load the disk as a loopback device
$ parted $LOOPBACK_DEVICE mklabel msdos # Create a MBR partition label
$ parted $LOOPBACK_DEVICE mkpart primary fat32 1MiB 100% # Create a partition starting at 1 MiB that spans the rest of the disk with label fat32
$ losetup -d $LOOPBACK_DEVICE # Delete the loopback device
$ LOOPBACK_DEVICE=$(losetup --find --show --partscan disk.img ) # Reload, the partitions will now be loaded and be accessible as well
$ mkfs.vfat -F 32 ${LOOPBACK_DEVICE}p1 # Create a FAT32 partition
$ DISK_NAME="/mnt/disk" 
$ mkdir -p $DISK_NAME # Create a mount point
$ mount -t vfat ${LOOPBACK_DEVICE}p1 $DISK_NAME # Mount the image
$ cd $DISK_NAME
$ mkdir boot
$ grub-install --no-nvram --directory /usr/lib/grub/i386-pc/ --modules="normal part_msdos multiboot2 fat" $LOOPBACK_DEVICE --no-floppy --target=i386-multiboot # Install grub

You can now copy your kernel image to /mnt/disk/boot.

$ umount $DISK_NAME # unmount the disk image
$ losetup -d $LOOPBACK_DEVICE # remove the loopback device entries (similar to ejecting a USB)

You can now use a BIOS-booting virtual machine, such as Bochs, to boot from this disk. When the grub shell comes up, use the following to boot (assuming you used multiboot2)

multiboot2 (hd0,msdos1)/boot/<yourkernelimage>
boot

If you are using an EFI device, you may need to get the grub bios files. On debian, these are in the grub-pc-bin package. Do not install this package, instead:

mkdir grub-bios
sudo apt download grub-pc-bin # Download the package to the current working directory
dpkg-deb -R grub-pc-bin_<version>.deb grub-bios/ # Unpack the deb file to grub-bios

You will have to change the --directory in your grub-install to point to:

$(pwd)/grub-bios/usr/lib/grub/i386-pc


Requirements

you'll need the following:


Required Steps

Creating an Empty Image

First of all, let's create an empty disk image file, let's say 128 megabytes in size.

$ dd if=/dev/zero of=diskimage.dd bs=1048576 count=128

Creating the Partitioning Table

As the GPT is more complex thing than MBR partitioning tables, we'll use fdisk, which has an interactive interface (for macOS you will need to use the gnu fdisk). Inputs (that you're supposed to type) are after the ":" colons.

$ fdisk diskimage.dd

Welcome to fdisk (util-linux 2.30.2).
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 0xfa00b86e.

Command (m for help): g
Created a new GPT disklabel (GUID: E6B4945A-8308-448B-9ACA-0E656854CF66).

Command (m for help): n p
Partition number (1-128, default 1): 1
First sector (2048-262110, default 2048): 2048
Last sector, +sectors or +size{K,M,G,T,P} (2048-262110, default 262110): +8M

Created a new partition 1 of type 'Linux filesystem' and of size 8 MiB.

Command (m for help): t 1
Selected partition 1
Partition type (type L to list all types): 1
Changed type of partition 'Linux filesystem' to 'EFI System'.

Command (m for help): w
The partition table has been altered.Syncing disks.
$

The commands used were:

  • g - create a new GPT partitioning table
  • n p - new primary partition
  • 1 2048 +8M - first partition, starting sector, partition size
  • t 1 - change the type of the first partition
  • 1 - type 1 is EFI System Partition (or ESP in short)
  • w - write out changes

Creating a File System on the First Partition

Now this is not at simple as it seems, because we have to create the file system somewhere in the middle of a file. For that, we'll create a loop device.

$ losetup -o $((2048*512)) --sizelimit $((8*1024*1024)) -f diskimage.dd

Here the arguments are:

  • -o $((2048*512)) - tells losetup that our partition is starting at the 2048th sector (one sector is 512 bytes)
  • --sizelimit $((8*1024*1024)) - specifies the limit in 8 megabytes
  • -f - tells to find a suitable loopback device
  • and the last parameter is our disk image's filename.

To see which device was used for your image, you can do

$ losetup -a

which will list all loopback devices.

Now that we have a device which points inside the image file exactly to the partition, we can create a FAT filesystem on it:

$ mkfs.vfat -F 16 -n "EFI System" /dev/loop0

Here

  • -F 16 - tells to create a FAT16
  • -n "EFI System" - sets the label for the partition (don't change, some firmware checks for this)
  • /dev/loop0 - is the loopback device (change to the one you saw in losetup -a output)

Adding Files to the Partition

The next step is to mount our loopback device, so that we can copy files to the partition.

$ mkdir somedir
$ mount /dev/loop0 somedir

For simplicity, let's call our kernel MSDOS.SYS and out configuration file CONFIG.SYS:

$ cp MSDOS.SYS CONFIG.SYS somedir/

Some boot loaders are so called two-stage loaders. This means they have two parts: a boot sector and a 2nd stage file. You must copy that 2nd stage to the partition:

$ cp 2NDSTAGE.BIN somedir/

UEFI will need a special PE executable named EFI/BOOT/BOOT(arch).EFI, which you can create with GNU-EFI:

$ mkdir somedir/EFI somedir/EFI/BOOT
$ cp BOOTX64.EFI somedir/EFI/BOOT

Once you're finished with the copy, it is important to dismount the partition:

$ umount somedir
$ rmdir somedir

Adding the BIOS Boot Loader

If you're planning to create a hybrid image with the PMBR (that you can also boot on legacy BIOS machines), then you need to add the legacy BIOS boot loader code:

$ dd if=boot.bin of=diskimage.dd conv=notrunc bs=446 count=1
$ dd if=boot.bin of=diskimage.dd conv=notrunc bs=1 count=2 skip=510 seek=510

The first command copies the code, and the second one the two magic bytes at the end of the sector. This way the bytes 446 - 510 (where the partitioning table resides) won't be overwritten. It is very important preserve the ESP entry required by UEFI.

Here the arguments mean:

  • if=boot.bin - the name of the boot loader, should be 512 bytes
  • of=diskimage.dd - the disk image
  • conv=notrunc - tells dd to update an existing image file instead of creating a new one
  • bs=446 - block size (sector size is 512 bytes, but we want to write less)
  • skip=510 - skip that many blocks (in second line bs is 1, so skip 510 bytes)
  • count=1 - tells dd to update one sector only

See Also