How to create partitions in Linux

How to Create Partitions in Linux Creating partitions in Linux is a fundamental skill for system administrators, developers, and advanced users who need to manage disk storage efficiently. Whether you're setting up a new system, adding storage space, or organizing data across multiple drives, understanding Linux partitioning is essential for optimal system performance and data management. This comprehensive guide will walk you through various methods to create partitions in Linux, from command-line tools to graphical interfaces, ensuring you have the knowledge and confidence to manage your storage effectively. Understanding Linux Partitions Before diving into the practical steps, it's crucial to understand what partitions are and why they matter in Linux systems. What Are Partitions? A partition is a logical division of a hard disk drive (HDD) or solid-state drive (SSD) that the operating system treats as a separate storage unit. Think of partitions as rooms in a house – each serves a specific purpose and can be managed independently. Types of Partitions in Linux Linux systems typically use two main partition table formats: 1. MBR (Master Boot Record): Traditional format supporting up to 4 primary partitions 2. GPT (GUID Partition Table): Modern format supporting up to 128 partitions with better reliability Common Partition Types - Primary Partitions: Main partitions that can contain an operating system - Extended Partitions: Container partitions that can hold multiple logical partitions - Logical Partitions: Subdivisions within extended partitions - Swap Partitions: Virtual memory space used when RAM is full Prerequisites and Safety Considerations Before You Begin Creating partitions involves modifying disk structures, which can result in data loss if done incorrectly. Always follow these safety guidelines: 1. Backup your data: Create complete backups of important files 2. Unmount partitions: Ensure target partitions are unmounted 3. Check disk health: Verify the disk is functioning properly 4. Plan your layout: Design your partition scheme beforehand Required Permissions Most partitioning operations require root privileges. Use `sudo` or switch to the root user: ```bash sudo su - ``` Identifying Your Disks First, identify available disks and existing partitions: ```bash List all block devices lsblk Display partition information fdisk -l Show mounted filesystems df -h ``` Method 1: Creating Partitions with fdisk The `fdisk` utility is the most traditional and widely available partitioning tool in Linux. It's command-line based and works well with both MBR and GPT partition tables. Basic fdisk Usage Start by launching fdisk for your target disk: ```bash sudo fdisk /dev/sdb ``` Replace `/dev/sdb` with your actual disk device. Common disk naming conventions: - `/dev/sda`, `/dev/sdb`, etc. for SATA/SCSI drives - `/dev/nvme0n1`, `/dev/nvme1n1`, etc. for NVMe drives - `/dev/vda`, `/dev/vdb`, etc. for virtual machines Step-by-Step Partition Creation with fdisk 1. Enter fdisk interactive mode: ```bash sudo fdisk /dev/sdb ``` 2. Display current partition table: ``` Command (m for help): p ``` 3. Create a new partition: ``` Command (m for help): n ``` 4. Choose partition type: - `p` for primary partition - `e` for extended partition 5. Select partition number (1-4 for primary partitions) 6. Set starting sector (press Enter for default) 7. Set ending sector or size: - Enter specific sector number - Use size notation: `+10G` for 10 gigabytes, `+500M` for 500 megabytes 8. Write changes to disk: ``` Command (m for help): w ``` Practical Example: Creating Multiple Partitions Let's create a practical partition layout on a 100GB disk: ```bash sudo fdisk /dev/sdb Create first partition (20GB for root filesystem) Command (m for help): n Partition type: p Partition number: 1 First sector: [Enter for default] Last sector: +20G Create second partition (4GB for swap) Command (m for help): n Partition type: p Partition number: 2 First sector: [Enter for default] Last sector: +4G Create third partition (remaining space for home directory) Command (m for help): n Partition type: p Partition number: 3 First sector: [Enter for default] Last sector: [Enter for default - uses remaining space] Write changes Command (m for help): w ``` Setting Partition Types After creating partitions, you may want to set specific partition types: ```bash Change partition type Command (m for help): t Partition number: 2 Hex code: 82 # 82 = Linux swap Common partition type codes: 83 = Linux filesystem 82 = Linux swap 8e = Linux LVM ef = EFI System ``` Method 2: Using parted for Advanced Partitioning The `parted` utility offers more advanced features and better support for GPT partition tables. It can work both interactively and with command-line arguments. Interactive parted Usage 1. Launch parted: ```bash sudo parted /dev/sdb ``` 2. Create GPT partition table (if needed): ``` (parted) mklabel gpt ``` 3. Create partitions: ``` (parted) mkpart primary ext4 1MiB 20GiB (parted) mkpart primary linux-swap 20GiB 24GiB (parted) mkpart primary ext4 24GiB 100% ``` 4. Display partition table: ``` (parted) print ``` 5. Exit parted: ``` (parted) quit ``` Non-Interactive parted Commands For scripting or automation, use parted with command-line arguments: ```bash Create GPT partition table sudo parted /dev/sdb mklabel gpt Create EFI system partition (512MB) sudo parted /dev/sdb mkpart ESP fat32 1MiB 513MiB sudo parted /dev/sdb set 1 esp on Create root partition (30GB) sudo parted /dev/sdb mkpart primary ext4 513MiB 30GiB Create home partition (remaining space) sudo parted /dev/sdb mkpart primary ext4 30GiB 100% ``` Aligning Partitions for Optimal Performance Modern storage devices benefit from proper partition alignment: ```bash Check alignment sudo parted /dev/sdb align-check optimal 1 Create aligned partitions sudo parted /dev/sdb mkpart primary ext4 1MiB 10GiB ``` Method 3: Graphical Partitioning with GParted GParted provides a user-friendly graphical interface for partition management, making it ideal for users who prefer visual tools. Installing GParted On Ubuntu/Debian: ```bash sudo apt update sudo apt install gparted ``` On Red Hat/CentOS/Fedora: ```bash sudo yum install gparted # CentOS/RHEL sudo dnf install gparted # Fedora ``` Using GParted 1. Launch GParted: ```bash sudo gparted ``` 2. Select target disk from the dropdown menu in the top-right corner 3. Create new partition table (if needed): - Go to Device → Create Partition Table - Choose partition table type (GPT recommended for new systems) 4. Create partitions: - Right-click on unallocated space - Select "New" - Configure partition settings: - File system type - Size - Label - Click "Add" 5. Apply changes: Click the green checkmark to execute all pending operations GParted Live USB For partitioning system disks or working on computers without GParted installed, use GParted Live: 1. Download GParted Live ISO from the official website 2. Create a bootable USB drive 3. Boot from the USB drive 4. Use GParted in the live environment Creating and Formatting File Systems After creating partitions, you need to format them with appropriate file systems. Common Linux File Systems - ext4: Default file system for most Linux distributions - xfs: High-performance file system, good for large files - btrfs: Modern file system with advanced features - fat32: Compatible with Windows and other operating systems Formatting Partitions ```bash Format as ext4 sudo mkfs.ext4 /dev/sdb1 Format as xfs sudo mkfs.xfs /dev/sdb2 Format as fat32 sudo mkfs.fat -F32 /dev/sdb3 Create swap partition sudo mkswap /dev/sdb4 ``` Setting File System Labels Labels make partitions easier to identify: ```bash Set label during formatting sudo mkfs.ext4 -L "MyRootFS" /dev/sdb1 Set label on existing file system sudo e2label /dev/sdb1 "NewLabel" # ext2/3/4 sudo xfs_admin -L "NewLabel" /dev/sdb2 # xfs ``` Mounting New Partitions After creating and formatting partitions, mount them to make them accessible: Temporary Mounting ```bash Create mount point sudo mkdir /mnt/mydata Mount partition sudo mount /dev/sdb1 /mnt/mydata Verify mount df -h /mnt/mydata ``` Permanent Mounting with fstab Edit `/etc/fstab` to automatically mount partitions at boot: ```bash sudo nano /etc/fstab ``` Add entries for your partitions: ``` Device Mount Point FS Type Options Dump Pass /dev/sdb1 /home ext4 defaults 0 2 /dev/sdb2 none swap sw 0 0 UUID=xxx-xxx /data xfs defaults,noatime 0 2 ``` Using UUIDs for Stability UUIDs are more reliable than device names: ```bash Find partition UUIDs sudo blkid Use UUID in fstab UUID=12345678-1234-1234-1234-123456789012 /data ext4 defaults 0 2 ``` Advanced Partitioning Scenarios Creating LVM Partitions Logical Volume Manager (LVM) provides flexible disk management: ```bash Create physical volume sudo pvcreate /dev/sdb1 Create volume group sudo vgcreate myvg /dev/sdb1 Create logical volume sudo lvcreate -L 10G -n mylv myvg Format and mount sudo mkfs.ext4 /dev/myvg/mylv sudo mkdir /mnt/lvm sudo mount /dev/myvg/mylv /mnt/lvm ``` RAID Partitions Create software RAID for redundancy: ```bash Create RAID 1 array sudo mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sdb1 /dev/sdc1 Format RAID array sudo mkfs.ext4 /dev/md0 Save RAID configuration sudo mdadm --detail --scan >> /etc/mdadm.conf ``` Encrypted Partitions Encrypt sensitive data partitions: ```bash Create encrypted partition sudo cryptsetup luksFormat /dev/sdb1 Open encrypted partition sudo cryptsetup luksOpen /dev/sdb1 encrypted_data Format encrypted partition sudo mkfs.ext4 /dev/mapper/encrypted_data ``` Troubleshooting Common Issues Partition Table Corruption If you encounter partition table corruption: ```bash Attempt automatic repair sudo fsck /dev/sdb1 Use TestDisk for advanced recovery sudo apt install testdisk sudo testdisk ``` Device Busy Errors When getting "device busy" errors: ```bash Check what's using the device sudo lsof /dev/sdb1 sudo fuser -mv /dev/sdb1 Force unmount if necessary sudo umount -f /dev/sdb1 ``` Partition Alignment Issues Fix alignment problems: ```bash Check alignment with parted sudo parted /dev/sdb align-check optimal 1 Recreate partitions with proper alignment Start partitions at 1MiB (2048 sectors) for optimal alignment ``` Kernel Not Recognizing New Partitions Force kernel to re-read partition table: ```bash Re-read partition table sudo partprobe /dev/sdb Alternative method sudo hdparm -z /dev/sdb Check if partitions are recognized cat /proc/partitions ``` Best Practices for Linux Partitioning Planning Your Partition Scheme 1. Separate system and user data: Keep `/` and `/home` on different partitions 2. Dedicated swap partition: Create swap partition sized 1-2x your RAM 3. Boot partition: Consider separate `/boot` for complex setups 4. Log partition: Separate `/var/log` for servers to prevent log flooding Recommended Partition Sizes For a typical desktop system with a 500GB drive: - EFI System Partition: 512MB (if using UEFI) - Root (/): 50-100GB - Swap: 8-16GB (based on RAM) - Home (/home): Remaining space Security Considerations 1. Encrypt sensitive partitions: Use LUKS encryption for personal data 2. Set appropriate mount options: Use `noexec` for data partitions 3. Regular backups: Implement automated backup strategies 4. Monitor disk health: Use SMART monitoring tools Conclusion Creating partitions in Linux is a powerful skill that enables efficient storage management and system organization. Whether you prefer command-line tools like `fdisk` and `parted` or graphical interfaces like GParted, understanding the fundamentals of partitioning will help you optimize your Linux systems for better performance and reliability. Remember to always backup your data before making partition changes, plan your partition layout carefully, and choose the appropriate tools for your specific needs. With practice and careful attention to detail, you'll become proficient at managing Linux storage systems effectively. The key to successful partitioning lies in understanding your system's requirements, following best practices, and being prepared to troubleshoot common issues. Whether you're setting up a new server, organizing a desktop system, or managing storage in a virtualized environment, these partitioning techniques will serve you well in your Linux journey.