# 104.2 Maintain the integrity of filesystems

Candidates should be able to maintain a standard filesystem, as well as the extra data associated with a journalling filesystem.
Key Knowledge Areas
* Verify the integrity of filesystems.
* Monitor free space and inodes.
* Repair simple filesystem problems.
Once a filesystem has been created on your block device you will want to know how to monitor the filesystem and check it for errors, recovering from errors where possible. Fortunately the filesystem provide several command and utilities to aid in this process.

## Monitoring Disk Usage

The df (disk free) and disk usage (du) commands can be used to report on the amount of free disk space and query how much space directories and files are using. df works on a device level, as opposed to a directory level.
The df tool shows used and available disk space. By default this is given in blocks of 1K.
* $ df -h
* Filesystem Size Used Avail Use% Mounted on
* /dev/hda9 289M 254M 20M 93% /
* /dev/hda2 23M 7.5M 14M 35% /boot
* none 62M 0 61M 0% /dev/shm
* /dev/hda5 1.4G 181M 1.1G 13% /share
* /dev/hda7 787M 79M 669M 11% /tmp
* /dev/hda3 4.3G 3.4G 813M 81% /usr
* /dev/hda6 787M 121M 627M 17% /var
* //192.168.123.2/share12G 8.8G 3.7G 71% /mnt/smb
The du command will display disk usage. This is done on a per directory basis. du cannot display available space since this information is only available at a device level.
The following command will list the current usage of the /etc directory in human readable units (using the -h switch) and will only print the grand total (using the -s switch):
* # du -sh /etc
* 62M /etc/

## File System Checking, Repair and Maintenance

