Skip to content
Tutorials Center
My learning

Topics

Lesson 78 of 95 ยท System Administration

Disks, Partitions, mkfs and LVM

Add and manage storage on Linux: partition tables, fdisk and parted, ext4 and xfs, mounting with fstab, LVM volumes, resizing and a RAID overview.

Sooner or later a server runs low on space, or you add a new disk, or a database needs its own volume. Handling storage is a core system administration skill, and it is also one where a single mistyped device name can destroy data. In Disks, Mounts, Logs and Monitoring you learned to read the state of your disks. This lesson is about changing it: slicing a disk into partitions, putting a file system on them, mounting them at boot, and using LVM (the Logical Volume Manager) to make volumes you can grow later.

Everything here normally requires root and a real block device, so the sandbox cannot do it. The commands appear in read-only blocks. To understand what the tools actually write to disk, we build models in Bash that you can run: a partition table written byte by byte into a disk image file, an LVM allocator with extents, an fstab checker and a RAID capacity calculator.

The layers of storage

Storage on Linux is a stack. Each layer hides the one below it and gives the one above a simpler thing to use:

flowchart TD
    D["Physical disk<br/>/dev/sdb"] --> P["Partition table + partitions<br/>/dev/sdb1"]
    P --> PV["LVM physical volume (PV)"]
    PV --> VG["Volume group (VG)<br/>a pool of extents"]
    VG --> LV["Logical volume (LV)<br/>/dev/vg0/data"]
    LV --> FS["File system<br/>ext4 or xfs"]
    FS --> M["Mount point<br/>/srv/data"]
    P -.-> FS

Without LVM, a file system sits directly on a partition (dotted arrow). With LVM there is a pool in between, which is what makes resizing easy.

Devices appear under /dev as block devices: /dev/sda is the first SCSI/SATA disk, /dev/nvme0n1 is the first NVMe drive, and numbers or p1 suffixes name its partitions (/dev/sda1, /dev/nvme0n1p1). Virtual machines often use /dev/vda. Names can change between boots, which is why you mount by UUID or label instead.

CONSOLE
$ lsblk                         # tree of disks and partitions
$ lsblk -f                      # add file system type, label, UUID, mount point
$ sudo blkid                    # UUIDs and types of every block device
$ sudo fdisk -l /dev/sdb        # partition table of one disk

Golden rule: before any destructive command, run lsblk -f and confirm the device is the empty new disk, not your system disk.

Partition tables: MBR and GPT

A new disk is just a numbered series of sectors (usually 512 bytes or 4 KiB). A partition table in the first sectors says "sectors A to B form partition 1". There are two formats:

MBR (msdos) GPT
Max disk size 2 TiB 9.4 ZB (practically unlimited)
Partitions 4 primary (or 3 + extended) 128 by default
Where it lives first 512-byte sector start and end of disk (backup copy)
Needed for old BIOS machines UEFI boot, large disks

Use GPT for new disks. MBR is still worth understanding because it is simple: the first sector holds 446 bytes of boot code, four 16-byte partition entries, and the two-byte signature 55 AA. Each entry stores a type code, the first sector (LBA) and the number of sectors, as little-endian 32-bit numbers. Let us build a disk image file, write such a table into it, and decode it again:

Bash Writing and reading an MBR partition table

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Real tools do exactly this and more (alignment to 1 MiB, GPT backup headers, checksums). Starting partitions at sector 2048 (1 MiB) is the modern alignment, and it is why free space "disappears" at the front of a disk.

fdisk, gdisk and parted

fdisk is the classic interactive tool (modern versions handle GPT too). The keys you need are p (print), g (new GPT) or o (new MBR), n (new partition), t (change type), d (delete) and w (write and exit). Nothing is changed on the disk until you press w; q leaves without saving. All read-only here:

CONSOLE
$ sudo fdisk /dev/sdb
Command (m for help): g
Command (m for help): n          # accept defaults, then give +20G as the last sector
Command (m for help): p
Command (m for help): w

parted does the same in one line, which is handy in scripts:

CONSOLE
$ sudo parted -s /dev/sdb mklabel gpt
$ sudo parted -s /dev/sdb mkpart primary ext4 1MiB 20GiB
$ sudo parted -s /dev/sdb mkpart primary 20GiB 100%
$ sudo parted /dev/sdb print
$ sudo partprobe /dev/sdb          # ask the kernel to re-read the table

Creating file systems: mkfs

A new partition is raw. mkfs writes the file system structures onto it. This erases whatever was there.

CONSOLE
$ sudo mkfs.ext4 -L data /dev/sdb1          # ext4 with the label "data"
$ sudo mkfs.xfs  -L logs /dev/sdb2          # xfs
$ sudo mkswap /dev/sdb3 && sudo swapon /dev/sdb3    # swap space

Which one? Both are mature journaling file systems:

ext4 xfs
Default on Debian, Ubuntu RHEL, Rocky, Fedora (server)
Grow online (resize2fs) online (xfs_growfs)
Shrink yes, offline only no
Strengths general purpose, simple repair very large files and directories, parallel I/O
Repair e2fsck xfs_repair (unmounted)

