This tutorial covers the fundamental concepts and best practices for managing storage as a system administrator. You’ll learn about common storage devices, partitioning schemes, file systems, mounting, and monitoring storage usage.

Beginner30 minutes

Step 1: Understanding Storage Devices

- Hard Disk Drives (HDDs) vs Solid State Drives (SSDs)

- SATA vs SAS vs NVMe interfaces

- Form factors: 2.5", 3.5", M.2, U.2

- Capacity and performance considerations

Practice Exercise

Research the pros and cons of using SSDs vs HDDs for a database server workload. What factors would you consider when making a recommendation?

Show Solution
For a database server, the key factors are:
1) Performance - SSDs offer much faster read/write speeds and lower latency which can greatly improve DB performance
2) Durability - enterprise SSDs have high endurance to handle heavy write workloads
3) Capacity - HDDs still offer higher capacities at lower costs
4) Budget - SSDs are more expensive per GB than HDDs

Recommendation:
- If maximizing performance is critical and budget allows, use enterprise SSDs
- For very large DBs where high capacity is needed, HDDs may be more economical
- Consider a hybrid approach - SSDs for hot data, HDDs for cold data

Step 2: Disk Partitioning

- What are partitions and why they are used

- MBR vs GPT partition tables

- Primary, extended, and logical partitions

- Partition types (e.g. Linux, swap, LVM)

- Creating and modifying partitions with fdisk and parted

# Create a new primary partition using fdisk
fdisk /dev/sda

Command (m for help): n
Partition type (p for primary, e for extended): p
Partition number (1-4): 2  
First sector: [press Enter]  
Last sector: +5G

Command (m for help): w

Practice Exercise

You have a new 500GB HDD that needs to be partitioned for a Linux server. Create a partitioning scheme that includes separate partitions for root, home, var and swap. Determine appropriate sizes for each.

Show Solution
Suggested partition scheme for a 500GB HDD:

1. /boot - 512MB 
- Stores boot loader and kernel images
- Keep small as it is rarely written to

2. swap - 4GB
- Swap size of 4GB should suffice for most workloads
- Can increase if running memory intensive apps

3. / (root) - 50GB
- Needs to store OS and system files
- 50GB provides room for future growth

4. /var - 50GB
- Stores variable data like logs and databases
- Isolating it prevents other partitions from filling up

5. /home - Remaining space
- Stores user data and files
- Giving it the most space allows flexibility as needs change

Step 3: File Systems

- What is a file system and why they are needed

- Common Linux file systems: ext4, XFS, Btrfs

- File system features - journaling, copy-on-write, RAID

- Creating file systems with mkfs

- Checking and repairing file systems with fsck

# Create an ext4 file system on /dev/sda2
mkfs.ext4 /dev/sda2

# Run a file system check on /dev/sda2  
fsck /dev/sda2

Practice Exercise

You are setting up a new file server that will store large video files. Research the available file system options and make a recommendation. Consider the specific needs of a media server.

Show Solution
For a video file server, the key requirements are:
1) Large file support - individual files may be multiple GBs in size
2) Scalability - the volume of files will grow over time
3) Sequential read performance - for smooth video playback

Recommendation: Use the XFS file system
- Designed for high scalability and capacity
- Supports file sizes up to 8 exbibytes 
- Optimized for large files and linear reads
- Provides good performance even when nearly full
- Includes online defragmentation and expansion
- ext4 and Btrfs are also viable options, but XFS is purpose-built for this use case

Step 4: Mounting File Systems

- What is mounting and why it is necessary

- Mounting devices vs mounting file systems

- Temporary mounts vs persistent mounts

- Configuring mounts in /etc/fstab

- Using the mount and umount commands

# Mount /dev/sda2 to /mnt temporarily
mount /dev/sda2 /mnt

# Configure /dev/sda2 to mount automatically on boot
echo "/dev/sda2 /mnt ext4 defaults 0 0" >> /etc/fstab

Practice Exercise

You have a removable USB drive that you need to mount persistently with user-level read/write access. However, the drive uses exFAT rather than a Linux file system. What steps would you take to address this?

Show Solution
To persistently mount an exFAT USB drive in Linux: 

1. Install the exFAT drivers and utilities
- On Ubuntu/Debian: `sudo apt install exfat-fuse exfat-utils`
- On RHEL/CentOS: `sudo yum install fuse-exfat exfat-utils`  

