udev-block-add-change 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. # Get lock only in dom0 - there are so many block devices so it causes xenstore
  42. # deadlocks sometimes.
  43. if [ -f /etc/qubes-release ]; then
  44. # Skip xenstore-write if cannot obtain lock. This can mean very early system startup
  45. # stage without /run mounted (or populated). Devices will be rediscovered later
  46. # by qubes-core startup script.
  47. exec 9>>/var/run/qubes/block-xenstore.lock || exit 0
  48. flock 9
  49. fi
  50. xenstore-write "$XS_KEY/desc" "$DESC" "$XS_KEY/size" "$SIZE" "$XS_KEY/mode" "$MODE"
  51. echo QUBES_EXPOSED=1
  52. # Make sure that block backend is loaded
  53. /sbin/modprobe xen-blkback 2> /dev/null || /sbin/modprobe blkbk