The Linux Device Mapper framework provides the low-level foundation for modern enterprise storage technologies, including Logical Volume Manager (LVM2), LUKS full-disk encryption (dm-crypt), hardware multipathing (dm-multipath), software RAID, and thin-provisioned snapshots. While high-level tools like lvcreate or cryptsetup automate everyday storage provisioning, system engineers frequently encounter degraded arrays, locked device nodes, or corrupted mapping tables where direct interaction with the kernel Device Mapper driver is required. The dmsetup utility provides direct command-line control over device mapper tables, active targets, suspend/resume states, and low-level block ioctl interfaces.
Understanding Device Mapper Targets and Mapping Tables
In the Linux kernel, the Device Mapper operates by translating I/O requests directed at a virtual block device (e.g. /dev/mapper/target-name) into physical block offsets on underlying disks. Each mapped device is defined by a mapping table composed of one or more contiguous segments:
logical_start_sector num_sectors target_type target_arguments
Common device mapper target types include:
linear: Maps a contiguous range of logical sectors directly to a physical block device (the basis of standard LVM volumes).striped: Distributes I/O across multiple physical block devices using RAID-0 chunk striping.crypt: Transparently encrypts and decrypts sectors using kernel crypto API ciphers (LUKS).snapshotandsnapshot-origin: Manages copy-on-write differential blocks for point-in-time volume images.thinandthin-pool: Provides over-committed, dynamic space allocation.zero: Discards write requests and returns zeroes on read operations (similar to/dev/zerofor block devices).
Auditing Active Mapped Devices and Targets
To inspect all currently active Device Mapper block devices on your Linux system, execute:
sudo dmsetup ls
To view detailed status including major/minor numbers, open reference counts, and target types:
sudo dmsetup info -c
The column Open ref cnt indicates how many active processes, filesystems, or higher-level mapper devices currently hold the block device open. A device cannot be removed while its reference count exceeds zero.
To audit which kernel target modules are currently compiled or loaded in memory:
sudo dmsetup targets
The output reports each target type (e.g. linear v1.4.0, striped v1.6.0, crypt v1.24.0) and its module version.
Inspecting Detailed Mapping Tables and Status
To view the exact sector layout of an active device mapper device, use dmsetup table:
sudo dmsetup table /dev/mapper/ubuntu--vg-ubuntu--lv
A typical output for a linear LVM logical volume displays:
0 209715200 linear 8:2 2048
This entry indicates that 209,715,200 sectors (100 GiB) starting at logical sector 0 map linearly to major/minor block device 8:2 (e.g. /dev/sda2) starting at physical sector 2048.
To query dynamic runtime status (such as thin-pool metadata usage, snapshot fill percentages, or error counts):
sudo dmsetup status /dev/mapper/vg0-pool0-tpool
Creating a Custom Mapped Block Device
You can create custom block devices directly through dmsetup without LVM. For instance, creating a fast zero-sink device for benchmarking:
# Define a 10 GiB (20971520 sectors of 512 bytes) zero device
echo "0 20971520 zero" | sudo dmsetup create benchmark-zero
# Verify device creation in /dev/mapper/
ls -la /dev/mapper/benchmark-zero
# Test reading zeroes at high speed
sudo dd if=/dev/mapper/benchmark-zero of=/dev/null bs=1M count=1000 status=progress
To create a linear device combining two separate disks into a single continuous 20 GiB block volume:
# Table syntax: [start_sector] [length] [target] [underlying_device] [device_offset]
sudo dmsetup create combined-disk << 'EOF'
0 20971520 linear /dev/sdb 0
20971520 20971520 linear /dev/sdc 0
EOF
Suspending, Reloading, and Resuming Block Devices
One of the most powerful features of the Device Mapper architecture is the ability to alter live mapping tables without unmounting active filesystems. This mechanism powers live LVM volume expansion and storage migration.
To modify or expand a device mapper table live:
- Suspend I/O: Freeze pending transactions to ensure filesystem consistency:
sudo dmsetup suspend /dev/mapper/combined-disk - Load New Table: Inject the updated sector table into the inactive slot:
echo "0 41943040 linear /dev/sdb 0" | sudo dmsetup reload /dev/mapper/combined-disk - Resume I/O: Reactivate the device mapper pipeline with the new geometry:
sudo dmsetup resume /dev/mapper/combined-disk
Removing Stuck and Orphaned Mapper Devices
During storage migrations or hard drive detachments, device nodes can become locked in the mapper table with stale state. To cleanly teardown a device:
# Remove a specific device
sudo dmsetup remove /dev/mapper/benchmark-zero
# Force removal if unmount fails due to stale reference locks
sudo dmsetup remove -f /dev/mapper/benchmark-zero
# Clean up all unused devices (devices with 0 open reference counts)
sudo dmsetup remove_all --deferred
Directly managing Device Mapper targets through dmsetup unlocks essential low-level recovery mechanisms, enabling administrators to diagnose storage bottlenecks, reconstruct degraded block volumes, and recover critical enterprise infrastructure.