Block devices

A disk is cut into partitions, and file systems sit on them

vda: one 40 GiB disk, drawn to scalevda1 0.6 GiBvfat → /boot/efivda2 1 GiBxfs → /bootvda3 38.4 GiBan LVM physical volumelogical volumes carved from itrhel-root 34.4 GiB → /swap 4NAME SIZE TYPE MOUNTPOINTSvda 40G disk├─vda1 600M part /boot/efi├─vda2 1G part /boot└─vda3 38.4G part ├─rhel-root 34.4G lvm / └─rhel-swap 4G lvm [SWAP]$ lsblk draws the same treelsblk -f adds each file system’s type and UUIDA disk is divided into partitions; file systems live on partitions or on logical volumes made from them.The standard RHEL layout: a small EFI partition, /boot, and the rest handed to LVM for the root file system and swap. Disks appearas /dev/sda (SATA, SCSI), /dev/nvme0n1 (NVMe) or /dev/vda (virtual), and partitions add a number: vda1, nvme0n1p1.

Block devices are split into partitions; file systems live on partitions or on logical volumes. lsblk draws the tree.

Block devices · Going deeper · 1 of 2

GPT replaced MBR: more partitions, any size, a backup copy

MBR (msdos)GPTPartitions4 primary, or 3 + extended128 by defaultLargest disk2 TiB (32-bit sectors × 512 bytes)8 ZiB: no practical limitCopies of the tableone, at the starttwo: start and end, with checksumsBoots withlegacy BIOSUEFI (and BIOS with a small partition)Use itonly for old systemsalways, for new disks# parted /dev/vdb mklabel gpt# parted /dev/vdb mkpart data xfs 1MiB 10GiBfdisk and gdisk ask questions;parted can be scriptedGPT has replaced MBR: more partitions, any disk size, and a backup copy of the table.MBR stores sector numbers in 32 bits, so it cannot address past 2 TiB on 512-byte sectors. Start partitions at 1 MiB so they line upwith the disk’s physical blocks. After changing a disk in use, run partprobe or udevadm settle so the kernel sees the new table.

GPT allows 128 partitions and disks beyond the 2 TiB MBR limit, and keeps a backup of the table at the end of the disk.

Block devices · Going deeper · 2 of 2

XFS or ext4: mostly your distribution’s default

TypeDefault onCan shrink?Good forMake itXFSRHEL, AlmaLinuxnolarge files, many writersmkfs.xfsext4Ubuntu, Debianyes, unmountedgeneral purposemkfs.ext4vfatEFI partitions-EFI boot, USB sticksmkfs.vfatBtrfsFedora, SUSEyessnapshots, checksumsmkfs.btrfstmpfs/run, /dev/shm-RAM only, lost at rebootmount -t tmpfsXFS and ext4 do the same job; the choice is mostly your distribution’s default.Both journal their metadata, so a crash costs seconds of checking, not hours. ext4 can be shrunk and its fsck is simpler; XFSscales further and is what Red Hat supports and tests. vfat is for the EFI partition and for media other systems must read.Check and repair with the matching tool, on an unmounted file system: xfs_repair, or e2fsck (fsck.ext4).

XFS is RHEL’s default and cannot shrink; ext4 is Ubuntu’s and can; vfat is for EFI and removable media.

Adding storage

Five steps from a raw disk to a mount that survives reboot

1 partitionparted /dev/vdbmkpart ...or fdisk, gdisk2 file systemmkfs.xfs/dev/vdb1mkfs.ext4, mkfs.vfat3 mount pointmkdir /dataan empty directory4 mount nowmount /dev/vdb1/datagone at reboot5 mount at bootUUID=… /dataxfs defaults 0 0a line in /etc/fstab# mount -a && findmnt /dataTARGET SOURCE FSTYPE OPTIONS/data /dev/vdb1 xfs rw,relatime,...test the fstab line before you reboot:a bad line can stop the machine bootingNew storage takes five steps: partition, make a file system, make a directory, mount it, and record it in fstab.Skip the last step and the disk is gone after a reboot. Skip the test and a typo can drop the next boot into emergency mode.mount -a mounts everything in fstab that is not mounted yet, so it fails now, while you are watching, instead of at 3 a.m.

Partition, mkfs, make a directory, mount, and add the line to /etc/fstab; then test with mount -a.

fstab

