block-snapshot 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. HOTPLUG_STORE="/var/run/xen-hotplug/${XENBUS_PATH//\//-}"
  14. get_dev() {
  15. dev=$1
  16. if [ -L "$dev" ]; then
  17. dev=$(readlink -f "$dev") || fatal "$dev link does not exist."
  18. fi
  19. if [ -f "$dev" ]; then
  20. file=$dev
  21. inode=$(stat -c '%i' "$file")
  22. devnum=$(stat -c '%D' "$file")
  23. if [ -z "$inode" ] || [ -z "$devnum" ]
  24. then
  25. release_lock "block"
  26. fatal "Unable to lookup $file: dev: $devnum inode: $inode"
  27. fi
  28. dev_list=$(losetup -a | grep ' \[0*'${devnum}'\]:'${inode} | cut -d : -f 1)
  29. for loopdev in $dev_list; do
  30. # found existing loop to this file
  31. echo $loopdev
  32. return
  33. done
  34. # assign new loop device
  35. loopdev=$(losetup -f 2>/dev/null || find_free_loopback_dev)
  36. if [ "$loopdev" = '' ]
  37. then
  38. release_lock "block"
  39. fatal 'Failed to find an unused loop device'
  40. fi
  41. do_or_die losetup "$loopdev" "$file"
  42. echo $loopdev
  43. else
  44. test -e "$dev" || fatal "$dev does not exist."
  45. test -b "$dev" || fatal "$dev is not a block device nor file."
  46. fi
  47. }
  48. get_dm_snapshot_name() {
  49. base=$1
  50. cow=$2
  51. echo snapshot-$(stat -c '%D:%i' "$base")-$(stat -c '%D:%i' "$cow")
  52. }
  53. create_dm_snapshot() {
  54. local base_dev cow_dev base_sz
  55. dm_devname=$1
  56. base=$2
  57. cow=$3
  58. if [ ! -e /dev/mapper/$dm_devname ]; then
  59. # prepare new snapshot device
  60. base_dev=$(get_dev $base)
  61. cow_dev=$(get_dev $cow)
  62. base_sz=$(blockdev --getsz $base_dev)
  63. do_or_die dmsetup create $dm_devname --table "0 $base_sz snapshot $base_dev $cow_dev P 256"
  64. fi
  65. }
  66. create_dm_snapshot_origin() {
  67. local base_dev base_sz
  68. dm_devname=$1
  69. base=$2
  70. if [ ! -e /dev/mapper/$dm_devname ]; then
  71. # prepare new snapshot-origin device
  72. base_dev=$(get_dev $base)
  73. base_sz=$(blockdev --getsz $base_dev)
  74. do_or_die dmsetup create $dm_devname --table "0 $base_sz snapshot-origin $base_dev"
  75. fi
  76. }
  77. t=$(xenstore_read_default "$XENBUS_PATH/type" 'MISSING')
  78. case "$command" in
  79. add)
  80. case $t in
  81. snapshot|origin)
  82. p=$(xenstore_read "$XENBUS_PATH/params")
  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)
  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")
  165. fi
  166. if [ -z "$node" ]; then
  167. fatal "No device node to remove"
  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" -a -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 || true 2> /dev/null
  215. fi
  216. done
  217. release_lock "block"
  218. exit 0
  219. ;;
  220. esac
  221. ;;
  222. esac
  223. # vim:sw=2:et: