Managing Filesystems: Beyond the Basics

Whenever I sit down at an unfamiliar computer system for the first time, the first thing I do is run the df command. I like to get a feel for the landscape of that system, so to speak, before I proceed to explore it further. This makes sense to me since managing the filesystem is always a central area of concern for any system administrator. The basic tasks related to filesystems are simple and familiar to almost everyone: managing and mounting filesystems, performing simple filesystem consistency checks with fsck, and the like. This month, I delve deeper into this topic, discussing other tools for managing and manipulating filesystems, and describing the process for adding a new disk or partition.

Guru Lou Shot 3
Files Systems Galore: You can add file systems to a new kernel with Linux’s GUI-based configurator.

Whenever I sit down at an unfamiliar computer system for the first time, the first thing I do is run the df command. I like to get a feel for the landscape of that system, so to speak, before I proceed to explore it further. This makes sense to me since managing the filesystem is always a central area of concern for any system administrator. The basic tasks related to filesystems are simple and familiar to almost everyone: managing and mounting filesystems, performing simple filesystem consistency checks with fsck, and the like. This month, I delve deeper into this topic, discussing other tools for managing and manipulating filesystems, and describing the process for adding a new disk or partition.

Linux supports a very large number of filesystems for local disk partitions (in addition to several others that can be used to access remote disks via the network). You can determine which of these are supported in the currently running Linux kernel by examining the contents of the /proc/filesystems file.

Adding kernel support for any of these filesystems is quite easy and may be done by selecting the corresponding configuration options when building a new kernel. For example, if you use makexconfig, then you can simply select the desired filesystem type within its Filesystems category. If you need support for a filesystem type because you want to move a disk from another environment onto a Linux system, be aware that you will also need to enable the appropriate partition type (via the Partition Types category) if the disk’s original operating- system environment uses a different partition-table format from the one that Linux uses.

The second extended filesystem, referred to as ext2, is the native Linux filesystem right now. It is very similar to other modern Unix filesystems but most closely resembles the Berkeley Fast Filesystem used by BSD systems. The maximum size of an ext2 filesystem is 4 TB, while the maximum file size is currently limited to 2 GB by the Linux kernel.

ext2 revolves principally around two internal data structures: the superblock and the inodes. The superblock is a table that contains important information about the filesystem, such as its label, size, and total number of inodes. As such, the superblock serves as a sort of master description of the filesystem structure. Accordingly, without a superblock, the filesystem becomes unreadable. For this reason, there are duplicate superblocks placed at various points within the filesystem.

The inodes are the basic file-level data structure, and every file within the filesystem is physically described by one of them. The inodes contain information such as the file creation and modification times, the total file size, and a list of blocks that actually contain the file’s data. For larger files, the latter can include disk locations for additional lists of data blocks (known as indirect blocks) or even double or triple indirect lists of blocks. Filenames are associated with inodes via directory entries, which consist of filename-and-inode pairs.

Linux offers a number of utilities for creating and manipulating filesystems. I will consider some of them in the context of adding a new disk (or disk partition) to a Linux system (see the Adding a Disk/Partition to a Linux System sidebar, pg. 78, for an overview of these steps). The procedure for doing this is quite intuitive, but at present you must perform them from the Linux command line since most of the graphical administrative tools provide little support for this process. I’ll begin the discussion at the point after the new disk has been attached to the computer system, and will gloss over the details of the more familiar parts of the process, such as mounting a filesystem.

Adding a Disk/Partition to a Linux System

Adding a new disk or partition to the Linux filesystem is straightforward, and the steps for doing so are outlined below. (The corresponding commands are listed in parentheses.)

1. Physically attach the disk to the computer system (if necessary).

2. Partition the new disk if desired (fdisk or cfdisk).

3. Determine the special file associated with the new disk/partition (MAKEDEV).

4. Create a filesystem on the partition (mkfs).

5. Check the new filesystem for consistency errors (fsck).

6. Mount the new filesystem (mount).

7. Specifying the filesystem’s boot-time behavior and other options in the filesystem configuration file (/etc/fstab).

