104.2 Maintaining the Integrity of Filesystems

Maintaining the Integrity of Filesystems: A Comprehensive Guide for LPIC-1 #

Maintaining the integrity of filesystems is crucial for ensuring the reliability and performance of your Linux systems. This guide covers essential commands and utilities for verifying filesystem integrity, monitoring space, and repairing filesystem issues on both Ubuntu/Debian and Enterprise Linux distributions.

Key Knowledge Areas #

  1. Verify the integrity of filesystems.
  2. Monitor free space and inodes.
  3. Repair simple filesystem problems.

Important Commands and Utilities #

  • du
  • df
  • fsck
  • e2fsck
  • mke2fs
  • tune2fs
  • xfs_repair
  • xfs_fsr
  • xfs_db

Verify the Integrity of Filesystems #

Ubuntu/Debian #

fsck (File System Consistency Check)

The fsck command is used to check and optionally repair one or more Linux filesystems.

sudo fsck /dev/sdXn
  • -A: Check all filesystems.
  • -R: Skip root filesystem.
  • -N: Don’t execute, just show what would be done.

e2fsck

Specifically for ext2/ext3/ext4 filesystems.

sudo e2fsck -f /dev/sdXn

Enterprise Linux (RHEL/CentOS) #

fsck

sudo fsck /dev/sdXn

xfs_repair

Used for XFS filesystems, which are common in Enterprise Linux environments.

sudo xfs_repair /dev/sdXn

Monitor Free Space and Inodes #

Ubuntu/Debian #

df (Disk Free)

Displays the amount of disk space available on the filesystem.

df -h
  • -h: Human-readable format.

du (Disk Usage)

Estimates file space usage.

du -sh /path/to/directory
  • -s: Display only a total for each argument.
  • -h: Human-readable format.

Enterprise Linux (RHEL/CentOS) #

df

df -h

du

du -sh /path/to/directory

Repair Simple Filesystem Problems #

Ubuntu/Debian #

fsck

Run fsck with the -y option to automatically answer ‘yes’ to prompts.

sudo fsck -y /dev/sdXn

e2fsck

Force checking and repair.

sudo e2fsck -p /dev/sdXn

Enterprise Linux (RHEL/CentOS) #

fsck

sudo fsck -y /dev/sdXn

xfs_repair

sudo xfs_repair /dev/sdXn

Additional Commands and Utilities #

Ubuntu/Debian #

mke2fs

Used to create an ext2/ext3/ext4 filesystem.

sudo mke2fs /dev/sdXn

tune2fs

Adjusts tunable filesystem parameters on ext2/ext3/ext4 filesystems.

sudo tune2fs -l /dev/sdXn

Enterprise Linux (RHEL/CentOS) #

xfs_fsr

Reorganizes and defragments XFS filesystems.

sudo xfs_fsr /dev/sdXn

xfs_db

Debugs an XFS filesystem.

sudo xfs_db /dev/sdXn

Real-World Examples #

Example 1: Checking and Repairing an ext4 Filesystem #

  1. Unmount the filesystem:

    sudo umount /dev/sdXn
    
  2. Run e2fsck to check and repair:

    sudo e2fsck -f /dev/sdXn
    

Example 2: Checking and Repairing an XFS Filesystem #

  1. Unmount the filesystem:

    sudo umount /dev/sdXn
    
  2. Run xfs_repair:

    sudo xfs_repair /dev/sdXn
    

Example 3: Monitoring Disk Usage #

  1. Check disk space usage:

    df -h
    
  2. Check directory space usage:

    du -sh /var/log
    

By mastering these commands and understanding the differences between Ubuntu/Debian and Enterprise Linux, you’ll be well-prepared to maintain filesystem integrity and handle related issues effectively.