Skip to main content

LVM

Author does not take any responsibilities on the guide below.

Create new physical volume, volume group and logical group. Mount it to freshly created directory. Add to fstab for automatic mount during boot process.

# apt install lvm2
dnf install lvm2

hostname
export host="$(hostname)"
lsblk

export dev="/dev/sdb"
export size="149.9G"
# export fs="ext4"
export fs="xfs"

pvcreate ${dev}
vgcreate vg-${host}-data ${dev}
lvcreate -L ${size} -n lv-${host}-data vg-${host}-data
lvscan

mkfs.${fs} /dev/vg-${host}-data/lv-${host}-data
mkdir -p /mnt/${host}-data
mount /dev/vg-${host}-data/lv-${host}-data /mnt/${host}-data
df -h

#TODO: add into /fstab with oneliner
echo "add me to fstab"
mount | grep ${host}
nano /etc/fstab
# /dev/mapper/vg--host--data-lv--host--data /mnt/host-data ext4 defaults 0 1
systemctl daemon-reload

# when possible, test VM restart to ensure disk will be mounted
shutdown -r now

To extend the volume, we have to attach new physical (virtual) disk on hypervisor.

Preparations

We need to understand which disk need to be extended: In my case it is a root which is full.

df -h

and verify it is LVM

lsblk

After adding the disk, it should appear in the list:

lsblk

Let's note which file system in use: 'XFS' or 'ext4', will be needed later..

lsblk -f

We are good to go, prepare for activities. Replace your values for variables dev and fs with correct ones. Subtract 0.1 from partition size:

export dev="/dev/sdc"

Disk need to be marked as a "Physical Volume" to be able to join the "volume group" and list:

pvcreate ${dev}
pvscan

First, let's define, which 'volume group' need to be extended

vgscan

In my case, it is ol_vbox. Let's define it:

export vg="ol_vbox"

Let add "new Physical Volume" to the "Virtual Group":

echo ${vg}
echo ${dev}
vgextend ${vg} ${dev}

List Physical Volumes, new disk should be shown

pvscan

Now it is a time to extend Logical Volume. Let's observe current ones first

lvdisplay

In my case, "Volume Group" is linked to "Logical Volume" has path "/dev/ol_vbox/root". Set up extension size.

export lv="/dev/ol_vbox/root"
export size="49.9G"

and extend "Logical Volume"

lvextend -L +${size} ${lv}

Depending on which file system is in use, note current partition size, expand it and check size again:

df -h

# for "ext4"
resize2fx ${lv}

# for "xfs"
xfs_growfs ${lv}

df -h

Q.E.D.

Storage management using LVM is a logical process, once understood.