qvm-usb 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. 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 hasattr(os, "geteuid") and os.geteuid() == 0:
  46. if not options.force_root:
  47. print >> sys.stderr, "*** Running this tool as root is strongly discouraged, this will lead you in permissions problems."
  48. print >> sys.stderr, "Retry as unprivileged user."
  49. print >> sys.stderr, "... or use --force-root to continue anyway."
  50. exit(1)
  51. if options.do_list + options.do_attach + options.do_detach > 1:
  52. print >> sys.stderr, "Only one of -l -a -d is allowed!"
  53. exit (1)
  54. qvm_collection = QubesVmCollection()
  55. qvm_collection.lock_db_for_reading()
  56. qvm_collection.load()
  57. qvm_collection.unlock_db()
  58. if options.do_attach:
  59. if (len (args) != 2):
  60. parser.error ("You must provide vm name and device!")
  61. vm = qvm_collection.get_vm_by_name(args[0])
  62. if vm is None:
  63. parser.error ("Invalid VM name: %s" % args[0])
  64. # FIXME: here we assume that device is always in form "domain:dev",
  65. # which can be changed in the future
  66. if args[1].find(":") < 0:
  67. parser.error("Invalid device syntax: %s" % args[1])
  68. backend_vm = qvm_collection.get_vm_by_name(args[1].split(":")[0])
  69. if backend_vm is None:
  70. parser.error("No such VM: {}".format(args[1].split(":")[0]))
  71. dev_list = usb_list(qvm_collection, vm=backend_vm)
  72. if not args[1] in dev_list.keys():
  73. parser.error("Invalid device name: %s" % args[1])
  74. dev = dev_list[args[1]]
  75. assert backend_vm is not None
  76. kwargs = {}
  77. # if options.frontend:
  78. # kwargs['frontend'] = options.frontend
  79. kwargs['auto_detach'] = options.auto_detach
  80. try:
  81. usb_attach(qvm_collection, vm, dev, **kwargs)
  82. except QubesException as e:
  83. print >> sys.stderr, "ERROR: %s" % str(e)
  84. sys.exit(1)
  85. elif options.do_detach:
  86. if (len (args) < 1):
  87. parser.error ("You must provide device or vm name!")
  88. if len(args) > 1:
  89. parser.error ("Too many parameters")
  90. # Check if provided name is VM
  91. vm = qvm_collection.get_vm_by_name(args[0])
  92. if vm is not None:
  93. #kwargs = {}
  94. #if options.frontend:
  95. # kwargs['frontend'] = options.frontend
  96. # usb_detach(vm, **kwargs)
  97. #else:
  98. usb_detach_all(qvm_collection, vm)
  99. else:
  100. # Maybe usbvm:device?
  101. # FIXME: nasty copy-paste from attach code half a page above
  102. # FIXME: here we assume that device is always in form "domain:dev",
  103. # which can be changed in the future
  104. if args[0].find(":") < 0:
  105. parser.error("Invalid device syntax: %s" % args[0])
  106. backend_vm = qvm_collection.get_vm_by_name(args[0].split(":")[0])
  107. if backend_vm is None:
  108. parser.error("No such VM: {}".format(args[0].split(":")[0]))
  109. dev_list = usb_list(qvm_collection, vm=backend_vm)
  110. if not args[0] in dev_list.keys():
  111. parser.error("Invalid device name: %s" % args[0])
  112. dev = dev_list[args[0]]
  113. attached_to = usb_check_attached(qvm_collection, dev)
  114. if attached_to is None:
  115. print >> sys.stderr, "WARNING: Device not connected to any VM"
  116. exit(0)
  117. usb_detach(qvm_collection, attached_to, dev)
  118. else:
  119. if len(args) > 0:
  120. parser.error("Too many parameters")
  121. # do_list
  122. for dev in usb_list(qvm_collection).values():
  123. attached_to = dev['connected-to']
  124. attached_to_str = ""
  125. if attached_to:
  126. attached_to_str = " (attached to %s)" % (attached_to.name)
  127. print "%s\t%s%s" % (dev['name'], dev['desc'], attached_to_str)
  128. exit (0)
  129. main()