Skip to content
ALB Wiki

Disks and Storage

This is an old revision of this page — revision 5 (1970-01-21 18:02:21 UTC). It may differ from the current revision.

Storage is a four-layer stack: the physical disk is a block device, partitions carve it into regions, a filesystem structures each region, and mount grafts it into the tree. This chapter covers each layer — the tools that see them (lsblk, fdisk), the ones that shape them (mkfs, parted), the /etc/fstab that makes mounts permanent, and the recovery work for when disks go bad.

Seeing the disks

lsblk                    # the tree — disks, partitions, mounts
lsblk -f                 # + filesystems and UUIDs
fdisk -l                 # partition tables, every disk
blkid                    # UUIDs + fs types — fstab's input
df -hT                   # mounted usage + types
findmnt                  # the mount tree
smartctl -a /dev/sda     # the disk's own health report (SMART)
hdparm -I /dev/sda       # hardware info

lsblk is the first command — the visual map; blkid gives the UUIDs fstab prefers over /dev/sdX names (names shift; UUIDs don't).

Partitioning — the three tools

fdisk /dev/sda       # MBR + GPT — the classic
gdisk /dev/sda       # GPT specialist — the UEFI path
parted /dev/sda      # scriptable — `parted -l` lists all
cfdisk /dev/sda      # fdisk with menus — the kind one

MBR vs GPT: MBR — 4 primaries (or extended), BIOS-era, 2TB ceiling. GPT — 128 partitions, GUIDs everywhere, UEFI's partner, a backup table at the disk's end. Partition types carry numbers: 8300 Linux filesystem, 8200 swap, EF00 the EFI System Partition, 8E00 LVM.

Filesystems — mkfs and friends

mkfs.ext4 /dev/sda3      # the default choice
mkfs.xfs /dev/sda3       # large-scale
mkfs.btrfs /dev/sda3     # CoW + snapshots
mkfs.vfat -F32 /dev/sda1 # the ESP / USB sticks
mkfs.exfat /dev/sda3     # cross-platform big files
mkswap /dev/sda2         # swap
swapon /dev/sda2         # activate swap
tune2fs -l /dev/sda3     # ext4's tunables
mkfs.ext4 -L rootfs /dev/sda3   # a label

ext4 = boring correct. btrfs = snapshots/send. xfs = throughput at scale. The FS choice is the disk's personality.

Mounting — graft and persist

mount /dev/sda3 /mnt            # temporary
mount -o ro /dev/sda3 /mnt      # read-only
mount -o remount,rw /           # remount live — the rescue move
umount /mnt                     # release
umount -l /mnt                  # lazy — "device is busy" escape

/etc/fstab makes it permanent — six fields:

/dev/sda3  /      ext4  defaults  0  1
UUID=xxx   /home  ext4  defaults  0  2
/dev/sda2  none   swap  sw        0  0
tmpfs      /tmp   tmpfs defaults  0  0

mount -a re-reads fstab — test fstab with it before rebooting; a broken fstab drops to emergency mode. Options worth knowing: noatime (skip access-time writes), ro, noexec/nosuid/nodev (hardening), _netdev (wait for network), nofail (don't die if absent), x-systemd.automount (lazy-mount).

The fstab grammar — all six fields

# <device>        <mount>    <type>  <options>              <dump> <pass>
/dev/sda1         /boot/efi  vfat    umask=0077               0      1
/dev/sda2         none       swap    pri=1                    0      0
/dev/sda3         /          ext4    defaults                 0      1
proc              /proc      proc    nosuid,noexec,nodev      0      0
sysfs             /sys       sysfs   nosuid,noexec,nodev      0      0
devpts            /dev/pts   devpts  gid=5,mode=620           0      0
tmpfs             /run       tmpfs   defaults                 0      0
devtmpfs          /dev       devtmpfs mode=0755,nosuid        0      0
tmpfs             /tmp       tmpfs   defaults                 0      0
UUID=xxx          /data      ext4    defaults,_netdev         0      2

dump=0 (the old backup flag); pass=fsck order (1=root, 2=others, 0=skip). Options: ro rw noexec nosuid nodev noatime user nouser auto noauto uid= gid= umask= defaults _netdev. UUID= beats /dev/sdX — device names reorder; UUIDs don't.

The dd/ddrescue grammar — the surgical copy

dd if=/dev/sda of=disk.img bs=4M status=progress conv=fsync   # whole-disk image
dd if=img.iso of=/dev/sdb bs=4M status=progress oflag=direct  # burn a USB
dd if=/dev/zero of=file bs=1M count=1024                      # allocate a file
dd if=/dev/urandom of=file bs=1M count=100                    # random fill
dd if=disk.img of=/dev/sda bs=4M conv=fsync                   # write back
ddrescue -n /dev/sda rescued.img mapfile                      # the easy pass first
ddrescue -d -r3 /dev/sda rescued.img mapfile                  # retry the hard parts
ddrescue --idirect /dev/sda3 part.img map                     # a partition

ddrescue's mapfile makes recovery resumable — -n skips scraping on pass 1, -r3 retries later. Never dd a mounted filesystem.

The partition layouts — the schemes that matter

The classic simple:          The UEFI scheme:            The LFS scheme:
/dev/sda1  /boot   ext4      /dev/sda1  ESP      vfat    /dev/sda1  ESP     vfat
/dev/sda2  swap    swap      /dev/sda2  swap     swap    /dev/sda2  swap    swap
/dev/sda3  /       ext4      /dev/sda3  /        ext4    /dev/sda3  /       ext4

The separated:               The LVM scheme:             The encrypted:
sda1 ESP · sda2 /            sda1 ESP · sda2 /boot       sda1 ESP · sda2 /boot
sda3 /home · sda4 /var       sda3 LUKS→LVM               sda3 LUKS→dm-crypt→LVM→/+/home
                             →root · home · swap LVs     →root · home · swap

Why separate /home: reinstalls wipe / but keep /home — the standard trick. /var separated keeps logs/spool from filling the root. /boot separated for encryption or exotic filesystems.

The filesystem comparison — choosing

FSSnapshotsCoWSpeedStabilityThe job
ext4nonofastbulletproofThe default — boring correct
xfsnonofastest at scaleprovenHuge files/filesystems
btrfsyesyesgoodmaturingSnapshots, subvolumes, send/recv
zfsyesyesheavyprovenThe everything-fs — out-of-tree
f2fsnonoflash-tunedgoodSSD/flash-native
tmpfsn/an/aRAM—/run, /tmp
vfatnono——ESP + interop
exfatnono——USB + cross-platform
iso9660/udf/squashfsnono——CDs/live images

The deeper layers — LVM, RAID, dm-crypt

pvcreate /dev/sda3        # physical volume
vgcreate vg0 /dev/sda3    # volume group
lvcreate -L 10G -n data vg0  # logical volume
mkfs.ext4 /dev/vg0/data   # treat it like a partition
lvextend -L +5G /dev/vg0/data   # grow it — resize2fs after

LVM stacks virtual partitions over physical — resizable, snapshot-able, movable across disks. mdadm does software RAID (mirror/stripe). dm-crypt/LUKS encrypts a whole partition — cryptsetup luksFormat + luksOpen → a /dev/mapper/ decrypted view. The modern stack: disk → LUKS → LVM → filesystem → mount — each layer a /dev/ node.

The rescue work — when disks go bad

fsck -n /dev/sda3             # check, don't fix — NEVER on a mounted fs
fsck.ext4 -y /dev/sda3        # fix — unmounted only!
e2fsck -b 32768 /dev/sda3     # the backup superblock
badblocks /dev/sda            # surface scan
ddrescue /dev/sda img logfile # the dying-disk imager — rescue first
testdisk /dev/sda             # partition recovery
photorec img                  # file carving from an image
mount -o loop img /mnt        # the rescued image browsed

fsck on a mounted filesystem destroys it — always unmount first. For dying hardware, ddrescue images what still reads before the disk dies fully.

Where storage goes wrong

SymptomCause
fstab boots to emergencyTypo'd UUID/device — mount -a would've caught it
"device or resource busy" on umountA process holds it — lsof /mnt, umount -l
fsck won't runFS mounted — unmount first
Wrong partition got mkfs'dlsblk first — always
Disk full but files fitInodes — df -i
Mount point empty after rebootfstab missing the entry
SMART warningsThe disk is dying — image it now, not later

Exercises

  1. lsblk -f — map your real disks' layers.
  2. Partition a scratch disk, mkfs.ext4, mount, fstab it, mount -a, reboot-verify.
  3. blkid — UUIDs vs /dev names; write both fstab forms.
  4. mount -o remount,ro / on a scratch VM — feel the read-only world.
  5. smartctl -a your disk — read its self-assessment.

See also

Categories: Handbook · Linux · Linux Handbook · Storage
This page was last edited 1970-01-21 18:02:21 UTC by Devin. What links here