Thursday, July 2, 2009

The Linux Superblock


So what is a Superblock? Let's try to understand...

When you create a file system on a hard drive it will be sub devided into multiple file system blocks. These blocks are used for two purposes:

1. To store user data (Main usage)
2. Some blocks used for to store file system's metadata.

The metadata contains data about your file system, ie: superblock, inodes, usage, status and more. Therefore a superblock is one of the core elements of the filesystems metadata.

Each linux filesystem (ext2,ext3,ext4,reiserfs...) has a superblock.
As we can see with dumpe2fs (which used to querry ext filesystems) command there are multiple superblocks on the filesystem, one primary and couple of backups.

This is how it looks on my Linux system:

$dumpe2fs /dev/hda6 grep -i | superblock

Primary superblock at 1, Group descriptors at 2-2

Backup superblock at 8193, Group descriptors at 8194-8194
Backup superblock at 24577, Group descriptors at 24578-24578
Backup superblock at 40961, Group descriptors at 40962-40962
Backup superblock at 57345, Group descriptors at 57346-57346
Backup superblock at 73729, Group descriptors at 73730-73730

A Superblock contains information about the file system like -

* File system type
* Size
* Status
* Information about other metadata

It is critical and may cause severe problems when it's corrupted.
When superblock is corruped we will not able to run command like:

$e2fsck -f /dev/hda2

The system will prompt for error on superblock, a handful option will be replacement of the damaged superblock with a backup one, to do so we will have to determine the number of sector which the backup superblock resides:

Backup superblock at 8193, Group descriptors at 8194-8194

After we verified the sector, we can tell the filesystem to use the alternative superblock instead of the damaged one:

$e2fsck -f -b 8193 /dev/hda6