xl-qvm-usb-attach.py 1.3 KB

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