block_remove 859 B

1234567891011121314151617181920212223242526272829303132
  1. #!/bin/sh
  2. NAME=${DEVNAME#/dev/}
  3. XS_KEY="qubes-block-devices/$NAME"
  4. xenstore-rm "$XS_KEY"
  5. # If device was connected to some VM - detach it
  6. # Notice: this can be run also in VM, so we cannot use xl...
  7. device_detach() {
  8. xs_path=$1
  9. xenstore-write $xs_path/online 0 $xs_path/state 5
  10. # Wait for backend to finish dev shutdown
  11. try=30
  12. # -lt will break loop also when 'state' will be empty
  13. while [ "`xenstore-read $xs_path/state 2> /dev/null`" -lt 6 ]; do
  14. try=$[ $try - 1 ]
  15. [ "$try" -le 0 ] && break
  16. sleep 0.1
  17. done
  18. xenstore-rm $xs_path
  19. }
  20. for XS_DEV_PATH in `xenstore-ls -f backend/vbd | grep 'backend/vbd/[0-9]*/[0-9]* ' | cut -f 1 -d ' '`; do
  21. CUR_DEVICE=`xenstore-read "$XS_DEV_PATH/params"`
  22. if [ "$CUR_DEVICE" == "$DEVNAME" ]; then
  23. device_detach "$XS_DEV_PATH"
  24. exit 0
  25. fi
  26. done