8. Modify filesystem characteristics on ongoing basis as necessary (tune2fs).

Finding the Special Name

Before you can create a filesystem on the physical disk (or disk partition) you are adding, you first need to know which special device name to use when referring to that disk. There are a couple of ways of figuring this out. For example, you can examine the boot logs via the dmesg command (pipe it to grep and search for [hs]d[a-z]) or by looking in the log file if you have captured it via the syslog facility. Alternatively, you can examine the /proc/ partitions file to see a list of all disk partitions that the system recognizes if you are using the /proc filesystem (which most distributions enable by default).

Very occasionally, you will need to create the device-special files for the disk. You can do so with the MAKEDEV script found in the /dev directory, as in this example, which would create special files for the sixth SCSI disk:

# cd /dev; MAKEDEV sdf

Tip: Partition Placement and Performance

If you’re adding a new disk to Linux system where I/O performance is important, and you’re planning to divide the disk into multiple partitions, a little forethought about partition placement can gain you a nontrivial performance advantage.

For example, filesystems for which you expect random-access I/O patterns to dominate (i.e. a filesystem holding a large database, a large number of small files, and the like) can benefit by being placed at the physical center of the disk. Since such I/O is seek-time limited, minimizing the distance the disk heads need to move produces an appreciable I/O speed up.

Therefore, if you want to divide the disk in half but also want to favor the I/O for this particular filesystem, then create three partitions comprising one-fourth, one-half, and one-fourth of the disk, respectively.

Creating Partitions

Next, you need to use a utility such as fdisk in order to divide the disk into partitions. In this case, I use cfdisk, a pseudo-graphical partitioning tool present on most Linux systems. You start the utility by entering the cfdisk command and specifying the desired disk as its argument:

# cfdisk /dev/sdf

Guru cfDisk 1
Figure One: The cfdisk disk-partitioning utility.

The utility is illustrated in Figure One (which shows a fully partitioned disk).

The cfdisk main window always displays a table of the current disk-partition layout. The successive columns in the table indicate the partition name/ special file, any flags set for it, the type of partition (primary or logical/extended), the partition’s label (if any), and the partition size in megabytes. You can use the up- and down-arrow keys to move the highlight bar within this list.

Commands that are applicable to the currently selected item are displayed at the bottom of the screen and may be accessed via the left- and right-arrow keys or by pressing the first letter of the command name. For example, in the figure, cfdisk commands will act upon the first partition on the disk, and cfdisk’s Type command, which is used to specify the partition type, is currently active, although it has not yet been executed.

For disks that have unpartitioned space available, the final entry in the partition list will be one with its FS Type field set to Free Space. When such an item is selected, one of the available cfdisk commands is New, which creates a new partition. The utility will prompt you for additional data it needs, such as the partition size. After a new partition has been created, you can use other cfdisk commands to specify additional characteristics of the partition. Once you have finished manipulating the partition table with cfdisk, use the Write command to save the updated table to disk. Alternatively, exiting from the utility with Quit will discard changes you may have made. If you have made any substantive layout changes to the partition table on the disk (i.e., other than merely specifying partition labels or changing partition types), you should reboot the system before proceeding.

mke2fs Options

-L label
Specify a label for the partition (maximum of 11 DOS-compliant characters).

-b sizeSet filesystem block size — the unit in which disk space is allocated for the filesystem — specified in bytes (the default is 1024).

-cCheck the partition for bad blocks before creating the filesystem.

-m percentSpecify the percentage of filesystem space to reserve, making it accessible only by root. The default is five percent of the disk, which is half of the traditional Unix value. In these days of large multigigabyte disks, even this percentage may be worth rethinking; I’ve been pushing this value down to two to three percent recently.

-i ratioSpecify bytes/inode value: Create one inode for each chunk of this many bytes. The default value of 4096 usually creates more than you’ll ever need and probably isn’t worth changing, since the disk-space savings of doing so are trivial. Alternatively, you can use -N number to specify an explicit number of inodes to create.

-v -qSelect verbose or quiet output (respectively).

Creating a Filesystem

