qvm-usb 6.9 KB

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