2. Create a mount point directory
- `sudo mkdir /mnt/usb-drive`
- Apply appropriate ownership and permissions

3. Identify the device name
- `lsblk` to list block devices and find the USB drive (e.g. /dev/sdb1)

4. Add an entry to /etc/fstab to mount on boot
- Get the UUID: `sudo blkid /dev/sdb1`
- Add to /etc/fstab: 
  `UUID=1234 /mnt/usb-drive exfat defaults,auto,umask=000,users 0 0`
- `umask=000` allows all users RW access, `users` allows non-root users to mount
  
5. Test the mount
- `sudo mount -a` to mount all from /etc/fstab
- Verify it mounted: `mount | grep usb-drive`

Step 5: Logical Volume Management (LVM)

- What is LVM and its benefits

- LVM architecture - PVs, VGs, LVs

- Creating and managing LVM storage pools

- Resizing and snapshotting LVM volumes

- Practical use cases for LVM

# Create a physical volume from /dev/sda2
pvcreate /dev/sda2

# Create a volume group called "vg_data" 
vgcreate vg_data /dev/sda2
  
# Create a 100GB logical volume called "lv_database"  
lvcreate -L 100G -n lv_database vg_data

Practice Exercise

Scenario: You manage a database server with two 500GB HDDs. The database recently ran out of space, causing downtime. Your manager has purchased two additional 500GB HDDs. Design a new storage scheme using LVM that will improve capacity, performance and redundancy.

Show Solution
To meet the goals of increasing capacity, performance and redundancy:

1. Create a RAID 10 array from the 4 HDDs
- RAID 10 provides a good balance of redundancy and performance
- Usable capacity will be 1TB 
- Commands:
  - `mdadm --create /dev/md0 --level=raid10 --raid-devices=4 /dev/sda /dev/sdb /dev/sdc /dev/sdd`

2. Create a physical volume from the RAID array
- `pvcreate /dev/md0`

3. Create a volume group called "vg_database"  
- `vgcreate vg_database /dev/md0` 

4. Create logical volumes for data and logs
- Allocate more space to data as it will grow over time
- `lvcreate -L 800G -n lv_data vg_database`
- `lvcreate -L 200G -n lv_logs vg_database`

5. Create file systems on each LV and mount them
- `mkfs.xfs /dev/vg_database/lv_data`
- `mkfs.xfs /dev/vg_database/lv_logs`
- Edit /etc/fstab to mount them persistently

Benefits of this scheme:
- Redundancy from RAID 10 protects against disk failures
- Striping in RAID 10 allows for increased read/write performance  
- Separating data and logs onto different LVs allows for different backup policies
- VG can be extended in the future by adding more PVs
- LVs can be resized dynamically as needed

Step 6: Monitoring Storage Usage

- Why monitoring storage is important

- Using df to check file system usage

- Using du to find directories consuming the most space

- Monitoring tools like Nagios and Zabbix

- Setting up alerts for low disk space

- Strategies for managing storage capacity

# Check file system disk usage
df -h

# Find the 5 largest directories in /var
du -sh /var/* | sort -rh | head -n5

Practice Exercise

Your root partition is 90% full and you need to free up space. Walk through the troubleshooting steps you would follow to identify the cause and remediate the issue.

Show Solution
1. Check overall disk usage for all file systems
- `df -h`
- Note which partition(s) have high utilization
  
2. Identify the largest directories on the affected partition
- `du -sh /path/to/partition/* | sort -rh | head -n10`
- This will show the top 10 directories by size

3. Investigate the large directories  
- Look for unexpected growth, e.g. runaway logs, old backups, large core dumps
- Check if the directories are supposed to be that large  
- Use `du` within large dirs to drill down further

4. Take appropriate action based on the cause
- Delete old log files and archives
- Move non-critical data to another partition
- Truncate large log files
- Purge old packages and cached files: 
  - `apt-get autoremove`, `apt-get clean`, `yum clean all`
- As a last resort, expand the partition or add a new disk
  
5. Implement monitoring to prevent future issues  
- Set up disk usage monitoring in tool like Nagios
- Configure alerts for 80% or 90% usage thresholds
- Schedule regular purging of temp files and old logs
- Review storage usage quarterly to identify growth trends