Device Management
Quick Reference
# List devices
lsblk
lsblk -f # With filesystem info
fdisk -l
blkid
# Partitioning
fdisk /dev/sdX # MBR
gdisk /dev/sdX # GPT
parted /dev/sdX # Both
# Filesystem
mkfs.ext4 /dev/sdX1
mkfs.xfs /dev/sdX1
mkfs.btrfs /dev/sdX1
# Mount
mount /dev/sdX1 /mnt
umount /mnt
findmnt
# LVM
pvcreate /dev/sdX1
vgcreate vg0 /dev/sdX1
lvcreate -L 10G -n lv0 vg0
Device Discovery
Listing Block Devices
# lsblk - Recommended for overview
lsblk
lsblk -f # Filesystem info
lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINT,UUID
lsblk -p # Full device paths
lsblk --json # JSON output
# Example output
# NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
# sda 8:0 0 500G 0 disk
# ├─sda1 8:1 0 512M 0 part /boot/efi
# ├─sda2 8:2 0 100G 0 part /
# └─sda3 8:3 0 399.5G 0 part /home
# nvme0n1 259:0 0 1.8T 0 disk
# └─nvme0n1p1 259:1 0 1.8T 0 part /data
Device Identifiers
# blkid - Show UUIDs and labels
blkid
blkid /dev/sda1
blkid -o list
# Device paths
ls -la /dev/disk/by-id/
ls -la /dev/disk/by-uuid/
ls -la /dev/disk/by-path/
ls -la /dev/disk/by-label/
# Use stable identifiers in fstab
# UUID=abc123-def456 /mount ext4 defaults 0 2
# LABEL=data /data xfs defaults 0 2
# Find device by UUID
findfs UUID=abc123-def456
Device Information
# fdisk - Partition info
fdisk -l
fdisk -l /dev/sda
# Detailed device info
hdparm -I /dev/sda # SATA/IDE drives
smartctl -a /dev/sda # SMART data
nvme list # NVMe devices
nvme smart-log /dev/nvme0n1 # NVMe SMART
# udevadm - Device attributes
udevadm info /dev/sda
udevadm info --query=all --name=/dev/sda
udevadm info --attribute-walk /dev/sda
Partitioning
GPT vs MBR
| Feature | MBR | GPT |
|---|---|---|
Max disk size |
2 TB |
9.4 ZB (unlimited practical) |
Max partitions |
4 primary (or 3 + extended) |
128 (default) |
Boot compatibility |
BIOS and UEFI (with CSM) |
UEFI (native) |
Backup |
None |
Backup at end of disk |
Tools |
fdisk, parted |
gdisk, parted |
fdisk (MBR)
# Start fdisk
fdisk /dev/sda
# Common commands:
# m - help
# p - print partition table
# n - new partition
# d - delete partition
# t - change partition type
# a - toggle bootable flag
# w - write changes and exit
# q - quit without saving
# Non-interactive (scripted)
echo -e "n\np\n1\n\n+10G\nw" | fdisk /dev/sda
# Using sfdisk (scriptable)
sfdisk /dev/sda << EOF
, 10G, L
, 20G, L
, , L
EOF
gdisk (GPT)
# Start gdisk
gdisk /dev/sda
# Common commands (similar to fdisk):
# ? - help
# p - print partition table
# n - new partition
# d - delete partition
# c - change partition name
# t - change type code
# w - write changes
# q - quit
# Convert MBR to GPT (backup first!)
gdisk /dev/sda
# Then use 'w' to write GPT
# Common partition type codes:
# 8300 - Linux filesystem
# 8200 - Linux swap
# ef00 - EFI System
# fd00 - Linux RAID
# 8e00 - Linux LVM
parted (MBR and GPT)
# Interactive mode
parted /dev/sda
# Commands:
# print - show partitions
# mklabel gpt - create GPT table
# mklabel msdos - create MBR table
# mkpart primary ext4 1MiB 100GiB
# rm 1 - delete partition 1
# resizepart 1 200GiB
# name 1 mydata
# quit
# Non-interactive
parted -s /dev/sda mklabel gpt
parted -s /dev/sda mkpart primary ext4 1MiB 100GiB
parted -s /dev/sda mkpart primary ext4 100GiB 200GiB
parted -s /dev/sda set 1 boot on
# Align partitions (important for SSDs)
parted -s /dev/sda align-check optimal 1
# Print in machine-readable format
parted -m /dev/sda print
Partition Alignment
# Check alignment
parted /dev/sda align-check optimal 1
# Modern drives need 1MiB alignment
# Start first partition at 1MiB (2048 sectors for 512-byte)
# fdisk automatically aligns on recent versions
# For parted, always use MiB/GiB units
# Check current alignment
parted /dev/sda unit s print # Sectors
# First partition should start at 2048s or higher (multiple of 2048)
Filesystems
Creating Filesystems
# ext4 (most common)
mkfs.ext4 /dev/sda1
mkfs.ext4 -L mylabel /dev/sda1 # With label
mkfs.ext4 -b 4096 /dev/sda1 # Block size
mkfs.ext4 -m 1 /dev/sda1 # 1% reserved (default 5%)
# XFS (good for large files)
mkfs.xfs /dev/sda1
mkfs.xfs -L mylabel /dev/sda1
mkfs.xfs -f /dev/sda1 # Force overwrite
# Btrfs (CoW, snapshots)
mkfs.btrfs /dev/sda1
mkfs.btrfs -L mylabel /dev/sda1
mkfs.btrfs -d raid1 -m raid1 /dev/sda1 /dev/sdb1 # RAID1
# FAT32 (USB drives, EFI)
mkfs.vfat -F 32 /dev/sda1
mkfs.vfat -F 32 -n LABEL /dev/sda1
# exFAT (large files, cross-platform)
mkfs.exfat /dev/sda1
# Swap
mkswap /dev/sda2
swapon /dev/sda2
Filesystem Labels and UUIDs
# Set label (ext4)
e2label /dev/sda1 mylabel
tune2fs -L mylabel /dev/sda1
# Set label (XFS)
xfs_admin -L mylabel /dev/sda1
# Set label (Btrfs)
btrfs filesystem label /dev/sda1 mylabel
# Change UUID (ext4)
tune2fs -U random /dev/sda1
tune2fs -U $(uuidgen) /dev/sda1
# Change UUID (XFS)
xfs_admin -U generate /dev/sda1
# View labels and UUIDs
blkid
lsblk -f
Filesystem Maintenance
# Check filesystem (UNMOUNT FIRST!)
fsck /dev/sda1
fsck -y /dev/sda1 # Auto-fix
e2fsck -f /dev/sda1 # Force ext check
xfs_repair /dev/sda1 # XFS repair
# Resize filesystem
# ext4 (can grow online)
resize2fs /dev/sda1
resize2fs /dev/sda1 50G # Specific size
# XFS (can only grow, must be mounted)
xfs_growfs /mnt
# Btrfs (can grow and shrink online)
btrfs filesystem resize max /mnt
btrfs filesystem resize -10G /mnt
# Check filesystem usage
df -h
df -i # Inode usage
# Check for errors
dmesg | grep -i "filesystem\|ext4\|xfs"
Mounting
Manual Mounting
# Basic mount
mount /dev/sda1 /mnt
# With options
mount -o rw,noatime /dev/sda1 /mnt
mount -o ro /dev/sda1 /mnt # Read-only
mount -o remount,rw /mnt # Remount with different options
# By UUID
mount UUID=abc123-def456 /mnt
# By label
mount LABEL=data /mnt
# Specific filesystem type
mount -t ext4 /dev/sda1 /mnt
mount -t ntfs-3g /dev/sda1 /mnt
# Unmount
umount /mnt
umount /dev/sda1
umount -l /mnt # Lazy unmount (when busy)
umount -f /mnt # Force unmount (NFS)
# Check what's using mount point
fuser -m /mnt
lsof +f -- /mnt
Mount Options
# Common mount options
defaults # rw,suid,dev,exec,auto,nouser,async
rw # Read-write
ro # Read-only
noatime # Don't update access times (performance)
nodiratime # Don't update directory access times
relatime # Update atime relative to mtime (default)
discard # Enable TRIM for SSDs
noexec # No execution of binaries
nosuid # Ignore setuid bits
nodev # No device files
sync # Synchronous I/O
async # Asynchronous I/O (default)
# Security options
noexec,nosuid,nodev # Common for /tmp, /var/tmp
# Example
mount -o rw,noatime,discard /dev/sda1 /mnt
fstab Configuration
# /etc/fstab format:
# <device> <mountpoint> <type> <options> <dump> <pass>
# By UUID (recommended)
UUID=abc123-def456 / ext4 defaults,noatime 0 1
UUID=def789-ghi012 /home ext4 defaults,noatime 0 2
UUID=jkl345-mno678 /boot/efi vfat umask=0077 0 1
# By label
LABEL=data /data xfs defaults,noatime 0 2
# Swap
UUID=pqr901-stu234 none swap sw 0 0
# Temporary filesystems
tmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0
# NFS mount
server:/export /nfs nfs defaults,_netdev 0 0
# Mount options explained:
# dump (5th field): 0=no backup, 1=backup
# pass (6th field): 0=no fsck, 1=root, 2=other filesystems
LVM (Logical Volume Manager)
LVM Concepts
Physical Volume (PV) → Volume Group (VG) → Logical Volume (LV)
/dev/sda1 vg0 /dev/vg0/root
/dev/sdb1 /dev/vg0/home
/dev/vg0/data
Physical Volumes
# Create physical volume
pvcreate /dev/sda1
pvcreate /dev/sdb1
# List physical volumes
pvs
pvdisplay
pvdisplay /dev/sda1
# Remove physical volume
pvremove /dev/sda1
# Move data from one PV to another
pvmove /dev/sda1 /dev/sdb1
pvmove /dev/sda1 # Move to any available PV in VG
Volume Groups
# Create volume group
vgcreate vg0 /dev/sda1
vgcreate vg0 /dev/sda1 /dev/sdb1 # Multiple PVs
# List volume groups
vgs
vgdisplay
vgdisplay vg0
# Extend volume group
vgextend vg0 /dev/sdc1
# Reduce volume group (after moving data)
vgreduce vg0 /dev/sda1
# Rename volume group
vgrename vg0 newvg
# Remove volume group
vgremove vg0
Logical Volumes
# Create logical volume
lvcreate -L 10G -n root vg0 # Fixed size
lvcreate -l 100%FREE -n data vg0 # All remaining space
lvcreate -l 50%VG -n home vg0 # 50% of VG
lvcreate -l 100%PVS -n lv0 vg0 /dev/sda1 # All of specific PV
# List logical volumes
lvs
lvdisplay
lvdisplay /dev/vg0/root
# Extend logical volume
lvextend -L +5G /dev/vg0/root # Add 5G
lvextend -l +100%FREE /dev/vg0/root # All free space
lvextend -L 20G /dev/vg0/root # Set to 20G
# Resize filesystem after extend
resize2fs /dev/vg0/root # ext4
xfs_growfs /mount/point # XFS (must be mounted)
# Extend LV and resize filesystem in one command
lvextend -r -L +5G /dev/vg0/root
# Reduce logical volume (BACKUP FIRST!)
umount /dev/vg0/root
e2fsck -f /dev/vg0/root
resize2fs /dev/vg0/root 10G
lvreduce -L 10G /dev/vg0/root
mount /dev/vg0/root /mnt
# Remove logical volume
lvremove /dev/vg0/root
# Rename logical volume
lvrename vg0 oldname newname
LVM Snapshots
# Create snapshot
lvcreate -L 5G -s -n root-snap /dev/vg0/root
# List snapshots
lvs -a
lvs -o +snap_percent
# Mount snapshot (read-only by default)
mount -o ro /dev/vg0/root-snap /mnt/snap
# Restore from snapshot
lvconvert --merge /dev/vg0/root-snap
# Reboot for root volume or remount for others
# Remove snapshot
lvremove /dev/vg0/root-snap
Thin Provisioning
# Create thin pool
lvcreate -L 100G --thinpool thin-pool vg0
# Create thin volumes
lvcreate -V 50G --thin -n thin-vol1 vg0/thin-pool
lvcreate -V 50G --thin -n thin-vol2 vg0/thin-pool
# Thin volumes can overcommit
# Total thin volumes > pool size (careful!)
# Monitor usage
lvs -o +data_percent,snap_percent
RAID
Software RAID (mdadm)
# Create RAID arrays
# RAID 0 (stripe)
mdadm --create /dev/md0 --level=0 --raid-devices=2 /dev/sda1 /dev/sdb1
# RAID 1 (mirror)
mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sda1 /dev/sdb1
# RAID 5 (stripe with parity)
mdadm --create /dev/md0 --level=5 --raid-devices=3 /dev/sda1 /dev/sdb1 /dev/sdc1
# RAID 6 (stripe with double parity)
mdadm --create /dev/md0 --level=6 --raid-devices=4 /dev/sd[a-d]1
# RAID 10 (stripe of mirrors)
mdadm --create /dev/md0 --level=10 --raid-devices=4 /dev/sd[a-d]1
RAID Management
# Check status
cat /proc/mdstat
mdadm --detail /dev/md0
mdadm --examine /dev/sda1
# Save configuration
mdadm --detail --scan >> /etc/mdadm.conf
# Add spare disk
mdadm --add /dev/md0 /dev/sdc1
# Remove disk
mdadm --fail /dev/md0 /dev/sda1
mdadm --remove /dev/md0 /dev/sda1
# Replace failed disk
mdadm --fail /dev/md0 /dev/sda1
mdadm --remove /dev/md0 /dev/sda1
# Replace physical disk
mdadm --add /dev/md0 /dev/sdd1
# Stop and remove array
mdadm --stop /dev/md0
mdadm --zero-superblock /dev/sd[ab]1
udev Rules
Understanding udev
# udev handles device events
# Rules in /etc/udev/rules.d/ and /lib/udev/rules.d/
# Get device attributes for rules
udevadm info --query=all --name=/dev/sda
udevadm info --attribute-walk /dev/sda
# Monitor device events
udevadm monitor
udevadm monitor --property
Writing udev Rules
# /etc/udev/rules.d/99-custom.rules
# Match by attribute, assign name/symlink
# Give USB drive a persistent name by serial
SUBSYSTEM=="block", ENV{ID_SERIAL}=="WD-ABC123", SYMLINK+="myusb"
# Run script when device added
ACTION=="add", SUBSYSTEM=="block", ENV{ID_SERIAL}=="WD-ABC123", RUN+="/usr/local/bin/backup.sh"
# Set I/O scheduler for SSDs
ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="0", ATTR{queue/scheduler}="none"
# Set permissions
SUBSYSTEM=="tty", ATTRS{idVendor}=="1234", ATTRS{idProduct}=="5678", MODE="0666"
Encryption (LUKS)
Creating Encrypted Volume
# Create LUKS container
cryptsetup luksFormat /dev/sda1
# With specific options
cryptsetup luksFormat --type luks2 --cipher aes-xts-plain64 --key-size 512 /dev/sda1
# Open encrypted volume
cryptsetup luksOpen /dev/sda1 cryptdata
# Creates /dev/mapper/cryptdata
# Create filesystem
mkfs.ext4 /dev/mapper/cryptdata
# Mount
mount /dev/mapper/cryptdata /mnt
# Close
umount /mnt
cryptsetup luksClose cryptdata
Troubleshooting
Device Not Appearing
# Rescan SCSI bus
echo "- - -" > /sys/class/scsi_host/host0/scan
# Rescan all hosts
for host in /sys/class/scsi_host/host*; do
echo "- - -" > $host/scan
done
# Force kernel to reread partitions
partprobe /dev/sda
blockdev --rereadpt /dev/sda
# Check dmesg for errors
dmesg | tail -50
dmesg | grep -i "sd\|nvme\|error"
Disk Health
# Check SMART status
smartctl -H /dev/sda
smartctl -a /dev/sda
# Run SMART tests
smartctl -t short /dev/sda
smartctl -t long /dev/sda
# Check test results
smartctl -l selftest /dev/sda
# NVMe health
nvme smart-log /dev/nvme0n1
Filesystem Issues
# Force fsck at boot
touch /forcefsck
# Or via tune2fs
tune2fs -C 100 /dev/sda1 # Force fsck in 100 mounts
# Repair XFS
xfs_repair /dev/sda1
xfs_repair -L /dev/sda1 # Zero log (data loss risk)
# Recover ext4 superblock
mke2fs -n /dev/sda1 # Find backup superblock locations
e2fsck -b 32768 /dev/sda1 # Use backup superblock
Quick Reference
# Device discovery
lsblk -f # List with filesystem
blkid # UUIDs and labels
fdisk -l # Partition tables
# Partitioning
gdisk /dev/sdX # GPT partitioning
parted /dev/sdX # Interactive partitioning
# Filesystem
mkfs.ext4 /dev/sdX1 # Create ext4
mkfs.xfs /dev/sdX1 # Create XFS
fsck /dev/sdX1 # Check filesystem
# Mount
mount /dev/sdX1 /mnt # Mount
umount /mnt # Unmount
findmnt # Show mounts
# LVM
pvs / vgs / lvs # List PV/VG/LV
pvcreate /dev/sdX1 # Create PV
vgcreate vg0 /dev/sdX1 # Create VG
lvcreate -L 10G -n lv0 vg0 # Create LV
lvextend -r -L +5G /dev/vg0/lv0 # Extend with resize
# RAID
mdadm --create /dev/md0 ... # Create array
cat /proc/mdstat # Check status
# Encryption
cryptsetup luksFormat /dev/sdX1 # Create LUKS
cryptsetup luksOpen /dev/sdX1 name # Open
cryptsetup luksClose name # Close