Unknownpgr

Creating a Bootable Image with a Minimal Setup

2023-12-05 03:31:09 | English, Korean

This post was translated from Korean into English by AI.

I somehow ended up having to build a Linux image myself. I actually plan to use Yocto, but before doing so, I was curious about what it takes to create a minimally bootable image, so I looked into it.

Make Minimal Bootable Disk

The Linux boot process is fairly complex, but it can be summarized as follows.

  1. The BIOS, UEFI, or another firmware interface performs various initialization tasks and then looks for a bootable disk.
  2. If it finds a bootloader in the bootable disk's MBR section or GPT EFI partition, it runs the bootloader.
  3. The bootloader loads the initramfs and kernel.
  4. The kernel performs various initialization tasks, including setting up memory management.
  5. It then runs the init process specified in the initramfs.
  6. The init process mounts the root file system and starts the required services.

Therefore, creating a minimally bootable image requires the following:

1. Linux Kernel

  1. Download and build the kernel. The config file generated by default may not work universally, so it is a good idea to use a config file provided by a distribution such as Ubuntu.
    • If you are using Ubuntu or a similar distribution, you can find the file with the /boot/config-$(uname -r) command.
    • You may encounter a credential issue. If so, refer to the link below.
      • https://askubuntu.com/questions/1329538/compiling-the-kernel-5-11-11/1329625#1329625
  2. The kernel is created at arch/x86/boot/bzImage. (For the x86 architecture)

2. Initramfs

  1. Download busybox to create the initramfs.
    • git clone git://busybox.net/busybox.git
  2. Build busybox using make.
    • make defconfig
    • make
  3. Create the initramfs using the make install command. The initramfs is created in the _install directory.
  4. Add the following init script to the _install directory.
    #!/bin/sh
    mount -t proc none /proc
    mount -t sysfs none /sys
    mount -t devtmpfs none /dev
    exec /bin/sh
    
  5. Make the init script executable using the chmod +x init command.
  6. From the _install directory, create the initramfs using the find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../../initramfs.cpio.gz command.

3. Bootloader

  1. Prepare a blank disk, then create an EFI partition using the fdisk command.
    • Be careful not to format the host disk.
    • Since the goal is to create a minimally bootable image, do not create any other partitions.
  2. Format the EFI partition as FAT32 using the mkfs.fat -F 32 /dev/sdXy command.
  3. Mount the EFI partition using the mount /dev/sdXy /mnt command.
  4. Copy the kernel and initramfs to the boot directory on the EFI partition. (e.g. /mnt/boot)
  5. Install GRUB using the grub-install --boot-directory=/mnt/boot --efi-directory=/mnt /dev/sdX command.

References


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -