Bootable Disk: Difference between revisions

From OSDev.wiki
Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content added Content deleted
Line 89: Line 89:
$ cp MSDOS.SYS CONFIG.SYS somedir/
$ cp MSDOS.SYS CONFIG.SYS somedir/
</source>
</source>
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:
Once we finished with the copy, it is important to dismount the partition:
<source lang="bash">
$ cp 2NDSTAGE.BIN somedir/
</source>

Once you're finished with the copy, it is important to dismount the partition:
<source lang="bash">
<source lang="bash">
$ umount somedir
$ umount somedir

Revision as of 15:33, 19 March 2020

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

Difficulty level

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. 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/

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

$ umount somedir
$ rmdir somedir

Adding the Boot Loader

And as the final step, we add the boot loader code:

$ dd if=boot.bin of=diskimage.dd conv=notrunc bs=512 count=1

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=512 - block size (sector size) is 512 bytes
  • count=1 - tells dd to update one sector only

See Also