Week 4 - Lab: KVM Storage
Management
Module: Operating Systems 3 (Virtualisation & Cloud Technologies)
Topic: KVM Disk Images, LVM, Snapshots, and NFS
Estimated Time: 120 Minutes
Lab Overview
In this lab, you will go "under the hood" of virtualization storage. Instead of relying on the Proxmox
GUI, you will work directly with the KVM storage layer. You will create raw disk images, manage them
with qemu-img, partition them with LVM, and perform manual snapshots and backups. Finally,
you will configure networked storage using NFS.
Objectives:
- Create and manage Virtual Disk Images (
rawandqcow2) usingqemu-img. - Manage storage inside a Linux VM using Logical Volume Manager (LVM).
- Perform internal snapshots using KVM command-line tools.
- Execute "Cold Backups" by manipulating disk image files directly.
- Configure Network File System (NFS) for shared storage.
Prerequisites: - A running Proxmox VE host (Shell Access Required). - A Linux VM (for LVM and NFS sections). - Basic understanding of Linux file permissions.
Part 1: Virtual Disk Management (KVM)
At the lowest level, a Virtual Machine's "hard drive" is just a file on the host operating system. We
will use the qemu-img tool to manipulate these files.
Step 1: Inspecting Disk Images
-
Open the Proxmox Shell (Datacenter > Node > Shell).
-
Navigate to the image directory:
bash cd /var/lib/vz/images/ ls -lhYou might see directories for your existing VMs (e.g.,100,101). -
Inspect an existing image (replace
[VMID]with an actual ID):bash qemu-img info [VMID]/vm-[VMID]-disk-0.qcow2(If you don't have a VM, skip to Step 2). -
Field Report:
- Image Format:
[ qcow2 / raw ] - Virtual Size:
[ _____________________________ ] - Disk Size (actual space used):
[ _____________________________ ]
- Image Format:
Critical Thinking:
- Why is "disk size" often smaller than "virtual size"?
[ _________________________________________________________________________ ](Hint: Sparse files)
Step 2: Creating New Images
-
Create a RAW image (1GB):
bash qemu-img create -f raw test-disk.raw 1G -
Create a QCOW2 image (1GB):
bash qemu-img create -f qcow2 test-disk.qcow2 1G -
Compare sizes:
bash ls -lh test-disk.* -
Field Report:
- RAW file size:
[ _____________________________ ] - QCOW2 file size:
[ _____________________________ ]
- RAW file size:
Step 3: Format Conversion & Compression
You can convert disk formats (e.g., to save space or import from another hypervisor).
-
Convert RAW to Compressed QCOW2:
bash qemu-img convert -c -f raw -O qcow2 test-disk.raw test-disk-compressed.qcow2 -
Verify details:
bash qemu-img info test-disk-compressed.qcow2 -
Field Report:
- Final size:
[ _____________________________ ]
- Final size:
Step 4: Resizing Images
-
Expand the QCOW2 image:
bash qemu-img resize test-disk.qcow2 +2G -
Verify new size:
bash qemu-img info test-disk.qcow2Expected: Virtual size should now be 3GB.
Part 2: Managing Storage inside the Guest (LVM)
Now that you understand the "physical" disk (the file on the host), let's look at how the Guest OS manages that space using Logical Volume Manager (LVM).
Note: For this section, use your Ubuntu/Linux VM. You can use the
test-disk.raw we created earlier by attaching it to your VM via Proxmox GUI (Hardware >
Add > Hard Disk > Import/Use existing), OR simpler: just create a dummy file inside the VM to
practice LVM.
Method: Using a loopback file inside the VM (Safe Mode)
-
Log into your Linux VM.
-
Create a 1GB file (simulates a hard drive):
bash dd if=/dev/zero of=/root/virtual_disk.img bs=1M count=1000 -
Attach it to a loop device:
bash losetup /dev/loop0 /root/virtual_disk.img -
Initialize Physical Volume (PV):
bash pvcreate /dev/loop0 -
Create Volume Group (VG):
bash vgcreate lab_vg /dev/loop0 -
Create Logical Volume (LV) (500MB):
bash lvcreate -n lab_vol -L 500M lab_vg -
Format and Mount:
bash mkfs.ext4 /dev/lab_vg/lab_vol mkdir /mnt/test_volume mount /dev/lab_vg/lab_vol /mnt/test_volume -
Verify:
bash df -h /mnt/test_volume lvs -
Field Report:
- LV Name:
[ _____________________________ ] - Filesystem Type:
[ ext4 / xfs ]
- LV Name:
Part 3: VM Snapshots (KVM CLI)
Proxmox GUI uses these same commands in the background. Let's do it manually on a QCOW2 file.
-
Back on the Proxmox Host Shell.
-
Take a snapshot of your test QCOW2 image:
bash qemu-img snapshot -c snap1 test-disk.qcow2-cstands for Create. -
List snapshots:
bash qemu-img snapshot -l test-disk.qcow2 -
Field Report:
- Snapshot ID:
[ _____ ] - Snapshot Name:
[ _____ ]
- Snapshot ID:
-
Restore a snapshot (Hypothetical command): To apply a snapshot, you would use
-a(Apply).bash # qemu-img snapshot -a snap1 test-disk.qcow2
Critical Thinking:
- Can you take a snapshot of a
rawformat disk? [ _________________________________________________________________________ ](Try it ontest-disk.rawand see what happens).
Part 4: VM Backups (Image Operations)
A "Cold Backup" is simply copying the disk image while the VM is off.
-
Ensure the VM/Disk is not in use.
-
Create a backup copy:
bash cp test-disk.qcow2 test-disk.qcow2.backup -
Verify the backup:
bash ls -lh test-disk.qcow2* -
Simulate Corruption:
bash echo "CORRUPTION" > test-disk.qcow2 # The file is now broken. -
Restore from Backup:
bash rm test-disk.qcow2 cp test-disk.qcow2.backup test-disk.qcow2 -
Field Report:
- Why must the VM be offline for a simple
cpbackup? [ _________________________________________________________________________ ]
- Why must the VM be offline for a simple
Part 5: ZFS Storage (Advanced)
ZFS is the gold standard for enterprise storage. It features self-healing, instant snapshots, and RAID capabilities without needing a hardware controller. We will simulate this using file-backed storage (since we don't have extra physical disks).
Step 1: Create Simulated Disks
- Create two 1GB files:
bash truncate -s 1G /root/disk -
Verify the status:
bash zpool statusYou should seepool: tankandstate: ONLINEwith a mirror of disk1 and disk2.
Step 3: Self-Healing in Action
-
Create some crucial data:
bash echo "This data must survive corruption" > /tank/crucial.txt sha256sum /tank/crucial.txt -
Simulate Disk Corruption (Sabotage one "disk"):
bash # Overwrite the beginning of disk1 with garbage dd if=/dev/urandom of=/root/disk1.img bs=1M count=10 conv=notrunc -
Attempt to read data (ZFS will detect and fix on-the-fly):
bash cat /tank/crucial.txtIt should still read successfully! ZFS read from the good copy (disk2). -
Check Status and Repair:
bash zpool scrub tank zpool statusYou should see "checksum errors" ondisk1.img, but "Repairing" or "Repaired".
Step 4: Native Snapshots
-
Take an instant snapshot:
bash zfs snapshot tank@bookmark1 -
Delete the file:
bash rm /tank/crucial.txt -
Rollback instantly:
bash zfs rollback tank@bookmark1 cat /tank/crucial.txt -
Field Report:
- ZFS Pool Health:
[ _____________________________ ] - Checksum Errors Detected:
[ _____________________________ ] - Data Survived Corruption:
[ Yes / No ]
- ZFS Pool Health:
Step 5: ZFS Volumes (Zvols) - The VM Connection
So far, we used ZFS like a file system (storing text files). But Virtual Machines need Block Devices (like hard drives). ZFS provides this through Zvols.
-
Create a Zvol (Virtual Block Device):
bash # Create a 500MB block device on the 'tank' pool zfs create -V 500M tank/vm-disk-1 -
Verify the Device:
bash ls -l /dev/zvol/tank/vm-disk-1Notice it looks like a device file (brw-rw----), not a regular file. -
Use it (Attach to VM): In a real Proxmox VM, this
/dev/zvol/tank/vm-disk-1path acts exactly like a physical hard drive. KVM writes raw blocks directly to it.Option A: Format directly on Host (Quick Test)
bash mkfs.ext4 /dev/zvol/tank/vm-disk-1Option B: Attach to your "Lab 2" VM (Manual KVM) Recall the manual KVM VM we created in Lab 2. We can attach this Zvol to it as a secondary hard drive.
- Stop the VM (if it's running).
- Start it with the Zvol attached:
We simply add another
-driveargument pointing to our Zvol block device.bash qemu-system-x86_64 \ -enable-kvm \ -m 512 \ -drive file=test-disk.qcow2,format=qcow2 \ -drive file=/dev/zvol/tank/vm-disk-1,format=raw \ -cdrom alpine-virt-3.20.0-x86_64.iso \ -boot d -
Verify inside the VM:
- Log in (user:
root, no password). - Run
fdisk -l. - You should see
/dev/vdb(the 500MB Zvol) alongside/dev/vda(the QCOW2 disk)!
- Log in (user:
Lab Checkpoint
Practical Skills Checklist
- Created
rawandqcow2images usingqemu-img - Inspected image details (virtual vs disk size)
- Converted and resized disk images via CLI
- Created partitions using LVM inside a guest
- Created an internal snapshot using
qemu-img snapshot - Performed a cold backup using file copy
- Created a ZFS mirrored pool using file-backed storage
- Demonstrated ZFS self-healing after data corruption
- Created a ZFS Volume (Zvol) to act as a VM block device
Theoretical Understanding Checklist
- Explained the difference between
Virtual SizeandDisk Size - Understood why
qcow2supports snapshots butrawdoes not - Explained the function of LVM (PV, VG, LV)
- Identified the risk of "Cold Backups" (Downtime)
- Explained how ZFS differs from traditional RAID (Checksumming/CoW)
- Differentiated between a ZFS Dataset (File System) and a Zvol (Block Device)
Troubleshooting Scenarios
Scenario 1: qemu-img shows disk size is 0 bytes, but virtual size is
100GB.
Explanation:
[ _________________________________________________________________________ ]
Scenario 2: You try to take a snapshot of a raw disk and get an
error.
Solution:
[ _________________________________________________________________________ ]
Scenario 3: ZFS pool shows status "DEGRADED".
Action Required:
[ _________________________________________________________________________ ]
Lab Reflection
-
Storage Layering: How does the KVM file (
.qcow2) relate to the LVM (/dev/vg/lv) inside the guest?[ _________________________________________________________________________ ] -
Format Choice: When would you use
raw(Performance) vsqcow2(Features)?[ _________________________________________________________________________ ] -
ZFS Concepts: KVM needs a Block Device. Does it use a ZFS Dataset or a Zvol? Why?
[ _________________________________________________________________________ ] -
ZFS vs Hardware RAID: Why is ZFS considered safer against "silent bit rot" than traditional RAID controller cards?
[ _________________________________________________________________________ ][ _________________________________________________________________________ ] -
Next Concepts: What would you like to learn next (advanced networking, Kubernetes storage)?
[ _________________________________________________________________________ ]