Thursday, April 21, 2011

Check a linux filesystem with an alternate superblock

A filesystem contains different data structures after is created and one of the most important things that is present is the superblock - because is that important there is more then one superblock.
How to find the superblocks and how to do the filesystem check will be shown bellow.

Since some partitions are labeled will we need to find the associated device of the label. How to look on for the label/device association is to follow.

# this is my /etc/fstab

LABEL=/                 /                       ext3    defaults        1 1
LABEL=/opt              /opt                    ext3    defaults        1 2
LABEL=/tmp              /tmp                    ext3    defaults        1 2
LABEL=/usr              /usr                    ext3    defaults        1 2
LABEL=/home             /home                   ext3    defaults        1 2
LABEL=/logs             /logs                   ext3    defaults        1 2
LABEL=/var              /var                    ext3    defaults        1 2
LABEL=/boot             /boot                   ext3    defaults        1 2
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
LABEL=SWAP-sda9         swap                    swap    defaults        0 0

# i want to check the partition with label /opt  
# first step - find what device is associated with the label /opt
# to do this we use the command e2label
# if you have one or two partitions is as simple as running e2label on those
# i have a few partitions so i made a small chained command 

root# for i in `mount | awk '{print $1}' | grep '/'`; do echo -n "$i=" && e2label $i   ;done
/dev/hda10=/opt # this is the one I need

# running dumpe2fs
root# dumpe2fs /dev/hda10 |grep 'Backup superblock'
  Backup superblock at 32768, Group descriptors at 32769-32769
  Backup superblock at 98304, Group descriptors at 98305-98305
  Backup superblock at 163840, Group descriptors at 163841-163841
  Backup superblock at 229376, Group descriptors at 229377-229377
  Backup superblock at 294912, Group descriptors at 294913-294913
  Backup superblock at 819200, Group descriptors at 819201-819201
  Backup superblock at 884736, Group descriptors at 884737-884737
  Backup superblock at 1605632, Group descriptors at 1605633-1605633

# note that you may have a different output
# the number after Backup superblock at is the superblock you want

# run fsck.ext3 or fsck.ext2 ... or any other command for your filesystem (reiser etc)
# first umount the partition

root# umount /opt
# thenn fsck  
root# fsck.ext3  -b 32768 /dev/hda10

# after you are done mount back the partition