snapshot.txt 6.7 KB

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