How to Properly fsck and Repair an ext3 Filesystem Stored in a Disk Image File


2 views

When working with filesystem images (as opposed to physical block devices), many administrators encounter unexpected fsck behaviors. The key principle remains the same: a filesystem is a filesystem regardless of whether it resides on physical media or inside a file.

Let's recreate the exact scenario from the question to demonstrate proper handling:

# Create 20MB disk image (40960 blocks * 512 bytes)
dd if=/dev/zero of=disk-image bs=512 count=40960

# Format as ext3
mkfs.ext3 -q disk-image

# Mount and test operations
mkdir -p mount-point
sudo mount -o loop disk-image mount-point
echo "Test content" > mount-point/testfile.txt
sudo umount mount-point

The size mismatch warning occurs because:

  • ext3 maintains multiple size attributes (i_size vs actual blocks allocated)
  • File-based storage adds abstraction layer that can affect timing of writes
  • The -c flag performs bad block checking which can influence results

For file-based filesystems, use this comprehensive check:

# First, ensure clean unmount status
sudo fsck.ext3 -n disk-image

# For thorough check (without bad block scan):
sudo fsck.ext3 -f -y disk-image

# If bad blocks are suspected:
sudo fsck.ext3 -c -c -y disk-image

Typical responses you might see:

# Minor correction example
"Pass 1: Checking inodes, blocks, and sizes
Inode 185, i_size is 16384, should be 17408. Fix? yes"

# Severe corruption example
"/dev/loop0: The filesystem size is wrong (358272 blocks vs 358400)"
  • Always unmount properly before fsck
  • Consider using sync option when mounting: mount -o loop,sync
  • For production use, implement proper locking during operations
  • Regularly check with tune2fs -l disk-image for filesystem statistics

For serious corruption cases:

# Attempt journal recovery first
sudo fsck.ext3 -fy -b 32768 disk-image

# As last resort, superblock backup
sudo fsck.ext3 -fy -B 1024 disk-image

Remember that filesystem-in-file solutions are perfectly valid, but require the same care as physical devices. The warning you encountered is normal for first-time fsck runs and doesn't indicate serious problems.


When working with disk images containing filesystems (like ext3/ext4), the checking process differs slightly from physical block devices. Here's a complete technical breakdown:

# Example creation sequence:
dd if=/dev/zero of=disk-image bs=1M count=100
mkfs.ext3 -q disk-image
mkdir -p mountpoint
sudo mount -o loop disk-image mountpoint

The critical aspect is ensuring the filesystem is not mounted during checking. For file-based filesystems, use:

sudo umount /path/to/mountpoint  # MUST unmount first
e2fsck -f -y -v disk-image

Key flags explanation:

  • -f : Force check even if filesystem seems clean
  • -y : Automatically answer "yes" to repairs
  • -v : Verbose output

The size discrepancy warning you encountered typically occurs when:

Inode X, i_size is A, should be B. Fix?

This usually indicates:

  • Last-minute writes weren't fully synced before unmount
  • Partial block allocation during file operations
  • Non-atomic metadata updates

For comprehensive verification:

# Bad blocks detection (two passes):
e2fsck -c -c -f -v disk-image

# Alternative using fsck.ext3:
fsck.ext3 -pf disk-image

When working with sparse files:

e2fsck -f -E unshare_blocks disk-image

For CI/CD pipelines or automated testing:

#!/bin/bash
IMAGE=$1
if ! sudo umount /mnt/test &>/dev/null; then
    echo "Unmount failed - check for stale mounts"
fi
if sudo e2fsck -f -n "$IMAGE"; then
    echo "Filesystem clean"
else
    sudo e2fsck -f -y "$IMAGE" || exit 1
fi

For large disk images, consider these optimizations:

# Use direct I/O to avoid caching:
e2fsck -f -E direct_io disk-image

# Parallel checking (ext4 only):
e2fsck -f -C0 disk-image