Linux Labs EspañolES

Storage: disks, LVM and RAID Labs

Build a volume group and grow a mounted LV across disks

Practitioner25 minYour VMRHEL and Debian families

Objective

lv_data, mounted and written to, grows twice without unmounting: once inside the VG, once after a third disk joins. lvs shows it spanning three PVs.

Requirements

  • A VM where you have sudo. The lab creates disk images under /var/tmp and attaches them as loop devices, so no spare disk is needed.
  • The lvm2 package (installed by default on RHEL; apt install lvm2 on Ubuntu).

Setup

bash
for i in 1 2 3; do sudo truncate -s 1G /var/tmp/lab-pv$i.img; done
for i in 1 2 3; do sudo losetup -f --show /var/tmp/lab-pv$i.img; done   # note the three devices
# RHEL 9 devices file: allow the loop devices
command -v lvmdevices && for d in /dev/loop0 /dev/loop1 /dev/loop2; do sudo lvmdevices --adddev $d; done

Steps

  1. Turn the first two loop devices into PVs and create vg_lab from them.
  2. Create lv_data of 600 MiB with XFS, mount it at /srv/data and write a test file.
  3. Grow lv_data and its filesystem by 800 MiB in one command, while mounted.
  4. Add the third device to the VG and give lv_data every remaining free extent.
  5. Show which PVs hold lv_data and confirm the test file is intact.

Verification

bash
sudo lvs -o lv_name,lv_size,devices vg_lab
df -h /srv/data
sudo vgs vg_lab
cat /srv/data/probe.txt
Show the solution
bash
sudo pvcreate /dev/loop0 /dev/loop1
sudo vgcreate vg_lab /dev/loop0 /dev/loop1
sudo lvcreate -n lv_data -L 600M vg_lab
sudo mkfs.xfs /dev/vg_lab/lv_data
sudo mkdir -p /srv/data && sudo mount /dev/vg_lab/lv_data /srv/data
echo linux-labs | sudo tee /srv/data/probe.txt
sudo lvextend -r -L +800M vg_lab/lv_data
sudo pvcreate /dev/loop2 && sudo vgextend vg_lab /dev/loop2
sudo lvextend -r -l +100%FREE vg_lab/lv_data
sudo lvs -o lv_name,lv_size,devices vg_lab

lvs prints one line per segment, with loop0, loop1 and loop2 in the devices column. VFree in vgs is 0.

Clean up:

bash
sudo umount /srv/data
sudo vgremove -y vg_lab
sudo pvremove /dev/loop0 /dev/loop1 /dev/loop2
for d in /dev/loop0 /dev/loop1 /dev/loop2; do sudo lvmdevices --deldev $d 2>/dev/null; sudo losetup -d $d; done
sudo rm /var/tmp/lab-pv*.img