cheat-sheets

Storage and Filesystems

Partitioning, creating file systems, checking disk health and measuring disk speed on Linux. Mounting is covered in Mount; volume management in LVM and software RAID in RAID with mdadm.

Documentation: smartmontools · fio · Btrfs docs

Verification: commands follow the man pages and common practice. No command was executed. mkfs, parted, wipefs and fio write tests destroy data on the target. Check the device name with lsblk every time.

See also: ZFS · Disk Usage · XFS · LVM · RAID with mdadm · Mount · NFS · Linux Performance Troubleshooting · Linux


Identify Disks #

COMMANDDESCRIPTION
lsblk -fDisks, partitions, file systems, UUIDs, mount points
lsblk -o NAME,SIZE,TYPE,MODEL,SERIAL,ROTAModel and serial (ROTA=1 is a spinning disk)
sudo blkidUUIDs and file system types
sudo fdisk -lPartition tables of all disks
ls -l /dev/disk/by-id/Stable names. Use these in scripts and ZFS/RAID setups
sudo smartctl --scanDisks smartctl can see
df -hT / df -iSpace and inodes per file system

Partition #

Use GPT on anything new. parted and sgdisk are scriptable; fdisk and cfdisk are interactive.

bash
sudo parted /dev/sdb --script mklabel gpt mkpart data ext4 1MiB 100%
sudo parted /dev/sdb print
COMMANDDESCRIPTION
sudo fdisk /dev/sdbInteractive: g new GPT, n new partition, t type, w write
sudo cfdisk /dev/sdbMenu-driven
sudo sgdisk -n 1:0:0 -t 1:8300 /dev/sdbOne Linux partition using the whole disk
sudo sgdisk -t 1:8e00 /dev/sdbSet type to Linux LVM
sudo sgdisk -t 1:fd00 /dev/sdbSet type to Linux RAID
sudo sgdisk -Z /dev/sdbDestroy GPT and MBR structures
sudo partprobe /dev/sdbMake the kernel re-read the table
sudo wipefs -a /dev/sdbRemove all file system and RAID signatures
sudo growpart /dev/sda 1Grow a partition into new space (cloud-guest-utils)

Create File Systems #

COMMANDDESCRIPTION
sudo mkfs.ext4 -L data /dev/sdb1ext4 with a label
sudo mkfs.xfs -L data /dev/sdb1XFS (add -f to overwrite)
sudo mkfs.btrfs -L data /dev/sdb1Btrfs
sudo mkfs.vfat -F 32 /dev/sdb1FAT32 (USB sticks, EFI partition)
sudo mkfs.exfat /dev/sdb1exFAT (needs exfatprogs)
sudo mkswap /dev/sdb2 && sudo swapon /dev/sdb2Swap partition
sudo tune2fs -m 1 /dev/sdb1Lower ext4 root-reserved blocks from 5% to 1% on a data disk
sudo e2label /dev/sdb1 data / sudo xfs_admin -L data /dev/sdb1Change a label

Mount permanently with the UUID from blkid:

UUID=<UUID>  /srv/data  ext4  defaults,nofail  0  2

Test it with sudo mount -a before rebooting. Details in Mount.


Choosing a File System #

FSGOOD FORNOTES
ext4General purpose, root disksMature, can shrink and grow
XFSLarge files, big arrays, RHEL defaultGrow only, cannot shrink
BtrfsSnapshots, checksums, compressionBuilt-in RAID1 and RAID10 are fine; RAID5/6 is not recommended
ZFSPools, snapshots, send/receiveOut of tree on Linux. See Proxmox Storage
FAT32 / exFATRemovable media, EFINo permissions or journaling

Check and Repair #

Unmount first (or boot rescue media for the root file system).

COMMANDDESCRIPTION
sudo fsck -f /dev/sdb1Check and repair ext4 (add -n to check only)
sudo e2fsck -fy /dev/sdb1ext4, answer yes to all repairs
sudo xfs_repair /dev/sdb1XFS repair (-n to dry run)
sudo btrfs check /dev/sdb1Btrfs read-only check. --repair is a last resort
sudo btrfs scrub start -B /mnt/dataVerify checksums on a mounted Btrfs
sudo dumpe2fs -h /dev/sdb1ext4 superblock info
sudo xfs_info /mnt/dataXFS geometry

Btrfs Basics #

