Lab 5 of 8 · Storage, filesystems and LVM
Build and grow an LVM volume on practice disks
Create two practice disks, combine them with LVM, mount a filesystem at boot, then grow it while it is in use.
- Time
- 45–60 minutes
- Cost
- Free.
What you learn
Disks fill up, and servers need more space without downtime. LVM builds volumes from disks and grows them while they are in use.
- Finding what uses space with df and du
- Physical volumes, volume groups and logical volumes
- Mounting a filesystem at boot with /etc/fstab, safely
- Growing a volume while it is mounted
In the AI era: An AI-written disk command can wipe the wrong disk, and a wrong fstab line can stop a server from booting. Sysadmins check device names first, verify fstab before relying on it, and practise on spare disks, as you do here.
Get set up
Commands use Homebrew in the Terminal app. Already have a tool? Skip it.
- A terminalOfficial install page ↗
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"Use the Terminal app. The command installs Homebrew, which the other macOS commands use.
Before you start
Finish lab 1.
Start your server and open your repository: run
multipass start sysadmin-labandmultipass shell sysadmin-labon your computer, thencd linux-admin-labon the server.
Step by step
1 Find out what is using space
dfshows free space per filesystem;duadds up folder sizes.df -h / sudo du -xh /var --max-depth=1 2>/dev/null | sort -h | tail -n 5You should see:The free space on
/, then the five largest folders under/var.2 Create two practice disks
Real servers get new disks from the cloud or the hardware. Here, two files act as disks through loop devices, so nothing real can be harmed.
D1andD2remember the device names for the next steps; if you open a new shell, find them again withsudo losetup -a.sudo apt install -y lvm2 sudo truncate -s 1G /var/tmp/disk1.img /var/tmp/disk2.img D1=$(sudo losetup --find --show /var/tmp/disk1.img) D2=$(sudo losetup --find --show /var/tmp/disk2.img) echo "$D1 $D2"You should see:Two device names, such as
/dev/loop3 /dev/loop4.3 Build the LVM volume
A physical volume (PV) is a disk prepared for LVM, a volume group (VG) pools PVs, and a logical volume (LV) is the slice you format and mount.
sudo pvcreate "$D1" sudo vgcreate labvg "$D1" sudo lvcreate --name data --size 800M labvg sudo lvsYou should see:A logical volume
datainlabvgof800.00m.4 Create a filesystem and mount it
ext4 is the usual Linux filesystem.
sudo mkfs.ext4 -q /dev/labvg/data sudo mkdir -p /mnt/data sudo mount /dev/labvg/data /mnt/data df -h /mnt/dataYou should see:About 780M of space mounted on
/mnt/data.5 Ask AI for the fstab line, then verify it
Ask your AI assistant: "Write an /etc/fstab line to mount the ext4 logical volume /dev/labvg/data at /mnt/data at boot, without stopping boot if it is missing." Compare it with the line below, which uses
nofailbecause practice disks disappear after a restart. Then let the system check the file and prove the line works before you trust it.echo '/dev/labvg/data /mnt/data ext4 defaults,nofail 0 2' | sudo tee -a /etc/fstab sudo findmnt --verify sudo umount /mnt/data sudo mount -a findmnt /mnt/dataYou should see:findmnt --verifyreports no errors, andmount -amounts/mnt/dataagain from the fstab line.6 Grow it while it is in use
Add the second disk to the group, then grow the volume and its filesystem in one step. Nothing is unmounted.
echo "test data" | sudo tee /mnt/data/keep.txt sudo pvcreate "$D2" sudo vgextend labvg "$D2" sudo lvextend --resizefs --size +900M /dev/labvg/data df -h /mnt/data cat /mnt/data/keep.txtYou should see:The size grows to about 1.7G, and
test datais still there.7 Write your notes
Create the file with
nano notes/05-storage.md.# Storage and LVM ## Space on my server - Free space on `/`: ... - Largest folder under `/var`: ... ## My LVM setup - Physical volumes: ... - Volume group: ... - Logical volume and size before and after growing: ... ## fstab - The AI's line, and how it differed from mine: ... - Why `nofail` matters: ...8 Commit and push
Save your work to GitHub from the server.
git add notes git commit -m "Add storage and LVM notes" git push9 Clean up the practice disks
Remove the fstab line first, so the server never looks for a disk that no longer exists.
sudo umount /mnt/data sudo sed -i '\#/dev/labvg/data#d' /etc/fstab sudo lvremove -y labvg/data sudo vgremove -y labvg sudo losetup -d "$D1" sudo losetup -d "$D2" sudo rm /var/tmp/disk1.img /var/tmp/disk2.img grep -c labvg /etc/fstabYou should see:0: the fstab line is gone.
You are done when
You built an LVM volume from a practice disk and mounted it.
You grew it while it was mounted, and your test file survived.
Your fstab line passed
findmnt --verify, and you removed it when cleaning up.
We check your public GitHub repository and give you a page to share with recruiters, and can add the project to your resume.