Other choices exist: btrfs and ZFS add snapshots, checksums and built-in volume management, and tmpfs keeps files in RAM. Pick the distribution's default unless you have a reason; the better skill is knowing how to check and grow it.

File system families and journaling

What does a file system actually have to do? Map names to data, remember free space, keep owner, permissions and timestamps (the inode), and, above all, survive a crash without corrupting itself. Features that distinguish the many Linux file systems:

  • Journaling. Before changing the structures on disk, the file system writes a short note (the journal) of what it is about to do. After a power cut it replays or discards the journal in seconds instead of scanning the whole disk with fsck. ext3 and ext4 offer three modes through the data= mount option: writeback journals only metadata and gives no ordering guarantee for file contents (fastest, but after a crash a file can contain stale data); ordered (the default) writes file data first and then journals the metadata, so no file points at garbage; journal journals data as well, which is the safest and the slowest because every block is written twice.
  • Extents. Instead of listing every block of a big file, store start + length ranges. Less metadata, less fragmentation (ext4, xfs, btrfs).
  • Delayed allocation. Hold the data in RAM and choose disk blocks only when it is flushed, so a file can be placed in one contiguous run. Persistent pre-allocation (fallocate) reserves space for a file up front.
  • Write barriers and checksums. Barriers (on by default) make the drive flush its cache in the right order; journal checksums let the kernel detect a torn journal. Btrfs and ZFS go further and checksum all data.
  • Copy-on-write (CoW) and snapshots. Btrfs and ZFS never overwrite data in place, which gives cheap snapshots and clones.

The family tree you will see in books and exam questions:

File system Era and role Notes
ext2 1993 No journal; still used on small boot partitions and flash media
ext3 2001 ext2 plus a journal; online upgrade with tune2fs -j
ext4 2008, the usual default Extents, delayed allocation, journal checksums, up to 1 EiB volumes and 16 TiB files
XFS SGI 1993, RHEL default Excellent with large files and parallel I/O; grows but cannot shrink
Btrfs Modern CoW Snapshots, compression, built-in RAID and checksums; default on some distributions (openSUSE, Fedora Workstation)
ZFS Out-of-tree on Linux Pools, snapshots, strong checksums; license keeps it out of the kernel
bcachefs Newest CoW design A multi-device CoW file system with caching tiers; it entered the mainline kernel in 6.7, but its maintenance has since moved toward out-of-tree packages, so check your distribution
JFS (IBM) and ReiserFS / Reiser4 2000s Journaled alternatives; ReiserFS was removed from recent kernels and is only of historical interest
Xiafs 1993 An early rival of ext2, long gone
F2FS Flash-friendly Designed for SSDs and phone storage
vfat, exFAT, NTFS Compatibility USB sticks, SD cards and Windows disks; no Unix permissions (the mount options fake them)
tmpfs, procfs, sysfs Virtual Live in RAM or inside the kernel; nothing is stored on disk

Ask the system what it is using. stat -f reports the file system type of any path, and findmnt or df -T list it per mount point:

Bash Which file system holds this directory?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The output depends on the sandbox (it often shows overlayfs or tmpfs), which is the point: the same commands work on any file system, and the type only matters when you choose tools such as resize2fs, xfs_growfs or btrfs.

Mounting and /etc/fstab

A file system is used by mounting it on an empty directory. See Linux Filesystem Layout for the standard directories.

CONSOLE
$ sudo mkdir -p /srv/data
$ sudo mount /dev/sdb1 /srv/data
$ findmnt /srv/data                 # confirm it
$ sudo umount /srv/data             # fails with "target is busy" if a process still uses it

That mount is gone after a reboot. To make it permanent add a line to /etc/fstab, which has six whitespace-separated fields:

TEXT
# device                                   mount point  type  options           dump  pass
UUID=3f5b8a2e-1c4d-4e7a-9b21-0a5c7d9e1f33  /srv/data    ext4  defaults,noatime  0     2
  1. the device, best as UUID= (from blkid) or LABEL=, because /dev/sdb1 can change;
  2. the mount point;
  3. the file system type;
  4. mount options (defaults, noatime, ro, nofail to boot even if the disk is absent, noexec, nosuid);
  5. dump flag, nearly always 0;
  6. pass: the order of file system checks at boot, 1 for the root file system, 2 for others, 0 for none.

A typo in fstab can stop a machine from booting, so test before you reboot: sudo mount -a mounts everything listed, and findmnt --verify checks the file for mistakes. The next example is a checker for the most common errors. It reads a sample fstab we create ourselves:

Bash A small fstab linter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Every warning corresponds to a real incident: a missing field is a hard error, device names move when you add a disk, duplicate mount points hide one another, and a missing nofail on a removable disk can drop a server into emergency mode at boot.

LVM: flexible volumes

Plain partitions are rigid: to enlarge one you need free space right after it. LVM removes that limit. You hand LVM one or more disks or partitions, it pools their space, and you carve logical volumes out of the pool. Vocabulary:

  • PV (physical volume): a disk or partition prepared for LVM (pvcreate).
  • VG (volume group): a pool made of one or more PVs (vgcreate).
  • LV (logical volume): a slice of the pool, which holds a file system (lvcreate).
  • PE (physical extent): the allocation unit, 4 MiB by default. Space is always handed out in whole extents.
