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?
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):
Here is an example of the whole process.
This is our hard disk:
it contains a single partition (/dev/loop0p1) that is 4.7GB in size. This is the file system on the partition:
By default, it has the same size as the partition (4.7GB). But only 2.1GB (45%) of the filesystem is used. This means we could potentially shrink the filesystem and partition to just 2.1 GB without losing any data.
The first step is to use resize2fs with the -M switch on the partition. Similar to a disk defragmenter, this command will attempt to move all files to the beginning of the file system to form one contiguous block. This then allows to shrink the file system to its smallest possible size.
The file system looks now like this:
The hard disk now contains a 4.7GB partition with a 2.1GB file system in it that is 100% used. The next step is to shrink the partition size to fit the smaller file system.
For that, we need to calculate the filesystem size. The dumpe2fs tool is very useful for that, it shows detailed information about a file system.
This tells us that there are 565950 blocks and the block size is 4096 bytes. This allows us to calculate the filesystem size:
565950 blocks * 4096 bytes = 2318131200 bytes
From that, we can calculate the filesystem size in sectors. From the fdisk output above, we know that the hard disk sector size is 512 bytes:
2318131200 bytes / 512 = 4527600 sectors
Because the partition does not start at sector 0, we need to add the start sector from the fdisk output:
4527600 + 8192 (start sector) = 4535792
This is the new end sector for our partition. Just to play it safe, add 10 sectors (about 5 KB) to that number: 4535802
Now we can use parted to shrink the partition to this new end sector. The "unit s" command is used to switch the units to sectors.
It gives us a warning about potential data loss, but because we moved all data to the beginning of the partition before, this can safely be ignored. Done! We now have a 2.1GB partition with a 2.1GB filesystem that's 100% occupied.