The next step is to create a filesystem with the mkfs command (or, more accurately, the mke2fs command; mkfs is a unified front-end utility for the filesystem-creation commands for all of the various filesystem types). The simplest form of this command involves simply specifying the filesystem type to its -t option followed by the partition where the filesystem is to be built, as in this example, which creates an ext2 filesystem on the second partition on the second SCSI disk:

# mkfs -t ext2 /dev/sdb2
# mke2fs /dev/sdb2

The equivalent direct mke2fs command is given below it. You can see some of the mke2fs arguments in the mke2fs Options sidebar above.

Here is a mke2fs command that creates a filesystem on a disk partition designed to be used for scratch files from large computational chemistry simulations:

# mke2fs -L scratch -c -q
-m 2 -N 1000 /dev/hdc1

This command creates a filesystem on the first partition on the third IDE disk after checking it for bad blocks, labeling it scratch, reserving only two percent of the space, and creating only 1,000 inodes. This latter value makes sense in this special case because there will never actually be more than 100 files and directories on the filesystem. If this assumption ever turns out to be untrue, there will be a problem.

fsck Options


Force a check even if the filesystem does not appear to need one. By default, filesystems are checked only when they are “dirty” — in other words, when they have not been dismounted properly or after a certain period of time has elapsed or number of system reboots has occurred.

Preen the filesystem, automatically fixing all problems that can be safely corrected without any data loss.

Answer yes to all prompts. This has the effect of automatically fixing all problems that are found, even ones that may potentially produce a loss of data.

-b block
Specify an alternate location for the filesystem’s superblock. This option is useful in the extremely rare instance in which the main superblock has been damaged but the majority of the filesystem is probably still intact. Alternate superblocks are typically found at the locations 8193, 13685, and so on, and are printed out by the mkfs utility when the filesystem is created.

Checking the New Filesystem

The next step in the process is to check the new filesystem with the fsck utility. Here you specify the desired filesystem at its argument along with the -f and -y options (see the fsck Options sidebar below). When you do this, fsck performs a consistency check on the filesystem, which compares various items in the filesystem’s data structures that serve as cross references for one another.

fsck can locate and fix structural problems with filesystems such as disk blocks marked as free that are actually in use and vice versa, incorrect link counts in inodes (indicating missing or extra directory entries for the corresponding file), and inconsistencies between the inode total size value and the actual amount of data blocks contained in the lists. As such, fsck can ensure the consistency of the filesystem’s defining data structures. However, keep in mind that its scope is limited to the filesystem structure; it can do nothing for missing or corrupted data within structurally sound files.

Mounting the New Filesystem

Guru Filesystem 2
Figure Two: Adding a filesystem to /etc/fstab with linuxconf.

All that remains at this point is to mount the new filesystem and to add it to the filesystem configuration file, /etc/fstab (if desired). The step can be performed using a graphical administration tools such as linuxconf. Figure Two shows the linuxconf facility for adding a new filesystem to /etc/fstab.

Linux provides a couple of utilities useful for determining the status of and reconfiguring existing ext2 filesystems. The first of these is tune2fs. This command’s -l option will list the attributes of the specified ext2 filesystem.

Most of the items you will see here are self-explanatory and correspond to options that may be specified to the mke2fs command. Some data in the list determine when fsck will next check the filesystem, even if it is marked clean. By default, Linux checks the filesystem every 20 system reboots and every 30 days. Note that the time-period-based interval is specified in seconds.

You can modify many of these filesystem attributes using other options to tune2fs. For example, the following command disables the time-between-checks function by setting the number of seconds between checks to 0 (-i), and sets the maximum number of mounts between checks to 50 (-c):

# tune2fs -i 0 -c 25 /dev/hdb5
Setting maximal mount count to 50
Setting interval between check 0 seconds

While a more thorough discussion is warranted, space dictates that I stop now. However, these utilities should make you well-equipped to manage and manipulate your Linux filesystems.

Æleen Frisch is the author of O’Reilly & Associates’ Essential System Administration. She can be reached at aefrisch@lorentzian.com.

Comments are closed.