LVM and RAID¶
Practical 4.5 lvm-and-raid-annex.pdf · PDF
Friday, 18:00. Joan, from design at Tramuntana Apps, tries to save a video and gets No space left on device. Last week you gave design a partition of 2 GB. It is full. And partitions do not stretch. What now?
The old answer: buy a bigger disk, copy everything across, switch the server off. Tonight. In Mounting file systems you attached partitions to folders. Today you meet two tools that put several disks behind one folder: one to make space flexible, one to survive a dead disk.
What you will learn
- Name the three layers of LVM (physical volume, volume group, logical volume) and what each one is for
- Build a logical volume, format it, mount it, and grow it live with a new disk
- Compare RAID 0, 1, 5, 6 and 10: minimum disks, usable space, how many disks may die
- Build a RAID 1 with
mdadm, break a disk on purpose and rebuild it - Read
/proc/mdstatand tell a healthy array from a degraded one - Why two disks in one LVM volume make your data less safe, not more
LVM: a binder instead of a wall¶
LVM (Logical Volume Manager) puts a layer between the disks and the file systems, so space can be moved and grown.
A partition is a wall of bricks: once it is built, it stays where it is. LVM works like a ring binder. You put packs of blank sheets into it, and you place dividers wherever you want. Need a bigger section? Add another pack and move the divider. Nobody takes the section out to do it.
| Layer | Short | In the binder | Command |
|---|---|---|---|
| Physical volume | PV | A pack of blank sheets: a disk (or partition) handed to LVM | pvcreate |
| Volume group | VG | The binder: all the sheets of all the packs, in one pile | vgcreate |
| Logical volume | LV | A section between two dividers. This is what you format and mount | lvcreate |
flowchart TB
subgraph PVs["Physical volumes (the packs of sheets)"]
VDC["<b>/dev/vdc</b><br>1 GiB disk"]
VDD["<b>/dev/vdd</b><br>1 GiB disk"]
end
VG["<b>vg_tram</b><br>volume group: the binder"]
LV["<b>lv_media</b><br>logical volume: one section"]
FS["<b>ext4</b><br>file system"]
MNT["<b>/srv/media</b><br>the folder Joan uses"]
VDC --> VG
VDD -->|"added later with vgextend"| VG
VG -->|"lvcreate cuts a section"| LV
LV -->|"mkfs.ext4"| FS
FS -->|"mount"| MNT
The device you format and mount is /dev/vg_tram/lv_media. In df it appears as /dev/mapper/vg_tram-lv_media: two names, one volume.
Where you will meet it
The Debian installer offers "use entire disk and set up LVM". Many servers are installed that way, exactly for Joan's Friday.
Build a volume and fill it¶
In deb-lab you add four new virtual disks of 1 GiB (they appear as vdc, vdd, vde, vdf; use lsblk to check yours). Then you install the tools and build the three layers on the first disk:
sudo apt install lvm2 mdadm
sudo pvcreate /dev/vdc
sudo vgcreate vg_tram /dev/vdc
sudo lvcreate -n lv_media -L 800M vg_tram
sudo mkfs.ext4 /dev/vg_tram/lv_media
sudo mkdir /srv/media
sudo mount /dev/vg_tram/lv_media /srv/media
sudo chown $USER: /srv/media
df -h /srv/media
Each step answers with one line: Physical volume "/dev/vdc" successfully created., Volume group "vg_tram" successfully created, Logical volume "lv_media" created. and df -h shows a volume of about 770M.
Why 800M and not 1G? A 1 GiB disk gives 1020.00m to the group, not 1024: LVM keeps a little for its own notes.
Now play Joan's Friday. A 700 MB video fits; the next 200 MB one does not:
Grow it without stopping¶
Add the second disk to the binder, then move the divider:
vgextendaddsvddto the group.vgsnow shows#PV 2and about1.21gfree (VFree).lvextend -L +1Ggrows the logical volume by 1 GB. The-ralso resizes the file system inside it.df -hjumps to about1.8G, and the 200 MB video fits.
Did you unmount? Did you copy anything? Did Joan have to stop? No, three times. lsblk now shows vg_tram-lv_media under both vdc and vdd.
Two layers, two sizes
Forget the -r and the volume grows, but df does not move: the binder section got bigger and the file system's index did not notice. Fix it with sudo resize2fs /dev/vg_tram/lv_media.
Careful: more disks is not more safety
lv_media now lives on two disks. If either dies, the volume is broken. LVM gives you flexibility, not safety. Safety is the next section.
RAID: several disks, one volume¶
In 1988, at the University of California, Berkeley, David Patterson, Garth Gibson and Randy Katz published A Case for Redundant Arrays of Inexpensive Disks. Their idea: instead of one huge, expensive mainframe disk, use many cheap PC disks together.
There was a catch. With many disks, one of them fails much more often. So some of the space must be redundant: extra information that lets you survive a dead disk. The paper described several ways to do it and called them levels. Today the "I" in RAID is usually read as independent.
RAID (Redundant Array of Independent Disks) combines several disks into one volume, using redundancy so it can keep working when a disk dies.
Stay with paper: the disks are notebooks, and you are writing a story across them. Take n disks of size S:
| Level | How it writes | Min. disks | Usable space | Disks that may die |
|---|---|---|---|---|
| RAID 0 | Odd pages in notebook A, even pages in B. Fast | 2 | n × S | 0 |
| RAID 1 | Every page written twice: a mirror | 2 | S | n − 1 |
| RAID 5 | Pages spread over all; one page of every row is a parity (check) page | 3 | (n − 1) × S | 1 |
| RAID 6 | Like 5, with two check pages per row | 4 | (n − 2) × S | 2 |
| RAID 10 | Pairs of mirrors (1), then pages split between the pairs (0) | 4 | (n ÷ 2) × S | 1 for sure |
RAID 0 is not redundant at all
It has "RAID" in its name and zero redundancy. Lose one notebook and every second page of the story is missing: all lost. RAID 10 can survive a second dead disk only if it is in the other pair.
Parity in one minute¶
You know binary. Take two data pages and add them bit by bit without carry: same bits give 0, different bits give 1. Store the result on a third disk.
Now disk B dies. Add A and the parity the same way: 1010 and 1100 give 0110. Disk B is back. That is how RAID 5 survives one dead disk with only one extra disk of space.
Choosing a level¶
Tramuntana's server has four bays and 4 TB disks. They need at least 10 TB and must survive one dead disk. RAID 0 gives 16 TB but survives nothing. RAID 1, 6 and 10 give 8 TB or less. RAID 5 gives (4 − 1) × 4 = 12 TB and survives one disk: that is the one.
A mirror, a dead disk, a deleted file¶
Aina, from admin, wants the invoices to survive a dead disk. You build a RAID 1 with mdadm, the Linux tool for software RAID, on the two remaining disks:
The \ at the end of a line means "the command goes on". mdadm asks two questions: about a bitmap (press Enter) and Continue creating array [y/N]? (type y). Then you format, mount and save an invoice:
cat /proc/mdstat
sudo mkfs.ext4 /dev/md0
sudo mkdir /srv/admin
sudo mount /dev/md0 /srv/admin
sudo chown $USER: /srv/admin
echo '1200 EUR' > /srv/admin/invoice-001.txt
df -h /srv/admin
df says about 988M: two disks of 1 GiB, room for one. The second disk is the copy.
Reading /proc/mdstat¶
The file /proc/mdstat is the array's health report:
| You see | It means |
|---|---|
[2/2] |
Two disks wanted, two working |
[UU] |
Both disks are up |
[2/1] [U_] |
One disk missing: the array is degraded. It still works, and the next failure is the last one |
vdf[1](F) |
That disk is marked failed |
vdf[2](S) |
A spare: added, waiting to be filled |
recovery = 42.0% |
The new disk is being rebuilt from the good one |
Now kill a disk on purpose, read the invoice, and replace the disk:
sudo mdadm /dev/md0 --fail /dev/vdf
cat /proc/mdstat
cat /srv/admin/invoice-001.txt
sudo mdadm /dev/md0 --remove /dev/vdf
sudo mdadm /dev/md0 --add /dev/vdf
cat /proc/mdstat
After --fail you see [U_], and the invoice still prints. After --add, run cat /proc/mdstat several times quickly: the rebuild of 1 GiB takes under ten seconds, then [UU] again.
The other accident¶
Pau, the intern, cleans the wrong folder:
The array is perfectly healthy, [UU]. And the invoice is on neither disk. You said "delete", and the mirror deleted it on both disks at the same moment. RAID copies your mistakes as fast as your work.
An honest note about the lab
In deb-lab your four "disks" are four files on the same real disk of the PC. Your RAID protects nothing real today. It is a flight simulator: the same commands, no danger.
Clean up¶
The four disks must end empty: one becomes the backup disk in Backups. Undo everything, top to bottom:
cd ~
sudo umount /srv/admin /srv/media
sudo mdadm --stop /dev/md0
sudo mdadm --zero-superblock /dev/vde /dev/vdf
sudo lvremove -y vg_tram/lv_media
sudo vgremove vg_tram
sudo pvremove /dev/vdc /dev/vdd
sudo rmdir /srv/admin /srv/media
lsblk
lsblk must show vdc to vdf with nothing under them. All the steps, with space for your evidence, are on the Practical 4.5 sheet.
Key takeaways¶
- LVM has three layers: physical volumes (disks) go into a volume group, and you cut logical volumes from it to format and mount.
vgextend+lvextend -rgrow a mounted volume with a new disk, without unmounting.- LVM gives flexibility, not safety: a volume spread over two disks breaks if either dies.
- RAID uses redundancy to survive dead disks: RAID 1 mirrors, RAID 5 and 6 use parity, RAID 0 has none.
/proc/mdstatshows the array's health:[UU]is healthy,[U_]is degraded but still working.- RAID answers one question only: what happens when a disk dies. A deleted file is deleted on every disk.
Quick check¶
1. In LVM, which piece do you format with mkfs and mount?
- The logical volume
- The physical volume
- The volume group
- The partition table
A logical volume is the section you cut from the volume group, and it is what holds the file system. Physical volumes are the raw disks handed to LVM; the volume group is the pool that joins them.
2. What does sudo lvextend -r -L +1G vg_tram/lv_media do?
- Grows the volume and its file system by 1 GB
- Adds a new disk of 1 GB to the volume group
- Makes a 1 GB copy of the volume on another disk
- Shrinks the volume to a total size of 1 GB
-L +1G adds 1 GB to the logical volume, and -r resizes the file system inside it too. Adding a disk to the group is a different command: vgextend.
3. A logical volume is spread over two disks with LVM. One disk dies. What happens to the volume?
- It is broken, because part of it lived on the dead disk
- It keeps working, because LVM keeps a copy on the other disk
- It shrinks to the size of the disk that is still alive
- It switches to read-only mode until a new disk is added
LVM gives flexibility, not redundancy. The volume needs every disk it lives on, so two disks means two chances to lose it.
4. RAID 5 with four disks of 2 TB. How much usable space do you get?
- 6 TB
- 2 TB
- 4 TB
- 8 TB
RAID 5 uses the space of one disk for parity: (n − 1) × S = (4 − 1) × 2 TB = 6 TB. In exchange, any one disk may die.
5. /proc/mdstat shows [2/1] [U_]. What does it mean?
- One disk of the mirror is missing and the array still works
- The array has stopped and the data cannot be read
- Both disks are healthy and the array is in sync
- Every file on the array has been lost
Two disks wanted, one working: the array is degraded. It still serves the data from the healthy disk, but the next failure is the last one, so you replace the dead disk now.
6. Disk A holds 1011 and the parity disk holds 1100. Disk B died. What was on disk B?
- 0111
- 1011
- 1100
- 0011
Parity is the bit-by-bit sum without carry (same = 0, different = 1). Apply it to A and the parity: 1011 and 1100 give 0111. That is how RAID 5 rebuilds a dead disk.
7. A RAID 1 array is healthy. Marta deletes a folder by mistake. Select the two that are true.
- The folder is gone from both disks of the mirror
- The array still shows [UU] in /proc/mdstat
- The folder can be copied back from the second disk
- The array becomes degraded until the folder returns
A mirror writes every change to both disks at once, deletions included. Nothing failed, so the array stays healthy, and the folder is lost on both disks. RAID protects against a dead disk, not against a mistake.
Your mirror did its job perfectly, and the invoice is still gone. A mirror is not a copy in another place or another moment. Next: what really protects your data — Backups.