As with filesystem monitoring there are numerous tools for maintaining a Linux filesystem which are provided by the creators of the respective filesystem.
If the file system is damaged or corrupt, then the fsck utility should be run against the partition (the minimum requirement is that the file system be unmounted or mounted read-only).
fsck acts as a front that automatically detects the file system type of a partition. Then as with mkfs, the tools fsck.ext2, fsck.ext3 fsck.ext4 or fsck.xfs will be called accordingly to carry out the system check and, if necessary, repair. Since ext3 is the main filesystem type for Linux there is a e2fsck command that only handles this filesystem type. You invoke the filesystem check as follows explicitly specify a file system type with the following syntax:
fsck –t <fstype> <device>
Example: Checking a reiserfs filesystem on the /dev/sdb10 device:
* # fsck –t reiserfs /dev/sdb10
* # fsck.reiserfs /dev/sdb10
Ext File System Maintenance utilities
As the extended filesystem is the most widely used and deployed filesystem on Linux, the tools for ext filesystem support are more numerous and comprehensive than for other filesystems.
Ext File System Debug Commands
The debugfs and dumpe2fs are seldom used but can be useful in providing low level information about an ext2, ext3 or ext4 filesystem.
debugfs [ -b blocksize ] [ -s superblock ] [ -f cmd_file ] [ -R request ] [ -V ] [ [ -w ] [ -c ] [ -i ] [ device ] ]
The debugfs program is an interactive file system debugger. It can be used to examine and change the state of an ext2/3/4 file system.
Once in the debugfs shell, internal commands can then be used to change directory, examine inode data, remove files, create links, dump the ext3 journal logs etc. While this is a very powerful command, it should be used with caution, generally only after the fsck command has failed to make any headway.
dumpe2fs [ -bfhixV ] [ -ob superblock ] [ -oB blocksize ] device
dumpe2fs prints the super block and block group information for the filesystem present on device.
* dumpe2fs /dev/hda1
* dumpe2fs 1.35 (28-Feb-2004)
* Filesystem volume name: /boot1
* Last mounted on: <not available>
* Filesystem UUID: d741042c-3eaf-49ee-94c1-7dd8c5459764
* Filesystem magic number: 0xEF53
* Filesystem revision #: 1 (dynamic)
* Filesystem features: has_journal ext_attr resize_inode dir_index filetype needs_recovery sparse_super
* Default mount options: (none)
* Filesystem state: clean
* Errors behavior: Continue
* Filesystem OS type: Linux
* Inode count: 25584
* Block count: 102280
* Reserved block count: 5114
* Free blocks: 80564
* Free inodes: 25537
* First block: 1
* Block size: 1024
* Fragment size: 1024
* Reserved GDT blocks: 256
* Blocks per group: 8192
* Fragments per group: 8192
* Inodes per group: 1968
* Inode blocks per group: 246
* Filesystem created: Sat May 7 10:40:51 2005
* Last mount time: Sun May 29 04:08:01 2005
* Last write time: Sun May 29 04:08:01 2005
* Mount count: 10
* Maximum mount count: -1
* Last checked: Sat May 7 10:40:51 2005
* Check interval: 0 (<none>)
* Reserved blocks uid: 0 (user root)
* Reserved blocks gid: 0 (group root)
* First inode: 11
* Inode size: 128
* Journal inode: 8
* Default directory hash: tea
* Directory Hash Seed: 50108791-6a0a-41ff-9608-0485c993eaf9
* Journal backup: inode blocks
* Group 0: (Blocks 1-8192)
* Primary superblock at 1, Group descriptors at 2-2
* Block bitmap at 259 (+258), Inode bitmap at 260 (+259)
* Inode table at 261-506 (+260)
* 0 free blocks, 1942 free inodes, 2 directories
* Free blocks:
* Free inodes: 27-1968
tune2fs
tune2fs allows you to adjust various filesystem parameters on Linux extended filesystems. The following is a list of the most common parameters used to adjust extended filesystem settings:
* -c sets the number of times a filesystem will be mounted before a filesystem check is forced. This is usually at next boot but can be run when a filesystem is manually unmounted.
* -C sets the number of times the filesystem was mounted since it was last checked.
* -L sets the volume label, this used to be used to uniquely identify hard disk partitions but is being replaced with UUIDs.
* -i sets the maximum time between filesystem checks. A filesystem check will be forced when either the time expires or the maximum number of mounts has been exceeded, which ever comes first.
* -j adds journaling to an ext2 filesysem making it an ext3 filesystem.
Running tune2fs -l will print out the current settings for a filesystem.
* tune2fs 1.41.11 (14-Mar-2010)
* Filesystem volume name: <none>
* Last mounted on: /
* Filesystem UUID: 6f4746c6-777e-4937-92ee-de98cf8f5aa4
* Filesystem magic number: 0xEF53
* Filesystem revision #: 1 (dynamic)
* Filesystem features: has_journal ext_attr resize_inode dir_index filetype needs_recovery extent flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
* Filesystem flags: signed_directory_hash
* Default mount options: (none)
* Filesystem state: clean
* Errors behavior: Continue
* Filesystem OS type: Linux
* Inode count: 26558464
* Block count: 106205707
* Reserved block count: 5310285
* Free blocks: 62912050
* Free inodes: 25721258
* First block: 0
* Block size: 4096
* Fragment size: 4096
* Reserved GDT blocks: 998
* Blocks per group: 32768
* Fragments per group: 32768
* Inodes per group: 8192
* Inode blocks per group: 512
* RAID stride: 32747
* Flex block group size: 16
* Filesystem created: Sat Feb 13 00:48:21 2010
* Last mount time: Tue Sep 28 07:27:39 2010
* Last write time: Tue Sep 21 09:14:03 2010
* Mount count: 22
* Maximum mount count: 25
* Last checked: Tue Sep 21 09:14:03 2010
* Check interval: 15552000 (6 months)
* Next check after: Sun Mar 20 09:14:03 2011
* Lifetime writes: 709 GB
* Reserved blocks uid: 0 (user root)
* Reserved blocks gid: 0 (group root)
* First inode: 11
* Inode size: 256
* Required extra isize: 28
* Desired extra isize: 28
* Journal inode: 8
* First orphan inode: 404853
* Default directory hash: half_md4
* Directory Hash Seed: 982f8e6c-db49-49b0-8f65-bce3725b5196
* Journal backup: inode blocks
XFS File System Maintenance Utilities
The tools that come with XFS for filesystem integrity checking are xfs_info and xfs_metadump.
xfs_metdump is a filesystem debugging utility, that dumps xfs filesystem meta-data to a file. The file can then be used to debug the files or as a backup. Later the meta-data can be restored with the xfs_restore utility.
Used files, terms and utilities:* du
* df
* fsck
* e2fsck
* mke2fs
* debugfs
* dumpe2fs
* tune2fs
* xfs tools (such as xfs_metadump and xfs_info)
Previous Chapter | Next Chapter