qvm-block 6.5 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 hasattr(os, "geteuid") and 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. 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['desc'] = dev['device']
  82. dev['mode'] = 'w'
  83. else:
  84. dev_list = block_list(qvm_collection)
  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. kwargs = {}
  89. if options.frontend:
  90. kwargs['frontend'] = options.frontend
  91. if options.ro:
  92. kwargs['mode'] = "r"
  93. else:
  94. kwargs['mode'] = dev['mode']
  95. kwargs['auto_detach'] = options.auto_detach
  96. try:
  97. block_attach(qvm_collection, vm, dev, **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. block_detach(vm, **kwargs)
  113. else:
  114. block_detach_all(vm)
  115. else:
  116. # Maybe device?
  117. dev_list = block_list(qvm_collection)
  118. if not args[0] in dev_list.keys():
  119. parser.error ("Invalid VM or device name: %s" % args[0])
  120. dev = dev_list[args[0]]
  121. attached_to = block_check_attached(qvm_collection, dev)
  122. if attached_to is None:
  123. print >> sys.stderr, "WARNING: Device not connected to any VM"
  124. exit(0)
  125. block_detach(attached_to['vm'], attached_to['frontend'])
  126. else:
  127. # do_list
  128. if len(args) > 0:
  129. parser.error ("Too many parameters")
  130. kwargs = {}
  131. kwargs['qvmc'] = qvm_collection
  132. kwargs['system_disks'] = options.system_disks
  133. for dev in block_list(**kwargs).values():
  134. attached_to = block_check_attached(qvm_collection, dev)
  135. attached_to_str = ""
  136. if attached_to:
  137. attached_to_str = " (attached to '%s' as '%s')" % (
  138. attached_to['vm'].name, 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()