DEV Community

Cover image for How to Resize an LVM Volume in Linux
Nucu Labs
Nucu Labs

Posted on

How to Resize an LVM Volume in Linux

Introduction

Hi everyone!

This post is a quick how-to resize a LVM volume for a Linux system. One mistake that I've made when setting up my
mini PC Linux server with Alma Linux is that I didn't pay attention to the defaults when partitioning the disk.

I woke up a few months later unable to install packages and several docker services failing, the main reason:
insufficient disk space on /.

mini pc server next to opensuse mug

It turns out that by default the /home partition got 400GB of space allocated and the / partition got only ~70GB.

That is bad.

Filesystem                  Size  Used Avail Use% Mounted on
/dev/mapper/almalinux-root   70G   42G   29G  60% /
/dev/mapper/almalinux-home  400G  8.0G  392G   2% /home
Enter fullscreen mode Exit fullscreen mode

Since I was using LVM and /home has an XFS filesystem type, resizing it was not straightforward. Luckily with the help
of an AI assistant I've managed to find a solution and I'm sharing it here for reference.

Resizing Home

Here's how you typically resize the /home partition.

ext4

If home is ext4 then things are a bit easier, you'd want to boot into recovery, unmount it and shrink it.

sudo umount /home
sudo e2fsck -f /dev/mapper/almalinux-home
sudo resize2fs /dev/mapper/almalinux-home 100G
sudo lvreduce -L 100G /dev/mapper/almalinux-home
Enter fullscreen mode Exit fullscreen mode

Then extend /

sudo lvextend -r -L +300G /dev/mapper/almalinux-root
Enter fullscreen mode Exit fullscreen mode

Note: I have not tested this flow.

xfs

If the /home is XFS, then things are a bit complicated. Home cannot be resized and it has to be recreated.

You can use the following commands to check for available space. Note that this output is after I resized the partition.

➜  ~ sudo lvs
  LV   VG        Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home almalinux -wi-ao----  70.00g                                                    
  root almalinux -wi-ao---- 399.50g                                                    
  swap almalinux -wi-ao----   5.85g                                                    
➜  ~ sudo lvs
  LV   VG        Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home almalinux -wi-ao----  70.00g                                                    
  root almalinux -wi-ao---- 399.50g                                                    
  swap almalinux -wi-ao----   5.85g                                                    
➜  ~ sudo pvs
  PV             VG        Fmt  Attr PSize   PFree
  /dev/nvme0n1p3 almalinux lvm2 a--  475.35g    0 
Enter fullscreen mode Exit fullscreen mode

If there's no space left then you can't grow /. You will have to destroy and recreate the /home.

To do that you need to do the following steps:

  1. Back-up the contents from /home
sudo rsync -aXS /home/ /root/home-backup/
Enter fullscreen mode Exit fullscreen mode

I didn't have much data in it, only the gitlab-runner config and in my case it was a fast and small back-up.

  1. Recreate the /home

The following commands will destroy all the data in /home and will create it with 70G of space

sudo umount /home
sudo lvremove /dev/mapper/almalinux-home
sudo lvcreate -L 70G -n home almalinux
sudo mkfs.xfs /dev/mapper/almalinux-home
sudo mount /home
Enter fullscreen mode Exit fullscreen mode

If you encounter errors when unmounting and mounting /home check the following section for troubleshooting and fixing tips.

Troubleshooting mount and unmount

You won't be able to unmount /home if an user is using it. I had to shut down gitlab runner before unmounting.

You can check what is keeping fs busy with the following commands:

➜  ~ sudo fuser -vm /home
                     USER        PID ACCESS COMMAND
/home:               root     kernel mount /home
                     root       1449 ..c.. gitlab-runner

➜  ~ sudo lsof +D /home | head -100
COMMAND    PID USER   FD   TYPE DEVICE SIZE/OFF     NODE NAME
gitlab-ru 1449 root  cwd    DIR  253,2        6 16777344 /home/gitlab-runner
Enter fullscreen mode Exit fullscreen mode

