Mastering fdisk on Ubuntu: Partition Management Guide

Overview

Efficient storage administration on Ubuntu hinges on mastering foundational disk management utilities. This technical guide explores the essential capabilities of fdisk, covering everything from auditing block devices and inspecting low-level sector alignments to safely manipulating partition boundaries within an isolated virtual loopback environment.

Enumerating Storage Devices and Partition Schemes (-l Option)

Executing sudo fdisk -l inspects the host kernel’s detected storage topology, generating an inventory of connected physical and logical block devices along with their partition maps.
The generated report itemizes aggregate drive capacities, sector geometry, globally unique identifiers (GUIDs) or disk signatures, active slices, and configured filesystem identifiers.
⚙️ [Flag Reference]

-l : Dumps the partition layout for every detected storage unit or an explicitly referenced target node.
-u : Toggles coordinate display formatting between raw sectors and legacy cylinders.
-b [크기] : Explicitly overrides the reported hardware sector dimension in bytes (common values include 512, 1024, 2048, or 4096).
-s [파티션] : Queries the total capacity of a targeted partition slice in standard 1024-byte blocks.
-v : Displays current build and binary release information for the fdisk suite.

Inspecting full partition layout with sudo fdisk -l

The composite pipeline sudo fdisk -l /dev/sda 2>/dev/null || sudo fdisk -l /dev/vda 2>/dev/null || sudo fdisk -l /dev/nvme0n1 2>/dev/null cycles through conventional primary storage identifiers, selectively querying the first responsive drive node while suppressing stderr messages.
This fallback probe isolates the primary system volume regardless of whether the underlying hypervisor or bare-metal environment serves the root mount via legacy SATA interfaces (/dev/sda), paravirtualized VirtIO drivers (/dev/vda), or high-speed NVMe controllers (/dev/nvme0n1).

Querying specific primary block device

Querying Block Counts and Boundary Sectors (-s & -u Options)

Running ROOT_DISK=$(lsblk -ndo PKNAME $(df / | tail -1 | awk '{print $1}') 2>/dev/null || echo 'sda') programmatically resolves the parent disk controller housing the current root filesystem hierarchy and exports the identifier into a shell variable.
This automated query ensures script portability across diverse virtualization hypervisors and storage driver interfaces by dynamically targeting the active system drive.

Resolving root block storage parent identifier

The call sudo fdisk -s /dev/$ROOT_DISK 2>/dev/null || true computes the raw total capacity of the selected drive volume, expressing the metric directly in 1024-byte (1KB) block allocations.
Systems administrators rely on this single-line diagnostic to verify usable device capacity without parsing verbose partition mapping tables.

Checking overall block allocations

Invoking sudo fdisk -u=sectors -l /dev/$ROOT_DISK 2>/dev/null || true renders the partition table using discrete logical sectors for all boundary addresses and offsets.
Verifying these exact sector indexes allows engineers to ensure 4K/advanced format partition alignment, preventing the performance degradation often triggered by misaligned filesystem boundaries.

Reviewing sector-level boundary alignment

Constructing a Safe Loopback Disk Testing Sandbox

To avoid accidental data destruction on active drives, initialize an isolated operational sandbox by executing mkdir -p /tmp/fdisk_practice && cd /tmp/fdisk_practice.

Provisioning testing directory structure

Next, allocate a raw 100MB sparse storage image filled with zeroed bytes by running dd if=/dev/zero of=test_disk.img bs=1M count=100.

Generating raw virtual storage image

Attach the raw binary file to the next available system loopback interface with partition scanning enabled by invoking sudo losetup -fP test_disk.img.

Binding image to virtual loopback controller

Extract the dynamically assigned loopback device path and store it in an environment variable with LOOP_DEV=$(losetup -a | grep test_disk.img | head -n 1 | cut -d: -f1).

Extracting active loopback device path

Confirm that the loop device variable was successfully populated by evaluating echo "Target loop device: $LOOP_DEV".

Validating target loop device mapping

Running sudo fdisk -l $LOOP_DEV interrogates the loopback device to view its sector size and storage properties.
Because the underlying image was freshly initialized from zeroed blocks, the output confirms that no valid partition label or signature has been committed yet.

Checking loopback device partition status

Partitioning Storage via Interactive Menu Automation

Re-verify the targeted loopback node before initiating modifications by running LOOP_DEV=$(losetup -a | grep test_disk.img | head -n 1 | cut -d: -f1).

Re-querying loopback target

The command stream printf "n\np\n1\n\n+50M\np\nw\n" | sudo fdisk $LOOP_DEV pipes automated keystrokes straight into the interactive fdisk utility, carving out a 50MB primary partition, displaying the resulting layout, and committing the changes to disk.
This automated sequence initiates a new partition (n), selects a primary partition structure (p), designates index slot one (1), accepts default starting sector coordinates, allocates 50 megabytes (+50M), prints the in-memory partition table (p), and commits the updated partition map to the disk header before closing (w).

Automating partition slicing in interactive mode

Verify the newly configured partition layout by executing sudo fdisk -l $LOOP_DEV, confirming that the 50MB block slice is properly registered within the virtual partition table.

Inspecting committed 50MB partition layout

Tear down the virtual block binding and release kernel references by running sudo losetup -d $LOOP_DEV.

Detaching loopback device

Finally, purge the temporary test files and recover host storage by executing rm -rf /tmp/fdisk_practice.

Cleaning up scratch directory

This walkthrough demonstrated practical disk layout analysis and partition creation workflows using fdisk on Ubuntu.
Because low-level partitioning directly rewrites drive headers, always maintain verified backups before modifying production storage volumes.