5

This is what've done to shrink /home partition filesystem:

e2fsck /dev/sda1

resize2fs /dev/sda1 150G

As of now, if I run df it is going to show me that the partition has shrunk from 210G to 150G (which is not true).

Since I've only reduced filesystem size, how can I shrink the partition size using fdisk or parted?

7
  • 1
    Was /home mounted whilst you reduced its file system size? What does the man page for parted say about changing partition size? Commented Jul 3, 2021 at 14:19
  • @JeremyBoden it was not mounted. I've rebooted my computer, kepts pressing SHIFT and used Lubuntu advanced settings in order to boot as root. After that I've proceeded to use mentioned commands. Commented Jul 3, 2021 at 14:22
  • I understand your concern about me not reading man page, but I'm too afraid to risk doing it by myself because I don't have an installation disk with me right now, if anything goes wrong. Commented Jul 3, 2021 at 14:24
  • 1
    df doesn't report the partition size. It reports the filesystem size, which you shrank to 150G. To shrink a partition, the best tool is probably gparted. However, if the data in that filesystem has any value, I recommend taking a backup first, because it is easy to break things. Commented Jul 3, 2021 at 14:39
  • 1
    If you had used gparted - a GUI program, it would have resized your file system (if safe to do so) and resized your partition. It would still be recommended, in a view-only mode to get a pictorial view of your partitions. Commented Jul 3, 2021 at 14:44

2 Answers 2

4

First, the usual warning: back up your data. Resizing partitions and file systems is never a 100% safe operation.

If a graphical environment with GParted is available to you, the simplest solution is to:

  1. re-grow the /dev/sda1 partition to the size of the file system it contains: right-click on it, select "Check" and apply all operations; then

  2. shrink the partition to the desired size: right-click on it, select "Resize/Move", set the desired "New size", confirm and apply all operations; GParted takes care of resizing the file system too.

Alternatively, using command line tools, you can use dumpe2fs or tune2fs to get the file system's block size and block count and re-create the to-shrink partition, making the new one start at the same byte the original one did and end at that offset plus the size (in bytes) of the shrunk file system.

Here is a complete example that can be used to play around with the relevant commands, using GNU parted and a loop device backed by a regular file:

  • We first create a block device, partition it and create a 210 MiB file system on the first (and only) partition:

    $ fallocate -l 211MiB testfile
    $ sudo losetup -f testfile
    $ sudo parted /dev/loop0 unit MiB mklabel msdos mkpart primary 1 100%FREE
    $ lsblk /dev/loop0
    NAME      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
    loop0       7:0    0  211M  0 loop 
    └─loop0p1 259:0    0  210M  0 part
    $ sudo mkfs.ext4 -L test -v /dev/loop0p1 210M
    $ sudo mount /dev/loop0p1 /mnt/temp
    $ df /mnt/temp
    Filesystem     1K-blocks  Used Available Use% Mounted on
    /dev/loop0p1      202353    14    187491   1% /mnt/temp
    
  • Then we resize the file system to 150 MiB, ending up in a state analogous to the one described in your question:

    $ sudo umount /mnt/temp
    $ sudo e2fsck -f /dev/loop0p1
    $ sudo resize2fs /dev/loop0p1 150M
    $ sudo mount /dev/loop0p1 /mnt/temp
    $ df /mnt/temp
    Filesystem     1K-blocks  Used Available Use% Mounted on
    /dev/loop0p1      144716  1550    132414   2% /mnt/temp
    $ sudo umount /mnt/temp
    
  • Now we get the target size for the partition, based on the current size of the file system it hosts:

    $ sudo dumpe2fs -h /dev/loop0p1
    

    or

    $ sudo tune2fs -l /dev/loop0p1
    

    where the relevant lines are:

    Block count:              153600
    First block:              1
    Block size:               1024
    

    allowing us to compute the new partition size, in bytes, as:

    153600 * 1024 = 157286400
    

    We then need the current starting byte of the partition (pretending we do not already know it):

    $ sudo parted /dev/loop0 unit b print
    Number  Start     End         Size        Type     File system  Flags
     1      1048576B  221249535B  220200960B  primary  ext4
    

    and we now know that the new partition will start at byte 1048576 and end at byte

    1048576 + 157286400 -1 = 158334975
    

    Note that parted counts starting from 0 and uses inclusive intervals; hence, one byte has to be subtracted from the partition's size to compute its ending byte.

    We can now re-create the partition and check the result:

    $ sudo parted /dev/loop0 unit b rm 1 mkpart primary 1048576 158334975
    $ lsblk /dev/loop0
    NAME      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
    loop0       7:0    0  211M  0 loop 
    └─loop0p1 259:0    0  150M  0 part
    

    Running sudo e2fsck -f /dev/loop0p1 anew will then alert you if you happened to shrink the partition too much, allowing you to fix it before damaging your file system.

3
  • Thanks for the answer. I'll try it, as soon as I get my hands on a drive that I can store my backups. Commented Jul 3, 2021 at 14:47
  • @surfboardcompound I added a simpler alternative based on GParted, in case it's available to you.
    – fra-san
    Commented Jul 3, 2021 at 15:03
  • Check serverfault.com/a/796767 where it says "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."
    – parsley72
    Commented Apr 30 at 2:59
0

You known after shrink fs,
 have to calculate new partition size & resize partition table.
 And mbr / gpt partition table..


If don't have command line hobby, do it in GUI is easy:

gnome-disk-utility

gnome-disk-unitily_resize.png

gparted

gparted_resize.png


Especially, gparted also show fs size (if it not equal to part size)

gparted_info

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .