blob: ad6949bff2e392d63e7ddb82391746687ea6235e [file] [log] [blame]
Paolo 'Blaisorblade' Giarrussoe4845852005-09-22 21:44:29 -07001Device-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.
Mikulas Patockad698aa42009-12-10 23:52:30 +000011*) 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.
Paolo 'Blaisorblade' Giarrussoe4845852005-09-22 21:44:29 -070020
21
Mikulas Patockad698aa42009-12-10 23:52:30 +000022There are three dm targets available:
23snapshot, snapshot-origin, and snapshot-merge.
Paolo 'Blaisorblade' Giarrussoe4845852005-09-22 21:44:29 -070024
25*) snapshot-origin <origin>
26
27which will normally have one or more snapshots based on it.
Paolo 'Blaisorblade' Giarrussoe4845852005-09-22 21:44:29 -070028Reads 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
Paolo 'Blaisorblade' Giarrusso411f1142005-11-07 01:01:01 -080035A snapshot of the <origin> block device is created. Changed chunks of
Paolo 'Blaisorblade' Giarrussoe4845852005-09-22 21:44:29 -070036<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
Mike Snitzerb0d3cc02015-10-08 18:05:41 -040044after 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.
Paolo 'Blaisorblade' Giarrussoe4845852005-09-22 21:44:29 -070051
52
Mikulas Patockad698aa42009-12-10 23:52:30 +000053* snapshot-merge <origin> <COW device> <persistent> <chunksize>
54
55takes the same table arguments as the snapshot target except it only
56works with persistent snapshots. This target assumes the role of the
57"snapshot-origin" target and must not be loaded if the "snapshot-origin"
58is still present for <origin>.
59
60Creates a merging snapshot that takes control of the changed chunks
61stored in the <COW device> of an existing snapshot, through a handover
62procedure, and merges these chunks back into the <origin>. Once merging
63has started (in the background) the <origin> may be opened and the merge
64will continue while I/O is flowing to it. Changes to the <origin> are
65deferred until the merging snapshot's corresponding chunk(s) have been
66merged. Once merging has started the snapshot device, associated with
67the "snapshot" target, will return -EIO when accessed.
68
69
70How snapshot is used by LVM2
71============================
Paolo 'Blaisorblade' Giarrussoe4845852005-09-22 21:44:29 -070072When you create the first LVM2 snapshot of a volume, four dm devices are used:
73
741) a device containing the original mapping table of the source volume;
752) a device used as the <COW device>;
763) a "snapshot" device, combining #1 and #2, which is the visible snapshot
77 volume;
784) the "original" volume (which uses the device number used by the original
79 source volume), whose table is replaced by a "snapshot-origin" mapping
80 from device #1.
81
82A fixed naming scheme is used, so with the following commands:
83
84lvcreate -L 1G -n base volumeGroup
85lvcreate -L 100M --snapshot -n snap volumeGroup/base
86
87we'll have this situation (with volumes in above order):
88
89# dmsetup table|grep volumeGroup
90
91volumeGroup-base-real: 0 2097152 linear 8:19 384
92volumeGroup-snap-cow: 0 204800 linear 8:19 2097536
93volumeGroup-snap: 0 2097152 snapshot 254:11 254:12 P 16
94volumeGroup-base: 0 2097152 snapshot-origin 254:11
95
96# ls -lL /dev/mapper/volumeGroup-*
97brw------- 1 root root 254, 11 29 ago 18:15 /dev/mapper/volumeGroup-base-real
98brw------- 1 root root 254, 12 29 ago 18:15 /dev/mapper/volumeGroup-snap-cow
99brw------- 1 root root 254, 13 29 ago 18:15 /dev/mapper/volumeGroup-snap
100brw------- 1 root root 254, 10 29 ago 18:14 /dev/mapper/volumeGroup-base
101
Mikulas Patockad698aa42009-12-10 23:52:30 +0000102
103How snapshot-merge is used by LVM2
104==================================
105A merging snapshot assumes the role of the "snapshot-origin" while
106merging. As such the "snapshot-origin" is replaced with
107"snapshot-merge". The "-real" device is not changed and the "-cow"
108device is renamed to <origin name>-cow to aid LVM2's cleanup of the
109merging snapshot after it completes. The "snapshot" that hands over its
110COW device to the "snapshot-merge" is deactivated (unless using lvchange
111--refresh); but if it is left active it will simply return I/O errors.
112
113A snapshot will merge into its origin with the following command:
114
115lvconvert --merge volumeGroup/snap
116
117we'll now have this situation:
118
119# dmsetup table|grep volumeGroup
120
121volumeGroup-base-real: 0 2097152 linear 8:19 384
122volumeGroup-base-cow: 0 204800 linear 8:19 2097536
123volumeGroup-base: 0 2097152 snapshot-merge 254:11 254:12 P 16
124
125# ls -lL /dev/mapper/volumeGroup-*
126brw------- 1 root root 254, 11 29 ago 18:15 /dev/mapper/volumeGroup-base-real
127brw------- 1 root root 254, 12 29 ago 18:16 /dev/mapper/volumeGroup-base-cow
128brw------- 1 root root 254, 10 29 ago 18:16 /dev/mapper/volumeGroup-base
Mike Snitzerc53a3812010-03-06 02:29:56 +0000129
130
131How to determine when a merging is complete
132===========================================
133The snapshot-merge and snapshot status lines end with:
134 <sectors_allocated>/<total_sectors> <metadata_sectors>
135
136Both <sectors_allocated> and <total_sectors> include both data and metadata.
137During merging, the number of sectors allocated gets smaller and
138smaller. Merging has finished when the number of sectors holding data
139is zero, in other words <sectors_allocated> == <metadata_sectors>.
140
141Here is a practical example (using a hybrid of lvm and dmsetup commands):
142
143# lvs
144 LV VG Attr LSize Origin Snap% Move Log Copy% Convert
145 base volumeGroup owi-a- 4.00g
146 snap volumeGroup swi-a- 1.00g base 18.97
147
148# dmsetup status volumeGroup-snap
1490 8388608 snapshot 397896/2097152 1560
150 ^^^^ metadata sectors
151
152# lvconvert --merge -b volumeGroup/snap
153 Merging of volume snap started.
154
155# lvs volumeGroup/snap
156 LV VG Attr LSize Origin Snap% Move Log Copy% Convert
157 base volumeGroup Owi-a- 4.00g 17.23
158
159# dmsetup status volumeGroup-base
1600 8388608 snapshot-merge 281688/2097152 1104
161
162# dmsetup status volumeGroup-base
1630 8388608 snapshot-merge 180480/2097152 712
164
165# dmsetup status volumeGroup-base
1660 8388608 snapshot-merge 16/2097152 16
167
168Merging has finished.
169
170# lvs
171 LV VG Attr LSize Origin Snap% Move Log Copy% Convert
172 base volumeGroup owi-a- 4.00g