Six fields per line, and name the file system by UUID

UUID=7b1f…e2c4what: the file system, by UUID/datawhere: the mount pointxfstypedefaults,nofailoptions0dump0fsck orderWhy not /dev/vdb1?Device names follow detection order. Add a disk and vdb can become vdc;the UUID is written inside the file system and never changes. blkid shows it.fsck order: 1 for /, 2 for the rest, 0 to skip (XFS ignores it).Other ways to name itLABEL=data/dev/mapper/vg0-dataLVM names are stable tooUseful optionsnofail boot even if missingnoexec no programs from here_netdev wait for the networkEach fstab line has six fields: what, where, type, options, dump and check order. Name the file system by UUID.systemd turns every line into a .mount unit at boot. If a listed file system is missing and not marked nofail, boot stops in emergencymode: the console then asks for the root password, and you fix the line there. After editing, run systemctl daemon-reload and mount -a.

An fstab line gives what, where, type, options, dump and check order; mount by UUID because device names can change.

LVM

Pool the disks, then cut volumes from the pool

physical volumes/dev/vdb 20 GiB/dev/vdc 10 GiBvolume group vg0: 30 GiB of 4 MiB extents, one poollogical volumes, cut from the pool; one spans both disksapp 12 GiB → /srvlogs 14 GiB → /var/log4 GiB free# pvcreate /dev/vdb /dev/vdc# vgcreate vg0 /dev/vdb /dev/vdc# lvcreate -n app -L 12G vg0# lvcreate -n logs -L 14G vg0# pvs; vgs; lvsdevice: /dev/vg0/appalso /dev/mapper/vg0-appLVM pools disks into a volume group and cuts logical volumes from it, free of disk boundaries.A logical volume can be larger than any one disk, grow while mounted, and move to new disks without downtime. You pay with onemore layer to understand. Sizes drawn to scale. Inspect with pvs, vgs and lvs, or the longer pvdisplay, vgdisplay and lvdisplay.

Physical volumes form a volume group; logical volumes are cut from it and can span disks and grow online.

Growing a volume

A full file system, fixed in one command while in use

Before: /var/log is 91% fulllogs 14 GiB, 12.7 GiB used# lvextend -r -L +4G /dev/vg0/logs-r grows the file system in the same stepAfter: same data, more room, never unmountedlogs 18 GiB, 71% usedXFS: grows only. ext4: grows online, shrinks unmounted.Pool empty? vgextend vg0 /dev/vdd first.With LVM, a full file system is fixed in one command, while it stays mounted and in use.lvextend -r grows the volume and then the file system on it (xfs_growfs or resize2fs). Without -r the volume grows but the filesystem does not, and df shows no change: the most common LVM mistake. Plan for growth: XFS, RHEL’s default, cannot be shrunk.

lvextend -r grows a logical volume and its file system together, online; XFS can grow but never shrink.

Growing a volume · Going deeper · 1 of 3

Swap holds idle memory so RAM can do useful work

Memory on a 16 GiB server under pressureprograms 9.9 GiBpage cache 5.4 GiB: given back on demandRAM 16 GiBswap 4 GiB: 0.75 GiB of idle pages moved out# mkswap /dev/vg0/swap2; swapon /dev/vg0/swap2/etc/fstab: UUID=... none swap defaults 0 0$ swapon --show; free -hWhen the swap rate stays high,the machine is short of memory:vmstat 1, columns si and soSwap is disk space for memory pages nobody is using, so RAM stays free for the ones that are.A little swap use is healthy: idle pages leave and the space goes to cache. Constant swapping in and out is not: disk is thousands oftimes slower than RAM. Swap can be a partition, a logical volume or a file (fallocate, chmod 600, mkswap, swapon). Sizes illustrative.

Swap stores inactive memory pages on disk; some use is normal, constant swapping means the machine needs more memory.

Growing a volume · Going deeper · 2 of 3

NFS joins a server’s folder to your tree; autofs mounts it on demand

