| | {{infobox |
| | | title = Disks & Storage |
| | | chapter = Linux Handbook, chapter 21 — partitioning, filesystems, mount, fstab, LVM, RAID |
| | | core | block devices → partitions → filesystems → mountpoints |
| | | tools | lsblk · fdisk/gdisk/parted · mkfs · mount · fstab · smartctl |
| | | skills | carve a disk · mount it right · rescue a failing one |
| | | next | Security — the defensive layer |
| | }} |
| | |
| | **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 |
| | |
| | ```sh |
| | 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 |
| | |
| | ```sh |
| | 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 |
| | |
| | ```sh |
| | 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 |
| | |
| | ```sh |
| | 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 partition layouts — the schemes that matter |
| + | |
| + | ```text |
| + | 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 |
| + | |
| + | | FS | Snapshots | CoW | Speed | Stability | The job | |
| + | | --- | --- | --- | --- | --- | --- | |
| + | | ext4 | no | no | fast | bulletproof | The default — boring correct | |
| + | | xfs | no | no | fastest at scale | proven | Huge files/filesystems | |
| + | | btrfs | yes | yes | good | maturing | Snapshots, subvolumes, send/recv | |
| + | | zfs | yes | yes | heavy | proven | The everything-fs — out-of-tree | |
| + | | f2fs | no | no | flash-tuned | good | SSD/flash-native | |
| + | | tmpfs | n/a | n/a | RAM | — | /run, /tmp | |
| + | | vfat | no | no | — | — | ESP + interop | |
| + | | exfat | no | no | — | — | USB + cross-platform | |
| + | | iso9660/udf/squashfs | no | no | — | — | CDs/live images | |
| + | |
| | ## The deeper layers — LVM, RAID, dm-crypt |
| | |
| | ```sh |
| | 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 |
| | |
| | ```sh |
| | 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 |
| | |
| | | Symptom | Cause | |
| | | --- | --- | |
| | | fstab boots to emergency | Typo'd UUID/device — `mount -a` would've caught it | |
| | | "device or resource busy" on umount | A process holds it — `lsof /mnt`, `umount -l` | |
| | | fsck won't run | FS mounted — unmount first | |
| | | Wrong partition got mkfs'd | `lsblk` first — always | |
| | | Disk full but files fit | Inodes — `df -i` | |
| | | Mount point empty after reboot | fstab missing the entry | |
| | | SMART warnings | The 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 |
| | |
| | - [[Filesystem]] · [[Linux]] · [[Getting Linux]] · [[LFS Preparation]] |
| | |
| | [[Category:Linux]] |
| | [[Category:Handbook]] |
| | [[Category:Storage]] |
| | |