Recently i shrinked an ext4 filesystem size to 500GB
df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 493G 64G 404G 14% /
now i want to shrink the partition size to fit exactly to the filesystem size.
I tried to used parted and the resizepart command. The problem is when parted asks for the new size. If i choose 500GB the resulting partition is smaller from 500GB and as a result the underlying filesystem cannot fit on this partition. Any hint on how to do the correct size calculations?
Best Answer
Sizes reported by
df
will be incorrect as they account only for data blocks and miss blocks used internally by the filesystem as well as the reserved blocks.The easy way is to shrink your filesystem to be smaller than you want by at least 10%. Resize the partition to the size you want then grow the filesystem with resize2fs.
If you want to calculate it by hand you have to know how large the filesystem is internally. Check this with
tune2fs -l /dev/sda2
and multiply the Block count by the Block size. When resizing the partition in parted switch the units to sectors withunit s
andprint
the table to get the start sector and logical sector size. Divide the total size in bytes from above by the sector size. Round up to the nearest multiple of 2048 and resize to this number of sectors (end sector = size in sectors + start sector - 1).Equation (runable in python just fill in the first 4 values):