Shrinking ext4 partition on command line [duplicate]

Solution 1:

Yes, you need to run fdisk to actually change the partition table.

After you resize your file system with resize2fs (the partition must be unmounted), look at the output of the command:

$ resize2fs /dev/sdb1 24G
...
The filesystem on /dev/sdb1 is now 6291456 (4k) blocks long.

Remember the number of blocks and the block length. This only changed the size of the filesystem, but not the partition. You'll need to delete the partition and recreate it with the correct size, using the fdisk command:

$ fdisk /dev/sdb

You can type p to see the partition table:

Device     Boot Start      End  Sectors Size Id Type
/dev/sdb1  *     2048 52428766 52426719  25G 83 Linux

Type d to delete the partition. Then n to add a new partition, use the same values as the original one but changing the last sector to the one that fits the current filesystem. It accepts a relative size for the last sector, so calculate the resulting size by multiplying the number of blocks with the block length that gave the resize2fs command. In this example:

6291456 * 4k = 25165824k

When adding the new partition and you get asked for the last sector, type the resulting size (note the + prefix and the uppercase K): +25165824K

Type a if the partition was a bootable one, and p to view the new partition table:

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sdb1  *        2048 50333695 50331648   24G 83 Linux

Type w to write changes to disk. After that you should now see the new size.