Linux addpart Tutorial: Add Partitions Without Rebooting

Overview

In enterprise Linux environments, repartitioning block storage often raises operational concerns regarding unexpected system reboots or service outages. The low-level addpart utility solves this challenge by letting system administrators directly inform the running Linux kernel of new partition layouts on the fly. This practical tutorial walks through creating a virtual loopback disk, injecting partition definitions straight into the kernel routing table, validating device nodes, and tearing down sandbox resources safely.

1. Setting Up a Virtual Loop Device Sandbox

Begin by provisioning the underlying user-space toolchains, notably util-linux and coreutils, which house standard partition inspection and manipulation binaries.

Updating package index and installing required partition utilities

To avoid interfering with existing disks, isolate the entire experiment within a dedicated scratch directory by running mkdir -p /tmp/addpart_lab && cd /tmp/addpart_lab.

Creating and entering temporary lab directory

Next, allocate a raw 100MB sparse zero-filled container file with dd if=/dev/zero of=test_disk.img bs=1M count=100 status=none, simulating an unformatted block storage unit.

Allocating raw 100MB virtual disk image file

Expose this flat image to the subsystem as an active virtual block device via losetup -fP test_disk.img, scanning for partition boundaries while attaching to the first free slot.

Binding virtual image to an available loop device node

Inspect attached virtual drives using losetup -a | grep test_disk.img to discover the dynamically assigned path, in this case /dev/loop1.

Verifying loop device assignment

2. Registering Partition Tables into the Kernel via addpart

Capture the assigned loop device node automatically by parsing the loop setup metadata and storing the path in a shell variable with LOOP_DEV=$(losetup -j test_disk.img | cut -d: -f1).

Extracting loop device node path into environment variable

Confirm the value held by the $LOOP_DEV variable using echo "Target device: $LOOP_DEV", which reports that /dev/loop1 is primed for live partition injection.

Checking target device variable contents

Now invoke addpart to signal kernel storage subsystems directly without touching on-disk partition tables like MBR or GPT headers.
By passing the target device $LOOP_DEV, partition number 1, starting offset 2048, and a length of 50000 sectors, the kernel immediately allocates a fresh partition entry.

Notifying kernel of partition 1 geometry via addpart

Query the kernel’s block layout with lsblk $LOOP_DEV.
The output verifies that /dev/loop1 immediately enumerates the newly recognized child device node loop1p1, sized at approximately 24.4MB.

Inspecting block hierarchy with lsblk

3. Expanding Partition Sets and Validating System Recognition

Refresh the shell environment variable mapping using LOOP_DEV=$(losetup -j test_disk.img | cut -d: -f1) to maintain an unbroken target context for subsequent steps.

Refreshing target loop device path

Append another non-overlapping partition region to the same block device by running addpart a second time.
Supplying $LOOP_DEV, index 2, start boundary 52048, and size 50000 registers the second slice into memory instantly—bypassing any traditional reboot cycles.

Adding second partition with custom sector bounds

Confirm that system-level device tables register both slices by reading the kernel’s real-time partition registry with cat /proc/partitions | grep $(basename $LOOP_DEV).
Both child partitions, loop1p1 and loop1p2, are fully cataloged alongside their respective block allocations.

Verifying registered partition slices in procfs

For a granular breakdown of sector boundaries and sector counts, run partx --show $LOOP_DEV.
This utility details exact partition indices, start and end coordinates, and total capacity acknowledged by current driver layers.

Displaying detailed sector layout with partx

4. De-registering Slices and Tearing Down Test Infrastructure

Before unbinding virtual hardware, re-verify the active device string with LOOP_DEV=$(losetup -j /tmp/addpart_lab/test_disk.img | cut -d: -f1).

Re-evaluating target loop node prior to teardown

Instruct the kernel to drop the first logical slice using delpart $LOOP_DEV 1, cleanly detaching partition index 1 from runtime tables.

Removing partition 1 with delpart

Execute delpart $LOOP_DEV 2 to purge the remaining second partition reference from the kernel.

Removing partition 2 from active kernel tracking

Release the underlying loop attachment and return the loopback device back to the pool via losetup -d $LOOP_DEV.

Detaching loop device binding

Finally, wipe away the scratch directory along with the disk image using rm -rf /tmp/addpart_lab.

Purging temporary lab workspace and image files

Mastering addpart grants systems administrators precise, on-the-fly control over storage partitioning without service interruptions or reboots. Incorporate these low-level kernel helpers into automation scripts whenever dynamic storage allocation is required in production environments.