qvm-usb 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/usr/bin/python2
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2010 Marek Marczykowski <marmarek@invisiblethingslab.com>
  6. #
  7. # This program is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU General Public License
  9. # as published by the Free Software Foundation; either version 2
  10. # of the License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. #
  21. #
  22. from qubes.qubes import QubesVmCollection, QubesException
  23. from qubes.qubesutils import usb_list,usb_attach,usb_detach,usb_detach_all,usb_check_attached
  24. from optparse import OptionParser
  25. import sys
  26. import os
  27. pvusb_enable_flagfile = '/var/lib/qubes/pvusb-enable.flag'
  28. def main():
  29. usage = "usage: %prog -l [options]\n"\
  30. "usage: %prog -a [options] <vm-name> <device-vm-name>:<device>\n"\
  31. "usage: %prog -d [options] <device-vm-name>:<device>\n"\
  32. "List/set VM USB devices."
  33. # "usage: %prog -d [options] <vm-name>\n"\
  34. parser = OptionParser (usage)
  35. parser.add_option ("-l", "--list", action="store_true", dest="do_list", default=False)
  36. parser.add_option ("-a", "--attach", action="store_true", dest="do_attach", default=False)
  37. parser.add_option ("-d", "--detach", action="store_true", dest="do_detach", default=False)
  38. # parser.add_option ("-f", "--frontend", dest="frontend",
  39. # help="Specify device id at destination VM [default: first unused]")
  40. parser.add_option ("--no-auto-detach", dest="auto_detach", action="store_false", default=True,
  41. help="Fail when device already connected to other VM")
  42. parser.add_option ("--force-root", action="store_true", dest="force_root", default=False,
  43. help="Force to run, even with root privileges")
  44. (options, args) = parser.parse_args ()
  45. if not os.path.exists(pvusb_enable_flagfile):
  46. print >> sys.stderr, ""
  47. print >> sys.stderr, "******* WARNING *** WARNING *** WARNING *** WARNING *******"
  48. print >> sys.stderr, "*** ***"
  49. print >> sys.stderr, "*** PVUSB passthrough kernel support is still unstable. ***"
  50. print >> sys.stderr, "*** It can CRASH your VMs. ***"
  51. print >> sys.stderr, "*** ***"
  52. print >> sys.stderr, "***********************************************************"
  53. print >> sys.stderr, ""
  54. print >> sys.stderr, "To use it, you need install kernel from \"unstable\" repository"
  55. print >> sys.stderr, "If you still want to enable it, type capital YES"
  56. print >> sys.stderr, ""
  57. prompt = raw_input ("Do you want enable PV USB support? ")
  58. if prompt == "YES":
  59. open(pvusb_enable_flagfile, "w").close()
  60. else:
  61. exit(1)
  62. if os.geteuid() == 0:
  63. if not options.force_root:
  64. print >> sys.stderr, "*** Running this tool as root is strongly discouraged, this will lead you in permissions problems."
  65. print >> sys.stderr, "Retry as unprivileged user."
  66. print >> sys.stderr, "... or use --force-root to continue anyway."
  67. exit(1)
  68. if options.do_list + options.do_attach + options.do_detach > 1:
  69. print >> sys.stderr, "Only one of -l -a -d is allowed!"
  70. exit (1)
  71. if options.do_attach or options.do_detach:
  72. qvm_collection = QubesVmCollection()
  73. qvm_collection.lock_db_for_reading()
  74. qvm_collection.load()
  75. qvm_collection.unlock_db()
  76. if options.do_attach:
  77. if (len (args) != 2):
  78. parser.error ("You must provide vm name and device!")
  79. vm = qvm_collection.get_vm_by_name(args[0])
  80. if vm is None:
  81. parser.error ("Invalid VM name: %s" % args[0])
  82. # FIXME: here we assume that device is always in form "domain:dev", which can be changed in the future
  83. if args[1].find(":") < 0:
  84. parser.error ("Invalid device syntax: %s" % args[1])
  85. dev_list = usb_list()
  86. if not args[1] in dev_list.keys():
  87. parser.error ("Invalid device name: %s" % args[1])
  88. dev = dev_list[args[1]]
  89. backend_vm = qvm_collection.get_vm_by_name(dev['vm'])
  90. assert backend_vm is not None
  91. kwargs = {}
  92. # if options.frontend:
  93. # kwargs['frontend'] = options.frontend
  94. kwargs['auto_detach'] = options.auto_detach
  95. try:
  96. usb_attach(vm, backend_vm, dev['device'], **kwargs)
  97. except QubesException as e:
  98. print >> sys.stderr, "ERROR: %s" % str(e)
  99. sys.exit(1)
  100. elif options.do_detach:
  101. if (len (args) < 1):
  102. parser.error ("You must provide device or vm name!")
  103. if len(args) > 1:
  104. parser.error ("Too many parameters")
  105. # Check if provided name is VM
  106. vm = qvm_collection.get_vm_by_name(args[0])
  107. if vm is not None:
  108. #kwargs = {}
  109. #if options.frontend:
  110. # kwargs['frontend'] = options.frontend
  111. # usb_detach(vm, **kwargs)
  112. #else:
  113. usb_detach_all(vm)
  114. else:
  115. # Maybe usbvm:device?
  116. # FIXME: nasty copy-paste from attach code half a page above
  117. # FIXME: here we assume that device is always in form "domain:dev", which can be changed in the future
  118. if args[0].find(":") < 0:
  119. parser.error ("Invalid device syntax: %s" % args[0])
  120. dev_list = usb_list()
  121. if not args[0] in dev_list.keys():
  122. parser.error ("Invalid device name: %s" % args[0])
  123. dev = dev_list[args[0]]
  124. backend_vm = qvm_collection.get_vm_by_name(dev['vm'])
  125. assert backend_vm is not None
  126. attached_to = usb_check_attached('', backend_vm.xid, dev['device'])
  127. if attached_to is None:
  128. print >> sys.stderr, "WARNING: Device not connected to any VM"
  129. exit(0)
  130. usb_detach(backend_vm, attached_to)
  131. else:
  132. if len(args) > 0:
  133. parser.error ("Too many parameters")
  134. # do_list
  135. for dev in usb_list().values():
  136. attached_to = usb_check_attached('', dev['xid'], dev['device'])
  137. attached_to_str = ""
  138. if attached_to:
  139. attached_to_str = " (attached to %s:%s)" % (attached_to['vm'], attached_to['frontend'])
  140. print "%s\t%s%s (USBv%s)" % (dev['name'], dev['desc'], attached_to_str, dev['usb_ver'])
  141. exit (0)
  142. main()