Creating a Filesystem

Filesystems are not created by the kernel itself but by the mke2fs userspace tool. Although I am more concerned with the work of the kernel, I discuss this important aspect of filesystem work briefly below. mk2efs not only shares the space on a partition between management information and useful data, but also creates a simple directory structure on the storage medium so that the filesystem can be mounted.

Which management information is meant? When a newly formatted12 Ext2 partition is mounted, it already contains a standard subdirectory named lost+found to hold defective blocks of the data

12It is, of course, possible to argue about the subtle difference between low-level formatting and filesystem creation and to insist on a distinction between the two terms. I take a pragmatic approach and use both terms synonymously, as most Unix users do, because there is no danger of confusing the two.

medium (thanks to the quality of today's hard disks, it is almost always empty). This involves the following steps:

1. An inode and a data block are reserved and initialized for the root directory. The data block contains a file list with three entries: ., .., and lost+found. As this is the root directory, both . and .. point back to the root inode that represents the root directory.

2. An inode and a data block are also reserved for the lost+found directory, which has only two entries: .. points back to the root inode and ., as usual, points to the inode of the directory itself.

Although mke2fs is designed for use with block special files, it is possible to use a regular file on a data medium to create a filesystem. This is because of the ''everything is just a file philosophy'' of Unix according to which the same routines can be used to handle regular files and block special devices, at least from the userspace perspective. Using regular files instead of block special files is a very good way of experimenting with filesystem structures without having to access existing filesystems with possibly important data or without having to bother with slow floppy disk drives. For this reason, I briefly discuss the steps involved below.

First, a file of arbitrary size is created using the dd standard utility.

[email protected]> dd if=/dev/zero of=img.1440 bs=1k count=1440 1550+0 records in 1440+0 records out

This creates a file with a size of 1.4 MiB, the same capacity as a 3.5-inch floppy disk. The file contains only zero bytes (with ASCII value 0) generated by /dev/zero.

mke2fs now creates a filesystem on the file:

[email protected]> /sbin/mke2fs img.1440

mke2fs 1.40.2 (12-Jul-2007)

img.1440 is not a block special device.

File System label=

OS type: Linux

Block size=1024 (log=0)

Fragment size=1024 (log=0)

184 inodes, 1440 blocks

72 blocks (5.00%) reserved for the super user

First data block=1

Maximum file system blocks=1572864

1 block group

8192 blocks per group, 8192 fragments per group 184 inodes per group

The data in img.1440 can be viewed using a hex editor to draw conclusions on the filesystem structure. od and hexedit are classic examples of such editors, but all distributions include numerous alternatives ranging from Spartan text-mode tools to sophisticated, user-friendly graphic applications.

An empty filesystem is not very interesting, so we need a way of filling the sample filesystem with data. This is done by mounting the filesystem using the loopback interface, as shown in the following example:

[email protected]> mount -t ext2 -o loop=/dev/loop0 img.1440 /mnt

The filesystem can then be manipulated in such a way as to give the impression that it is located on a regular partition of a block device. All changes are transferred to img.1440 and can be examined there.

Continue reading here: Filesystem Actions

Was this article helpful?

0 0