Saturday, September 3, 2016

File System Integrity

Checking the File System Integrity 
As with any other operating system, Linux partitions can sometimes encounter problems. These problems can be caused by a variety of conditions; however, my experience has been that the number-one source of partition corruption issues is a power outage that results in an unclean shutdown of the system. If your system goes down without properly unmounting a mounted partition, it’s very likely that data corruption will occur. 
This is a good argument for implementing an uninterruptible power supply (UPS) on your systems. Linux doesn’t like unexpected shutdowns! 
If something like this happens, you need to check your file systems for errors and make repairs, if necessary. This is done using the File System ChecK utility (fsck). To use this utility, you must first umount the file system you want to check. Then enter fsck device at the shell prompt. For example, if you want to check the file system on the first partition of the second hard drive in your system, you would enter fsck /dev/sdb1. The utility will then check the file system and report any errors encountered (or the lack thereof ). Here is sample output from fsck: 
openSUSE:~ # umount /dev/sdb1

fsck is really just a front end to the following utilities: 
• /sbin/fsck.msdos
• /sbin/fsck.cramfs
• /sbin/fsck.ext3
• /sbin/dosfsck
• /sbin/fsck.ext2
• /sbin/fsck
• /sbin/e2fsck
• /sbin/fsck.ext4
• /sbin/fsck.vfat
• /sbin/fsck.minix
• /sbin/fsck.reiserfs 
If you run fsck without any options, it will query your /etc/fstab file to determine the file system type of the partition you want to check and then load the appropriate module for the file system. As you can see in the preceding list, versions of fsck exist for the Reiser, ext2, ext3, and ext4 file systems. 
The fsck.reiserfs utility can be run using the reiserfsck front end. 
After the check is complete, you can remount the partition using the mount command. 
One very useful feature of e2fsck is its ability to restore a damaged superblock on ext2/3/4 file systems. Remember from earlier in this chapter that the superblock is the block at the beginning of the partition that contains information about the structure of the file system. Also, most Linux file systems keep backup copies of the superblock at various locations in the partition. If the super- block gets corrupted, you can restore it using one of these backup copies. The syntax is e2fsck –f –b backup_superblock device. The –f option tells e2fsck to force a check, even if the file system seems to be clean. The –b option tells e2fsck which copy of the superblock it should use. 
Here are some other useful file system management utilities: 
tune2fs This utility is used to adjust various file system parameters on ext2/3/4 file systems. These are some of the options you can use: 
–c Sets the number of mounts (n) after which the file system will automatically be checked by fsck. 
–i interval d|m|w Sets the maximum time between file system checks in (days), (months), or (weeks). 
–j Adds a journal to an ext2 file system. 
–L volume_label Sets the volume label of the file system.
Here is an example: 
      openSUSE:/ # tune2fs -L NewVol /dev/sdb1
      tune2fs 1.42.8 (20-Jun-2013)
reiserfstune This is the Reiser equivalent of the tune2fs utility. 
dumpe2fs Displays lots of useful information about ext2/3/4 file systems. Use the –h
option to make the output more readable. An example follows: 
openSUSE:/ # dumpe2fs -h /dev/sdb1 
• debugfs The debugfs program is an interactive file system debugger. It can be used to examine or even change the state of an ext2/3/4 file system. The syntax is debugfs device. When you run this command, the debugfs: prompt is displayed, as shown here: 
  openSUSE:/sbin # debugfs /dev/sdb1
  debugfs 1.42.8 (20-Jun-2013)
  debugfs:
By default, the file system is opened in read-only mode. If you need to make changes to the file system, you need to include the –w option to open it in read-write mode. Here are some of the commands you can enter at the debugfs: prompt: 
• show_super_stats Displays information equivalent to that displayed by dumpe2fs. 
• write in_file out_file Allows you to copy a file from the file system to another file system. This can be extremely useful in situations where you are trying to salvage an important file from a mangled file system. 
stat file_or_directory_name Displays extended information about the file or
directory specified. An example follows for a file named myfile2.txt: 
     Inode: 12   Type: regular    Mode:  0644   Flags: 0x80000
     Generation: 556089601    Version: 0x00000000:00000001
     User:     0   Group:     0   Size: 0
     File ACL: 0    Directory ACL: 0
•    Links: 1   Blockcount: 0
     Fragment:  Address: 0    Number: 0    Size: 0
      ctime: 0x4d812f93:d2d91b5c -- Wed Mar 16 15:45:55 2011
      atime: 0x4d812f93:d2d91b5c -- Wed Mar 16 15:45:55 2011
      mtime: 0x4d812f93:d2d91b5c -- Wed Mar 16 15:45:55 2011
     crtime: 0x4d812f93:d2d91b5c -- Wed Mar 16 15:45:55 2011
     Size of extra inode fields: 28
     EXTENTS:
     lines 1-12/12 (END)
list_deleted_inodes Displays a list of delete inodes in the file system.
You can do a lot with debugfs. We don’t have the time or space to cover everything here.
Have a look at the debugfs man page for more information. 

• xfs_admin The xfs equivalent of tune2fs. This utility actually uses the xfs_db executable to modify various parameters of an xfs file system. The file system must be unmounted before you can make any changes. Options you can use with this utility include the following: 
–j   Enables version 2 log format, which is a journal format supporting larger log buffers. 
–l   Displays the file system’s label. 
–u   Displays the file system’s universally unique identifier (UUID).
–L label Sets the file system’s label. 
–U uuid Sets the UUID of the file system. 

xfs_info Displays useful information about xfs file systems. 
xfs_metadump Dumps xfs file system metadata (such as filenames and file sizes) to a file.
Let’s next look at how you identify open files on a Linux file system. 


No comments:

Post a Comment