Thursday 25 February 2016

Resizing ext2/3/4 filesystems with resize2fs

This technique will come in handy for those who are not using LVM!

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 (or use parted for GP (MAKE A RECORD OF THIS DATA!):

fdisk -l

or (for GPT)

parted /dev/sda2
unit c
p

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

At the time (to my surprise) I got a message returned the device was busy - so to identify anything still accessing the disk we can run:

lsof /dev/sda2

Now we ensure that the filesystem is clean:

e2fsck -f /dev/sda2

We should now proceed by resizing the filesystem with resize2fs:

resize2fs -p /dev/sda2 500G

This could take several hours dependent on disk speeds etc. - it took me around 4 - 5 hours on relatively slow disks to resize a 5TB filesystem to 500GB.

You should see the output as something like:

'The filesystem on /dev/sdb1 is now 131072000 (4K) blocks long'

So the total cylinder count will be: 131072000 * 4 = 524288000 blocks. Which if you convert from KB to GB roughly equals 525GB (this is a good sanity test!)

Now we should delete the old partition using fdisk (or parted for GPT):

parted /dev/sdb

Lets get a list of partitions by running 'p'

Identify the partition number and issue the following to delete the partition:

rm
X (where X is the partition number)

Ensure the unit is set too cylinders!:

unit GB

Then create the new partition:

So start cylinder is 2048 - which is 1049KB - converted to GB is 0.001049 - 0GB rounded to the nearest GB.

For the end sector we know it's going to be 525GB with an extra  of at least 5% or so - so for my example I chose 550GB:

mkpart primary ext4 0 550

Now we ensure that the filesystem is clean:

e2fsck -f /dev/sda2

Finally re-mount / restart your system.

0 comments:

Post a Comment