previous contents up next

Unix for Advanced Users

12. The Unix Filesystem

12.4. Unix filesystem commands

We covered common commands for navigating file systems and for manipulating files in the sections Navigating Effectively and Manipulating Files and Text. Here, we emphasize commands for managing file systems.

12.4.1 Building file systems: mkfs

Before a disk partition can be used to store data, a file system must be built on it. That is, the information in the superblock and basic inodes is filled in. File systems are built using the mkfs command. mkfs requires at least two operands, one that specifies which type of file system is to be built (the command line flag for this parameter varies among Unices) and the name of the device file name that points to the partition. For example, mkfs -t minix /dev/sdb2 builds a minix file system on the second partition of the second SCSI disk on a Linux machine.

12.4.2 Checking and repairing file systems: fsck

fsck is used to check file system integrity and to repair damaged file systems. Typically it is run automatically every time a system boots up. If a file system was cleanly unmounted before the system went down, fsck checks to see how many times the file system has been mounted since the last thorough check. If the count is greater than a maximum, it thoroughly checks the file system, repairing errors that it finds. Thorough checking and repair is forced if the file system was not cleanly unmounted before the machine went down.

A system may not boot if the root file system is damaged. If so, a rescue disk system is needed to repair it. Rescue disk systems usually consist of one or two floppy disks that contain a kernel and a tiny root file system that contains the bare minimum that is needed to get a system up and repaired. fsck ought to be on rescue disk systems. Once the file sytem is repaired using fsck, and damaged files have been replaced, the system should be bootable.

It is extremely important that a file system not be mounted when it is checked using fsck, or if it is mounted that that it is mounted read-only. Otherwise, fsck and other some other process may both try to alter the file system at the same time resulting in disaster.

12.4.3 File system organization and mount table: fstab

When a system boots up the kernel mounts a root file system. The root file system must contain programs and files that get the system started. Once the system is started other non-essential file systems are mounted. These file systems are attached to the root file system at mount points, which are empty directories until file systems are attached at them.

What gets mounted at boot up? The partition that is to be mounted as the root file system is usually compiled into the kernel, or in some operating systems it can be passed to the kernel as an option at boot time. Non-essential file systems are specified in a file that is usualy called /etc/fstab. The table contains for each file system to be mounted the device filename (disk partition, floppy drive, cdrom, etc), the directory at which the file system will be mounted - the mount point, the type of file system that will be mounted (e.g., ufs, msdos, ext2, iso9660, etc) and options for mounting the file system. An important option is noauto, because it prevents a file system from being mounted automatically at boot up. The advantage of using noauto in fstab is that the associations between device file, mount point, file system type, and options in the table can be used later when mounting file systems.

Here is a sample /etc/fstab from a Linux system.

# device                 mountpoint       filesystemtype   options   dump   fsckorder 
 
/dev/sda3                 /                         ext2   defaults 1 1
/proc                     /proc                     proc   defaults
/dev/sda5                 /home                     ext2   defaults,nosuid 1 2
/dev/sda1                 /msdosc                   msdos  defaults,check=strict,gid=502,umask=0002,quiet 0 0
/dev/fd0                  /mnt/floppy               auto   defaults,noauto,user,nosuid 0 0
/dev/scd0                 /mnt/cdrom                iso9660 defaults,noauto,ro,user,nosuid 0 0
/dev/sdb1                 /mnt/jaz                  ext2   defaults,noauto 0 0
/dev/sda2                 none                      swap   sw
/dev/sdc1                 /newdisk                  ext2   defaults,noauto 0 0
/dev/sdc3                 /backup                   ext2   defaults,noauto 0 0
/dev/sda4                 none                      ignore 0 0 0

File systems are mounted using the mount command and unmounted using the umount command.

12.4.4 Mounting file systems: mount

If an entry for a file system exists within fstab, it can be mounted by invoking mount with only the mount point as an option. For example, mount /backup would mount at /backup, the device that was associated with it in fstab.

If no appropriate entry exists in fstab, the filesytem type, options, device file and mount point must all be specified on the command line. For example, mount -t hfs -o ro /dev/hdc1 /mnt/macfs mounts a Macintosh hfs file system on the first partition of harddisk c in read-only mode at /mnt/macfs on a linux system.

12.4.5 Unmounting file systems: umount

To unmount a file system, invoke umount with only the mount point as a parameter. For example, umount /backup unmounts the file system that is currently mounted at /backup.

12.4.6 Information about file systems: df and mount

df prints a table of mounted file systems showing device files, mount points, disk usage and often the amount of free space on the file system. For sample output see 7.2.4 How come the disk is full? df.

df does not show file system types or options. Command line options can be used to show file systems of certain types. To see file system types and options, use mount with no parameters. Here is sample output from mount on a Linux system.

/dev/sda3 on / type ext2 (rw)
/proc on /proc type proc (rw)
/dev/sda5 on /home type ext2 (rw,nosuid)
/dev/sda1 on /msdosc type msdos (rw,check=strict,gid=502,umask=0002,quiet)
/dev/sdc3 on /backup type ext2 (rw)

12.4.7 Disk usage summary: du

Disk space becomes short at times, and it is useful to know just how much disk space is used where within a file system. du summarizes disk space usage. Used without options, du prints the cumulative amount of disk space that is used by directory tree that is rooted at the current directory. It also travels the entire directory tree printing for each subdirectory tree the cumulative space used within it. du produces huge amounts of output if run from the root directory.

Sample output from a directory tree might look like:

701     ./Forms
304     ./Notes
19      ./devel/parse/doc
123     ./devel/parse/lib_date_3.2
93      ./devel/parse/regexp
1568    ./devel/parse
35      ./devel/tools
5       ./devel/turfdump/RCS
257     ./devel/turfdump
1863    ./devel
3148    .  
Here, the current directory and all of its descendents consume 3,148 blocks each of 1024 bytes, and subdirectory devel and its descendents contain 1,863 blocks.

The amount and type of output can be changed by specifying command line options. For example, -s prints disk space used by the current directory tree without reporting sizes of subtrees within it. Another option prints only the space used by files within directories and not cumulative space within subdirectory trees, but the command-line flag for this option varies among Unices.

12.4.8 Sniffing links: ls and find

Sometimes it is necessary to find all the hard links that are associated with a file. Recall that a hard link is the link between a directory entry and an inode. Some inodes have more than one directory entry pointing at them. Links other than the first are made with the ln command. The number of links can be identified from the output of ls -l. The number of links is given just after the file permissions and just before the file's owner. For example, in the following

total 42
-rw-r--r--   2 rrepasky users          5 Jul  6 17:12 file1
-rw-r--r--   2 rrepasky users          5 Jul  6 17:12 fileA
drwx------   2 rrepasky users        512 Jun 21 15:36 mail/
-rw-r--r--   1 rrepasky users      18366 Jun 15 15:40 ppp-scripts.tar.gz
ppp-scripts.tar.gz is a plain file with a single link to its inode. The directory mail has two links, one between the "mail" entry in current directory and the inode and one between the "." entry in mail and the inode. File1 and filea are both plain files with two links.

To find hard links to the same inode, it is necessary to know the inode number. Inode numbers can be displayed by using ls -i. In our example of file1 and fileA, ls -i yields

503956 file1          154432 mail/
503956 fileA          503948 ppp-iu.tar.gz.
Here, file1 and fileA share the same inode, and the mystery is solved. Finding hard links when the links are from different directories is more difficult.

The find command can be used to find all directory entries that reference a given inode. A desperate system administrator who has no idea where links originate might run find from the root directory (e.g., find / -inum 503956 -print).

previous contents up next