qvm-block 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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['mode'] = 'w'
  82. else:
  83. dev_list = block_list(qvm_collection)
  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. kwargs = {}
  88. if options.frontend:
  89. kwargs['frontend'] = options.frontend
  90. if options.ro:
  91. kwargs['mode'] = "r"
  92. else:
  93. kwargs['mode'] = dev['mode']
  94. kwargs['auto_detach'] = options.auto_detach
  95. try:
  96. block_attach(qvm_collection, vm, dev, **kwargs)
  97. except QubesException as e:
  98. print >> sys.stderr, "ERROR: %s" % str(e)
  99. sys.exit(1)
  100. elif options.do_detach:
  101. if (len (args) < 1):
  102. parser.error ("You must provide device or vm name!")
  103. if len(args) > 1:
  104. parser.error ("Too many parameters")
  105. # Check if provided name is VM
  106. vm = qvm_collection.get_vm_by_name(args[0])
  107. if vm is not None:
  108. kwargs = {}
  109. if options.frontend:
  110. kwargs['frontend'] = options.frontend
  111. block_detach(vm, **kwargs)
  112. else:
  113. block_detach_all(vm)
  114. else:
  115. # Maybe device?
  116. dev_list = block_list(qvm_collection)
  117. if not args[0] in dev_list.keys():
  118. parser.error ("Invalid VM or device name: %s" % args[0])
  119. dev = dev_list[args[0]]
  120. attached_to = block_check_attached(qvm_collection, dev)
  121. if attached_to is None:
  122. print >> sys.stderr, "WARNING: Device not connected to any VM"
  123. exit(0)
  124. block_detach(attached_to['vm'], attached_to['frontend'])
  125. else:
  126. # do_list
  127. if len(args) > 0:
  128. parser.error ("Too many parameters")
  129. kwargs = {}
  130. kwargs['qvmc'] = qvm_collection
  131. kwargs['system_disks'] = options.system_disks
  132. for dev in block_list(**kwargs).values():
  133. attached_to = block_check_attached(qvm_collection, dev)
  134. attached_to_str = ""
  135. if attached_to:
  136. attached_to_str = " (attached to '%s' as '%s')" % (
  137. attached_to['vm'].name, 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()