block.qubes 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/bin/bash
  2. HOTPLUG_STORE="/var/run/xen-hotplug/${XENBUS_PATH//\//-}"
  3. hd_arr[10]=a
  4. hd_arr[11]=b
  5. hd_arr[12]=c
  6. hd_arr[13]=d
  7. hd_arr[14]=e
  8. hd_arr[15]=f
  9. hexdigit()
  10. {
  11. if [ $1 -lt 10 ] ; then
  12. RET=$1
  13. else
  14. RET=${hd_arr[$1]}
  15. fi
  16. }
  17. hexnumber()
  18. {
  19. hexdigit $(($1/16))
  20. ret2=$RET
  21. hexdigit $(($1%16))
  22. HEXNUMBER="$ret2"$RET
  23. }
  24. process()
  25. {
  26. if ! [ "x""$1" = "xfile" ] ; then
  27. exec flock /var/run/qubes/hotplug-block /etc/xen/scripts/block $ORIG_ARGS
  28. fi
  29. while true ; do
  30. dev=$(losetup -f --show $2)
  31. if [ -n "$dev" ] ; then break ; fi
  32. done
  33. hexnumber ${dev:9:70}
  34. xenstore-write "$XENBUS_PATH/node" "$dev" \
  35. "$XENBUS_PATH/physical-device" "7:"$HEXNUMBER \
  36. "$XENBUS_PATH/hotplug-status" connected
  37. echo "$dev" > "$HOTPLUG_STORE-node"
  38. echo "file" > "$HOTPLUG_STORE-type"
  39. }
  40. #exec 2>>/tmp/block.$$
  41. #set -x
  42. export PATH="/sbin:/bin:/usr/bin:/usr/sbin:$PATH"
  43. XENBUS_PATH="${XENBUS_PATH:?}"
  44. if ! [ "$1" = "add" ] || ! [ -f /var/run/qubes/fast-block-attach ] ; then
  45. script=$(xenstore-read "$XENBUS_PATH/script")
  46. exec flock /var/run/qubes/hotplug-block $script "$@"
  47. fi
  48. ORIG_ARGS="$@"
  49. vars=$(xenstore-read "$XENBUS_PATH/type" "$XENBUS_PATH/params")
  50. process $vars
  51. exit 0