Taking from /home to grow the root partition

After a recent move from Wheezy to Jessie, the root partition was looking a little full – to the point where the GUI wasn’t working due to a lack of /tmp space. So needed to take care of that while troubleshooting other issues.

# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/dm-0              9.1G  8.4G  176M  99% /
udev                    10M     0   10M   0% /dev
tmpfs                  402M  6.5M  396M   2% /run
tmpfs                 1005M   68K 1005M   1% /dev/shm
tmpfs                  5.0M  4.0K  5.0M   1% /run/lock
tmpfs                 1005M     0 1005M   0% /sys/fs/cgroup
/dev/mapper/thor-home  904G  511G  347G  60% /home
/dev/sde1              228M   47M  170M  22% /boot
tmpfs                  201M     0  201M   0% /run/user/1024
tmpfs                  201M  8.0K  201M   1% /run/user/1000

Given that there is plenty of room on the /home partition and it’s all built on LVM, reallocating space from one to the other is fairly straightforward.

While you can grow an EXT4 filesystem while it is mounted, you can’t shrink one – and given that /home is constantly in use it was easier just to reboot into maintenance mode and take care of things that way.

# Unmount the home partition
umount /mnt/home
# Check the filesystem to make sure it is clean
e2fsck -F /dev/mapper/thor-home

# Shrink the filesystem to the desired size - in this case I'm reducing it to 880G
resize2fs -p /dev/mapper/thor-home 880G

# Shrink the logical volume to match
lvreduce -L 880G /dev/mapper/thor-home

# Check the file system again. If there are problems because the partition is too small, we
# can still recover by using lvextend until it succeeds e.g. lvextend -L +1G /dev/mapper/thor-home
e2fsck -F /dev/mapper/thor-home

That was the dangerous part. Extending the root filesystem to use the newly freed space is quite simple, but this time we are first growing the logical volume, then expanding the filesystem

# Extend the logical volume to use all the free space
lvextend -l +100%FREE /dev/mapper/thor-root
# Expand the filesystem
resize2fs -p /dev/mapper/thor-root

At this point a scan of the filesystem is a good idea, but unfortunately we can’t do that to a mounted filesystem, so instead we can trigger the check on reboot

touch /forcefsck

But in the meantime we can see that the root partition now has plenty of space

# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/dm-0               47G  8.8G   36G  20% /
udev                    10M     0   10M   0% /dev
tmpfs                  402M  6.5M  396M   2% /run
tmpfs                 1005M   68K 1005M   1% /dev/shm
tmpfs                  5.0M  4.0K  5.0M   1% /run/lock
tmpfs                 1005M     0 1005M   0% /sys/fs/cgroup
/dev/mapper/thor-home  867G  506G  317G  62% /home
/dev/sdb1              228M   47M  170M  22% /boot
tmpfs                  201M     0  201M   0% /run/user/1024
tmpfs                  201M  4.0K  201M   1% /run/user/1000

Leave a Reply

Your email address will not be published. Required fields are marked *

Please prove you are human, or at least not a very simple bot :) *