block-snapshot 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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=$(basename $0)
  73. t=${t#block-}
  74. case "$command" in
  75. add)
  76. case $t in
  77. snapshot|origin)
  78. p=$(xenstore_read_default "$XENBUS_PATH/params" 'MISSING')
  79. if [ "$p" == "MISSING" ]; then
  80. fatal "Missing device parameters ($t $XENBUS_PATH/params)"
  81. fi
  82. echo $p > "$HOTPLUG_STORE-params"
  83. echo $t > "$HOTPLUG_STORE-type"
  84. base=${p/:*/}
  85. cow=${p/*:/}
  86. if [ -L "$base" ]; then
  87. base=$(readlink -f "$base") || fatal "$base link does not exist."
  88. fi
  89. if [ -L "$cow" ]; then
  90. cow=$(readlink -f "$cow") || fatal "$cow link does not exist."
  91. fi
  92. # first ensure that snapshot device exists (to write somewhere changes from snapshot-origin)
  93. dm_devname=$(get_dm_snapshot_name "$base" "$cow")
  94. claim_lock "block"
  95. # prepare snapshot device
  96. create_dm_snapshot $dm_devname "$base" "$cow"
  97. if [ "$t" == "snapshot" ]; then
  98. #that's all for snapshot, store name of prepared device
  99. xenstore_write "$XENBUS_PATH/node" "/dev/mapper/$dm_devname"
  100. echo "/dev/mapper/$dm_devname" > "$HOTPLUG_STORE-node"
  101. write_dev /dev/mapper/$dm_devname
  102. elif [ "$t" == "origin" ]; then
  103. # for origin - prepare snapshot-origin device and store its name
  104. dm_devname=origin-$(stat -c '%D:%i' "$base")
  105. create_dm_snapshot_origin $dm_devname "$base"
  106. xenstore_write "$XENBUS_PATH/node" "/dev/mapper/$dm_devname"
  107. echo "/dev/mapper/$dm_devname" > "$HOTPLUG_STORE-node"
  108. write_dev /dev/mapper/$dm_devname
  109. fi
  110. # Save domain name for template commit on device remove
  111. domain=$(xenstore_read_default "$XENBUS_PATH/domain" '')
  112. if [ -z "$domain" ]; then
  113. domid=$(xenstore_read "$XENBUS_PATH/frontend-id")
  114. domain=$(xl domname $domid)
  115. fi
  116. echo $domain > "$HOTPLUG_STORE-domain"
  117. release_lock "block"
  118. exit 0
  119. ;;
  120. esac
  121. ;;
  122. prepare)
  123. t=$2
  124. case $t in
  125. snapshot|origin)
  126. p=$3
  127. base=${p/:*/}
  128. cow=${p/*:/}
  129. if [ -L "$base" ]; then
  130. base=$(readlink -f "$base") || fatal "$base link does not exist."
  131. fi
  132. if [ -L "$cow" ]; then
  133. cow=$(readlink -f "$cow") || fatal "$cow link does not exist."
  134. fi
  135. # first ensure that snapshot device exists (to write somewhere changes from snapshot-origin)
  136. dm_devname=$(get_dm_snapshot_name "$base" "$cow")
  137. claim_lock "block"
  138. # prepare snapshot device
  139. create_dm_snapshot $dm_devname "$base" "$cow"
  140. if [ "$t" == "snapshot" ]; then
  141. #that's all for snapshot, store name of prepared device
  142. echo "/dev/mapper/$dm_devname"
  143. elif [ "$t" == "origin" ]; then
  144. # for origin - prepare snapshot-origin device and store its name
  145. dm_devname=origin-$(stat -c '%D:%i' "$base")
  146. create_dm_snapshot_origin $dm_devname "$base"
  147. echo "/dev/mapper/$dm_devname"
  148. fi
  149. release_lock "block"
  150. exit 0
  151. ;;
  152. esac
  153. ;;
  154. remove|cleanup)
  155. if [ "$command" = "cleanup" ]; then
  156. t=$2
  157. else
  158. t=$(cat $HOTPLUG_STORE-type 2>/dev/null || echo 'MISSING')
  159. fi
  160. case "$t" in
  161. snapshot|origin)
  162. if [ "$command" = "cleanup" ]; then
  163. node=$3
  164. else
  165. node=$(cat "$HOTPLUG_STORE-node" 2> /dev/null)
  166. fi
  167. if [ -z "$node" ]; then
  168. #fatal "No device node to remove"
  169. #Most likely already removed
  170. exit 0
  171. fi
  172. if [ ! -e "$node" ]; then
  173. fatal "Device $node does not exists"
  174. fi
  175. claim_lock "block"
  176. use_count=$(dmsetup info $node|grep Open|awk '{print $3}')
  177. # do not remove snapshot if snapshot origin is still present
  178. if [ "${node/snapshot/}" != "$node" -a -e "/dev/mapper/origin-$(echo $node|cut -d- -f2)" ]; then
  179. use_count=1
  180. fi
  181. if [ "$use_count" -gt 0 ]; then
  182. log info "Device $node still in use - not removing"
  183. release_lock "block"
  184. exit 0
  185. fi
  186. # get list of used (loop) devices
  187. deps="$(dmsetup deps $node | cut -d: -f2 | sed -e 's#(7, \([0-9]\+\))#/dev/loop\1#g')"
  188. # if this is origin
  189. if [ "${node/origin/}" != "$node" ]; then
  190. # remove unused snapshots
  191. for snap in /dev/mapper/snapshot-$(echo $node|cut -d- -f2)-*; do
  192. use_count=$(dmsetup info $snap|grep Open|awk '{print $3}')
  193. if [ "$use_count" -eq 0 ]; then
  194. # unused snapshot - remove it
  195. deps="$deps $(dmsetup deps $snap | cut -d: -f2 | sed -e 's#(7, \([0-9]\+\))#/dev/loop\1#g')"
  196. log debug "Removing $snap"
  197. dmsetup remove $snap
  198. fi
  199. done
  200. if [ "$command" = "remove" ]; then
  201. # Commit template changes
  202. domain=$(cat "$HOTPLUG_STORE-domain")
  203. if [ "$domain" ]; then
  204. # Dont stop on errors
  205. /usr/bin/qvm-template-commit "$domain" || true
  206. fi
  207. fi
  208. fi
  209. if [ -e $node ]; then
  210. log debug "Removing $node"
  211. dmsetup remove $node
  212. fi
  213. # try to free loop devices
  214. for dev in $deps; do
  215. if [ -b "$dev" ]; then
  216. log debug "Removing $dev"
  217. losetup -d $dev 2> /dev/null || true
  218. fi
  219. done
  220. if [ -n "$HOTPLUG_STORE" ]; then
  221. rm $HOTPLUG_STORE-*
  222. fi
  223. release_lock "block"
  224. exit 0
  225. ;;
  226. esac
  227. ;;
  228. esac
  229. # vim:sw=2:et: