block-snapshot 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. echo "$dev"
  42. fi
  43. }
  44. get_dm_snapshot_name() {
  45. base=$1
  46. cow=$2
  47. echo snapshot-$(stat -c '%D:%i' "$base")-$(stat -c '%D:%i' "$cow")
  48. }
  49. create_dm_snapshot() {
  50. local base_dev cow_dev base_sz
  51. dm_devname=$1
  52. base=$2
  53. cow=$3
  54. if [ ! -e /dev/mapper/$dm_devname ]; then
  55. # prepare new snapshot device
  56. base_dev=$(get_dev $base)
  57. cow_dev=$(get_dev $cow)
  58. base_sz=$(blockdev --getsz $base_dev)
  59. do_or_die dmsetup create $dm_devname --table "0 $base_sz snapshot $base_dev $cow_dev P 256"
  60. fi
  61. }
  62. create_dm_snapshot_origin() {
  63. local base_dev base_sz
  64. dm_devname=$1
  65. base=$2
  66. if [ ! -e /dev/mapper/$dm_devname ]; then
  67. # prepare new snapshot-origin device
  68. base_dev=$(get_dev $base)
  69. base_sz=$(blockdev --getsz $base_dev)
  70. do_or_die dmsetup create $dm_devname --table "0 $base_sz snapshot-origin $base_dev"
  71. fi
  72. }
  73. t=$(basename $0)
  74. t=${t#block-}
  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. echo $p > "$HOTPLUG_STORE-params"
  84. echo $t > "$HOTPLUG_STORE-type"
  85. base=${p/:*/}
  86. cow=${p/*:/}
  87. if [ -L "$base" ]; then
  88. base=$(readlink -f "$base") || fatal "$base link does not exist."
  89. fi
  90. if [ -L "$cow" ]; then
  91. cow=$(readlink -f "$cow") || fatal "$cow link does not exist."
  92. fi
  93. # first ensure that snapshot device exists (to write somewhere changes from snapshot-origin)
  94. dm_devname=$(get_dm_snapshot_name "$base" "$cow")
  95. claim_lock "block"
  96. # prepare snapshot device
  97. create_dm_snapshot $dm_devname "$base" "$cow"
  98. if [ "$t" == "snapshot" ]; then
  99. #that's all for snapshot, store name of prepared device
  100. xenstore_write "$XENBUS_PATH/node" "/dev/mapper/$dm_devname"
  101. echo "/dev/mapper/$dm_devname" > "$HOTPLUG_STORE-node"
  102. write_dev /dev/mapper/$dm_devname
  103. elif [ "$t" == "origin" ]; then
  104. # for origin - prepare snapshot-origin device and store its name
  105. dm_devname=origin-$(stat -c '%D:%i' "$base")
  106. create_dm_snapshot_origin $dm_devname "$base"
  107. xenstore_write "$XENBUS_PATH/node" "/dev/mapper/$dm_devname"
  108. echo "/dev/mapper/$dm_devname" > "$HOTPLUG_STORE-node"
  109. write_dev /dev/mapper/$dm_devname
  110. fi
  111. # Save domain name for template commit on device remove
  112. domid=$(xenstore_read "$XENBUS_PATH/frontend-id")
  113. # Store name of target domain in case of stubdom
  114. domid=$(xenstore_read_default "/local/domain/$domid/target" "$domid")
  115. domain=$(xl domname $domid)
  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 2>/dev/null|grep Open|awk '{print $3}')
  177. if [ -z "$use_count" ]; then
  178. log info "Device $node already removed"
  179. release_lock "block"
  180. exit 0
  181. fi
  182. # do not remove snapshot if snapshot origin is still present
  183. if [ "${node/snapshot/}" != "$node" -a -e "/dev/mapper/origin-$(echo $node|cut -d- -f2)" ]; then
  184. use_count=1
  185. fi
  186. if [ "$use_count" -gt 0 ]; then
  187. log info "Device $node still in use - not removing"
  188. release_lock "block"
  189. exit 0
  190. fi
  191. # get list of used (loop) devices
  192. deps="$(dmsetup deps $node | cut -d: -f2 | sed -e 's#(7, \([0-9]\+\))#/dev/loop\1#g')"
  193. # if this is origin
  194. if [ "${node/origin/}" != "$node" ]; then
  195. # remove unused snapshots
  196. for snap in /dev/mapper/snapshot-$(echo $node|cut -d- -f2)-*; do
  197. use_count=$(dmsetup info $snap|grep Open|awk '{print $3}')
  198. if [ "$use_count" -eq 0 ]; then
  199. # unused snapshot - remove it
  200. deps="$deps $(dmsetup deps $snap | cut -d: -f2 | sed -e 's#(7, \([0-9]\+\))#/dev/loop\1#g')"
  201. log debug "Removing $snap"
  202. dmsetup remove $snap
  203. fi
  204. done
  205. if [ "$command" = "remove" ]; then
  206. # Commit template changes
  207. domain=$(cat "$HOTPLUG_STORE-domain")
  208. if [ "$domain" ]; then
  209. # Dont stop on errors
  210. if [ -r /var/lib/qubes/qubes-test.xml -a \
  211. "${domain#test-}" != "$domain" ]; then
  212. export QUBES_XML_PATH=/var/lib/qubes/qubes-test.xml
  213. fi
  214. /usr/bin/qvm-template-commit --offline-mode "$domain" || true
  215. fi
  216. fi
  217. fi
  218. if [ -e $node ]; then
  219. log debug "Removing $node"
  220. dmsetup remove $node
  221. fi
  222. # try to free loop devices
  223. for dev in $deps; do
  224. if [ -b "$dev" ]; then
  225. log debug "Removing $dev"
  226. losetup -d $dev 2> /dev/null || true
  227. fi
  228. done
  229. if [ -n "$HOTPLUG_STORE" ]; then
  230. rm $HOTPLUG_STORE-*
  231. fi
  232. release_lock "block"
  233. exit 0
  234. ;;
  235. esac
  236. ;;
  237. esac
  238. # vim:sw=2:et: