Storage & Filesystems
Disk Information and Discovery
Basic Disk Information
# List all block devices
lsblk # Tree view of all block devices
lsblk -f # Include filesystem information
lsblk -p # Show full device paths
# Disk usage and space
df -h # Human-readable filesystem usage
df -i # Inode usage
du -sh /path/* # Directory size summary
du -h --max-depth=1 /path # Directory sizes one level deep
# Hardware information
lshw -class disk # Hardware info for disk devices
hdparm -I /dev/sda # IDE/SATA disk information
smartctl -i /dev/sda # SMART disk information
Partition Information
# List partitions
fdisk -l # List all partitions
fdisk -l /dev/sda # List partitions on specific disk
parted -l # List partitions (parted)
gdisk -l /dev/sda # List GPT partitions
# Partition details
blkid # Show UUID and filesystem type
blkid /dev/sda1 # Show info for specific partition
file -s /dev/sda1 # Identify filesystem type
Disk Partitioning
MBR Partitioning (fdisk)
# Interactive partitioning
fdisk /dev/sda # Start interactive session
# Common fdisk commands (inside fdisk):
# p - print partition table
# n - new partition
# d - delete partition
# t - change partition type
# w - write changes and exit
# q - quit without saving
# Example: Create new partition
fdisk /dev/sda << EOF
n
p
1
t
83
w
EOF
GPT Partitioning (gdisk)
# Interactive GPT partitioning
gdisk /dev/sda # Start interactive session
# Common gdisk commands:
# p - print partition table
# n - new partition
# d - delete partition
# t - change partition type
# w - write changes and exit
# q - quit without saving
# Convert MBR to GPT
gdisk /dev/sda # Will offer to convert MBR to GPT
Advanced Partitioning (parted)
# Interactive parted
parted /dev/sda # Start interactive session
# Non-interactive examples
parted /dev/sda mklabel gpt # Create GPT label
parted /dev/sda mkpart primary ext4 1MiB 100% # Create partition
parted /dev/sda rm 1 # Remove partition 1
parted /dev/sda print # Print partition table
# Resize partition
parted /dev/sda resizepart 1 100% # Resize partition 1
Automated Partitioning (sfdisk)
# Backup partition table
sfdisk -d /dev/sda > sda-backup.txt
# Restore partition table
sfdisk /dev/sda < sda-backup.txt
# Create partitions from script
sfdisk /dev/sda << EOF
,500M,83
,2G,82
,,83
EOF
Filesystem Management
Filesystem Types Overview
# Common filesystem types:
# ext4 - Default Linux filesystem
# xfs - High-performance filesystem
# btrfs - Copy-on-write filesystem
# zfs - Advanced filesystem with built-in features
# ntfs - Windows filesystem
# vfat - FAT32 filesystem
# swap - Swap space
Creating Filesystems
ext4 Filesystem
# Basic ext4 creation
mkfs.ext4 /dev/sda1 # Create ext4 filesystem
mkfs.ext4 -L "MyLabel" /dev/sda1 # Create with label
mkfs.ext4 -m 1 /dev/sda1 # Reserve 1% for root (default 5%)
mkfs.ext4 -F /dev/sda1 # Force creation
# Advanced ext4 options
mkfs.ext4 -b 4096 /dev/sda1 # Set block size to 4KB
mkfs.ext4 -i 4096 /dev/sda1 # Bytes per inode ratio
mkfs.ext4 -j /dev/sda1 # Enable journaling
XFS Filesystem
# Basic XFS creation
mkfs.xfs /dev/sda1 # Create XFS filesystem
mkfs.xfs -L "MyLabel" /dev/sda1 # Create with label
mkfs.xfs -f /dev/sda1 # Force creation
# Advanced XFS options
mkfs.xfs -b size=4096 /dev/sda1 # Set block size
mkfs.xfs -d agcount=4 /dev/sda1 # Set allocation groups
Btrfs Filesystem
# Basic Btrfs creation
mkfs.btrfs /dev/sda1 # Create Btrfs filesystem
mkfs.btrfs -L "MyLabel" /dev/sda1 # Create with label
mkfs.btrfs -f /dev/sda1 # Force creation
# Multi-device Btrfs
mkfs.btrfs -d raid1 -m raid1 /dev/sda1 /dev/sdb1 # RAID1 setup
Other Filesystems
# Create swap
mkswap /dev/sda2 # Create swap space
swapon /dev/sda2 # Enable swap
# Create FAT32
mkfs.vfat /dev/sda1 # Create FAT32 filesystem
mkfs.vfat -F 32 /dev/sda1 # Explicitly create FAT32
Filesystem Maintenance
ext4 Maintenance
# Check filesystem
fsck.ext4 /dev/sda1 # Check ext4 filesystem
fsck.ext4 -f /dev/sda1 # Force check
fsck.ext4 -y /dev/sda1 # Answer yes to all questions
# Tune filesystem
tune2fs -l /dev/sda1 # Show filesystem parameters
tune2fs -L "NewLabel" /dev/sda1 # Change label
tune2fs -m 1 /dev/sda1 # Change reserved blocks to 1%
tune2fs -c 30 /dev/sda1 # Check every 30 mounts
XFS Maintenance
# Check XFS filesystem
xfs_check /dev/sda1 # Check XFS filesystem
xfs_repair /dev/sda1 # Repair XFS filesystem
xfs_repair -n /dev/sda1 # Dry run repair
# XFS administration
xfs_admin -l /dev/sda1 # Show XFS label
xfs_admin -L "NewLabel" /dev/sda1 # Change label
xfs_growfs /mnt/xfs # Grow XFS filesystem
Btrfs Maintenance
# Check Btrfs filesystem
btrfs check /dev/sda1 # Check Btrfs filesystem
btrfs check --repair /dev/sda1 # Repair Btrfs filesystem
# Btrfs administration
btrfs filesystem show # Show Btrfs filesystems
btrfs filesystem resize max /mnt/btrfs # Resize to maximum
btrfs scrub start /mnt/btrfs # Start scrub operation
Mount Operations
Basic Mounting
# Mount filesystem
mount /dev/sda1 /mnt # Mount to /mnt
mount -t ext4 /dev/sda1 /mnt # Specify filesystem type
mount -o ro /dev/sda1 /mnt # Mount read-only
mount -o rw /dev/sda1 /mnt # Mount read-write
# Unmount filesystem
umount /mnt # Unmount by mount point
umount /dev/sda1 # Unmount by device
umount -l /mnt # Lazy unmount
umount -f /mnt # Force unmount
Mount Options
# Common mount options
mount -o defaults /dev/sda1 /mnt # Default options
mount -o noatime /dev/sda1 /mnt # No access time updates
mount -o noexec /dev/sda1 /mnt # No execution allowed
mount -o nosuid /dev/sda1 /mnt # No SUID bits
mount -o nodev /dev/sda1 /mnt # No device files
mount -o sync /dev/sda1 /mnt # Synchronous writes
mount -o async /dev/sda1 /mnt # Asynchronous writes
mount -o user /dev/sda1 /mnt # Allow user mounting
mount -o uid=1000,gid=1000 /dev/sda1 /mnt # Set ownership
fstab Configuration
# Edit fstab
vi /etc/fstab
# fstab format: device mountpoint filesystem options dump pass
# Examples:
UUID=12345678-1234-1234-1234-123456789012 /home ext4 defaults 0 2
/dev/sda1 /mnt/data ext4 defaults,noatime 0 2
/dev/sda2 none swap sw 0 0
tmpfs /tmp tmpfs defaults,size=2G 0 0
# Test fstab entries
mount -a # Mount all fstab entries
findmnt --verify # Verify fstab syntax
Advanced Mount Operations
# Bind mounts
mount --bind /path/source /path/target # Bind mount
mount --rbind /path/source /path/target # Recursive bind mount
# Loop mounts
mount -o loop disk.img /mnt # Mount disk image
mount -o loop,offset=1048576 disk.img /mnt # Mount with offset
# Network mounts (covered in Network Storage section)
mount -t nfs server:/path /mnt # NFS mount
mount -t cifs //server/share /mnt # CIFS/SMB mount
Logical Volume Management (LVM)
Physical Volume Management
# Create physical volumes
pvcreate /dev/sda1 # Create PV on partition
pvcreate /dev/sdb /dev/sdc # Create PVs on multiple disks
# Display physical volumes
pvdisplay # Detailed PV information
pvs # Summary PV information
pvscan # Scan for PVs
# Manage physical volumes
pvresize /dev/sda1 # Resize PV after partition resize
pvmove /dev/sda1 # Move data from PV
pvremove /dev/sda1 # Remove PV
Volume Group Management
# Create volume groups
vgcreate vg01 /dev/sda1 # Create VG with one PV
vgcreate vg01 /dev/sda1 /dev/sdb1 # Create VG with multiple PVs
# Display volume groups
vgdisplay # Detailed VG information
vgs # Summary VG information
vgscan # Scan for VGs
# Modify volume groups
vgextend vg01 /dev/sdc1 # Add PV to VG
vgreduce vg01 /dev/sdc1 # Remove PV from VG
vgrename vg01 vg02 # Rename VG
vgremove vg01 # Remove VG
Logical Volume Management
# Create logical volumes
lvcreate -L 10G -n lv01 vg01 # Create 10GB LV
lvcreate -l 100%FREE -n lv02 vg01 # Use all free space
lvcreate -L 5G -n lv03 vg01 /dev/sda1 # Create LV on specific PV
# Display logical volumes
lvdisplay # Detailed LV information
lvs # Summary LV information
lvscan # Scan for LVs
# Modify logical volumes
lvextend -L +5G /dev/vg01/lv01 # Extend LV by 5GB
lvextend -L 15G /dev/vg01/lv01 # Extend LV to 15GB
lvreduce -L -2G /dev/vg01/lv01 # Reduce LV by 2GB
lvrename vg01 lv01 lv_new # Rename LV
lvremove /dev/vg01/lv01 # Remove LV
Filesystem Resize with LVM
# Extend filesystem after LV extension
# For ext4
lvextend -L +5G /dev/vg01/lv01
resize2fs /dev/vg01/lv01
# For XFS
lvextend -L +5G /dev/vg01/lv01
xfs_growfs /dev/vg01/lv01
# One-step extend (ext4 only)
lvextend -L +5G -r /dev/vg01/lv01 # Resize LV and filesystem
# Reduce filesystem and LV (ext4 only)
umount /dev/vg01/lv01
e2fsck -f /dev/vg01/lv01
resize2fs /dev/vg01/lv01 8G
lvreduce -L 8G /dev/vg01/lv01
LVM Snapshots
# Create snapshot
lvcreate -L 2G -s -n snap01 /dev/vg01/lv01 # Create 2GB snapshot
# Mount snapshot
mount /dev/vg01/snap01 /mnt/snapshot
# Merge snapshot back
umount /dev/vg01/lv01
umount /dev/vg01/snap01
lvconvert --merge /dev/vg01/snap01
# Remove snapshot
lvremove /dev/vg01/snap01
RAID Configuration
Software RAID (mdadm)
RAID Level Overview
# RAID levels:
# RAID 0 - Striping (no redundancy, best performance)
# RAID 1 - Mirroring (redundancy, can lose 1 disk)
# RAID 5 - Parity (redundancy, can lose 1 disk, min 3 disks)
# RAID 6 - Double parity (redundancy, can lose 2 disks, min 4 disks)
# RAID 10 - Stripe of mirrors (redundancy + performance)
Creating RAID Arrays
# Create RAID 1 (mirror)
mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sda1 /dev/sdb1
# Create RAID 5
mdadm --create /dev/md1 --level=5 --raid-devices=3 /dev/sda1 /dev/sdb1 /dev/sdc1
# Create RAID 10
mdadm --create /dev/md2 --level=10 --raid-devices=4 /dev/sda1 /dev/sdb1 /dev/sdc1 /dev/sdd1
# Add spare device
mdadm --create /dev/md0 --level=1 --raid-devices=2 --spare-devices=1 /dev/sda1 /dev/sdb1 /dev/sdc1
Managing RAID Arrays
# Display RAID information
cat /proc/mdstat # Show RAID status
mdadm --detail /dev/md0 # Detailed array information
mdadm --examine /dev/sda1 # Examine RAID component
# Add/remove devices
mdadm --add /dev/md0 /dev/sdc1 # Add device to array
mdadm --remove /dev/md0 /dev/sdc1 # Remove device from array
mdadm --fail /dev/md0 /dev/sdc1 # Mark device as failed
# Stop/start arrays
mdadm --stop /dev/md0 # Stop array
mdadm --assemble /dev/md0 /dev/sda1 /dev/sdb1 # Assemble array
mdadm --assemble --scan # Auto-assemble all arrays
RAID Configuration File
# Generate configuration
mdadm --detail --scan >> /etc/mdadm/mdadm.conf
# Update initramfs
update-initramfs -u # Ubuntu/Debian
dracut -f # Red Hat/CentOS
Hardware RAID
# Hardware RAID tools vary by vendor:
# - MegaCLI (LSI/Broadcom)
# - Arcconf (Adaptec)
# - hpacucli (HP)
# Common operations (example with MegaCLI):
MegaCli -AdpAllInfo -aALL # Show adapter info
MegaCli -LDInfo -Lall -aALL # Show logical drives
MegaCli -PDList -aALL # Show physical drives
Storage Encryption
LUKS Encryption
Creating Encrypted Devices
# Create LUKS encrypted device
cryptsetup luksFormat /dev/sda1 # Format with LUKS
cryptsetup open /dev/sda1 encrypted # Open encrypted device
mkfs.ext4 /dev/mapper/encrypted # Create filesystem
mount /dev/mapper/encrypted /mnt # Mount encrypted filesystem
# Close encrypted device
umount /mnt
cryptsetup close encrypted
Managing LUKS Keys
# Add key slot
cryptsetup luksAddKey /dev/sda1
# Remove key slot
cryptsetup luksRemoveKey /dev/sda1
# Change key
cryptsetup luksChangeKey /dev/sda1
# Show key slots
cryptsetup luksDump /dev/sda1
Automated Mounting with LUKS
# Add to /etc/crypttab
encrypted /dev/sda1 none luks
# Add to /etc/fstab
/dev/mapper/encrypted /mnt ext4 defaults 0 2
# Use key file
echo "mypassword" > /etc/luks-key
chmod 600 /etc/luks-key
# In /etc/crypttab:
encrypted /dev/sda1 /etc/luks-key luks
dm-crypt without LUKS
# Create plain dm-crypt device
cryptsetup create encrypted /dev/sda1
mkfs.ext4 /dev/mapper/encrypted
mount /dev/mapper/encrypted /mnt
# Close device
umount /mnt
cryptsetup remove encrypted
Network Storage
NFS (Network File System)
NFS Server Setup
# Install NFS server
apt install nfs-kernel-server # Ubuntu/Debian
yum install nfs-utils # Red Hat/CentOS
# Configure exports (/etc/exports)
/srv/nfs/data 192.168.1.0/24(rw,sync,no_subtree_check)
/srv/nfs/home 192.168.1.100(rw,sync,no_root_squash)
# Apply exports
exportfs -a # Export all
exportfs -r # Re-export all
exportfs -u 192.168.1.100:/srv/nfs/data # Unexport specific
# Start NFS service
systemctl start nfs-kernel-server
systemctl enable nfs-kernel-server
NFS Client Setup
# Install NFS client
apt install nfs-common # Ubuntu/Debian
yum install nfs-utils # Red Hat/CentOS
# Mount NFS share
mount -t nfs server:/srv/nfs/data /mnt
mount -t nfs -o vers=4 server:/srv/nfs/data /mnt
# Persistent mount in /etc/fstab
server:/srv/nfs/data /mnt nfs defaults 0 0
server:/srv/nfs/data /mnt nfs vers=4,rsize=8192,wsize=8192 0 0
SMB/CIFS
SMB Server Setup (Samba)
# Install Samba
apt install samba # Ubuntu/Debian
yum install samba # Red Hat/CentOS
# Configure Samba (/etc/samba/smb.conf)
[data]
path = /srv/samba/data
writable = yes
guest ok = no
valid users = user1,user2
# Add Samba user
smbpasswd -a username
# Start Samba service
systemctl start smbd
systemctl enable smbd
SMB Client Setup
# Install SMB client
apt install cifs-utils # Ubuntu/Debian
yum install cifs-utils # Red Hat/CentOS
# Mount SMB share
mount -t cifs //server/share /mnt -o username=user
mount -t cifs //server/share /mnt -o credentials=/etc/samba/credentials
# Credentials file (/etc/samba/credentials)
username=myuser
password=mypassword
domain=mydomain
# Persistent mount in /etc/fstab
//server/share /mnt cifs credentials=/etc/samba/credentials,uid=1000,gid=1000 0 0
iSCSI
iSCSI Target Setup
# Install iSCSI target
apt install tgt # Ubuntu/Debian
yum install scsi-target-utils # Red Hat/CentOS
# Configure target (/etc/tgt/targets.conf)
<target iqn.2023-01.com.example:target01>
backing-store /dev/sdb
initiator-address 192.168.1.100
incominguser user password
</target>
# Start target service
systemctl start tgt
systemctl enable tgt
iSCSI Initiator Setup
# Install iSCSI initiator
apt install open-iscsi # Ubuntu/Debian
yum install iscsi-initiator-utils # Red Hat/CentOS
# Configure initiator name
echo "InitiatorName=iqn.2023-01.com.example:initiator01" > /etc/iscsi/initiatorname.iscsi
# Discover targets
iscsiadm -m discovery -t sendtargets -p 192.168.1.200
# Login to target
iscsiadm -m node -T iqn.2023-01.com.example:target01 -p 192.168.1.200 --login
# Automatic login
iscsiadm -m node -T iqn.2023-01.com.example:target01 -p 192.168.1.200 --op update -n node.startup -v automatic
Storage Monitoring
Disk Usage Monitoring
# Real-time disk usage
watch -n 1 df -h # Watch disk usage
du -sh /* | sort -hr # Sort directories by size
ncdu / # Interactive disk usage analyzer
# Find large files
find / -type f -size +100M -exec ls -lh {} \; 2>/dev/null
find / -type f -size +1G -exec ls -lh {} \; 2>/dev/null
I/O Monitoring
# I/O statistics
iostat -x 1 # Extended I/O statistics
iotop # I/O usage by process
iotop -o # Show only processes with I/O
# Disk I/O monitoring
watch -n 1 cat /proc/diskstats
sar -d 1 5 # Disk activity statistics
Performance Monitoring
# Filesystem performance
time dd if=/dev/zero of=/tmp/test bs=1M count=1000 # Write test
time dd if=/tmp/test of=/dev/null bs=1M # Read test
hdparm -t /dev/sda # Disk read speed test
hdparm -T /dev/sda # Cache read speed test
# Inode usage
df -i # Inode usage
find / -xdev -type f | wc -l # Count files on filesystem
Storage Troubleshooting
Filesystem Errors
# Check filesystem errors
dmesg | grep -i error # Check kernel messages
journalctl -u systemd-fsck* # Check fsck logs
# Force filesystem check
touch /forcefsck # Force fsck on next boot
shutdown -rF now # Reboot with fsck
# Read-only filesystem recovery
mount -o remount,rw / # Remount root as read-write
fsck /dev/sda1 # Check and repair filesystem
Disk Space Issues
# Find disk space usage
du -sh /var/log/* # Check log sizes
find /var/log -name "*.log" -size +100M # Find large log files
# Clean up space
apt autoclean # Clean package cache (Ubuntu/Debian)
yum clean all # Clean package cache (Red Hat/CentOS)
journalctl --vacuum-size=100M # Clean systemd logs
Mount Issues
# Check mount status
mount | grep /dev/sda1 # Check if mounted
findmnt /dev/sda1 # Find mount point
lsof +f -- /mnt # Find processes using mount point
# Force unmount
fuser -m /mnt # Find processes using mount point
fuser -km /mnt # Kill processes using mount point
umount -l /mnt # Lazy unmount
Recovery Procedures
# Boot from rescue disk
# Mount root filesystem
mkdir /mnt/root
mount /dev/sda1 /mnt/root
chroot /mnt/root
# Recover deleted files
testdisk # Partition recovery tool
photorec # File recovery tool
extundelete /dev/sda1 --restore-all # Recover ext3/4 files
Backup and Recovery
Filesystem Backup
# dd backup
dd if=/dev/sda of=/backup/sda.img bs=4M status=progress
dd if=/dev/sda of=/backup/sda.img.gz bs=4M status=progress | gzip
# Restore from dd backup
dd if=/backup/sda.img of=/dev/sda bs=4M status=progress
gunzip -c /backup/sda.img.gz | dd of=/dev/sda bs=4M status=progress
# Incremental backup with rsync
rsync -avH --delete /source/ /backup/
rsync -avH --delete --link-dest=/backup/previous /source/ /backup/current/
LVM Backup
# Backup LVM configuration
vgcfgbackup # Backup VG configuration
vgcfgbackup vg01 # Backup specific VG
# Restore LVM configuration
vgcfgrestore vg01 # Restore VG configuration
vgcfgrestore -f /etc/lvm/backup/vg01 vg01 # Restore from specific backup
RAID Backup
# Backup RAID configuration
mdadm --detail --scan > /etc/mdadm/mdadm.conf.backup
# Save RAID metadata
mdadm --examine /dev/sda1 > /backup/sda1-raid-metadata.txt
Performance Optimization
Filesystem Optimization
# Mount options for performance
mount -o noatime,nodiratime /dev/sda1 /mnt # Disable access time updates
mount -o data=writeback /dev/sda1 /mnt # Faster ext4 journaling
mount -o barrier=0 /dev/sda1 /mnt # Disable barriers (risky)
# Filesystem tuning
tune2fs -o journal_data_writeback /dev/sda1 # Set ext4 journal mode
tune2fs -O ^has_journal /dev/sda1 # Disable ext4 journaling
I/O Scheduler Optimization
# Check current scheduler
cat /sys/block/sda/queue/scheduler
# Set I/O scheduler
echo deadline > /sys/block/sda/queue/scheduler
echo noop > /sys/block/sda/queue/scheduler # For SSDs
echo cfq > /sys/block/sda/queue/scheduler # For HDDs
# Persistent scheduler setting
echo 'ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{queue/scheduler}="deadline"' > /etc/udev/rules.d/60-scheduler.rules
Cache and Buffer Optimization
# Drop caches
echo 1 > /proc/sys/vm/drop_caches # Drop page cache
echo 2 > /proc/sys/vm/drop_caches # Drop dentries and inodes
echo 3 > /proc/sys/vm/drop_caches # Drop all caches
# Tune writeback behavior
echo 5 > /proc/sys/vm/dirty_ratio # Dirty pages limit
echo 10 > /proc/sys/vm/dirty_background_ratio # Background writeback
Best Practices
Storage Planning
- Capacity Planning - Monitor growth trends and plan for 80% capacity
- Performance Requirements - Choose appropriate filesystem and RAID levels
- Backup Strategy - Implement regular backups and test recovery procedures
- Monitoring - Set up alerts for disk space and I/O issues
Filesystem Selection
- ext4: General purpose, stable, good performance
- XFS: Large files, high performance, online resizing
- Btrfs: Snapshots, compression, advanced features
- ZFS: Enterprise features, integrity checking, snapshots
Security Considerations
- Encryption - Use LUKS for sensitive data
- Access Controls - Implement proper file permissions
- Audit Trails - Monitor file access and modifications
- Backup Security - Encrypt backup data
Common Pitfalls to Avoid
- Full Disk Space - Monitor and maintain free space
- Inode Exhaustion - Monitor inode usage
- No Backups - Always have tested backup procedures
- Ignoring SMART - Monitor disk health regularly
- Wrong Filesystem - Choose appropriate filesystem for use case