Linux Partition Process

Introduction

Partitioning is an essential step in setting up a Linux system. It involves dividing a storage device into separate sections, each of which can be managed independently. This guide explains the standard Linux partition process with detailed steps and examples.

Partition types

Partitioning Tools

Common Filesystems

Partitioning Schemes

Partitioning Guidelines

Partitioning Best Practices

Common Partitioning Commands

Steps for Partitioning in Linux

  1. Identify the Disk: Use the lsblk or fdisk -l command to list available disks.
  2. Create a New Partition: Use a partitioning tool like fdisk or parted to create a new partition.
  3. Format the Partition: Format the partition with a filesystem such as ext4 using the mkfs command.
  4. Mount the Partition: Mount the partition to a directory using the mount command.
  5. Update fstab: Add an entry to the /etc/fstab file to ensure the partition is mounted automatically on boot.

Example: Partitioning a Disk

Step 1: Identify the Disk

sudo lsblk

This command lists all available disks and partitions. Identify the target disk (e.g., /dev/sdb).

Step 2: Create a New Partition

sudo fdisk /dev/sdb

Follow the interactive prompts to create a new partition.

For example, type n to create a new partition, then specify the partition type (primary or extended), partition number, and size.

Here’s a quick guide on the fdisk commands:

After creating the partition, type w to write the changes and exit.

After partition create run lsblk this will show us new partition, If lsblk will not showing any new disk partition so run this command:

sudo partprobe

This command informs the OS of partition table changes without rebooting.

Step 3: Format the Partition

sudo mkfs.ext4 /dev/sdb1

This formats the partition /dev/sdb1 with the ext4 filesystem.

Step 4: Mount the Partition

sudo mkdir /mnt/new_partition
sudo mount /dev/sdb1 /mnt/new_partition

The partition is now mounted to /mnt/new_partition.

Step 5: Update fstab

echo '/dev/sdb1 /mnt/new_partition ext4 defaults 0 0' | sudo tee -a /etc/fstab

This ensures the partition is mounted automatically on boot.

Example: Creating a Partition with fdisk

To create a new partition using fdisk, follow these steps:

  1. Open the terminal and run sudo fdisk /dev/sdX, replacing sdX with your target disk (e.g., sda).
  2. Type n to create a new partition.
  3. Select the partition type (primary or extended).
  4. Specify the partition number and size.
  5. Type w to write the changes and exit.

After creating the partition, format it with a filesystem using mkfs and mount it as described in the previous section.

Example: Creating a Partition with parted

To create a new partition using parted, follow these steps:

  1. Open the terminal and run sudo parted /dev/sdX, replacing sdX with your target disk (e.g., sda).
  2. Type mklabel gpt to create a new GPT partition table.
  3. Type mkpart primary ext4 1MiB 20GiB to create a primary partition from 1MiB to 20GiB.
  4. Type quit to exit parted.
  5. Format the new partition with sudo mkfs.ext4 /dev/sdX1, replacing sdX1 with the new partition name.
  6. Mount the partition with sudo mount /dev/sdX1 /mnt/new_partition.

After creating the partition, you can add it to /etc/fstab for automatic mounting on boot.