Safely Deactivate LVM Trees with blkdeactivate

blkdeactivate stops supported LVM, device-mapper, MD RAID or VDO devices in dependency order. You are not left guessing which holder to unwind first. The guide uses the dmsetup package installed on this machine, version 2:1.02.185-3ubuntu3.2. Allow 10 to 20 minutes for inspection and a controlled deactivation.

You need root privileges for the state-changing command and a maintenance window if the devices support services. Warning: deactivation makes a block device unavailable to applications. It is not a safe way to remove a mounted filesystem, and it does not copy or preserve data for you. Confirm the device tree and stop its users before proceeding.

1. Confirm the installed tool and its boundary

First verify that the command is the one supplied by the local package. It is a shell script that calls tools such as lsblk, dmsetup, lvm and mdadm according to the device type.

$ command -v blkdeactivate
/usr/sbin/blkdeactivate
$ dpkg-query -W -f='${Package} ${Version}\n' dmsetup
dmsetup 2:1.02.185-3ubuntu3.2
$ blkdeactivate --help
blkdeactivate: Utility to deactivate block devices

  blkdeactivate [options] [device...]
    - Deactivate block device tree.
      If devices are specified, deactivate only supplied devices and their holders.

The installed command has no useful --version output, so use the package query above when recording what was tested. The manual page is for LVM tools 2.03.16(2), dated 18 May 2022, while the package version identifies the locally installed release.

Checkpoint: You have confirmed /usr/sbin/blkdeactivate, the package version, and the syntax that accepts an optional device argument.

2. Inspect the devices without changing state

Use lsblk to map names, types, mountpoints and holders. This is an ordinary read-only command, so it normally does not need sudo.

$ lsblk -o NAME,KNAME,TYPE,SIZE,FSTYPE,MOUNTPOINTS,PKNAME,HOTPLUG
NAME                 KNAME TYPE  SIZE FSTYPE MOUNTPOINTS PKNAME HOTPLUG
nvme0n1              nvme0n1 disk  100G                         0
├─nvme0n1p2          nvme0n1p2 part  98G                         nvme0n1 0
│ └─vg0-root         dm-0  lvm    40G ext4   /             0
...

Your output will be different. The useful questions are: which logical volume or mapped device is the target, which devices hold it, and whether anything is mounted? With no device argument the script scans every supported block device; with a device argument it processes only that device tree and its holders, holders first.

Do not copy the example name /dev/vg0/root unless it exists on your host: pick an exact path from your own lsblk output. If the target is mounted, identify the application using the mountpoint and stop it through your normal service procedure before continuing.

3. Choose the default operation or explicit unmounting

By default a mounted device is skipped. This is the safer default: running blkdeactivate without -u never asks the tool to unmount filesystems, so its summary can report skipped devices while leaving them active.

$ sudo blkdeactivate /dev/vg0/root
Deactivating block devices:
  [SKIP]: unmount of /dev/mapper/vg0-root (dm-0) mounted on /srv/data

Treat a skip as a stop signal, not success. Check the state afterwards:

$ findmnt --source /dev/vg0/root
TARGET    SOURCE             FSTYPE OPTIONS
/srv/data /dev/mapper/vg0-root ext4   rw,relatime
$ lsblk -o NAME,TYPE,MOUNTPOINTS /dev/vg0/root

If you have confirmed that the mount can be taken offline, -u tells blkdeactivate to attempt the unmount before deactivation:

$ sudo blkdeactivate -u /dev/vg0/root
Deactivating block devices:
  [UMOUNT]: unmounting /dev/mapper/vg0-root (dm-0) mounted on /srv/data... done
  [LVM]: deactivating Logical Volume vg0/root... done

Warning: this is a service-disrupting action. The script deliberately skips several system mountpoints, including /, /usr, /var, /bin and /sbin, and it may also skip a mount that remains busy. Do not force a busy filesystem offline just to make the summary say done.

4. Deactivate one known tree

Use a device argument when you have a specific maintenance target. This example targets an LVM logical volume, unmounts it first, and lets the tool process its holders before the logical volume itself.

$ sudo blkdeactivate -u /dev/vg0/root
Deactivating block devices:
  [LVM]: deactivating Logical Volume vg0/root... done
$ lsblk -o NAME,TYPE,MOUNTPOINTS /dev/vg0/root
lsblk: /dev/vg0/root: not a block device

The final message is illustrative: after a successful LVM deactivation, a later inspection may no longer find the logical volume as an active block device. Verify with the LVM tools instead:

$ sudo lvs -o vg_name,lv_name,lv_attr,lv_active vg0/root
  VG  LV   Attr       Active
  vg0 root -wi------- inactive

Column spacing and the precise attribute string vary by LVM version; the check that matters is that the selected logical volume is inactive. If the command skipped a holder, deal with that dependency first and rerun only afterwards.

5. Add retries only when the failure is transient

$ sudo blkdeactivate -u -d retry -l retry /dev/vg0/root

None of these options repair a process that still has the device open, a filesystem that is still mounted, or a broken storage path. Find and stop the user first.

Warning: do not start with -d force. It requests forced device-mapper removal and can turn an unresolved dependency into a more disruptive failure. Use it only with a documented recovery plan and an explicit understanding of what owns the mapped device.

6. Expose the real tool errors when something is skipped

Without -e, errors from called tools are suppressed and the script usually just prints skipping. Add -e to see those diagnostics. -v enables verbose mode and also implies -e; -vv enables even more shell tracing.

$ sudo blkdeactivate -e -u /dev/vg0/root
Deactivating block devices:
  [LVM]: deactivating Logical Volume vg0/root... skipping
  device-mapper: remove ioctl on vg0-root failed: Device or resource busy

The exact error belongs to the underlying tool. Check open users with your normal host procedures, then repeat the read-only inspection:

$ findmnt --source /dev/vg0/root
$ sudo lsof /dev/mapper/vg0-root
$ sudo dmsetup info /dev/mapper/vg0-root

Do not treat an empty lsof result as proof that force removal is safe: device-mapper dependencies, kernel users and remote storage paths can still matter. If a deactivation partly succeeds, stop and record which entries reported done or skipping before attempting recovery.

7. Recover or restore access

blkdeactivate has no matching activate command; recovery depends on the device type. For an LVM logical volume, use the LVM activation command after confirming the volume group and storage are available:

$ sudo lvchange -ay vg0/root
$ sudo lvs -o vg_name,lv_name,lv_active vg0/root
  VG  LV   Active
  vg0 root active

Mount the filesystem only after checking it is the correct volume and the mountpoint is ready:

$ sudo mount /dev/vg0/root /srv/data
$ findmnt --source /dev/vg0/root
TARGET    SOURCE             FSTYPE OPTIONS
/srv/data /dev/mapper/vg0-root ext4   rw,relatime

For MD RAID, multipath and VDO devices, use the corresponding local tool and its documented activation procedure. Do not improvise a recovery command from the output alone. If the original service was stopped, restart it only after the device is active and mounted where that service expects it.

Done means