Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Disk Management in Linux

1. Introduction

Disk management in Linux involves configuring and managing disk storage, including partitioning, formatting, and mounting filesystems. This lesson covers essential tools and commands used in disk management.

2. Key Concepts

  • Disk: Physical storage device.
  • Partition: A segment of a disk that can be managed independently.
  • Filesystem: The structure that organizes data on a disk.
  • Mounting: Making a filesystem accessible at a certain point in the directory tree.

3. Disk Management Tools

Several tools are available for disk management in Linux:

  • fdisk: Command-line utility for disk partitioning.
  • parted: Advanced partition manipulation tool.
  • mkfs: Command to create a filesystem on a partition.
  • mount: Command to mount filesystems.

4. Partitioning Disks

To partition a disk using fdisk, follow these steps:

sudo fdisk /dev/sdX  # Replace sdX with your disk identifier
n                    # Create a new partition
p                    # Primary partition
1                    # Partition number
              # Default first sector
+20G                 # Size of the partition (20GB)
w                    # Write changes

After creating a partition, it is essential to format it:

sudo mkfs.ext4 /dev/sdX1  # Format the new partition as ext4

5. Filesystems

Common filesystems include:

  • ext4
  • xfs
  • btrfs
  • ntfs

To create an ext4 filesystem, use:

sudo mkfs.ext4 /dev/sdX1

6. Mounting Disks

To mount a partition:

sudo mount /dev/sdX1 /mnt/mydisk

To make the mount permanent, add an entry to /etc/fstab:

/dev/sdX1   /mnt/mydisk   ext4   defaults   0   2

7. Best Practices

Always back up critical data before making changes to disk partitions.

  • Use lsblk to view disk structure.
  • Document changes to partitions and filesystems.
  • Regularly check disk health using smartctl.

8. FAQ

What is the difference between a partition and a logical volume?

A partition is a fixed segment of a disk, while a logical volume is a flexible block of storage that can span multiple physical disks.

Can I resize a partition while the system is running?

It depends on the filesystem and whether the partition is mounted. Modern filesystems like ext4 support online resizing.

What is LVM?

Logical Volume Management (LVM) is a method for managing disk space that allows for flexible storage allocation.