block-snapshot 7.5 KB

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