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

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