Wednesday 24 February 2016

Shrinking a partition with an EXT3 filesystem

Warning: Always take a backup of the entire system before performing anything like this!

Firstly identify the partition you wish to shrink (MAKE A RECORD OF THIS DATA!):

df -h

Verify details with fdisk (MAKE A RECORD OF THIS DATA!):

fdisk -l

and it's file system:

parted /dev/sda2 -l

Now if you are attempting to resize a system partition you will need to download some kind of linux live cd distro e.g. the debian live rescue cd or if it's a data partition we can simply omit this step.

Unmount the partition:

umount /dev/sda2

Now we ensure that the partition is clean:

fsck -n /dev/sda2

Because you are unable to shrink EXT3 filesystems we must instead remove the partitions journal - effectively making it an EXT2 filesystem - which can be shrunk!

tune2fs -O ^has_journal /dev/sda2

And then we should force a filesystem check with:

e2fsck -f /dev/sda2

We should now shrink the partition size using the resize2fs utility:

WARNING: Be very careful here to ensure that the new size will have enough room to cater for the exsiting data in use on the partition - there's no safety net here! e.g. if sda2 was a total of 500GB and the used space was 150GB we would need to ensure that the new disk allocation we be something like 155GB:

resize2fs /dev/sda2 155G

IMPORTANT: Ensure you keep the output of the resize2fs command as you will need to take note of the amount of blocks allocated (and block size) later! ***

Sample output:

resize2fs 1.XX
Resizing the filesystem on /dev/sda2 to 5120000 (4k) blocks.

We will now have to delete our partition (sda2) from the partition table using fdisk (this will not lose any data!):

fdisk /dev/sda

Press 'd' >> Specify partition number: '2' >> Press 'n' to create a new partition >> Press 'p' for primary partition (or logical).

We are now asked for the size of the partition - the start and finish cylinders - we already know what the start cylinder is (as it's on our earlier 'fdisk -l' output - but we do not know the end cylinder - this can be calculated as follows:

Blocks (from the resize2fs output): 5120000 x 4(4k block) x 1.05 = 21504000

Note: The extra 5% is to ensure the partition is big enough.

We should also ensure it's in the proper formatting - so we would enter it as:

+21504000K

We should also ensure that our partition is set too active or not - if it was originally (you can toggle this by pressing 'a')

Press 'w' to write changes.

Then either reboot (if you are using a live CD) or simply continue if you are running this on the OS itself.

Run a filesystem check on the partition:

fsck -n /dev/sda2

And then convert it to EXT3 (create a journal):

tune2fs -j /dev/sda2

Reboot your system with:

shutdown -r now

and then check the mount points and partitions:

df -H

fdisk -L




0 comments:

Post a Comment