Pages

Sunday, November 28, 2010

Repairing Linux ext2 or ext3 file system


1) File system must be unmounted, you cannot repair it while it is running. Take system down to runlevel one (make sure you run all command as root user):
# init 1

2)Unmount file system, for example if it is /home (/dev/sda3) file system then type command:
# umount /home
OR
# umount /dev/sda3

3) Now run fsck on the partition:
# fsck /dev/sda3

However be sure to specify the file system type using -t option. Recenly one of our sys admin run the command on ext3 file system w/o specifying file system. Result was more corruption as fsck by default assumes ext2 file system.
# fsck -t ext3 /dev/sda3
OR
# fsck.ext3 /dev/sda3

Tip if you don't know your file system type then typing mount command will display file system type.

fsck will check the file system and ask which problems should be fixed or corrected. If you don't wanna type y every time then you can use pass -y option to fsck.
# fsck -y /dev/sda3

Please not if any files are recovered then they are placed in /home/lost+found directory by fsck command.

4) Once fsck finished, remount the file system:
# mount /home

5) Go to multiuser mode
# init 3


More help

fsck -n /dev/sda1

The output looks like this:

fsck 1.38 (30-Jun-2005)
e2fsck 1.38 (30-Jun-2005)
/dev/sda1: clean, 159037/1254176 files, 1095299/2504124 blocks

Next we remove the journal from /dev/sda1, thus turning it into an ext2 partition:

tune2fs -O ^has_journal /dev/sda1

The output looks like this:

tune2fs 1.38 (30-Jun-2005)

Then run

e2fsck -f /dev/sda1

e2fsck 1.38 (30-Jun-2005)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sda1: 164178/1254176 files (0.6% non-contiguous), 1051617/2504124 blocks

Now we resize our file system with resize2fs. resize2fs can resize ext2 file systems, but not ext3 file systems, that's why we had to turn /dev/sda1 to ext2. Currently, 4.1GB are used on /dev/sda1 (see the df -h output above), So it's safe to shrink it from 10GB to about 6GB (if you make it smaller than 4.1GB, you will lose data!). Therefore we run

resize2fs /dev/sda1 6000M

The output is as follows:

resize2fs 1.38 (30-Jun-2005)
Resizing the filesystem on /dev/sda1 to 1536000 (4k) blocks.
The filesystem on /dev/sda1 is now 1536000 blocks long.

Please take note of the amount of blocks (1536000) and their size (4k). We need that soon.

Now we delete our /dev/sda1 partition (don't be afraid, no data will be lost) and create a new, smaller one (but still big enough to hold our resized file system!). We can do this with fdisk:


How To Resize ext3 Partitions Without Losing Data

Assuming this is your partition table:

Dev Type Mount Size
sda1 nt40 nt40 1992M
sda2 linux / 1498M
sda3 swap 117M
sda4 extended 5036M
sda5 swap 117M
sda6 home /home 4910M


-backup your /home on to your tape drive.

-check and make sure the backup worked

-use fdisk to delete the home partition and create several smaller partitions:(make /dev/sda6 about 800 megs)

fdisk /dev/sda

-create a file system on /dev/sda6:

mke2fs /dev/sda6

-mount it and copy all files in /usr to sda6:

mount /dev/sda6 /mnt
cd /usr
ls (sanity check)
cp -a * /mnt
cd /mnt
ls (sanity check)

-change fstab to mount /usr/ as /dev/sda6:

/dev/sda6 /usr ext2 defaults 1 2

Don't leave this one there:

/dev/sda6 /home ext2 defaults 1 2

-reboot and check that everything is okay

mount (Did sda6 appear mounted on /usr?)
startx (Did X start up?)

-reboot into single user mode, umount /usr, and erase the /usr file on sda2:

init 1
cd /
umount /usr (if this doesn't work "fuser -k /usr" and then try again.)
mount (sanity check)
cd /usr
ls (sanity check)
rm -rf *

-reboot and check that disk space
df (or df -h)