fdisk

The fdisk command is a powerful utility in Linux used to manage disk partitions. It provides an interface to create, delete, resize, and manage disk partitions on a hard drive or other storage device. Below is a comprehensive guide to using fdisk.

Basic Usage

To start fdisk on a specific disk, you use the following command:

sudo fdisk /dev/sdX

Replace /dev/sdX with the appropriate disk identifier (e.g., /dev/sda, /dev/nvme0n1). This command opens an interactive session with fdisk where you can manage partitions.

Common Commands Within fdisk

Once you’re inside fdisk, several commands allow you to perform different operations. Here are some of the most commonly used commands:

  • m: Display the help menu.
  • p: Print the partition table of the disk, showing details like partition numbers, types, start and end sectors, and sizes.
  • n: Create a new partition.
  • d: Delete an existing partition.
  • t: Change the type (system ID) of a partition.
  • l: List known partition types.
  • w: Write the changes to the disk and exit.
  • q: Quit without saving changes.
  • v: Verify the disk.

Step-by-Step Guide to Using fdisk

1. Starting fdisk

To manage partitions on a disk (e.g., /dev/sda), start fdisk with:

sudo fdisk /dev/sda

2. View the Current Partition Table

To see the current partition layout, press p:

Command (m for help): p

This will display the partition table, including details like partition number, start and end sectors, size, and partition type.

3. Creating a New Partition

To create a new partition:

  1. Press n to create a new partition.
   Command (m for help): n
  1. Select the partition type. You will typically choose between primary or extended. You can create up to four primary partitions, or three primary and one extended partition.
   Partition type:
    p   primary (1 primary, 0 extended, 3 free)
    e   extended
   Select (default p): p
  1. Choose the partition number (1-4 for primary partitions).
  2. Select the starting sector. Press Enter to accept the default (which usually aligns with sector boundaries).
  3. Enter the size of the partition. You can specify it in kilobytes (K), megabytes (M), gigabytes (G), or terabytes (T). For example:
   Last sector, +sectors or +size{K,M,G,T,P} (4096-2097151, default 2097151): +20G
  1. The partition is now defined but not yet written to the disk.

4. Changing a Partition’s Type

To change the type (or system ID) of a partition:

  1. Press t.
   Command (m for help): t
  1. Enter the partition number you want to change.
  2. Enter the hex code of the desired partition type. Press l to list all possible types. For example, to set a Linux LVM partition type, enter 8e.

5. Deleting a Partition

To delete an existing partition:

  1. Press d.
   Command (m for help): d
  1. Enter the number of the partition you want to delete.
  2. The partition is marked for deletion but is not yet removed from the disk.

6. Writing Changes to Disk

After making changes, you must write them to the disk:

  1. Press w to write the changes to disk and exit.
   Command (m for help): w
  1. The disk’s partition table is updated, and fdisk exits.

7. Quitting Without Saving Changes

If you want to quit fdisk without saving any changes:

  1. Press q.
   Command (m for help): q
  1. This exits fdisk without writing any changes.

Example Workflow

Let’s say you want to create a new 20GB primary partition on /dev/sda.

  1. Start fdisk:
   sudo fdisk /dev/sda
  1. Create a new partition:
  • Press n to create a new partition.
  • Choose p for a primary partition.
  • Accept the default partition number (e.g., 2 if this is your second partition).
  • Accept the default first sector.
  • Type +20G to specify the size.
  1. Optionally, change the partition type by pressing t.
  2. Write the changes and exit by pressing w.

Verifying and Using the New Partition

After creating the partition:

  1. Update the Kernel Partition Table: Sometimes, you need to notify the kernel of changes:
   sudo partprobe
  1. Format the Partition: You may want to format the new partition before using it:
   sudo mkfs.ext4 /dev/sda2
  1. Mount the Partition: You can then mount the partition to make it available in the filesystem:
   sudo mount /dev/sda2 /mnt/newpartition

Summary

  • fdisk is an interactive tool for managing disk partitions in Linux.
  • View the current partition table with p.
  • Create new partitions with n.
  • Delete existing partitions with d.
  • Change partition types with t.
  • Write changes with w or quit without saving with q.

fdisk is a powerful utility, but it directly manipulates disk partitions, so always be cautious and double-check commands to avoid data loss.