qvm-block 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 block_list,block_attach,block_detach,block_detach_all,block_check_attached
  25. from qubes.qubesutils import kbytes_to_kmg, bytes_to_kmg
  26. from optparse import OptionParser
  27. import subprocess
  28. import sys
  29. import os
  30. def main():
  31. usage = "usage: %prog -l [options]\n"\
  32. "usage: %prog -a [options] <vm-name> <device-vm-name>:<device>\n"\
  33. "usage: %prog -A [options] <vm-name> <file-vm-name>:<file>\n"\
  34. "usage: %prog -d [options] <device-vm-name>:<device>\n"\
  35. "usage: %prog -d [options] <vm-name>\n"\
  36. "List/set VM block devices."
  37. parser = OptionParser (usage)
  38. parser.add_option ("-l", "--list", action="store_true", dest="do_list", default=False)
  39. parser.add_option ("-A", "--attach-file", action="store_true", dest="do_file_attach", default=False,
  40. help="Attach specified file instead of physical device")
  41. parser.add_option ("-a", "--attach", action="store_true", dest="do_attach", default=False)
  42. parser.add_option ("-d", "--detach", action="store_true", dest="do_detach", default=False)
  43. parser.add_option ("-f", "--frontend", dest="frontend",
  44. help="Specify device name at destination VM [default: xvdi]")
  45. parser.add_option ("--ro", dest="ro", action="store_true", default=False,
  46. help="Force read-only mode")
  47. parser.add_option ("--no-auto-detach", dest="auto_detach", action="store_false", default=True,
  48. help="Fail when device already connected to other VM")
  49. parser.add_option ("--show-system-disks", dest="system_disks", action="store_true", default=False,
  50. help="List also system disks")
  51. parser.add_option ("--force-root", action="store_true", dest="force_root", default=False,
  52. help="Force to run, even with root privileges")
  53. (options, args) = parser.parse_args ()
  54. if os.geteuid() == 0:
  55. if not options.force_root:
  56. print >> sys.stderr, "*** Running this tool as root is strongly discouraged, this will lead you in permissions problems."
  57. print >> sys.stderr, "Retry as unprivileged user."
  58. print >> sys.stderr, "... or use --force-root to continue anyway."
  59. exit(1)
  60. if options.do_file_attach:
  61. options.do_attach = True
  62. if options.do_list + options.do_attach + options.do_detach > 1:
  63. print >> sys.stderr, "Only one of -l -a/-A -d is allowed!"
  64. exit (1)
  65. if options.do_attach or options.do_detach:
  66. qvm_collection = QubesVmCollection()
  67. qvm_collection.lock_db_for_reading()
  68. qvm_collection.load()
  69. qvm_collection.unlock_db()
  70. if options.do_attach:
  71. if (len (args) != 2):
  72. parser.error ("You must provide vm name and device!")
  73. vm = qvm_collection.get_vm_by_name(args[0])
  74. if vm is None:
  75. parser.error ("Invalid VM name: %s" % args[0])
  76. # FIXME: here we assume that device is always in form "domain:dev", which can be changed in the future
  77. if args[1].find(":") < 0:
  78. parser.error ("Invalid device syntax (missing VM name): %s" % args[1])
  79. if options.do_file_attach:
  80. dev = {}
  81. (dev['vm'], dev['device']) = args[1].split(":")
  82. dev['mode'] = 'w'
  83. else:
  84. dev_list = block_list()
  85. if not args[1] in dev_list.keys():
  86. parser.error ("Invalid device name: %s" % args[1])
  87. dev = dev_list[args[1]]
  88. backend_vm = qvm_collection.get_vm_by_name(dev['vm'])
  89. assert backend_vm is not None
  90. kwargs = {}
  91. if options.frontend:
  92. kwargs['frontend'] = options.frontend
  93. if options.ro:
  94. kwargs['mode'] = "r"
  95. else:
  96. kwargs['mode'] = dev['mode']
  97. kwargs['auto_detach'] = options.auto_detach
  98. try:
  99. block_attach(vm, backend_vm, dev['device'], **kwargs)
  100. except QubesException as e:
  101. print >> sys.stderr, "ERROR: %s" % str(e)
  102. sys.exit(1)
  103. elif options.do_detach:
  104. if (len (args) < 1):
  105. parser.error ("You must provide device or vm name!")
  106. if len(args) > 1:
  107. parser.error ("Too many parameters")
  108. # Check if provided name is VM
  109. vm = qvm_collection.get_vm_by_name(args[0])
  110. if vm is not None:
  111. kwargs = {}
  112. if options.frontend:
  113. kwargs['frontend'] = options.frontend
  114. block_detach(vm, **kwargs)
  115. else:
  116. block_detach_all(vm)
  117. else:
  118. # Maybe device?
  119. dev_list = block_list()
  120. if not args[0] in dev_list.keys():
  121. parser.error ("Invalid VM or device name: %s" % args[0])
  122. dev = dev_list[args[0]]
  123. attached_to = block_check_attached(None, dev['device'], backend_xid = dev['xid'])
  124. if attached_to is None:
  125. print >> sys.stderr, "WARNING: Device not connected to any VM"
  126. exit(0)
  127. block_detach(None, attached_to['devid'], vm_xid=attached_to['xid'])
  128. else:
  129. # do_list
  130. if len(args) > 0:
  131. parser.error ("Too many parameters")
  132. kwargs = {}
  133. kwargs['system_disks'] = options.system_disks
  134. for dev in block_list(**kwargs).values():
  135. attached_to = block_check_attached(None, dev['device'], backend_xid = dev['xid'])
  136. attached_to_str = ""
  137. if attached_to:
  138. attached_to_str = " (attached to '%s' as '%s')" % (attached_to['vm'], attached_to['frontend'])
  139. size_str = bytes_to_kmg(dev['size'])
  140. print "%s\t%s %s%s" % (dev['name'], dev['desc'], size_str, attached_to_str)
  141. exit (0)
  142. main()