Creating ext2 filesystems from scratch

We’ve had to change the way libguestfs boots its appliance, so now the appliance will be an ext2 filesystem from an ordinary drive, instead of an initrd.

The problem is that we need to create the ext2 filesystem on the fly, and we can’t use libguestfs to do this (which would be way easier than the method I’m about to describe).

However e2fsprogs comes with a low-level library for manipulating ext2 filesystem images, and it is just about possible to use this to make an ext2 filesystem and put files and directories on to it.

The documentation on libext2fs is very light on detail, but I have written some example code:

http://oirase.annexia.org/tmp/ext2fs_test.c

Compile and run this with:

$ gcc -Wall test.c -o test -lext2fs -lcom_err
$ ./test
$ guestfish -a /tmp/test.img -m /dev/sda
><fs> ll /
drwxr-xr-x  4  500  500   1024 Aug 19 12:08 .
dr-xr-xr-x 20 root root      0 Aug 19 12:09 ..
-rw-r--r--  1 root root 100000 Aug 19 12:08 hello
drwx------  2 root root  12288 Aug 19 12:08 lost+found
drwxr-xr-x  2 root root   1024 Aug 19 12:08 mydir
><fs> ll /mydir/
total 3
drwxr-xr-x 2 root root 1024 Aug 19 12:08 .
drwxr-xr-x 4  500  500 1024 Aug 19 12:08 ..
-rw-r--r-- 1 root root   99 Aug 19 12:08 file_in_mydir
><fs> cat /mydir/file_in_mydir
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstu

It’s also advisable to read about how ext2 works internally.

Leave a comment

Filed under Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s