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
- MBR
- GPT
Partitioning Tools
- fdisk - A command-line utility for managing disk partitions.
- parted - A more advanced command-line tool that supports both MBR and GPT partition tables.
- gparted - A graphical partition editor for Linux.
Common Filesystems
- ext4 - The most commonly used filesystem in Linux.
- xfs - A high-performance filesystem suitable for large files.
- btrfs - A modern filesystem with advanced features like snapshots and RAID support.
- swap - A special partition used for virtual memory.
Partitioning Schemes
- Standard Partitioning - Divides the disk into primary and extended partitions.
- LVM (Logical Volume Management) - Allows for flexible disk management, enabling resizing and creating snapshots.
Partitioning Guidelines
- Root Partition (/) - Contains the operating system and should be at least 20GB.
- Home Partition (/home) - Stores user data; size depends on user needs.
- Swap Partition - Typically equal to the size of RAM, but can vary based on system usage.
- Boot Partition (/boot) - Optional, but useful for systems with complex boot configurations.
- EFI System Partition (ESP) - Required for UEFI systems, typically around 200-500MB.
Partitioning Best Practices
- Backup Data - Always back up important data before partitioning.
- Use GPT for Large Disks - For disks larger than 2TB, use GPT to avoid MBR limitations.
- Plan Partition Sizes - Estimate the size of each partition based on usage patterns.
- Keep a Separate /home Partition - This allows for easier upgrades and system reinstalls without losing user data.
- Consider LVM - If you need flexibility in resizing partitions, consider using LVM.
Common Partitioning Commands
- fdisk - Used for creating and managing partitions on MBR disks.
- parted - Used for creating and managing partitions on both MBR and GPT disks.
- mkfs - Used to format a partition with a specific filesystem (e.g.,
mkfs.ext4). - mount - Used to mount a partition to a directory.
- umount - Used to unmount a partition.
- lsblk - Lists all block devices and their partitions.
- blkid - Displays information about block devices, including UUIDs and filesystems.
- df - Displays disk space usage for mounted filesystems.
- partprobe - Informs the OS of partition table changes without rebooting.
Steps for Partitioning in Linux
- Identify the Disk: Use the
lsblkorfdisk -lcommand to list available disks. - Create a New Partition: Use a partitioning tool like
fdiskorpartedto create a new partition. - Format the Partition: Format the partition with a filesystem such as ext4 using the
mkfscommand. - Mount the Partition: Mount the partition to a directory using the
mountcommand. - Update fstab: Add an entry to the
/etc/fstabfile 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:
n- Create a new partition.p- Print the partition table.d- Delete a partition.t- Change the partition type.w- Write changes and exit.q- Quit without saving changes.
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:
- Open the terminal and run
sudo fdisk /dev/sdX, replacingsdXwith your target disk (e.g.,sda). - Type
nto create a new partition. - Select the partition type (primary or extended).
- Specify the partition number and size.
- Type
wto 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:
- Open the terminal and run
sudo parted /dev/sdX, replacingsdXwith your target disk (e.g.,sda). - Type
mklabel gptto create a new GPT partition table. - Type
mkpart primary ext4 1MiB 20GiBto create a primary partition from 1MiB to 20GiB. - Type
quitto exit parted. - Format the new partition with
sudo mkfs.ext4 /dev/sdX1, replacingsdX1with the new partition name. - 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.