qvm-block 6.6 KB

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