Partitioning and formatting disks from the command line.

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
3,489
Reaction score
3,222
Credits
31,259

Using fdisk to Create a Partition Table in Linux​

Difference Between GPT and DOS Partition Tables​

GPT (GUID Partition Table):

  • Supports disks larger than 2 TB.
  • Allows for an almost unlimited number of partitions.
  • Stores multiple copies of the partition table for redundancy.
  • Uses CRC32 checksums for integrity verification
DOS (MBR - Master Boot Record):

  • Limited to 2 TB disk size.
  • Supports up to 4 primary partitions (or 3 primary and 1 extended partition).
  • No built-in redundancy or integrity checks.

Advantages of GPT Over DOS​

  • Supports Larger Disks: GPT can handle disks larger than 2 TB, whereas DOS is limited to 2 TB

  • More Partitions: GPT allows for a virtually unlimited number of partitions, compared to DOS's limit of 4 primary partitions

  • Redundancy: GPT stores multiple copies of the partition table, enhancing data recovery in case of corruption

  • Integrity: GPT uses CRC32 checksums to ensure the integrity of the partition table

Creating Partitions with fdisk​

Example 1: Single Partition on /dev/sdb​

  1. Open fdisk for the device:
    Code:
    sudo fdisk /dev/sdb

  2. Create a new partition table (GPT):
    Code:
    g

  3. Create a new partition:
    Code:
    n

  4. Write changes and exit:
    Code:
    w

Example 2: Three Partitions on /dev/nvme0n1​

  1. Open fdisk for the device:
    Code:
    sudo fdisk /dev/nvme0n1

  2. Create a new partition table (GPT):
    Code:
    g

  3. Create the first partition (50GB):
    Code:
    n
    • When prompted for the first sector, press Enter to accept the default.
    • When prompted for the last sector, type +50G and press Enter.
  4. Create the second partition (100GB):
    Code:
    n
    • When prompted for the first sector, press Enter to accept the default.
    • When prompted for the last sector, type +100G and press Enter.
  5. Create the third partition (rest of the disk):
    Code:
    n
    • When prompted for the first sector, press Enter to accept the default.
    • When prompted for the last sector, press Enter to use the remaining space.
  6. Write changes and exit:
    Code:
    w

Example 3: USB Thumb Drive on /dev/sdc​

  1. Open fdisk for the device:
    Code:
    sudo fdisk /dev/sdc

  2. Create a new partition table (GPT):
    Code:
    g

  3. Create a new partition:
    Code:
    n

  4. Write changes and exit:
    Code:
    w

Partition Types​

  • Linux Filesystem: 83
  • Linux Swap: 82
  • EFI System: EF
  • Microsoft Data: 07
  • Linux Root: 8304
  • Linux Data: 8300

Formatting Partitions​

Single Partition Example​

  1. Format the partition as ext4:
    Code:
    sudo mkfs.ext4 /dev/sdb1

Three Partitions Example​

  1. Format the first partition as ext4:
    Code:
    sudo mkfs.ext4 /dev/nvme0n1p1

  2. Format the second partition as xfs:
    Code:
    sudo mkfs.xfs /dev/nvme0n1p2

  3. Format the third partition as exfat:
    Code:
    sudo mkfs.exfat /dev/nvme0n1p3

USB Thumb Drive Example​

  1. Format the partition as exfat:
    Code:
    sudo mkfs.exfat /dev/sdc1

Disk Format​

Disk formatting involves preparing a storage device for use by an operating system, which includes creating a file system that organizes and manages files on the disk

Advantages of exFAT Over FAT32​

  • Larger File Size Support: exFAT supports files larger than 4 GB, unlike FAT32

  • Larger Partition Size: exFAT can handle partitions larger than 8 TB, whereas FAT32 is limited to 8 TB

Getting UUID of Each Partition​

  1. Use blkid to get the UUID:
    Code:
    sudo blkid

  2. Example output:
    Code:
    /dev/sdb1: UUID="123e4567-e89b-12d3-a456-426614174000" TYPE="ext4"
    /dev/nvme0n1p1: UUID="123e4567-e89b-12d3-a456-426614174001" TYPE="ext4"
    /dev/nvme0n1p2: UUID="123e4567-e89b-12d3-a456-426614174002" TYPE="xfs"
    /dev/nvme0n1p3: UUID="123e4567-e89b-12d3-a456-426614174003" TYPE="exfat"
    /dev/sdc1: UUID="123e4567-e89b-12d3-a456-426614174004" TYPE="exfat"

Using UUID in /etc/fstab to Automount Partitions​

  1. Open /etc/fstab in a text editor:
    Code:
    sudo nano /etc/fstab

  2. Add entries for each partition:
    Code:
    UUID=123e4567-e89b-12d3-a456-426614174000 /mnt ext4 defaults 0 0
    UUID=123e4567-e89b-12d3-a456-426614174001 /mnt/data1 ext4 defaults 0 0
    UUID=123e4567-e89b-12d3-a456-426614174002 /mnt/data2 xfs defaults 0 0
    UUID=123e4567-e89b-12d3-a456-426614174003 /mnt/data3 exfat defaults 0 0
    UUID=123e4567-e89b-12d3-a456-426614174004 /mnt/usb exfat defaults 0 0

  3. Each partition needs its own mount point and individual line in /etc/fstab.

I hope this guide helps you understand how to use fdisk to create partition tables, format partitions, and set up automounting using UUIDs in Linux!
 
Last edited:


True, but you can't do them from the command line.
 


Members online


Top