CONSOLE
$ sudo pvcreate /dev/sdb1 /dev/sdc1
$ sudo vgcreate vg0 /dev/sdb1 /dev/sdc1
$ sudo lvcreate -L 20G -n data vg0
$ sudo mkfs.ext4 /dev/vg0/data
$ sudo mount /dev/vg0/data /srv/data
$ sudo pvs; sudo vgs; sudo lvs            # compact status tables

The payoff is growing. When /srv/data is full, extend the logical volume and the file system in one step, with the volume mounted and in use:

CONSOLE
$ sudo lvextend -r -L +10G /dev/vg0/data    # -r also resizes the file system
$ sudo lvextend -r -l +100%FREE /dev/vg0/data
$ sudo vgextend vg0 /dev/sdd1               # add a new disk to the pool first if the VG is full

Shrinking is possible only for ext4 and requires unmounting first; xfs cannot shrink. Snapshots (lvcreate -s -L 5G -n snap /dev/vg0/data) freeze a point-in-time view, useful for consistent backups. Remember that a snapshot is not a backup by itself; see Archives and Compression.

To feel how extents work, here is a small allocator that behaves like lvcreate and lvextend for a volume group of two PVs:

Bash An LVM model: extents and logical volumes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Notice that the LV is not tied to one disk: the 35 GiB data volume in this model spans extents from several PVs, and the file system on top never knows. This is why LVM can also move data off a failing disk with pvmove.

RAID overview

RAID combines several disks for speed or safety. It is not a backup: deleting a file deletes it on every disk. Linux software RAID is managed with mdadm; ZFS, btrfs and LVM can do similar things.

Level Disks (min) Usable capacity Survives Idea
RAID 0 2 n x size no failure striping: fast, no safety
RAID 1 2 1 x size n-1 failures mirroring
RAID 5 3 (n-1) x size 1 disk striping with distributed parity
RAID 6 4 (n-2) x size 2 disks double parity
RAID 10 4 n/2 x size 1 per mirror pair stripe of mirrors
CONSOLE
$ sudo mdadm --create /dev/md0 --level=5 --raid-devices=4 /dev/sd[b-e]1
$ cat /proc/mdstat                 # array state and rebuild progress
$ sudo mdadm --detail /dev/md0
$ sudo mdadm --detail --scan | sudo tee -a /etc/mdadm/mdadm.conf

A calculator makes the trade-offs concrete:

Bash RAID capacity and fault tolerance

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Check an array's health regularly and replace failed members promptly: a degraded RAID 5 with a second failure during the (long) rebuild loses everything. Keep real backups.

A safe workflow for adding a disk

  1. lsblk -f and dmesg | tail to identify the new device; double-check size and name.
  2. Partition (parted or fdisk), or give the whole disk to LVM.
  3. mkfs.<type> with a label.
  4. Create the mount point, mount, test by writing a file, then umount.
  5. Add the UUID= line to /etc/fstab with nofail for non-essential disks; run sudo mount -a.
  6. Check with df -hT and findmnt, and put the disk on your monitoring and backup lists.

To practise without risking a real disk, use a loop device: truncate -s 1G test.img; sudo losetup -fP --show test.img turns a file into a block device that you can partition, format and mount exactly like a disk.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why should /etc/fstab refer to disks by UUID= or LABEL= instead of /dev/sdb1?

    The kernel assigns names like /dev/sdb in discovery order, so they can shift. The UUID is stored inside the file system and identifies it reliably.
  2. 2. You need a file system that you may want to shrink later. Which is the right choice?

    ext4 supports offline shrinking with resize2fs; xfs can only be grown.
  3. 3. What does lvextend -r -L +10G /dev/vg0/data do?

    lvextend grows the LV by the amount after +, and -r (--resizefs) calls the right resize tool (resize2fs or xfs_growfs) afterwards.
  4. 4. Four 4 TB disks in RAID 5 give about how much usable space, and how many failures can they survive?

    RAID 5 uses one disk's worth of space for parity: (4 - 1) x 4 TB = 12 TB, and it tolerates the loss of one disk.
  5. 5. Which command ordering is correct for adding a brand-new disk /dev/sdb?

    Create the partition table first, make a file system on the partition or logical volume, then mount it, and finally record it in fstab so it survives a reboot.

Summary

  • Storage is a stack: disk, partition table (GPT for new disks), partitions, optionally LVM (PV, VG, LV), file system, mount point.
  • lsblk -f and blkid identify devices; double-check the name before any command that writes.
  • fdisk and parted edit partition tables; mkfs.ext4 and mkfs.xfs create file systems and erase what was there.
  • Mount temporarily with mount, permanently via six-field /etc/fstab lines using UUIDs; test with mount -a and findmnt --verify.
  • LVM allocates space in extents: pvcreate, vgcreate, lvcreate, and lvextend -r to grow a live volume; xfs only grows.
  • RAID improves availability or speed but is not a backup.

Next: Firewalls: ufw, iptables and nftables