16

Would it be possible on a running system to shrink ext4 partition and create another partition on the available free space such as it is done in Gparted?

I want to shrink /dev/sdb1 which isn't a system partition so unmounting isn't a problem.

I managed to resize /dev/sdb1 with resize2fs but after that I got lost, because fdisk didn't see any free space to create new partition on it.

Running Ubuntu Server 14.04.

David Foerster
  • 36,264
  • 56
  • 94
  • 147
Taavi
  • 686
  • After resize2fs you should resize /dev/sdb1 using gparted – Joril Apr 19 '17 at 11:48
  • There are several commands to run after each other if you want to do it with command line tools. It is much easier and safer to do with gparted, so I suggest that you do it with gparted, if it is OK to reboot the computer from a USB boot drive or DVD boot disk. (I realize that you might want to keep the server running.) – sudodus Sep 11 '17 at 13:31

1 Answers1

19

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.

ciencia
  • 306