COMMANDDESCRIPTION
sudo btrfs subvolume create /mnt/data/@docsCreate a subvolume
sudo btrfs subvolume list /mnt/dataList subvolumes
sudo btrfs subvolume snapshot -r /mnt/data/@docs /mnt/data/@docs-snapRead-only snapshot
sudo btrfs subvolume delete /mnt/data/@docs-snapDelete a snapshot
sudo btrfs filesystem usage /mnt/dataReal space use (plain df misleads on Btrfs)
sudo btrfs device add /dev/sdc /mnt/dataAdd a disk to the pool
sudo btrfs balance start -dusage=50 /mnt/dataRebalance data chunks that are under 50% full

Mount option for transparent compression: compress=zstd.


SMART Health (smartmontools) #

bash
sudo apt install smartmontools
COMMANDDESCRIPTION
sudo smartctl -H /dev/sdbOverall verdict (PASSED is not a guarantee)
sudo smartctl -a /dev/sdbFull report
sudo smartctl -x /dev/sdbEverything, including error logs
sudo smartctl -t short /dev/sdbShort self-test (about 2 minutes)
sudo smartctl -t long /dev/sdbExtended self-test (hours, runs in the background)
sudo smartctl -l selftest /dev/sdbSelf-test results
sudo smartctl -a -d sat /dev/sdbDisk behind a USB or SATA bridge
sudo smartctl -a /dev/nvme0NVMe disk
sudo nvme smart-log /dev/nvme0NVMe health with nvme-cli

Attributes worth watching on SATA disks:

ATTRIBUTEMEANING
Reallocated_Sector_Ct (5)Bad sectors remapped. Rising means the disk is failing
Current_Pending_Sector (197)Sectors waiting to be remapped. Not zero is a warning
Offline_Uncorrectable (198)Unreadable sectors
UDMA_CRC_Error_Count (199)Cable or connector problem
Temperature_Celsius (194)Keep under about 50 C
Percentage Used, Media and Data Integrity ErrorsNVMe wear and errors

Monitor with the smartd daemon. In /etc/smartd.conf:

DEVICESCAN -a -o on -S on -n standby,q -s (S/../.././02|L/../../6/03) -m admin@example.com

Then sudo systemctl enable --now smartmontools (Debian/Ubuntu) or smartd (RHEL).


Performance: iostat and iotop #

Part of sysstat (sudo apt install sysstat).

COMMANDDESCRIPTION
iostat -xz 1Per-disk utilisation, latency and queue each second
iostat -xz 1 -p sdbOne disk and its partitions
sudo iotop -oPaProcesses doing I/O, accumulated
pidstat -d 1Per-process disk I/O
vmstat 1wa column is CPU time waiting on I/O
sudo hdparm -tT /dev/sdbQuick sequential read test of a hard disk
dd if=/dev/zero of=/srv/data/test bs=1M count=1024 oflag=direct status=progressCrude write test. Delete the file afterwards

Read iostat -x: r_await / w_await are average latency in ms, aqu-sz is the queue depth, %util near 100 on a spinning disk means saturated (not a reliable signal on NVMe).


Benchmark with fio #

Test on a file on the file system you care about, not on a raw device that holds data.

COMMANDDESCRIPTION
fio --name=seqwrite --filename=/srv/data/fio.tmp --rw=write --bs=1M --size=2G --direct=1 --ioengine=libaio --iodepth=16Sequential write throughput
fio --name=seqread --filename=/srv/data/fio.tmp --rw=read --bs=1M --size=2G --direct=1 --ioengine=libaio --iodepth=16Sequential read throughput
fio --name=randread --filename=/srv/data/fio.tmp --rw=randread --bs=4k --size=2G --direct=1 --ioengine=libaio --iodepth=32 --runtime=60 --time_basedRandom 4k read IOPS
fio --name=randrw --filename=/srv/data/fio.tmp --rw=randrw --rwmixread=70 --bs=4k --size=2G --direct=1 --ioengine=libaio --iodepth=32 --runtime=60 --time_basedMixed 70/30 workload
fio --name=fsync --filename=/srv/data/fio.tmp --rw=write --bs=4k --size=256M --fsync=1Sync write latency (what databases and etcd feel)
rm /srv/data/fio.tmpClean up

Space Problems #

COMMANDDESCRIPTION
sudo du -xh --max-depth=1 / | sort -h | tailBiggest directories on the root file system
sudo ncdu -x /Interactive usage browser
df -iOut of inodes with free space left? Many tiny files
sudo lsof +L1Deleted files still held open by a process
sudo journalctl --vacuum-size=500MShrink the journal (systemd)
sudo fstrim -avTrim SSDs (a timer fstrim.timer normally does this weekly)
Search all sheets

Tip: / or Ctrl/Cmd+K to open, Esc to close.