Section 14.5. Maintaining the File System


[Page 580 (continued)]

14.5. Maintaining the File System

Several regular activities are required to assure the proper functioning of the file systems on your system. In addition to this, there may be instances where you need to add new file systems to the system.

14.5.1. File System Integrity

When the system first boots, all run level's boot scripts run a utility called fsck to check the integrity of the file system (Figure 14-3).


[Page 581]

Figure 14-3. Description of the fsck command.

Utility: fsck -p [ fileSystem ]*

fsck (file system check) scans the specified file systems and checks them for consistency. The kind of consistency errors that can exist include:

  • A block is marked as free in the bitmap but is also referenced from an inode.

  • A block is marked as used in the bitmap but is never referenced from an inode.

  • More than one inode refers to the same block.

  • An invalid block number.

  • An inode's link count is incorrect.

  • A used inode is not referenced from any directory.

If the -p option is used, fsck automatically corrects any errors that it finds. Without the -p option, it prompts the user for confirmation of any corrections that it suggests. If fsck finds a block that is used but is not associated with a named file, it connects it to a file whose name is equal to the block's inode number in the "/lost+found" directory.

If no file systems are specified, fsck checks the standard file systems listed in "/etc/fstab."

Linux has specialized fsck programs for different types of file systems. For example, when checking an ext2 or ext3 file system, fsck act as a front-end to e2fsck, which is the program that actually checks the file system.

For information about inodes, see Chapter 13, "Linux Internals."


Fortunately, fsck is very good at correcting errors. This means that you'll probably never have the joy of patching disk errors by hand as was done in "the good old days."

14.5.2. Disk Space

As I just mentioned, disk errors are uncommon and are generally corrected automatically. Disk usage problems, on the other hand, are very common. Many users treat the file system as if it were infinitely large, and create huge numbers of files without much thought. When I taught UNIX at UT Dallas, the disks would invariably fill up on the last day of the semester, just as everyone was trying to complete his project. Students would try to save their work from vi, and vi would respond with a "disk full" message. When they quit from vi, they would find that their file had been deleted.

To avoid running out of disk space, it's wise to run a shell script from cron that periodically runs the df utility to check the available disk space (Figure 14-4).

Figure 14-4. Description of the df command.

Utility: df [ fileSystem ]*

df (disk free) displays a table of used and available disk space on the specified mounted file systems. If no file system is specified, all mounted file systems are described.



[Page 582]

The exact format varies a bit between Linux distributions, partly because of the way file systems are mounted and named. Here's an example of df in action from Mandrake Linux:

$ df                ...list information about all file systems. Filesystem            Size  Used Avail Use% Mounted on  /dev/ide/host0/bus0/target0/lun0/part7                        2.4G  1.5G  795M  66% /  /dev/ide/host0/bus0/target0/lun0/part5                        198M  7.6M  180M   5% /boot  /dev/ide/host0/bus0/target0/lun0/part1                         16G  2.7G   14G  17% /mnt/windows $ _ 


Here's output from df on a SuSE Linux system:

$ df Filesystem        1K-blocks      Used Available Use% Mounted on /dev/hda6           3635216   1759508   1691044 51%  / tmpfs                 95744        16     95728  1%  /dev/shm $ _ 


You can find out how much disk space is left on the device where your home directory resides just as easily:

$ df . Filesystem        1K-blocks      Used Available Use% Mounted on /dev/hda6           3635216   1759508   1691044 51%  / $ _ 


If df reports that a disk is greater than 95% full, your script could detect this and send you some e-mail. Even better, your script could then run the du utility to determine which users are using the most disk space, and then automatically send them mail suggesting that they remove some files (Figure 14-5).

Figure 14-5. Description of the du command.

Utility: du [-h] [-s] [ fileName ]*

du displays the number of kilobytes that are allocated to each of the specified filenames. If a filename refers to a directory, its files are recursively described. When the -h option is used, the numeric values are changed to more human-readable values (i.e., 63844 blocks is displayed as 63M). When used with the -s option, du displays only the grand total (summary) for each file or directory. If no filenames are specified, the current directory is scanned.



[Page 583]

In the following example, I used du to find out how many kilobytes my current directory and all its files were using up. I then obtained a file-by-file breakdown of the disk usage:

$ du -s -h       ...obtain grand total of current directory. 9M    . $ du .                  ...obtain file-by-file listing. 91    ./proj/fall.89 158   ./proj/summer.89/proj4 159   ./proj/summer.89 181   ./proj/spring.90/proj2 21    ./proj/spring.90/proj1 204   ./proj/spring.90 455   ./proj ...                     ...other files were listed here. 38    ./sys5 859   ./sys6 9291  . $ _ 


14.5.3. Creating New File Systems

If you add a new disk drive to your system, you must do a few things before your system can use it as part of the file system:

1.

Partition and format the drive if necessary (as discussed in Chapter 2, "Installing Your Linux System").

2.

Create a file system on the medium.

3.

Mount the disk into the file system hierarchy.

Create a file system on the medium using mkfs (Figure 14-6).

Figure 14-6. Description of the mkfs command.

Utility: mkfs -t type specialFile [ sectorCount ]

mkfs creates a new file system on the specified special file. A new file system consists of a superblock, an inode list, a root directory, and a "lost+found" directory. The file system is built to be sectorCount sectors in size. Only a super-user can use this command.

As with fsck, mkfs is really a front-end to a specific file system creator for each specific file system supported (e.g., mke2fs), determined by the file system type specified by type.


Once the file system is created, it may be connected to the root file system by using the mount utility described on page 157 in Chapter 4, "GNU Utilities for Power Users."


[Page 584]

14.5.4. Backing up File Systems

Making a backup copy of file system information is the most important and most frequently overlooked task a system administrator should perform. It's frustrating to spend time at it, since you believe you'll never need the backup. But just like buying insurance on your car, you should do it, because if you ever do need it, not having it will be a big problem. The procedure and utilities for backing up the file system are described in Chapter 4, "GNU Utilities for Power Users."

Some distributions install dump and restore (they may be called e2fsdump and e2fsrestore), which are backup and restore utilities based on the Berkeley dump and restore utilities. dump (or e2fsdump) will work only on an ext2 or ext3 file system, as it is specific to those file systems, so these utilities are not generally useful if you might ever use other types of file systems.




Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net