Disk management is a crucial aspect of system administration. It involves managing disk space, partitions, file systems, and ensuring data integrity. This section will cover the following key topics:

  1. Understanding Disk Partitions
  2. Creating and Managing Partitions
  3. File Systems and Formatting
  4. Mounting and Unmounting File Systems
  5. Disk Usage and Quotas
  6. Practical Exercises

  1. Understanding Disk Partitions

Disk partitions are subdivisions of a physical disk drive. Each partition can be managed separately and can contain different file systems. Understanding partitions is essential for organizing data and optimizing disk usage.

Key Concepts:

  • Primary Partitions: The main partitions on a disk. A disk can have up to four primary partitions.
  • Extended Partitions: A type of primary partition that can contain multiple logical partitions.
  • Logical Partitions: Partitions within an extended partition.

Example:

|-------------------------|
| Primary | Primary | Extended |
|-------------------------|
| Logical | Logical | Logical |
|-------------------------|

  1. Creating and Managing Partitions

Tools:

  • fdisk: A command-line utility for managing disk partitions.
  • parted: A more advanced tool for managing partitions.

Example: Using fdisk

sudo fdisk /dev/sda
  • Command Explanation:
    • n: Create a new partition.
    • p: Print the partition table.
    • d: Delete a partition.
    • w: Write changes to disk and exit.

Example: Using parted

sudo parted /dev/sda
  • Command Explanation:
    • mklabel gpt: Create a new GPT partition table.
    • mkpart primary ext4 1MiB 100%: Create a new primary partition with ext4 file system.
    • print: Display the partition table.

  1. File Systems and Formatting

Common File Systems:

  • ext4: The default file system for many Linux distributions.
  • xfs: A high-performance file system.
  • btrfs: A modern file system with advanced features.

Formatting a Partition:

sudo mkfs.ext4 /dev/sda1
  • Command Explanation:
    • mkfs.ext4: Format the partition with the ext4 file system.
    • /dev/sda1: The partition to format.

  1. Mounting and Unmounting File Systems

Mounting:

sudo mount /dev/sda1 /mnt
  • Command Explanation:
    • mount: Mount a file system.
    • /dev/sda1: The partition to mount.
    • /mnt: The directory to mount the partition to.

Unmounting:

sudo umount /mnt
  • Command Explanation:
    • umount: Unmount a file system.
    • /mnt: The directory where the file system is mounted.

Persistent Mounts:

To make mounts persistent across reboots, add entries to /etc/fstab.

Example /etc/fstab Entry:

/dev/sda1 /mnt ext4 defaults 0 2

  1. Disk Usage and Quotas

Checking Disk Usage:

df -h
  • Command Explanation:
    • df: Display disk space usage.
    • -h: Human-readable format.

Setting Up Quotas:

  1. Install Quota Tools:

    sudo apt-get install quota
    
  2. Edit /etc/fstab: Add usrquota and/or grpquota to the relevant partition.

    /dev/sda1 /mnt ext4 defaults,usrquota,grpquota 0 2
    
  3. Remount the File System:

    sudo mount -o remount /mnt
    
  4. Initialize Quota Database:

    sudo quotacheck -cug /mnt
    
  5. Enable Quotas:

    sudo quotaon /mnt
    
  6. Set Quotas:

    sudo edquota -u username
    

  1. Practical Exercises

Exercise 1: Create and Format a Partition

  1. Use fdisk to create a new partition on /dev/sdb.
  2. Format the partition with the ext4 file system.
  3. Mount the partition to /mnt/data.

Solution:

sudo fdisk /dev/sdb
# Follow prompts to create a new partition
sudo mkfs.ext4 /dev/sdb1
sudo mount /dev/sdb1 /mnt/data

Exercise 2: Set Up Disk Quotas

  1. Install quota tools.
  2. Edit /etc/fstab to enable user quotas on /mnt/data.
  3. Initialize and enable quotas.
  4. Set a quota for a user.

Solution:

sudo apt-get install quota
sudo nano /etc/fstab
# Add usrquota to the relevant partition
sudo mount -o remount /mnt/data
sudo quotacheck -cug /mnt/data
sudo quotaon /mnt/data
sudo edquota -u username

Conclusion

In this section, we covered the essentials of disk management, including understanding partitions, creating and managing partitions, formatting file systems, mounting and unmounting file systems, and setting up disk quotas. Mastering these skills is crucial for effective system administration and ensuring optimal disk usage and data integrity. In the next section, we will delve into package management, another critical aspect of maintaining a Linux system.

© Copyright 2024. All rights reserved