guest@linuxthings: ~/guides/disks.md

guest@linuxthings:~/guides$ man disks

Disks & Filesystems

Disk Usage

df -h                      # Disk free space (human readable)
df -hT                     # ...including each filesystem's type
df -h /home                # Only the filesystem holding /home
df -i                      # Inode usage: a disk can be "full" with space left
df -h -x tmpfs -x devtmpfs # Hide virtual filesystems
du -sh dir/                # Disk usage of directory summary
du -h --max-depth=1        # Disk usage of immediate subdirectories
du -ah dir | sort -rh | head  # Largest files and folders first
ncdu /                     # Interactive disk usage browser (if installed)

Devices and Partitions

Disks appear under /dev: sda, sdb for SATA, SCSI and USB drives (partitions sda1, sda2) and nvme0n1 for NVMe SSDs (partitions nvme0n1p1, nvme0n1p2).

lsblk                      # Disks and partitions as a tree
lsblk -f                   # ...with filesystem type, label, UUID and mount point
sudo blkid                 # UUIDs and types of all filesystems
sudo fdisk -l              # Partition tables of all disks
sudo parted /dev/sda print # Same, using parted
sudo sfdisk -d /dev/sda > sda-table.txt  # Back up a partition table

Mounting

mount                      # List mounted filesystems
findmnt                    # Mounted filesystems as a tidy tree
sudo mkdir -p /mnt/usb     # Mount points are ordinary empty directories
sudo mount /dev/sdb1 /mnt  # Mount partition to directory
sudo mount -t ntfs3 /dev/sdb1 /mnt  # Specify the filesystem type
sudo mount -o ro /dev/sdb1 /mnt     # Mount read-only
sudo umount /mnt           # Unmount filesystem
sudo mount -a              # Mount everything listed in /etc/fstab

Always unmount removable media before unplugging it. If umount reports that the target is busy, a shell or program is still using it: cd elsewhere, or find the culprit with lsof +D /mnt or fuser -vm /mnt.

Mounting at Boot (/etc/fstab)

Each line of /etc/fstab describes one filesystem to mount at boot:

# device                                    mount point  type  options   dump  pass
UUID=3f1c2a9e-1b2c-4d5e-8f90-0a1b2c3d4e5f  /data        ext4  defaults  0     2

Refer to disks by UUID= (from blkid) rather than /dev/sdX, because device names can change between boots. After editing, run sudo mount -a to test the file before rebooting: a broken fstab can stop the system from booting.

Creating and Checking Filesystems

mkfs erases everything on the target. Check the device name with lsblk first.
sudo mkfs.ext4 /dev/sdb1             # Format as ext4
sudo mkfs.ext4 -L backups /dev/sdb1  # ...with a label
sudo mkfs.vfat -F 32 /dev/sdb1       # FAT32, readable almost everywhere
sudo e2label /dev/sdb1 backups       # Change an ext filesystem's label
sudo umount /dev/sdb1 && sudo fsck -f /dev/sdb1  # Check and repair (unmounted!)
sudo resize2fs /dev/sdb1             # Grow ext4 to fill its partition

Never run fsck on a mounted filesystem. To check the root filesystem, boot from a live USB first.

Logical Volumes (LVM)

LVM pools physical disks into a volume group and carves resizable logical volumes out of it.

sudo pvs; sudo vgs; sudo lvs                  # Summaries of PVs, VGs and LVs
sudo pvcreate /dev/sdb1                       # Prepare a partition for LVM
sudo vgcreate data /dev/sdb1                  # Create a volume group
sudo lvcreate -L 20G -n projects data         # Create a 20 GB logical volume
sudo mkfs.ext4 /dev/data/projects             # Format it like any partition
sudo vgextend data /dev/sdc1                  # Add another disk to the group
sudo lvresize --resizefs -L +10G /dev/data/projects  # Grow the LV and its filesystem

LVM on its own gives no redundancy: if one disk in a volume group fails, the whole group is at risk. Combine it with RAID (mdadm) or keep good backups.

guest@linuxthings:~/guides$