Powered By GitBook
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.
2
fsck fsck.ext3 fsck.fat fsck.nfs
3
fsck.cramfs fsck.ext4 fsck.minix fsck.vfat
4
fsck.ext2 fsck.ext4dev fsck.msdos
5
6
[email protected]:~# ls /sbin/fsck*
7
/sbin/fsck /sbin/fsck.ext3 /sbin/fsck.fat /sbin/fsck.nfs
8
/sbin/fsck.cramfs /sbin/fsck.ext4 /sbin/fsck.minix /sbin/fsck.vfat
9
/sbin/fsck.ext2 /sbin/fsck.ext4dev /sbin/fsck.msdos
Copied!
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):
1
[email protected]:~# fsck /dev/sda1
2
fsck from util-linux 2.27.1
3
e2fsck 1.42.13 (17-May-2015)
4
/dev/sda1 is mounted.
5
e2fsck: Cannot continue, aborting.
Copied!
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 :
1
[email protected]:~# fsck /dev/sdb1
2
fsck from util-linux 2.27.1
3
e2fsck 1.42.13 (17-May-2015)
4
/dev/sdb1: clean, 11/1310720 files, 126322/5242624 blocks
Copied!
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:
1
[email protected]:~# fsck -N /dev/sdb1
2
fsck from util-linux 2.27.1
3
[/sbin/fsck.ext3 (1) -- /dev/sdb1] fsck.ext3 /dev/sdb1
Copied!
-n causes these commands not to fix anything and just show what was going to be done:
1
[email protected]:~# fsck -n /dev/sdb1
2
fsck from util-linux 2.27.1
3
e2fsck 1.42.13 (17-May-2015)
4
/dev/sdb1: clean, 11/1310720 files, 126322/5242624 blocks
Copied!
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.
1
[email protected]:~# fsck -f /dev/sdb1
2
fsck from util-linux 2.27.1
3
e2fsck 1.42.13 (17-May-2015)
4
Pass 1: Checking inodes, blocks, and sizes
5
Pass 2: Checking directory structure
6
Pass 3: Checking directory connectivity
7
Pass 4: Checking reference counts
8
Pass 5: Checking group summary information
9
/dev/sdb1: 11/1310720 files (0.0% non-contiguous), 126322/5242624 blocks
10
Copied!
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 ):
1
[email protected]:~# blkid /dev/sdb1
2
/dev/sdb1: UUID="b9ca56aa-4ea3-4f61-92c8-18c867fd991f" SEC_TYPE="ext2" TYPE="ext3" PARTUUID="4d71bc84-01"
3
[email protected]untu16-1:~# fsck -N UUID="b9ca56aa-4ea3-4f61-92c8-18c867fd991f"
4
fsck from util-linux 2.27.1
5
e2fsck 1.42.13 (17-May-2015)
6
/dev/sdb1: clean, 11/1310720 files, 126322/5242624 blocks
Copied!
fsck command example
description
fsck -M /dev/sda1
prevents running fsck on mounted filesystem
fsck -t ext3 /dev/sdb1
Check Only a Specific Filesystem Type
fsck -y -f /dev/sdb1
pass “yes” to all the questions to fix
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.
1
[email protected]:~# mke2fs -n /dev/sdb1
2
mke2fs 1.42.13 (17-May-2015)
3
/dev/sdb1 contains a ext3 file system
4
created on Sat Jan 25 22:17:24 2020
5
Proceed anyway? (y,n) y
6
Creating filesystem with 5242624 4k blocks and 1310720 inodes
7
Filesystem UUID: 28ce54ac-abbd-4788-bab8-b71eae9a53bc
8
Superblock backups stored on blocks:
9
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
10
4096000
Copied!

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:
1
[email protected]:~# tune2fs -l /dev/sdb1
2
tune2fs 1.42.13 (17-May-2015)
3
Filesystem volume name: <none>
4
Last mounted on: <not available>
5
Filesystem UUID: b9ca56aa-4ea3-4f61-92c8-18c867fd991f
6
Filesystem magic number: 0xEF53
7
Filesystem revision #: 1 (dynamic)
8
Filesystem features: has_journal ext_attr resize_inode dir_index filetype sparse_super large_file
9
Filesystem flags: signed_directory_hash
10
Default mount options: user_xattr acl
11
Filesystem state: clean
12
Errors behavior: Continue
13
Filesystem OS type: Linux
14
Inode count: 1310720
15
Block count: 5242624
16
Reserved block count: 262131
17
Free blocks: 5116302
18
Free inodes: 1310709
19
First block: 0
20
Block size: 4096
21
Fragment size: 4096
22
Reserved GDT blocks: 1022
23
Blocks per group: 32768
24
Fragments per group: 32768
25
Inodes per group: 8192
26
Inode blocks per group: 512
27
Filesystem created: Wed Jan 22 23:42:22 2020
28
Last mount time: n/a
29
Last write time: Thu Jan 23 02:21:07 2020
30
Mount count: 0
31
Maximum mount count: -1
32
Last checked: Thu Jan 23 02:21:07 2020
33
Check interval: 0 (<none>)
34
Reserved blocks uid: 0 (user root)
35
Reserved blocks gid: 0 (group root)
36
First inode: 11
37
Inode size: 256
38
Required extra isize: 28
39
Desired extra isize: 28
40
Journal inode: 8
41
Default directory hash: half_md4
42
Directory Hash Seed: 73007ad4-213f-44cd-b8f0-4fbd0c1e90b2
43
Journal backup: inode blocks
44
Copied!
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:
1
[email protected]:~# tune2fs -L myroot /dev/sda1
2
tune2fs 1.42.13 (17-May-2015)
Copied!

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.
1
[email protected]:~# dumpe2fs /dev/sdb1 | grep superblock
2
dumpe2fs 1.42.13 (17-May-2015)
3
Primary superblock at 0, Group descriptors at 1-2
4
Backup superblock at 32768, Group descriptors at 32769-32770
5
Backup superblock at 98304, Group descriptors at 98305-98306
6
Backup superblock at 163840, Group descriptors at 163841-163842
7
Backup superblock at 229376, Group descriptors at 229377-229378
8
Backup superblock at 294912, Group descriptors at 294913-294914
9
Backup superblock at 819200, Group descriptors at 819201-819202
10
Backup superblock at 884736, Group descriptors at 884737-884738
11
Backup superblock at 1605632, Group descriptors at 1605633-1605634
12
Backup superblock at 2654208, Group descriptors at 2654209-2654210
13
Backup superblock at 4096000, Group descriptors at 4096001-4096002
14
Copied!

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.
1
[email protected]:~# debugfs /dev/sda1
2
debugfs 1.42.13 (17-May-2015)
3
debugfs: cd /etc/
4
debugfs: pwd
5
[pwd] INODE: 1179649 PATH: /etc
6
[root] INODE: 2 PATH: /
7
debugfs: stat passwd
8
9
Inode: 1187630 Type: regular Mode: 0644 Flags: 0x80000
10
Generation: 2904697195 Version: 0x00000000:00000001
11
User: 0 Group: 0 Size: 2388
12
File ACL: 0 Directory ACL: 0
13
Links: 1 Blockcount: 8
14
Fragment: Address: 0 Number: 0 Size: 0
15
ctime: 0x5e2064c5:64a1019c -- Thu Jan 16 16:57:33 2020
16
atime: 0x5e2cb8a0:4aa345f8 -- Sun Jan 26 01:22:32 2020
17
mtime: 0x5e2064c5:64a1019c -- Thu Jan 16 16:57:33 2020
18
crtime: 0x5e2064c5:64a1019c -- Thu Jan 16 16:57:33 2020
19
Size of extra inode fields: 32
20
EXTENTS:
21
(0):4265700
22
23
debugfs: ncheck 1187630
24
Inode Pathname
25
1187630 /etc/passwd
26
debugfs: q
Copied!

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
1
[email protected]:~# xfs_info /dev/sdb1
2
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=1310656 blks
3
= sectsz=512 attr=2, projid32bit=1
4
= crc=1 finobt=1 spinodes=0
5
data = bsize=4096 blocks=5242624, imaxpct=25
6
= sunit=0 swidth=0 blks
7
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
8
log =internal bsize=4096 blocks=2560, version=2
9
= sectsz=512 sunit=0 blks, lazy-count=1
10
realtime =none extsz=4096 blocks=0, rtextents=0
Copied!
xfs command
description
xfs_info
shows information
xfs_check
complete check of file system
xfs_repair
check and fixes problems
xfs_check -n
same as xfs_check
1
[email protected]:~# xfs_repair -n /dev/sdb1
2
xfs_repair: /dev/sdb1 contains a mounted and writable filesystem
3
4
fatal error -- couldn't initialize XFS library
Copied!
obviously xfs_repair does not work on mounted file system.
1
[email protected]:~# xfs_repair /dev/sdb1
2
Phase 1 - find and verify superblock...
3
Phase 2 - using internal log
4
- zero log...
5
- scan filesystem freespace and inode maps...
6
- found root inode chunk
7
Phase 3 - for each AG...
8
- scan and clear agi unlinked lists...
9
- process known inodes and perform inode discovery...
10
- agno = 0
11
- agno = 1
12
- agno = 2
13
- agno = 3
14
- process newly discovered inodes...
15
Phase 4 - check for duplicate blocks...
16
- setting up duplicate extent list...
17
- check for inodes claiming duplicate blocks...
18
- agno = 0
19
- agno = 1
20
- agno = 2
21
- agno = 3
22
Phase 5 - rebuild AG headers and trees...
23
- reset superblock...
24
Phase 6 - check inode connectivity...
25
- resetting contents of realtime bitmap and summary inodes
26
- traversing filesystem ...
27
- traversal finished ...
28
- moving disconnected inodes to lost+found ...
29
Phase 7 - verify and correct link counts...
30
done
Copied!
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.
1
df [OPTION]...[FILE]...
Copied!
If no file name is passed as an argument with df command then it shows the space available on all currently mounted file systems
2
Filesystem 1K-blocks Used Available Use% Mounted on
3
udev 462796 0 462796 0% /dev
4
tmpfs 98508 6544 91964 7% /run
5
/dev/sda1 50442940 5613840 42243708 12% /
6
tmpfs 492540 284 492256 1% /dev/shm
7
tmpfs 5120 4 5116 1% /run/lock
8
tmpfs 492540 0 492540 0% /sys/fs/cgroup
9
tmpfs 98508 48 98460 1% /run/user/1001
Copied!
-T print file system type, -h, –human-readable print sizes (in power of 1024):
2
Filesystem Type Size Used Avail Use% Mounted on
3
udev devtmpfs 452M 0 452M 0% /dev
4
tmpfs tmpfs 97M 6.4M 90M 7% /run
5
/dev/sda1 ext4 49G 5.4G 41G 12% /
6
tmpfs tmpfs 481M 284K 481M 1% /dev/shm
7
tmpfs tmpfs 5.0M 4.0K 5.0M 1% /run/lock
8
tmpfs tmpfs 481M 0 481M 0% /sys/fs/cgroup
9
tmpfs tmpfs 97M 48K 97M 1% /run/user/1001
Copied!
-H make numbers human readable also (in powers of 1000).
-i list inode information instead of block usage:
2
Filesystem Inodes IUsed IFree IUse% Mounted on
3
udev 115699 456 115243 1% /dev
4
tmpfs 123135 750 122385 1% /run
5
/dev/sda1 3211264 256212 2955052 8% /
6
tmpfs 123135 9 123126 1% /dev/shm
7
tmpfs 123135 6 123129 1% /run/lock
8
tmpfs 123135 17 123118 1% /sys/fs/cgroup
9
tmpfs 123135 27 123108 1% /run/user/1001
Copied!
Remember? there is no owner or access rights on vfat filesystems. vfat file format has no inodes!
df command example
description
df -a
dislpay all information includes pseudo, duplicate and inaccessible file systems.
df -Th /home
Display Information of /home File System
df -k or -m or -h
displays information in Bytes, MB , GB
df -t ext3
include specific file system type
df -x xfs
exclude specific file system type
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.
1
du [OPTION]... [FILE]...
Copied!
useful switch
description
-a
write count of all files, not just directories
-h
human readable Means we can see sizes in Bytes, KB, MB, GB,...
-c
grand total usage disk space at the last line
–max-depth=N
go N or fewer sub directories further
-s
display only total for each directory
2
16 ./.aptitude
3
8 ./.cache/dconf
4
12 ./.cache
5
8 ./.config/htop
6
8 ./.config/gedit
7
8 ./.config/dconf
8
4 ./.config/ibus/bus
9
8 ./.config/ibus
10
36 ./.config
11
4 ./.gnupg/private-keys-v1.d
12
48 ./.gnupg
13
4 ./.gconf
14
12 ./.elinks
15
16 ./.ssh
16
4 ./.nano
17
8 ./.local/share
18
12 ./.local
19
336 ./backup
20
8 ./.dbus/session-bus
21
12 ./.dbus
22
4 ./test-space
23
932 .
24
25
26
932K .
Copied!
--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:
file system
command
description
ext
tune2fs
Adjusts parameters on ext2 and ext3 filesystems and can set journaling .
ext
dumpe2fs
Prints the super block and block group descriptor information for an ext2 or ext3 filesystem.
ext
debugfs
Is an interactive filesystem debugger. Use it to examine or change the state of an ext2 or ext3 filesystem.
Reiserfs
reiserfstune
Displays and adjusts parameters on ReiserFS filesystems.
Reiserfs
debugreiserfs
Performs similar functions to dumpe2fs and debugfs for ReiserFS filesystems.
xfs
xfs_info
Displays XFS filesystem information.
xfs
xfs_growfs
Expands an XFS filesystem
xfs
xfs_admin
Changes the parameters of an XFS filesystem.
xfs
xfs_repair
Repairs an XFS filesystem when the mount checks are not sufficient to repair the system.
xfs
xfs_db
Examines or debugs an XFS filesystem.
btrfs
btrfs
Displays many aspects of btrfs filesystem information
btrfs
btrfsck
Check btrfs filesystems
btrfs
btrfs-find-root
Finds the block that is the root of the btrfs filesystem
btrfs
btrfs-debug-tree
Displays btrfs internal metadata
btrfs
btrfstune
Tune various btrfs filesystem parameters, and enables or disables some extended features
btrfs
btrfs-restore
Attempt to restore files from a damaged btrfs filesystem
.
.
.
With the special thanks of shawn powers.
.
.
Last modified 1yr ago