Shrinking a Partition in Linux
I recently had the need to shrink a partition that, when originally created, consumed the whole disk. Listed below are the steps that I followed to shrink the original partition down so that I could add a new 20G partition to the end of the disk.
Use fdisk to look at the partition table of the disk and decide which partion will be shrunk and what the new number of cylinders will be.
If you want to shrink the partition by a specific amount then use the “Units = cylinders …” line from the partition information to find the size of a cylinder and then divide size you want left by the cylinder size and round up.
For example, the disk c0d1 currently has just a single large partition:
Disk /dev/cciss/c0d1: 255 heads, 63 sectors, 8854 cylinders Units = cylinders of 16065 * 512 bytes Device Boot Start End Blocks Id System /dev/cciss/c0d1p1 1 8854 71119723+ 83 Linux
To shrink it down to get a 20G partition, we would compute the cylinder size as 16065*512 (8225280 bytes) and then divide 20G by that number:
(20*1024*1024*1024)/8225280 = 2610.83 = 2611 cylinders
That means partition 1 will need to be 8854-2611 = 6243 cylinders after resizing.
resize2fs uses a block size of 4K, so you’ll need to change the number of cylinders to 4K blocks. Round the number down if comes out as a fractional number.
(cylinder_size * cylinders) / 4096
In our example, cylinder size is 8225280, cylinders is 6423, so it works out to be:
(8225280*6423)/4096 = 12898186.87 = 12898186 4k blocks
Unmount the filesystem and run e2fsck on it to make sure it is cleaned up.
- stop services that are writing to the disk
umount /var/spool/news
e2fsck -f /dev/cciss/c0d1p1
Run resize2fs on the disk to shrink the filesystem:
resize2fs -p /dev/cciss/c0d1p1 12898186
Run fdisk to then shrink underlying partition. When running fdisk, you will delete the partition then create the same partition number starting at the same cylinder, but with the size of the new cylinder count.
Remember to write the partition table to the disk (w command) before exiting.
You can then mount up newly shrunk partition and create the new partition and filesystem.
Wednesday January 14, 2004 · Permalink