block-snapshot 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #!/bin/bash
  2. # Usage: block-snapshot add|remove img-file cow-file
  3. #
  4. # This creates dm-snapshot device on given arguments
  5. dir=$(dirname "$0")
  6. if [ "$1" = "prepare" ] || [ "$1" = "cleanup" ]; then
  7. . "$dir/xen-hotplug-common.sh"
  8. command=$1
  9. else
  10. . "$dir/block-common.sh"
  11. fi
  12. shopt -s nullglob
  13. if [ -n "$XENBUS_PATH" ]; then
  14. HOTPLUG_STORE="/var/run/xen-hotplug/${XENBUS_PATH//\//-}"
  15. fi
  16. get_dev() {
  17. dev=$1
  18. if [ -L "$dev" ]; then
  19. dev=$(readlink -f "$dev") || fatal "$dev link does not exist."
  20. fi
  21. if [ -f "$dev" ]; then
  22. file=$dev
  23. loopdev=$(losetup -j "$file" | head -1 | cut -d : -f 1)
  24. if [ -n "$loopdev" ]; then
  25. # found existing loop to this file
  26. echo "$loopdev"
  27. return
  28. fi
  29. # assign new loop device
  30. loopdev=$(losetup -f 2>/dev/null || find_free_loopback_dev)
  31. if [ "$loopdev" = '' ]
  32. then
  33. release_lock "block"
  34. fatal 'Failed to find an unused loop device'
  35. fi
  36. do_or_die losetup "$loopdev" "$file"
  37. echo "$loopdev"
  38. else
  39. test -e "$dev" || fatal "$dev does not exist."
  40. test -b "$dev" || fatal "$dev is not a block device nor file."
  41. fi
  42. }
  43. get_dm_snapshot_name() {
  44. base=$1
  45. cow=$2
  46. echo "snapshot-$(stat -c '%D:%i' "$base")-$(stat -c '%D:%i' "$cow")"
  47. }
  48. create_dm_snapshot() {
  49. local base_dev cow_dev base_sz
  50. dm_devname=$1
  51. base=$2
  52. cow=$3
  53. if [ ! -e "/dev/mapper/$dm_devname" ]; then
  54. # prepare new snapshot device
  55. base_dev=$(get_dev "$base")
  56. cow_dev=$(get_dev "$cow")
  57. base_sz=$(blockdev --getsz "$base_dev")
  58. do_or_die dmsetup create "$dm_devname" --table "0 $base_sz snapshot $base_dev $cow_dev P 256"
  59. fi
  60. }
  61. create_dm_snapshot_origin() {
  62. local base_dev base_sz
  63. dm_devname=$1
  64. base=$2
  65. if [ ! -e "/dev/mapper/$dm_devname" ]; then
  66. # prepare new snapshot-origin device
  67. base_dev=$(get_dev "$base")
  68. base_sz=$(blockdev --getsz "$base_dev")
  69. do_or_die dmsetup create "$dm_devname" --table "0 $base_sz snapshot-origin $base_dev"
  70. fi
  71. }
  72. t=$(xenstore_read_default "$XENBUS_PATH/type" 'MISSING')
  73. case "$command" in
  74. add)
  75. case $t in
  76. snapshot|origin)
  77. p=$(xenstore_read_default "$XENBUS_PATH/params" 'MISSING')
  78. if [ "$p" == "MISSING" ]; then
  79. fatal "Missing device parameters ($t $XENBUS_PATH/params)"
  80. fi
  81. base=${p/:*/}
  82. cow=${p/*:/}
  83. if [ -L "$base" ]; then
  84. base=$(readlink -f "$base") || fatal "$base link does not exist."
  85. fi
  86. if [ -L "$cow" ]; then
  87. cow=$(readlink -f "$cow") || fatal "$cow link does not exist."
  88. fi
  89. # first ensure that snapshot device exists (to write somewhere changes from snapshot-origin)
  90. dm_devname=$(get_dm_snapshot_name "$base" "$cow")
  91. claim_lock "block"
  92. # prepare snapshot device
  93. create_dm_snapshot "$dm_devname" "$base" "$cow"
  94. if [ "$t" == "snapshot" ]; then
  95. #that's all for snapshot, store name of prepared device
  96. xenstore_write "$XENBUS_PATH/node" "/dev/mapper/$dm_devname"
  97. echo "/dev/mapper/$dm_devname" > "$HOTPLUG_STORE-node"
  98. write_dev "/dev/mapper/$dm_devname"
  99. elif [ "$t" == "origin" ]; then
  100. # for origin - prepare snapshot-origin device and store its name
  101. dm_devname=origin-$(stat -c '%D:%i' "$base")
  102. create_dm_snapshot_origin "$dm_devname" "$base"
  103. xenstore_write "$XENBUS_PATH/node" "/dev/mapper/$dm_devname"
  104. echo "/dev/mapper/$dm_devname" > "$HOTPLUG_STORE-node"
  105. write_dev "/dev/mapper/$dm_devname"
  106. fi
  107. # Save domain name for template commit on device remove
  108. domain=$(xenstore_read_default "$XENBUS_PATH/domain" '')
  109. if [ -z "$domain" ]; then
  110. domid=$(xenstore_read "$XENBUS_PATH/frontend-id")
  111. domain=$(xl domname "$domid")
  112. fi
  113. echo "$domain" > "$HOTPLUG_STORE-domain"
  114. release_lock "block"
  115. exit 0
  116. ;;
  117. esac
  118. ;;
  119. prepare)
  120. t=$2
  121. case $t in
  122. snapshot|origin)
  123. p=$3
  124. base=${p/:*/}
  125. cow=${p/*:/}
  126. if [ -L "$base" ]; then
  127. base=$(readlink -f "$base") || fatal "$base link does not exist."
  128. fi
  129. if [ -L "$cow" ]; then
  130. cow=$(readlink -f "$cow") || fatal "$cow link does not exist."
  131. fi
  132. # first ensure that snapshot device exists (to write somewhere changes from snapshot-origin)
  133. dm_devname=$(get_dm_snapshot_name "$base" "$cow")
  134. claim_lock "block"
  135. # prepare snapshot device
  136. create_dm_snapshot "$dm_devname" "$base" "$cow"
  137. if [ "$t" == "snapshot" ]; then
  138. #that's all for snapshot, store name of prepared device
  139. echo "/dev/mapper/$dm_devname"
  140. elif [ "$t" == "origin" ]; then
  141. # for origin - prepare snapshot-origin device and store its name
  142. dm_devname=origin-$(stat -c '%D:%i' "$base")
  143. create_dm_snapshot_origin "$dm_devname" "$base"
  144. echo "/dev/mapper/$dm_devname"
  145. fi
  146. release_lock "block"
  147. exit 0
  148. ;;
  149. esac
  150. ;;
  151. remove|cleanup)
  152. if [ "$command" = "cleanup" ]; then
  153. t=$2
  154. else
  155. t=$(cat "$HOTPLUG_STORE-type" 2>/dev/null || echo 'MISSING')
  156. fi
  157. case "$t" in
  158. snapshot|origin)
  159. if [ "$command" = "cleanup" ]; then
  160. node=$3
  161. else
  162. node=$(cat "$HOTPLUG_STORE-node" 2> /dev/null)
  163. fi
  164. if [ -z "$node" ]; then
  165. #fatal "No device node to remove"
  166. #Most likely already removed
  167. exit 0
  168. fi
  169. if [ ! -e "$node" ]; then
  170. fatal "Device $node does not exists"
  171. fi
  172. claim_lock "block"
  173. use_count=$(dmsetup info "$node"|grep Open|awk '{print $3}')
  174. # do not remove snapshot if snapshot origin is still present
  175. if [ "${node/snapshot/}" != "$node" ] && [ -e "/dev/mapper/origin-$(echo "$node"|cut -d- -f2)" ]; then
  176. use_count=1
  177. fi
  178. if [ "$use_count" -gt 0 ]; then
  179. log info "Device $node still in use - not removing"
  180. release_lock "block"
  181. exit 0
  182. fi
  183. # get list of used (loop) devices
  184. deps="$(dmsetup deps "$node" | cut -d: -f2 | sed -e 's#(7, \([0-9]\+\))#/dev/loop\1#g')"
  185. # if this is origin
  186. if [ "${node/origin/}" != "$node" ]; then
  187. # remove unused snapshots
  188. for snap in /dev/mapper/snapshot-$(echo "$node"|cut -d- -f2)-*; do
  189. use_count=$(dmsetup info "$snap"|grep Open|awk '{print $3}')
  190. if [ "$use_count" -eq 0 ]; then
  191. # unused snapshot - remove it
  192. deps="$deps $(dmsetup deps "$snap" | cut -d: -f2 | sed -e 's#(7, \([0-9]\+\))#/dev/loop\1#g')"
  193. log debug "Removing $snap"
  194. dmsetup remove "$snap"
  195. fi
  196. done
  197. if [ "$command" = "remove" ]; then
  198. # Commit template changes
  199. domain=$(cat "$HOTPLUG_STORE-domain")
  200. if [ "$domain" ]; then
  201. # Dont stop on errors
  202. /usr/bin/qvm-template-commit "$domain" || true
  203. fi
  204. fi
  205. fi
  206. if [ -e "$node" ]; then
  207. log debug "Removing $node"
  208. dmsetup remove "$node"
  209. fi
  210. # try to free loop devices
  211. for dev in $deps; do
  212. if [ -b "$dev" ]; then
  213. log debug "Removing $dev"
  214. losetup -d "$dev" 2> /dev/null || true
  215. fi
  216. done
  217. if [ -n "$HOTPLUG_STORE" ]; then
  218. rm "$HOTPLUG_STORE-"*
  219. fi
  220. release_lock "block"
  221. exit 0
  222. ;;
  223. esac
  224. ;;
  225. esac
  226. # vim:sw=2:et: