block-snapshot 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. . "$dir/block-common.sh"
  7. get_dev() {
  8. dev=$1
  9. if [ -L "$dev" ]; then
  10. dev=$(readlink -f "$dev") || fatal "$dev link does not exist."
  11. fi
  12. if [ -f "$dev" ]; then
  13. file=$dev
  14. inode=$(stat -c '%i' "$file")
  15. devnum=$(stat -c '%D' "$file")
  16. if [ -z "$inode" ] || [ -z "$devnum" ]
  17. then
  18. release_lock "block"
  19. fatal "Unable to lookup $file: dev: $devnum inode: $inode"
  20. fi
  21. dev_list=$(losetup -a | grep ' \[0*'${devnum}'\]:'${inode} | cut -d : -f 1)
  22. for loopdev in $dev_list; do
  23. # found existing loop to this file
  24. echo $loopdev
  25. return
  26. done
  27. # assign new loop device
  28. loopdev=$(losetup -f 2>/dev/null || find_free_loopback_dev)
  29. if [ "$loopdev" = '' ]
  30. then
  31. release_lock "block"
  32. fatal 'Failed to find an unused loop device'
  33. fi
  34. do_or_die losetup "$loopdev" "$file"
  35. echo $loopdev
  36. else
  37. test -e "$dev" || fatal "$dev does not exist."
  38. test -b "$dev" || fatal "$dev is not a block device nor file."
  39. fi
  40. }
  41. get_dm_snapshot_name() {
  42. base=$1
  43. cow=$2
  44. echo snapshot-$(stat -c '%D:%i' "$base")-$(stat -c '%D:%i' "$cow")
  45. }
  46. create_dm_snapshot() {
  47. local base_dev cow_dev base_sz
  48. dm_devname=$1
  49. base=$2
  50. cow=$3
  51. if [ ! -e /dev/mapper/$dm_devname ]; then
  52. # prepare new snapshot device
  53. base_dev=$(get_dev $base)
  54. cow_dev=$(get_dev $cow)
  55. base_sz=$(blockdev --getsz $base_dev)
  56. do_or_die dmsetup create $dm_devname --table "0 $base_sz snapshot $base_dev $cow_dev P 256"
  57. fi
  58. }
  59. create_dm_snapshot_origin() {
  60. local base_dev base_sz
  61. dm_devname=$1
  62. base=$2
  63. if [ ! -e /dev/mapper/$dm_devname ]; then
  64. # prepare new snapshot-origin device
  65. base_dev=$(get_dev $base)
  66. base_sz=$(blockdev --getsz $base_dev)
  67. do_or_die dmsetup create $dm_devname --table "0 $base_sz snapshot-origin $base_dev"
  68. fi
  69. }
  70. t=$(xenstore_read_default "$XENBUS_PATH/type" 'MISSING')
  71. case "$command" in
  72. add)
  73. case $t in
  74. snapshot|origin)
  75. p=$(xenstore_read "$XENBUS_PATH/params")
  76. base=${p/:*/}
  77. cow=${p/*:/}
  78. if [ -L "$base" ]; then
  79. base=$(readlink -f "$base") || fatal "$base link does not exist."
  80. fi
  81. if [ -L "$cow" ]; then
  82. cow=$(readlink -f "$cow") || fatal "$cow link does not exist."
  83. fi
  84. # first ensure that snapshot device exists (to write somewhere changes from snapshot-origin)
  85. dm_devname=$(get_dm_snapshot_name "$base" "$cow")
  86. claim_lock "block"
  87. # prepare snapshot device
  88. create_dm_snapshot $dm_devname "$base" "$cow"
  89. if [ "$t" == "snapshot" ]; then
  90. #that's all for snapshot, store name of prepared device
  91. xenstore_write "$XENBUS_PATH/node" "/dev/mapper/$dm_devname"
  92. write_dev /dev/mapper/$dm_devname
  93. elif [ "$t" == "origin" ]; then
  94. # for origin - prepare snapshot-origin device and store its name
  95. dm_devname=origin-$(stat -c '%D:%i' "$base")
  96. create_dm_snapshot_origin $dm_devname "$base"
  97. xenstore_write "$XENBUS_PATH/node" "/dev/mapper/$dm_devname"
  98. write_dev /dev/mapper/$dm_devname
  99. fi
  100. release_lock "block"
  101. exit 0
  102. ;;
  103. esac
  104. ;;
  105. remove)
  106. case $t in
  107. snapshot|origin)
  108. node=$(xenstore_read "$XENBUS_PATH/node")
  109. if [ -z "$node" ]; then
  110. fatal "No device node to remove"
  111. fi
  112. if [ ! -e "$node" ]; then
  113. fatal "Device $node does not exists"
  114. fi
  115. claim_lock "block"
  116. use_count=$(dmsetup info $node|grep Open|awk '{print $3}')
  117. # do not remove snapshot if snapshot origin is still present
  118. if [ "${node/snapshot/}" != "$node" -a -e "/dev/mapper/origin-$(echo $node|cut -d- -f2)" ]; then
  119. use_count=1
  120. fi
  121. if [ "$use_count" -gt 0 ]; then
  122. log info "Device $node still in use - not removing"
  123. release_lock "block"
  124. exit 0
  125. fi
  126. # get list of used (loop) devices
  127. deps="$(dmsetup deps $node | cut -d: -f2 | sed -e 's#(7, \([0-9]\+\))#/dev/loop\1#g')"
  128. # if this is origin
  129. if [ "${node/origin/}" != "$node" ]; then
  130. # remove unused snapshots
  131. for snap in /dev/mapper/snapshot-$(echo $node|cut -d- -f2)-*; do
  132. use_count=$(dmsetup info $snap|grep Open|awk '{print $3}')
  133. if [ "$use_count" -eq 0 ]; then
  134. # unused snapshot - remove it
  135. deps="$deps $(dmsetup deps $snap | cut -d: -f2 | sed -e 's#(7, \([0-9]\+\))#/dev/loop\1#g')"
  136. log debug "Removing $snap"
  137. dmsetup remove $snap
  138. fi
  139. done
  140. fi
  141. if [ -e $node ]; then
  142. log debug "Removing $node"
  143. dmsetup remove $node
  144. fi
  145. # try to free loop devices
  146. for dev in $deps; do
  147. if [ -b "$dev" ]; then
  148. log debug "Removing $dev"
  149. losetup -d $dev || true 2> /dev/null
  150. fi
  151. done
  152. release_lock "block"
  153. exit 0
  154. ;;
  155. esac
  156. ;;
  157. esac
  158. # vim:sw=2:et: