104.2. Maintain the integrity of filesystems
Weight: 2
Description: Candidates should be able to maintain a standard filesystem, as well as the extra data associated with a journaling filesystem.
Key Knowledge Areas:
Verify the integrity of filesystems
Monitor free space and inodes
Repair simple filesystem problems
Terms and Utilities:
du
df
fsck
e2fsck
mke2fs
debugfs
dumpe2fs
tune2fs
XFS tools (such as xfs_metadump and xfs_info)
In cases when your system crashes or loses power, your filesystems may be left in an inconsistent state, with some changes completed and some not.
Operating with a damaged filesystem is not a good idea as you are likely to further compound any existing errors.We’ll take a look at some tools to help us manage such problems.
You should always back up your filesystem before attempting any repairs!
fsck
The main tool for checking and repairing filesystems is fsck
, which, like mkfs
, is really a front end to filesystem-checking routines for the various filesystem types.
Some of these are just links to e2fsck command and they are the same
The fsck command in Linux allows us to manually check for file system inconsistencies, Fsck command needs to be run with superuser privileges or root(ubuntu 16.04 here):
In order to use fsck the partition should be unmounted, otherwise it might cause damages!
Lets simply check file system on an unmounted ext3 partition (sdb1) and try to fix errors :
This command will attempt to check /dev/sdb1, and report any errors it finds.The exit code returned by fsck is one of following conditions:
0 No errors
1 Filesystem errors corrected
2 System should be rebooted
4 Filesystem errors left uncorrected
8 Operational error
16 Usage or syntax error
32 Checking canceled by user request
128 Shared-library error
-N
option just shows what would be executed but do not attempt to repair them:
-n
causes these commands not to fix anything and just show what was going to be done:
Normally, fsck will skip parts of the filesystem marked as "clean" — meaning all pending writes were successfully made. The -f ("force") option specifies that fsck should check parts of the filesystem even if they are not "dirty". The result is a less efficient, but a more thorough check.
What are inodes?
As we said Linux treating everything as a file (even the hardware devices). The keyboard, mouse, printers, monitor, hard disk, processes, even the directories are treated as files in Linux. The regular files contain data such as text (text files), music, videos (multimedia files) etc. Set aside the regular data, there are some other data about these files, such as their size, ownership, permissions, timestamp etc. This meta-data about a file is managed with a data structure known as an inode (index node).
We can also check file systems using their UUID.(use blkid command ):
For checking a XFS filesystem, wehave to use xfs_check command
Advanced tools
There are several more advanced tools that we can use to examine or repair a filesystem.
Tools for ext2 and ext3 filesystems
tune2fs:Adjusts parameters on ext2 and ext3 filesystems. Use this to add a journal
dumpe2fs: shows all super blocks info
debugfs: interactive file system editor
Super Blocks
You may be wondering how all these checking and repairing tools know where to start. Linux and UNIX filesystems usually have a superblock, which describes the filesystem metadata, or data describing the filesystem itself. This is usually stored at a known location, frequently at or near the beginning of the filesystem, and replicated at other well-known locations. You can use the -n
option of mke2fs
to display the superblock locations for an existing filesystem.
tune2fs
The ext family of file systems also has a utility called tune2fs
, which can be used to inspect information about the block count as well as information about whether the filesystem is journaled (ext3 or ext4) or not (ext2).
The tune2fs command allows you to set assorted filesystem parameters on a mounted ext2 or ext3 filesystem.
-l
shows contents of the filesystem superblock, including the current values of the parameters:
The command can also be used to set many parameters or convert an ext2 filesystem to ext3 by adding a journal using -j option:
tune2fs -j /dev/sdd1
Also we can use tune2fs for changing or modifying partition label:
dumpe2fs
dumpe2fs command is used to print the super block and blocks group information for the filesystem present on device.
printed information may be old or inconsistent when it is used with a mounted filesystem. Don’t forget to unmount your partition before using this command.
debugfs
Is an interactive filesystem debugger. Use it to examine or change the state of an ext2 or ext3 filesystem. It opens the filesystem in read-only mode unless we tell it not to (with -w
option).
if the filesystem is mounted, is alright for inspecting, but Do not attempt repair on a mounted filesystem.
xfs_info
xfs file system has it's own family commands. xfs_info is the same as tune2fs but for xfs.
xfs_info should be used on a mounted file system.
xfsprogs package must be installed
obviously xfs_repair does not work on mounted file system.
In XFS, you can only extend file system and can not reduce it.
Monitoring free space
On a storage device, a file or directory is contained in a collection of blocks. Information about a file is contained in an inode.
Reminder : inodes keeps information such as who the owner is, when the file was last accessed, how large it is, whether it is a directory, and who can read from or write to it. The inode number is also known as the file serial number and is unique within a particular filesystem.
Data blocks and inodes each take space on a filesystem, so we need to monitor the space usage to ensure that your filesystems have space for growth.
df
The df
(DiskFree) command is used to find out about the free and used space of file systems.
If no file name is passed as an argument with df command then it shows the space available on all currently mounted file systems
-T print file system type, -h, –human-readable print sizes (in power of 1024):
-H
make numbers human readable also (in powers of 1000).
-i list inode information instead of block usage:
Remember? there is no owner or access rights on vfat filesystems. vfat file format has no inodes!
The df
command gives information about a whole filesystem. Sometimes you might want to know how much space is used by a specific file or directory, To answer this kind of question, we use the du
command.
du
The Linux du
(Disk Usage) command, used to check the information of disk usage of files and directories on a machine.
--time option is used to display the last modification time in the output of du.
--exclude=PATTERN will exclude files that match PATTERN example: du -ah --exclude="*.txt" /home/payam
summary
Lets take a look at some other repairing tools beside tools which we have learned in this lesson:
.
.
.
https://developer.ibm.com/tutorials/l-lpic1-104-2/
https://www.thegeekstuff.com/2012/08/fsck-command-examples/
https://www.computerhope.com/unix/fsck.htm
https://www.geeksforgeeks.org/file-system-consistency-checker-fsck/https://www.serverwatch.com/tutorials/article.php/3797306/tune2fs-Makes-It-Easy-to-Play-With-Filesystems.htmhttps://jadi.gitbooks.io/lpic1/content/1042_maintain_the_integrity_of_filesystems.html
https://linoxide.com/linux-command/linux-inode/
https://www.geeksforgeeks.org/dumpe2fs-command-in-linux-with-examples/
https://www.geeksforgeeks.org/dumpe2fs-command-in-linux-with-examples/
https://www.geeksforgeeks.org/df-command-in-linux-with-examples/
https://www.geeksforgeeks.org/df-command-linux-examples/
https://www.tecmint.com/how-to-check-disk-space-in-linux/
https://www.geeksforgeeks.org/du-command-linux/
https://www.geeksforgeeks.org/du-command-linux-examples/
https://www.tecmint.com/check-linux-disk-usage-of-files-and-directories/
With the special thanks of shawn powers.
.
.
Last updated