The goal of this practical is for you to clone ext2, an existing (and widely used) file system for Linux and make some small modifications to it. Your cloned file system will be named ext3301 and you will be using it extensively for assignment 3.
Please note it is strongly recommended that you do all kernel development on the virtual image (we recommend ssh'ing in using ssh(1) or Putty on Windows) as it is very easy to crash the kernel you are working on while doing development. It is possible you may lose data if you work on a physical Linux machine and crash its kernel.
Like many other kernel subsystems, file systems are written as kernel modules and can either be compiled in to the kernel, or loaded at runtime. We will be using the latter method for this prac (and A3).
Before making a copy of the ext2 source tree, you need to install the kernel sources (the virtual image only contains the headers, which lacks the C files we need). Since the kernel tree is quite large (approximately 65 MB compressed), you should ensure your apt mirror is set to the AARNet mirror, which is quota-free from the ITEE network. The apt mirror is used whenever the system needs to download a package before installing it.
To do this, you need to edit /etc/apt/sources.list and set the URLs to point to http://mirror.aarnet.edu.au/pub/ubuntu/archive. This file is created by the default system install and is very verbose, containing a lot of settings that we don't need, so we will just override the file. Remove the file, and recreate it with the following lines only:
deb http://mirror.aarnet.edu.au/pub/ubuntu/archive lucid main universe deb http://mirror.aarnet.edu.au/pub/ubuntu/archive lucid-updates main universe deb http://mirror.aarnet.edu.au/pub/ubuntu/archive lucid-security main universe
Now update your package lists off the AARNet mirror by typing apt-get update. When this is finished successfully your terminal will say “Reading package lists... Done”. If you do not see that message, stop now and seek assistance from the tutor or newsgroups!
Be aware that if your apt mirror is not set to the AARNet mirror, the download will be counted toward your UQ download quota (if downloading on the ITEE lab computers or UQconnect wireless). You have been warned!
You can now install the kernel source tree by typing: apt-get install linux-source. Note that this only downloads the source, so we now have to extract it. The source tree is quite large when uncompressed, so you should make sure you have at least 500 MB free space on the virtual image before continuing.
Enter into the /usr/src directory and type: tar -xjf linux-source*.tar.bz2.
You can now gab the ext2 source code by typing: cp -rp /usr/src/linux-source-2.6.32/fs/ext2 ext3301 (you will want to do this inside your home directory, probably in an SVN working copy). If this fails with “No such file or directory”, you probably don't have the kernel source tree installed - go back and check to make sure it was installed properly.
Next we must modify the Makefile to build the file system as a kernel module, as by default Linux file system drivers are set to build into the kernel itself. Download the patch for the Makefile and apply it. (Hint: patch(1) might be useful here!)
Now you can build the kernel module just as you've done in previous pracs - type make and the kernel's build system will take care of the rest. You should now see a ext3301.ko file in your working directory. Try loading this module into the kernel with insmod. Did it succeed? It shouldn't have!
The reason loading the module failed was because the module attempted to register itself as a file system driver called ext2 - a name that the kernel already knows about! The kernel will only allow one file system of a given name to be loaded at any given time. To fix this, we need to rename our file system to ext3301. Scroll down the bottom of super.c and you should see the init_ext2_fs() function, which takes the form of the module's init function. Find the structure it registers and change the name member to ext3301. The kernel will now see this as a new file system that can be loaded into the kernel (although at this point it just operates exactly as ext2 does.
Now type make again and try to load the module. Does it work?
Now that the kernel knows about the new file system, we have to create a new image file and format it with an ext3301 file system. Since we haven't modified the inode structure, we can use the original ext2 formatting tool to create this image.
First, create an empty image file ready to format:
dd if=/dev/zero of=500K.img bs=500K count=0 seek=1
This copies 500 KB of data from /dev/zero (so, nothing!) to a file called 500K.img. Notice that we gave the count as 0? This tells dd to create what is called a sparse file, a file that when empty takes up very little space on disk. Compare the output of ls -hl 500K.img and du -h 500K.img for this file and you will see that it takes up less space than 500 KB.
Now, format the file system (remember that although our file system differs from ext2 the inode structure remains the same so we can mount ext2-formatted file systems with our driver):
mke2fs 500K.img
Create a mountpoint under the existing file system (do this once only): mkdir -p /mnt/ext3301.
Since Linux can only mount block devices as file systems, we must turn our image into a psuedo block device. This is known as a loop device. Luckily, the kernel can do this for us automatically when mounting:
mount -t ext3301 -o loop 500K.img /mnt/ext3301
Note that we specified -t ext3301 - this is the file system type and it tells the kernel to use your file system driver when accessing this mountpoint (if you omit it the kernel will probably autoselect the driver for ext2 which we don't want). The -o loop mount option tells the kernel that we are mounting something that isn't a block device and it should set up a loop mount.
Try creating a new file and directory in the file system under /mnt/ext3301.
When you have finished with the file system (or wish to reload your driver), you must unmount the loop device with umount /mnt/ext3301.
You are now ready to start working on assignment 3 when it is released. We strongly recommend reading up on the internals of ext2 now: it will be invaluable when you start assignment 3.
A good document on ext2 is located in the kernel source documentation: under Documentation/filesystems/ext2.txt.