xl-qvm-usb-detach.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/python
  2. ##
  3. ## This script is for dom0
  4. ## The syntax is modelled after "xl block-attach"
  5. ## FIXME: should be modelled after block-detach instead
  6. ##
  7. import sys
  8. import os
  9. import xen.lowlevel.xl
  10. # parse command line
  11. if (len(sys.argv)<4) or (len(sys.argv)>5):
  12. print 'usage: xl-qvm-usb-detach.py <frontendvm-xid> <backendvm-device> <frontendvm-device> [<backendvm-xid>]'
  13. sys.exit(1)
  14. frontendvm_xid=sys.argv[1]
  15. backendvm_device=sys.argv[2]
  16. frontend=sys.argv[3].split('-')
  17. if len(frontend)!=2:
  18. print 'Error: frontendvm-device must be in <controller>-<port> format'
  19. sys.exit(1)
  20. (controller, port)=frontend
  21. if len(sys.argv)>4:
  22. backendvm_xid=int(sys.argv[4])
  23. backendvm_name=xen.lowlevel.xl.ctx().domid_to_name(backendvm_xid)
  24. else:
  25. backendvm_xid=0
  26. cmd = "/usr/lib/qubes/vusb-ctl.py unbind '%s'" % backendvm_device
  27. if backendvm_xid == 0:
  28. os.system("sudo %s" % cmd)
  29. else:
  30. from qubes.qubes import QubesVmCollection
  31. qvm_collection = QubesVmCollection()
  32. qvm_collection.lock_db_for_reading()
  33. qvm_collection.load()
  34. qvm_collection.unlock_db()
  35. # launch
  36. qvm_collection.get_vm_by_name(backendvm_name).run(cmd, user="root")
  37. # FIXME: command injection
  38. os.system("xenstore-write /local/domain/%s/backend/vusb/%s/%s/port/%s ''"
  39. % (backendvm_xid, frontendvm_xid, controller, port))