Skip to content

Instantly share code, notes, and snippets.

@jowagner
Last active January 16, 2023 14:32
Show Gist options
  • Save jowagner/b36024636140ddf453c12eaf6e590b5d to your computer and use it in GitHub Desktop.
Save jowagner/b36024636140ddf453c12eaf6e590b5d to your computer and use it in GitHub Desktop.
Make snapshot of non-busy block device with device mapper in Linux
NAME=nameofsnapshot
DEVRO=/dev/read-only-backing-device
DEVCOW=/dev/space-for-writing-changes
SECTORS=$(blockdev --getsz $DEVRO)
TABLE=$(mktemp)
echo 0 $SECTORS snapshot $DEVRO $DEVCOW N 16 >> $TABLE
echo "Setting up $NAME with"
head $TABLE
dmsetup create $NAME < $TABLE
rm $TABLE
@jowagner
Copy link
Author

jowagner commented Nov 4, 2021

N = not persistent, will not survive reboot
16 = chunk size in sectors for CoW, here 8 KiB

Query status: dmsetup status $NAME
Remove snapshot: dmsetup remove $NAME

The backing device must not be "busy". Testing confirms this includes "mounted" but may also include other uses such as being an active PV of LVM, opened with cryptsetup or a MD RAID member.

References:

@hwk
Copy link

hwk commented Dec 19, 2022

Hi,
I tried this recipe (and many more from the net) but I get always the same error:
"device-mapper: reload ioctl on snap-sdb1 (253:0) failed: Device or resource busy
Command failed."
and "dmsetup status" says "No devices found". This is on a debian11 VirtualBox PC.
As always: any help appreciated, Hans

@jowagner
Copy link
Author

Thanks for reporting the issue. Trying it with and without mounting the backing device I get the same error message when the device is mounted, suggesting that dmsetup create cannot use mounted block devices. "Busy" probably also applies to other scenarios such as the backing device being part of an active software RAID. Will update the description.

@hwk
Copy link

hwk commented Dec 22, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment