block_add_change 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/bin/bash
  2. NAME=${DEVNAME#/dev/}
  3. DESC="${ID_MODEL} (${ID_FS_LABEL})"
  4. SIZE=$[ $(cat /sys/$DEVPATH/size) * 512 ]
  5. MODE=w
  6. XS_KEY="qubes-block-devices/$NAME"
  7. xs_remove() {
  8. if [ "$QUBES_EXPOSED" == "1" ]; then
  9. xenstore-rm "$XS_KEY"
  10. fi
  11. echo QUBES_EXPOSED=0
  12. }
  13. # Ignore mounted...
  14. if fgrep -q $DEVNAME /proc/mounts; then
  15. xs_remove
  16. exit 0
  17. fi
  18. # ... and used by device-mapper
  19. if [ -n "`ls -A /sys/$DEVPATH/holders 2> /dev/null`" ]; then
  20. xs_remove
  21. exit 0
  22. fi
  23. # ... and "empty" loop devices
  24. if [ "$MAJOR" -eq 7 -a ! -d /sys/$DEVPATH/loop ]; then
  25. xs_remove
  26. exit 0
  27. fi
  28. # Special case for CD
  29. if [ "$ID_TYPE" = "cd" ]; then
  30. if [ "$ID_CDROM_MEDIA" != "1" ]; then
  31. # Hide empty cdrom drive
  32. xs_remove
  33. exit 0
  34. fi
  35. MODE=r
  36. fi
  37. # Special description for loop devices
  38. if [ -d /sys/$DEVPATH/loop ]; then
  39. DESC=$(cat /sys/$DEVPATH/loop/backing_file)
  40. fi
  41. xenstore-write "$XS_KEY/desc" "$DESC" "$XS_KEY/size" "$SIZE" "$XS_KEY/mode" "$MODE"
  42. echo QUBES_EXPOSED=1
  43. # Make sure that block backend is loaded
  44. /sbin/modprobe xen-blkback 2> /dev/null || /sbin/modprobe blkbk