snapshot.rst 7.6 KB

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