Some brief notes for myself on growing a LVM root filesystem
I have a Fedora virtual machine image that I use to try out Fedora upgrades before I have to do them on my real system. Because it was created a long time ago, it had only a 20 GB disk. For a long time this was okay, but when I went to upgrade it from Fedora 34 to Fedora 35, the upgrade failed with an out of space error. Obviously the time had come to enlarge the disk and grow everything.
Growing the disk image depends on your virtualization system and
in any case is usually pretty simple and obvious; that took me no
time at all. First, you need to change the disk partitioning so
that the LVM partition is expanded to cover the new disk space.
Unfortunately, fdisk
doesn't seem to directly support doing this.
Some directions I found online suggested
using fdisk to delete and recreate the partition, but I thought
that was too alarming so I tried out GNU parted instead, because parted
has a 'resizepart'
operation. The magic unit to use in resizepart
to make it use up
all of the new free space is '-1s', which means 'the last sector
of the disk'. It turns out that parted
is more dangerous than I
expected, because unlike software like fdisk
, parted
writes
your changes to disk immediately. If I ever have to do this to real
disks that I cannot take virtual machine snapshots of and roll back
to if something goes wrong, I will use a different program.
(However, this is not a common thing to need to do on real disks. With real disks, you usually get a new disk and make a new partition table on it that has the right size from the start.)
With the partition resized (and the VM rebooted to get it to be fully recognized by the kernel), I needed to grow the LVM volume and the filesystem. This is a two step process. First, expand the LVM (physical) volume:
pvresize /dev/sda1
Second, grow the LVM partition to use all the space, and tell it to have ext4 resize things as well while we're here:
lvextend -l +100%FREE -r .../root
The -r
asks LVM to also resize the filesystem for you. Using -l
and its argument is a specific incantation for 'add 100% of the
free space'. The format of this argument is not clearly explained
in the lvextend
manpage, nor is the fact that it only works with
-l
(which specifies how much to grow in terms of extents) and not
-L
(which specifies how much to grow in more normal units). Since
I normally use -L
, this was puzzling and annoying; I spent a bit
of time flailing around and thinking that I had the syntax wrong
somehow.
(I'm sure I'll need to do this to other virtual machine images in the future, and I will feel really silly at myself if I don't write this down now.)
PS: If your LVM or root partition is not the last partition on your virtual disk, you have a big problem that is well beyond the scope of this entry. I suggest making a new virtual disk and copying things over somehow.
|
|