Skip to content

Disk Management

PotatoScript edited this page Mar 30, 2025 · 1 revision

Disk Management and Partitions in Linux

🌟 1. Introduction to Disk Management

In Linux, managing your disks is an important task to ensure that your data is stored properly, and your system works efficiently. Just like organizing files in your bedroom, managing disks and partitions helps organize your data so that your computer can find everything easily and quickly.


🧑‍💻 What is a Disk in Linux?

  • A disk is your physical storage, where data is stored (like your hard drive or SSD).
  • Partitions are sections or divisions on that disk, each acting as a separate storage unit.
  • Think of a disk like a book, and the partitions are like the chapters in that book. Each chapter (partition) can contain different types of information.

🔧 2. Understanding Partitions in Linux


💡 A partition is a part of your disk that’s separated from the rest. It’s like a section of your bookshelf where you keep different types of books: one for your homework, another for novels, and one more for magazines.

🗂️ Why Do We Use Partitions?

  • Organize data: You can separate your operating system files from your personal files.
  • Better performance: Organizing data into separate partitions can make your system faster.
  • Data protection: If something goes wrong with one partition, others can still be safe.

🚪 Types of Partitions

  1. Primary Partition: These are the main partitions where your data and operating system are stored.
  2. Extended Partition: This is a special partition that allows you to create more than four partitions.
  3. Logical Partition: These are partitions inside an extended partition, used to organize your data further.

🛠️ 3. Disk Management Tools in Linux

There are several tools in Linux that you can use to manage your disks and partitions. Each one has its purpose, and knowing which one to use helps you do the job faster and more efficiently.


🔹 1. fdisk – The Classic Tool

fdisk is a popular tool to manage disks and partitions. It allows you to create, delete, and modify partitions.

📚 Example: Listing Partitions

sudo fdisk -l

This will list all the disks and partitions on your system.

🔹 2. lsblk – The Block Devices Tool

lsblk is a tool that shows your disk partitions in a tree-like structure. It’s a simple way to understand how your disk is divided.

lsblk

📚 Example Output:

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0  500G  0 disk
├─sda1   8:1    0  100G  0 part /
├─sda2   8:2    0  200G  0 part /home
└─sda3   8:3    0  200G  0 part

This shows that sda is a 500GB disk, with three partitions:

  • sda1 (100GB) for the root system (/)
  • sda2 (200GB) for the home directory (/home)
  • sda3 (200GB) is unmounted.

🔹 3. parted – For More Complex Operations

parted is used for more complex partitioning, like resizing partitions or creating partitions with different file systems.

📚 Example: Resize a Partition

sudo parted /dev/sda resizepart 2 400GB

This command resizes partition sda2 to 400GB.


⚙️ 4. Creating a Partition

Creating partitions involves breaking up your disk into sections, like dividing a bookshelf into smaller spaces. You can use tools like fdisk or parted to do this.

📋 Step-by-Step Example Using fdisk:

  1. Open fdisk with your disk (e.g., /dev/sda):
sudo fdisk /dev/sda
  1. Create a new partition: Press n to create a new partition.

  2. Select the partition type: You can choose whether it’s a primary (p) or extended (e) partition.

  3. Set the partition size: Type the size you want for your new partition.

  4. Write changes to disk: Press w to save the changes.


🔧 5. Formatting Partitions

Once a partition is created, it needs to be formatted before it can store data. This means putting a file system on it, like ext4 (common in Linux), NTFS (for Windows), or FAT32.

📚 Step-by-Step Formatting a Partition:

Let’s say you just created a partition called /dev/sda3, and you want to format it with the ext4 file system.

sudo mkfs.ext4 /dev/sda3

This command formats the partition and makes it ready for storing files.


🚪 6. Mounting Partitions

After formatting, the partition needs to be mounted so the system can access it. Mounting a partition means telling your system where to put it so you can use it.

📋 Step-by-Step Mounting a Partition:

  1. Create a mount point (a directory where the partition will be mounted):
sudo mkdir /mnt/data
  1. Mount the partition:
sudo mount /dev/sda3 /mnt/data
  1. Access your partition: Now you can access your new partition by going to /mnt/data.

🛠️ 7. Automating Mounting with /etc/fstab

If you want the system to automatically mount the partition every time it boots up, you need to add it to the /etc/fstab file.

  1. Open the fstab file for editing:
sudo nano /etc/fstab
  1. Add the following line at the end:
/dev/sda3 /mnt/data ext4 defaults 0 0

This tells the system to mount /dev/sda3 to /mnt/data every time it starts.


💡 8. Resizing Partitions

Sometimes, you might want to increase or decrease the size of a partition. This is especially helpful when your system or data grows.

You can resize partitions using tools like gparted (a graphical tool) or parted.

📚 Example (using gparted):

  1. Open gparted:
sudo gparted
  1. Select the partition you want to resize.
  2. Drag the slider or enter the new size and apply the changes.

⚠️ 9. Deleting a Partition

If you no longer need a partition, you can delete it using fdisk or gparted.

📚 Step-by-Step Deleting a Partition:

  1. Open fdisk:
sudo fdisk /dev/sda
  1. Press d to delete a partition.
  2. Select the partition number you want to delete.
  3. Press w to save the changes.

Clone this wiki locally