Thursday, March 19, 2009

Create a root file system image

Copyright © 2009 Li Hong

Permission is granted to reprint or republic this article as long as the original source information and the copyright are kept.

The root filesystem is the filesystem that is contained on the same partition on which the root directory is located, and it is the filesystem on which all the other filesystems are mounted as the system is booted up. You can get a short and clear introduction to it at Root Filesystem Definition.

Most of time, the root filesystem resides on a real hardware such as a hard disk. But we can also make a virtual root filesystem out of a file and use it in a virtual machine. Combine it with a kernel image and a boot loader, you can setup a personal system immediately.


1 Create a hard disk image

A hard disk is complex, but can also be easy enough if you take it just as a sequence of bytes. See more introduction about a hard disk at Hard Disk

So using a file schema it is very handy to create a virtual disk:

     $dd if=/dev/zero of=root.img seek=1G bs=1 count=0

Please note, we use 'seek' action here, this will not allocate real space and is very fast. If you are using qemu, you can also create a compressed disk as follows. But I suggest you not to if you just want to do some tests and debug. Because it is inconvenient to use it under another virtual machine or to manage it directly without an emulator.

     $qemu-img create -f qcow root.img 1G

2 Format the disk

Su to root, then

     #losetup /dev/loop0 root.img #fdisk /dev/loop0

You can format this disk as what you like. To create only one partition is a googd choice, otherwise you will bother computing the offset to each partition when make the root file systems.


3 Make root file system

Assume there is only one partition in this disk. It should be 63 sectors after the beginning. That is at file offset 522 * 63 = 32256. So loop detach the disk first, then loop setup the partion and finally create a ext2 file system on it.

     #losetup --detach /dev/loop0

#losetup -o 32256 /dev/loop0 root.img

$mke2fs /dev/loop0

4 Copy files

Mount the partition first.

     #mount /dev/loop0 /mnt

Then copy all files (include grub files under /boot/grub) from an available root file system to /mnt. Umount and detach the disk

     #umount /mnt #losetup --detach /dev/loop0 #exit

5 Make the disk bootable

We use the grub to load the kernel and bootup the system. Because the disk can't boot up itself now, we should download a bootable grub floppy image ( grub.img) and run it in qemu. Then use grub to write the bootstrap into the head of the disk. (use 'qemu -h' to see the option information)

     $qemu -hda root.img -fda grub.img -boot a

When grub starts, write the bootstrap and then close qemu. Now you have a brand new system and are free to hack it with qemu.

     $qemu -hda root.img

You can install an new version of kernel easily either by booting up the this virtual disk in a virtual system or by managing root.img directly on a real box.

If you somewhat lack an adventurous spirit and feel these steps are still messy there is a easier way. Download a os distribution ISO file that you favor and then, after you finish the first step, run the following command to install a new root file system directly into this disk.

     $qemu -cdrom os.ISO -hda root.img -boot d

3 comments:

  1. I'm trying to do what you described here:

    dd if=/dev/zero of=root.img seek=20M bs=1 count=0

    qemu-img create -f qcow2 root.img 20M

    losetup /dev/loop0 root.img

    fdisk /dev/loop0

    losetup --detach /dev/loop0

    losetup -o 32256 /dev/loop0 root.img

    mke2fs /dev/loop0

    mkdir -p test

    mount -t ext2 /dev/loop0 test <-- here i stuck

    umount test

    losetup --detach /dev/loop0

    It prints:

    mount: wrong fs type, bad option, bad superblock on /dev/loop0,
    missing codepage or helper program, or other error
    In some cases useful info is found in syslog - try
    dmesg | tail or so

    What is wrong in my script? Thanks.

    ReplyDelete
  2. This post is good enough, but when you copy and paste a definition, you'd better to use quotes. I am shame of your school - Beida.

    ReplyDelete
  3. Hi buddy,
    There is a typo on step 3, on size of each sector, I think it is 512 instead of 522
    BTW, nice done

    ReplyDelete