1Device-mapper snapshot support 2============================== 3 4Device-mapper allows you, without massive data copying: 5 6*) To create snapshots of any block device i.e. mountable, saved states of 7the block device which are also writable without interfering with the 8original content; 9*) To create device "forks", i.e. multiple different versions of the 10same data stream. 11*) To merge a snapshot of a block device back into the snapshot's origin 12device. 13 14In the first two cases, dm copies only the chunks of data that get 15changed and uses a separate copy-on-write (COW) block device for 16storage. 17 18For snapshot merge the contents of the COW storage are merged back into 19the origin device. 20 21 22There are three dm targets available: 23snapshot, snapshot-origin, and snapshot-merge. 24 25*) snapshot-origin <origin> 26 27which will normally have one or more snapshots based on it. 28Reads will be mapped directly to the backing device. For each write, the 29original data will be saved in the <COW device> of each snapshot to keep 30its visible content unchanged, at least until the <COW device> fills up. 31 32 33*) snapshot <origin> <COW device> <persistent?> <chunksize> 34 35A snapshot of the <origin> block device is created. Changed chunks of 36<chunksize> sectors will be stored on the <COW device>. Writes will 37only go to the <COW device>. Reads will come from the <COW device> or 38from <origin> for unchanged data. <COW device> will often be 39smaller than the origin and if it fills up the snapshot will become 40useless and be disabled, returning errors. So it is important to monitor 41the amount of free space and expand the <COW device> before it fills up. 42 43<persistent?> is P (Persistent) or N (Not persistent - will not survive 44after reboot). O (Overflow) can be added as a persistent store option 45to allow userspace to advertise its support for seeing "Overflow" in the 46snapshot status. So supported store types are "P", "PO" and "N". 47 48The difference between persistent and transient is with transient 49snapshots less metadata must be saved on disk - they can be kept in 50memory by the kernel. 51 52When loading or unloading the snapshot target, the corresponding 53snapshot-origin or snapshot-merge target must be suspended. A failure to 54suspend the origin target could result in data corruption. 55 56 57* snapshot-merge <origin> <COW device> <persistent> <chunksize> 58 59takes the same table arguments as the snapshot target except it only 60works with persistent snapshots. This target assumes the role of the 61"snapshot-origin" target and must not be loaded if the "snapshot-origin" 62is still present for <origin>. 63 64Creates a merging snapshot that takes control of the changed chunks 65stored in the <COW device> of an existing snapshot, through a handover 66procedure, and merges these chunks back into the <origin>. Once merging 67has started (in the background) the <origin> may be opened and the merge 68will continue while I/O is flowing to it. Changes to the <origin> are 69deferred until the merging snapshot's corresponding chunk(s) have been 70merged. Once merging has started the snapshot device, associated with 71the "snapshot" target, will return -EIO when accessed. 72 73 74How snapshot is used by LVM2 75============================ 76When you create the first LVM2 snapshot of a volume, four dm devices are used: 77 781) a device containing the original mapping table of the source volume; 792) a device used as the <COW device>; 803) a "snapshot" device, combining #1 and #2, which is the visible snapshot 81 volume; 824) the "original" volume (which uses the device number used by the original 83 source volume), whose table is replaced by a "snapshot-origin" mapping 84 from device #1. 85 86A fixed naming scheme is used, so with the following commands: 87 88lvcreate -L 1G -n base volumeGroup 89lvcreate -L 100M --snapshot -n snap volumeGroup/base 90 91we'll have this situation (with volumes in above order): 92 93# dmsetup table|grep volumeGroup 94 95volumeGroup-base-real: 0 2097152 linear 8:19 384 96volumeGroup-snap-cow: 0 204800 linear 8:19 2097536 97volumeGroup-snap: 0 2097152 snapshot 254:11 254:12 P 16 98volumeGroup-base: 0 2097152 snapshot-origin 254:11 99 100# ls -lL /dev/mapper/volumeGroup-* 101brw------- 1 root root 254, 11 29 ago 18:15 /dev/mapper/volumeGroup-base-real 102brw------- 1 root root 254, 12 29 ago 18:15 /dev/mapper/volumeGroup-snap-cow 103brw------- 1 root root 254, 13 29 ago 18:15 /dev/mapper/volumeGroup-snap 104brw------- 1 root root 254, 10 29 ago 18:14 /dev/mapper/volumeGroup-base 105 106 107How snapshot-merge is used by LVM2 108================================== 109A merging snapshot assumes the role of the "snapshot-origin" while 110merging. As such the "snapshot-origin" is replaced with 111"snapshot-merge". The "-real" device is not changed and the "-cow" 112device is renamed to <origin name>-cow to aid LVM2's cleanup of the 113merging snapshot after it completes. The "snapshot" that hands over its 114COW device to the "snapshot-merge" is deactivated (unless using lvchange 115--refresh); but if it is left active it will simply return I/O errors. 116 117A snapshot will merge into its origin with the following command: 118 119lvconvert --merge volumeGroup/snap 120 121we'll now have this situation: 122 123# dmsetup table|grep volumeGroup 124 125volumeGroup-base-real: 0 2097152 linear 8:19 384 126volumeGroup-base-cow: 0 204800 linear 8:19 2097536 127volumeGroup-base: 0 2097152 snapshot-merge 254:11 254:12 P 16 128 129# ls -lL /dev/mapper/volumeGroup-* 130brw------- 1 root root 254, 11 29 ago 18:15 /dev/mapper/volumeGroup-base-real 131brw------- 1 root root 254, 12 29 ago 18:16 /dev/mapper/volumeGroup-base-cow 132brw------- 1 root root 254, 10 29 ago 18:16 /dev/mapper/volumeGroup-base 133 134 135How to determine when a merging is complete 136=========================================== 137The snapshot-merge and snapshot status lines end with: 138 <sectors_allocated>/<total_sectors> <metadata_sectors> 139 140Both <sectors_allocated> and <total_sectors> include both data and metadata. 141During merging, the number of sectors allocated gets smaller and 142smaller. Merging has finished when the number of sectors holding data 143is zero, in other words <sectors_allocated> == <metadata_sectors>. 144 145Here is a practical example (using a hybrid of lvm and dmsetup commands): 146 147# lvs 148 LV VG Attr LSize Origin Snap% Move Log Copy% Convert 149 base volumeGroup owi-a- 4.00g 150 snap volumeGroup swi-a- 1.00g base 18.97 151 152# dmsetup status volumeGroup-snap 1530 8388608 snapshot 397896/2097152 1560 154 ^^^^ metadata sectors 155 156# lvconvert --merge -b volumeGroup/snap 157 Merging of volume snap started. 158 159# lvs volumeGroup/snap 160 LV VG Attr LSize Origin Snap% Move Log Copy% Convert 161 base volumeGroup Owi-a- 4.00g 17.23 162 163# dmsetup status volumeGroup-base 1640 8388608 snapshot-merge 281688/2097152 1104 165 166# dmsetup status volumeGroup-base 1670 8388608 snapshot-merge 180480/2097152 712 168 169# dmsetup status volumeGroup-base 1700 8388608 snapshot-merge 16/2097152 16 171 172Merging has finished. 173 174# lvs 175 LV VG Attr LSize Origin Snap% Move Log Copy% Convert 176 base volumeGroup owi-a- 4.00g 177