nfs01exports /srv/homeNFS, TCP 2049client# mount -t nfs nfs01:/srv/home /mntor in fstab:nfs01:/srv/home /mnt nfs _netdev 0 0mounted at boot, all the timeautofs: mount on first use/etc/auto.master.d/home.autofs/home/remote /etc/auto.home/etc/auto.home* -rw nfs01:/srv/home/&cd /home/remote/ana mounts ana’s folderNFS makes a server’s directory part of your tree; autofs mounts it only when someone steps into it.An fstab NFS mount is always there, and a slow server can hold up the boot (_netdev waits for the network). autofs watches a directoryand mounts on first access, then unmounts after a few idle minutes; the * and & in the map give every user their own home from one line.Install nfs-utils (RHEL) or nfs-common (Ubuntu) and autofs, enable --now autofs, and showmount -e nfs01 lists what is exported.

NFS mounts a remote directory; autofs mounts it on first access and unmounts it when idle.

Disk quotas · Going deeper · 3 of 3

Cap what each user may store, with a warning line first

ana’s usage of /home, with a quotasoft 4 GiBhard 5 GiBOver the soft limit: a warning, and a grace period (7 days by default) to tidy up.At the hard limit, or when the grace period ends: writes fail.# mount -o remount,uquota /home (XFS: set in fstab as uquota)# xfs_quota -x -c 'limit bsoft=4g bhard=5g ana' /home# xfs_quota -x -c 'report -h' /homeA quota caps how much space a user or group may use on one file system, with a soft warning line and a hard stop.

A quota sets a soft limit with a grace period and a hard limit per user or group on one file system.

Lab

From a blank disk to a permanent, growing mount

# parted -s /dev/vdb mklabel gpt mkpart lvm 1MiB 100%1# pvcreate /dev/vdb1; vgcreate vg1 /dev/vdb12# lvcreate -n data -L 2G vg1; mkfs.xfs /dev/vg1/data3# blkid -s UUID -o value /dev/vg1/data43f9a0c52-6d1e-4b7a-9c3e-0d5a8b21e7f4# vi /etc/fstab add: UUID=3f9a… /data xfs defaults 0 0# mkdir /data; systemctl daemon-reload; mount -a; df -h /dataFilesystem Size Used Avail Use% Mounted on/dev/mapper/vg1-data 2.0G 47M 1.9G 3% /data# lvextend -r -L +1G /dev/vg1/data; df -h /data | tail -15/dev/mapper/vg1-data 3.0G 54M 2.9G 2% /data1GPT, one partitionthe whole new disk2Into LVMa PV and a VG3A volume and XFS2 GiB to start4Mounted by UUIDthen tested with mount -a5Grown onlinevolume and file system togetherLab: give a VM a second disk and take it from raw device to a growing, permanent mount.Add a 10 GiB disk to your VM first. Press the down arrow for the steps.

GPT, LVM, XFS and ext4, mounts by UUID and label, an online extension, new swap and an automounted NFS share.

Lab · Going deeper

From a blank disk to a permanent, growing mount

Add a 10 GiB virtual disk to your VM (it appears as /dev/vdb or /dev/sdb; check with lsblk). Take a snapshot of the VM first: fstab mistakes stop boots.

Steps

  1. Look before you cut. lsblk -f; blkid; findmnt --real. Confirm which device is the new, empty one.
  2. Partition. parted -s /dev/vdb mklabel gpt mkpart lvm 1MiB 6GiB mkpart data 6GiB 100%; lsblk /dev/vdb.
  3. Build LVM. pvcreate /dev/vdb1; vgcreate vg1 /dev/vdb1; lvcreate -n data -L 2G vg1; pvs; vgs; lvs.
  4. Make two file systems. mkfs.xfs /dev/vg1/data; mkfs.ext4 -L archive /dev/vdb2.
  5. Mount them permanently. One fstab line by UUID for /data, one by LABEL=archive for /archive. mkdir both, systemctl daemon-reload, mount -a, findmnt --verify.
  6. Grow online. Fill /data with fallocate -l 1500M /data/big; lvextend -r -L +1G /dev/vg1/data; df -h /data.
  7. Add swap. lvcreate -n swap -L 512M vg1; mkswap; add it to fstab; swapon -a; swapon --show; free -h.
  8. Mount over the network. If you have a second VM: export a directory with NFS, mount it by hand, then configure autofs so it mounts on first cd.
  9. Prove it. Reboot. /data, /archive and the swap must all be back, and /data must show 3 GiB.
Check: After a reboot, findmnt /data and findmnt /archive both answer, swapon --show lists the new swap, and df -h /data shows the grown size.

Lesson complete

You finished “Disks and file systems”

Nicely done.

Open in a new tab