If you get an invalid uid when mounting home you will need to edit /etc/fstab file.

You can get the uid of the new /home partition with the following commands:


➜  ~ sudo lsblk --fs
NAME               FSTYPE      FSVER    LABEL UUID                                   FSAVAIL FSUSE% MOUNTPOINTS
nvme0n1                                                                                             
├─nvme0n1p1        vfat        FAT32          E5B7-DC76                               589.9M     1% /boot/efi
├─nvme0n1p2        xfs                        b11659d8-531b-4d08-a809-e6740a32d0c5    333.8M    65% /boot
└─nvme0n1p3        LVM2_member LVM2 001       Tsnalq-dvtn-CVFS-h85H-aBcz-sOrs-jeQkfK                
  ├─almalinux-root xfs                        7e852464-4b83-4f16-a831-a9d794e584b7    350.6G    12% /
  ├─almalinux-swap swap        1              7e6ce228-b5f2-454b-b7fe-61480d34fcbd                  [SWAP]
  └─almalinux-home xfs                        c88fb303-2e9a-4ec1-8654-45ac7a0a675f     68.3G     2% /home


➜  ~ sudo blkid
/dev/mapper/almalinux-swap: UUID="7e6ce228-b5f2-454b-b7fe-61480d34fcbd" TYPE="swap"
/dev/nvme0n1p3: UUID="Tsnalq-dvtn-CVFS-h85H-aBcz-sOrs-jeQkfK" TYPE="LVM2_member" PARTUUID="69c5165d-0709-43ec-8791-9a4227c61164"
/dev/nvme0n1p1: UUID="E5B7-DC76" BLOCK_SIZE="512" TYPE="vfat" PARTLABEL="EFI System Partition" PARTUUID="3e215e38-446f-4f9e-a6d6-109f69cd1e04"
/dev/nvme0n1p2: UUID="b11659d8-531b-4d08-a809-e6740a32d0c5" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="7d56dc6b-8ba3-43d8-a3a6-cf76fee5b8cb"
/dev/mapper/almalinux-home: UUID="c88fb303-2e9a-4ec1-8654-45ac7a0a675f" BLOCK_SIZE="512" TYPE="xfs"
/dev/mapper/almalinux-root: UUID="7e852464-4b83-4f16-a831-a9d794e584b7" BLOCK_SIZE="512" TYPE="xfs"
Enter fullscreen mode Exit fullscreen mode

Then edit /etc/fstab with nano and replace the UID part (UUID=c88fb303-2e9a-4ec1-8654-45ac7a0a675f),
after that mount /home should work.

➜  ~ tail /etc/fstab 
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
UUID=7e852464-4b83-4f16-a831-a9d794e584b7 /                       xfs     defaults        0 0
UUID=b11659d8-531b-4d08-a809-e6740a32d0c5 /boot                   xfs     defaults        0 0
UUID=E5B7-DC76          /boot/efi               vfat    umask=0077,shortname=winnt 0 2
UUID=c88fb303-2e9a-4ec1-8654-45ac7a0a675f /home                   xfs     defaults        0 0
Enter fullscreen mode Exit fullscreen mode
  1. Restore the back-up

Restore the home back-up the following command and start stopped services.

sudo rsync -aHAX /root/home-backup/ /home/
Enter fullscreen mode Exit fullscreen mode
  1. Extend /

Extend the / volume with the 100% of FREE space with issuing of the following command.

sudo lvextend -r -l +100%FREE /dev/mapper/almalinux-root
Enter fullscreen mode Exit fullscreen mode

Conclusion

If we don't pay attention when installing a new Linux distro on a server we might get a surprise. Sometimes defaults
aren't that good. In this post we've explored how to shrink the /home volume and grow the / volume.

If the /home filesystem is of type ext4 then we just shrink it and grow /, otherwise if /home is xfs filesystem
we need to back up the data, recreate the /home partition from scratch, restore the back-up and then grow /.

Thank you for reading!

Top comments (0)