[dm-devel] Using dmsetup to create a snapshot...

Kevin Corry kevcorry at us.ibm.com
Thu Jul 22 15:30:47 UTC 2004

On Wednesday 21 July 2004 7:57 am, Manu Tayal wrote:
> Hi,
> I have installed device-mapper.1.00.18 on kernel 2.4.26. Now I want to
> create a snapshot using "dmsetup". I could not get hold of enough
> documentation to do the same. Can somebody tell me the steps which are
> required to do the same.
> My Setup: I have two hard disks on my system and I am using the second hard
> disk to experiment with device mapper. What I was trying to do was create a
> volume and install etx3 on the same. Then create a snapshot of it, mount
> the snapshot and view the same. I was able to achieve this via EVMS but not
> with dmsetup.
> Any pointers to good documentation would also help.

At this point, there isn't any written documentation about DM's snapshot 
implementation. I had hoped to write some to go into the 
Documentation/device-mapper/ directory in the kernel tree, but I got 
distracted with other work. I'm sure I'll get around to it eventually. For 
now I'd recommend looking through drivers/md/dm-snap.[ch] and 
drivers/md/dm-exception-store.c in the kernel source.

As for using dmsetup to create snapshots, I can't say I'd recommend that 
approach. The activation sequence is very tricky. To simply create a DM 
snapshot device, you'd do something like:

echo "0 <device_size_in_sectors> snapshot <origin_device> <cow_device> <p|n> \ 
<chunk_size_in_sectors> | dmsetup create <new_snapshot_name>

But, that's only part of the story. You also need a snapshot-origin device to 
actually monitor the I/Os to the origin volume and perform the 
copy-on-writes, which you create with:

echo "0 <device_size_in_sectors> snapshot-origin <origin_device>" \
| dmsetup create <new_origin_name>

Where <origin_device> is the same for both the snapshot and the origin. But, 
this isn't *really* what you want, either. Because for this to work, you 
would have you unmount the <origin_device> device and mount the 
<new_origin_name> device, so that I/O will actually go through the 
snapshot-origin device and be subject to the COW monitoring. But unmounting 
your origin volume and remounting through a different device is, of course, 
undesireable. You'd rather just allow the origin volume to remain mounted 
while you create the snapshot, since the whole point of snapshotting is to 
make backups of a live volume.

In order to do this properly, your origin volume must also be a DM device. For 
example, a simple dm-linear or dm-stripe device, like the ones created by 
EVMS or LVM2. You can obviously also create such a device with dmsetup. For 
this example let's say you have a dm-linear device named "my_data", which was 
created with dmsetup and is mounted using the device /dev/mapper/my_data, and 
let's also say that this device has the mapping "0 12345 linear /dev/hdc1 0", 
which means the device is 12345 sectors long and maps to /dev/hdc1 with no 

To create a proper snapshot of my_data while the device is mounted and in use, 
you need to do the following:

1. Suspend my_data to temporarily stop any I/O while the snapshot is being 
   dmsetup suspend my_data

2. Create the snapshot-origin device with no table.
   dmsetup create my_data_org

3. Read the table from my_data and load it into my_data_org.
   dmsetup table my_data | dmsetup load my_data_org

4. Resume this new table.
   dmsetup resume my_data_org

5. Create the snapshot device with no table.
   dmsetup create my_data_snap

6. Load the table into my_data_snap. This uses /dev/hdd1 as the COW device and 
uses a 32kB chunk-size.
   echo "0 `blockdev --getsize /dev/mapper/my_data` snapshot \
    /dev/mapper/my_data_org /dev/hdd1 p 64" | dmsetup load my_data_snap

7. Reload my_data as a snapshot-origin device that points to my_data_org.
   echo "0 `blockdev --getsize /dev/mapper/my_data` snapshot-origin \
    /dev/mapper/my_data_org" | dmsetup load my_data

8. Resume the snapshot and origin devices.
   dmsetup resume my_data_snap
   dmsetup resume my_data

With this setup, a filesystem mounted on /dev/mapper/my_data will not need to 
be remounted on a different device, but will now go through the 
snapshot-origin device and be subject to the COW monitoring. You can then 
mount /dev/mapper/my_data_snap to view the snapshot device and make your 
backup. I'll leave it as an exercise to the reader to figure out how to 
properly tear all this down. And if I've made any slight errors in this 
sequence, hopefully Alasdair will correct me. :)

If you're simply interested in using snapshots to do data backups, just stick 
with EVMS or LVM2 and save yourself a lot of headache. :)

Kevin Corry
kevcorry at us.ibm.com

More information about the dm-devel mailing list