Disk Management¶
Reference: Debian Wiki — DiskPartitioningGuide · Debian Wiki — fstab · Linux man-pages: mount, fstab, fdisk
Viewing Disks & Partitions¶
# List all block devices (disks, partitions, loop devices)
lsblk
# Detailed view with filesystem type and mount points
lsblk -f
# List disks with partition table info
sudo fdisk -l
# Show disk UUIDs (use these in fstab, not /dev/sdX)
blkid
# Specific device
blkid /dev/sda1
# Disk usage — how full are your filesystems?
df -h # human-readable sizes
df -hT # includes filesystem type
# Directory disk usage
du -sh /path/ # total size of directory
du -sh /* # size of each top-level directory
du -h --max-depth=1 /var # one level deep
Partition Management¶
fdisk (MBR / GPT)¶
# Open a disk for editing (destructive — be careful)
sudo fdisk /dev/sda
# fdisk interactive commands:
# p — print partition table
# n — new partition
# d — delete partition
# t — change partition type
# w — write changes and exit
# q — quit without saving
parted (preferred for large disks / GPT)¶
# Open disk
sudo parted /dev/sda
# parted commands:
# print — show partition table
# mklabel gpt — create GPT partition table
# mkpart primary ext4 0% 100% — create partition using full disk
# quit
Warning
Partition changes are destructive. Always verify the correct device (lsblk) before running fdisk or parted. /dev/sda vs /dev/sdb matters.
Back up any data on the disk first.
Filesystems¶
Back up first
mkfs erases everything on the partition. Back up any data you want to keep and double-check the device with lsblk first.
# Create ext4 filesystem on a partition
sudo mkfs.ext4 /dev/sdb1
# Create with a label
sudo mkfs.ext4 -L mydrive /dev/sdb1
# XFS (what my NAS uses for the big data/parity disks; needs xfsprogs)
sudo apt install xfsprogs
sudo mkfs.xfs /dev/sdb1
# Check and repair a filesystem (must be unmounted)
sudo fsck /dev/sdb1
sudo fsck -y /dev/sdb1 # auto-fix errors
# Resize ext4 filesystem (after expanding partition)
sudo resize2fs /dev/sdb1
Mounting¶
# Mount a partition
sudo mount /dev/sdb1 /mnt/data
# Mount with specific filesystem type
sudo mount -t ext4 /dev/sdb1 /mnt/data
# Mount an ISO
sudo mount -o loop image.iso /mnt/iso
# Unmount
sudo umount /mnt/data
sudo umount /dev/sdb1 # either works
# View currently mounted filesystems
mount | column -t
findmnt # tree view
/etc/fstab — Persistent Mounts¶
fstab defines filesystems that mount automatically at boot.
# Format:
# <device> <mountpoint> <fstype> <options> <dump> <pass>
# View current fstab
cat /etc/fstab
Example fstab Entries¶
# Mount by UUID (always prefer UUID over /dev/sdX — device names can change)
UUID=abc123-def456 /mnt/data ext4 defaults 0 2
# NFS share
192.168.1.20:/volume1/media /mnt/media nfs defaults,_netdev 0 0
# tmpfs (RAM-based temp filesystem)
tmpfs /tmp tmpfs defaults,size=1G 0 0
# Bind mount (make a directory appear at another path)
/source/path /target/path none bind 0 0
fstab Options¶
| Option | Meaning |
|---|---|
defaults |
rw, suid, dev, exec, auto, nouser, async |
noatime |
Don't update access time (performance improvement) |
ro |
Read-only |
_netdev |
Wait for network before mounting (NFS, SMB) |
nofail |
Don't fail boot if device missing |
user |
Allow non-root users to mount |
# Test fstab without rebooting
sudo mount -a # mounts everything in fstab not yet mounted
# Verify fstab syntax
sudo findmnt --verify
Disk Health¶
# Install smartmontools
sudo apt install smartmontools
# Quick health check
sudo smartctl -H /dev/sda
# Full SMART info
sudo smartctl -a /dev/sda
# Run short self-test (~2 min)
sudo smartctl -t short /dev/sda
# Run long self-test (~hours, do overnight)
sudo smartctl -t long /dev/sda
# Check test results
sudo smartctl -l selftest /dev/sda
SMART Attributes to Watch¶
| Attribute | Warning Sign |
|---|---|
Reallocated_Sector_Ct |
Any value > 0 |
Current_Pending_Sector |
Any value > 0 |
Offline_Uncorrectable |
Any value > 0 |
Reallocated_Event_Count |
Increasing count |
SMART monitoring with smartd¶
Running smartctl by hand only helps if you remember to. smartd (same package) checks every disk on a schedule,
runs self-tests and calls a hook when something is wrong. Debian's default config mails root, which goes nowhere
on a server without a mail setup, so I replace it with one line that runs a notify script instead. The same file is
on every server:
# -a health, attributes, error log and self-test log
# -o on -S on automatic offline data collection + attribute autosave (ATA)
# -n standby,24,q don't spin up sleeping disks for a check, but check at least every 24 polls
# -s (...) short self-test daily at 01:00; long self-test on the 2nd of each month at 06:00
# (after the nightly SnapRAID run; a 12 TB long test takes most of a day)
# -W 4,50,60 log temperature changes >= 4 C and >= 50 C; alert at >= 60 C
# -m <nomailer> -M exec no mail; run the hook instead. -M daily repeats an unresolved problem once a day
DEVICESCAN -a -o on -S on -n standby,24,q -s (S/../.././01|L/../02/./06) -W 4,50,60 -m <nomailer> -M daily -M exec /usr/local/sbin/smartd-notify
#!/bin/sh
# smartd sets SMARTD_DEVICE, SMARTD_FAILTYPE, SMARTD_MESSAGE, SMARTD_FULLMESSAGE, ...
case "$SMARTD_FAILTYPE" in
EmailTest) prio=2 ;;
Health|FailedHealthCheck|CurrentPendingSector|OfflineUncorrectableSector|SelfTest) prio=5 ;;
*) prio=4 ;;
esac
printf '%s\n\n%s\n' "$SMARTD_MESSAGE" "$SMARTD_FULLMESSAGE" | head -c 3500 |
curl -fsS -H "Authorization: Bearer $(cat /etc/smartd-ntfy-token)" \
-H "Title: Disk ${SMARTD_FAILTYPE}: ${SMARTD_DEVICE}" -H "Priority: $prio" \
--data-binary @- https://ntfy.<YOUR_DOMAIN>/alerts
sudo chmod 755 /usr/local/sbin/smartd-notify
sudo systemctl restart smartmontools # the service is called smartmontools on Debian (alias: smartd)
# Test the whole path: add "-M test" to the DEVICESCAN line, restart, and you get one EmailTest message per disk
Refer to disks by /dev/disk/by-id/ (serial) in anything that names a specific disk (dashboards, notes), since
/dev/sdX letters can change between boots.
What to do when a disk fails its long test: Storage lessons learned.
Swap¶
# View swap usage
swapon --show
free -h
# Create a swapfile
sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
# Make permanent — add to fstab
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
# Adjust swappiness (how aggressively kernel uses swap)
# 0 = avoid swap, 100 = use swap aggressively, 10-20 typical for servers
cat /proc/sys/vm/swappiness
sudo sysctl vm.swappiness=10
# Persist swappiness across reboots
# (Debian 13+ no longer reads /etc/sysctl.conf — use a file in /etc/sysctl.d/)
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
LVM Basics (Logical Volume Manager)¶
LVM adds a flexible abstraction over physical disks — resize volumes without partitioning.
# Show physical volumes
sudo pvs
sudo pvdisplay
# Show volume groups
sudo vgs
sudo vgdisplay
# Show logical volumes
sudo lvs
sudo lvdisplay
# Extend a logical volume by 10GB
sudo lvextend -L +10G /dev/vgname/lvname
# Resize the filesystem after extending LV
sudo resize2fs /dev/vgname/lvname # ext4
sudo xfs_growfs /mountpoint # xfs