2011-09-29 13:52:26 +02:00
|
|
|
#!/usr/bin/python2
|
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2010 Marek Marczykowski <marmarek@invisiblethingslab.com>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2012-04-12 18:03:40 +02:00
|
|
|
from qubes.qubes import QubesVmCollection, QubesException
|
2012-04-13 00:29:13 +02:00
|
|
|
from qubes.qubesutils import block_list,block_attach,block_detach,block_detach_all,block_check_attached
|
2011-11-08 01:28:43 +01:00
|
|
|
from qubes.qubesutils import kbytes_to_kmg, bytes_to_kmg
|
2011-09-29 13:52:26 +02:00
|
|
|
from optparse import OptionParser
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
|
|
|
|
def main():
|
|
|
|
usage = "usage: %prog -l [options]\n"\
|
2012-03-31 19:01:51 +02:00
|
|
|
"usage: %prog -a [options] <vm-name> <device-vm-name>:<device>\n"\
|
|
|
|
"usage: %prog -A [options] <vm-name> <file-vm-name>:<file>\n"\
|
|
|
|
"usage: %prog -d [options] <device-vm-name>:<device>\n"\
|
2011-09-29 13:52:26 +02:00
|
|
|
"usage: %prog -d [options] <vm-name>\n"\
|
2012-10-08 00:46:30 +02:00
|
|
|
"List/set VM block devices."
|
2011-09-29 13:52:26 +02:00
|
|
|
|
|
|
|
parser = OptionParser (usage)
|
|
|
|
parser.add_option ("-l", "--list", action="store_true", dest="do_list", default=False)
|
2012-03-31 19:01:51 +02:00
|
|
|
parser.add_option ("-A", "--attach-file", action="store_true", dest="do_file_attach", default=False,
|
|
|
|
help="Attach specified file instead of physical device")
|
2011-09-29 13:52:26 +02:00
|
|
|
parser.add_option ("-a", "--attach", action="store_true", dest="do_attach", default=False)
|
|
|
|
parser.add_option ("-d", "--detach", action="store_true", dest="do_detach", default=False)
|
|
|
|
parser.add_option ("-f", "--frontend", dest="frontend",
|
|
|
|
help="Specify device name at destination VM [default: xvdi]")
|
|
|
|
parser.add_option ("--ro", dest="ro", action="store_true", default=False,
|
|
|
|
help="Force read-only mode")
|
|
|
|
parser.add_option ("--no-auto-detach", dest="auto_detach", action="store_false", default=True,
|
|
|
|
help="Fail when device already connected to other VM")
|
2012-03-27 12:59:47 +02:00
|
|
|
parser.add_option ("--show-system-disks", dest="system_disks", action="store_true", default=False,
|
|
|
|
help="List also system disks")
|
2012-07-11 23:53:23 +02:00
|
|
|
parser.add_option ("--force-root", action="store_true", dest="force_root", default=False,
|
|
|
|
help="Force to run, even with root privileges")
|
2011-09-29 13:52:26 +02:00
|
|
|
|
|
|
|
(options, args) = parser.parse_args ()
|
|
|
|
|
2012-07-11 23:53:23 +02:00
|
|
|
if os.geteuid() == 0:
|
|
|
|
if not options.force_root:
|
|
|
|
print >> sys.stderr, "*** Running this tool as root is strongly discouraged, this will lead you in permissions problems."
|
|
|
|
print >> sys.stderr, "Retry as unprivileged user."
|
|
|
|
print >> sys.stderr, "... or use --force-root to continue anyway."
|
|
|
|
exit(1)
|
|
|
|
|
2012-03-31 19:22:07 +02:00
|
|
|
if options.do_file_attach:
|
2012-03-26 20:29:49 +02:00
|
|
|
options.do_attach = True
|
|
|
|
|
2011-09-29 13:52:26 +02:00
|
|
|
if options.do_list + options.do_attach + options.do_detach > 1:
|
2012-03-26 20:29:49 +02:00
|
|
|
print >> sys.stderr, "Only one of -l -a/-A -d is allowed!"
|
2011-09-29 13:52:26 +02:00
|
|
|
exit (1)
|
|
|
|
|
|
|
|
if options.do_attach or options.do_detach:
|
|
|
|
qvm_collection = QubesVmCollection()
|
|
|
|
qvm_collection.lock_db_for_reading()
|
|
|
|
qvm_collection.load()
|
|
|
|
qvm_collection.unlock_db()
|
|
|
|
|
|
|
|
if options.do_attach:
|
2012-10-15 11:40:08 +02:00
|
|
|
if (len (args) != 2):
|
|
|
|
parser.error ("You must provide vm name and device!")
|
2012-03-31 19:01:51 +02:00
|
|
|
vm = qvm_collection.get_vm_by_name(args[0])
|
2011-09-29 13:52:26 +02:00
|
|
|
if vm is None:
|
2012-03-31 19:01:51 +02:00
|
|
|
parser.error ("Invalid VM name: %s" % args[0])
|
|
|
|
# FIXME: here we assume that device is always in form "domain:dev", which can be changed in the future
|
|
|
|
if args[1].find(":") < 0:
|
2012-12-05 16:52:10 +01:00
|
|
|
parser.error ("Invalid device syntax (missing VM name): %s" % args[1])
|
2012-03-31 19:01:51 +02:00
|
|
|
if options.do_file_attach:
|
2012-03-26 20:29:49 +02:00
|
|
|
dev = {}
|
2012-03-31 19:01:51 +02:00
|
|
|
(dev['vm'], dev['device']) = args[1].split(":")
|
2012-03-26 20:29:49 +02:00
|
|
|
dev['mode'] = 'w'
|
|
|
|
else:
|
|
|
|
dev_list = block_list()
|
2012-03-31 19:01:51 +02:00
|
|
|
if not args[1] in dev_list.keys():
|
|
|
|
parser.error ("Invalid device name: %s" % args[1])
|
|
|
|
dev = dev_list[args[1]]
|
2011-09-29 13:52:26 +02:00
|
|
|
backend_vm = qvm_collection.get_vm_by_name(dev['vm'])
|
|
|
|
assert backend_vm is not None
|
|
|
|
kwargs = {}
|
|
|
|
if options.frontend:
|
|
|
|
kwargs['frontend'] = options.frontend
|
|
|
|
if options.ro:
|
|
|
|
kwargs['mode'] = "r"
|
|
|
|
else:
|
|
|
|
kwargs['mode'] = dev['mode']
|
|
|
|
kwargs['auto_detach'] = options.auto_detach
|
2012-04-12 18:03:40 +02:00
|
|
|
try:
|
|
|
|
block_attach(vm, backend_vm, dev['device'], **kwargs)
|
|
|
|
except QubesException as e:
|
|
|
|
print >> sys.stderr, "ERROR: %s" % str(e)
|
|
|
|
sys.exit(1)
|
2011-09-29 13:52:26 +02:00
|
|
|
elif options.do_detach:
|
|
|
|
if (len (args) < 1):
|
|
|
|
parser.error ("You must provide device or vm name!")
|
2012-10-15 11:40:08 +02:00
|
|
|
if len(args) > 1:
|
|
|
|
parser.error ("Too many parameters")
|
2011-09-29 13:52:26 +02:00
|
|
|
# Check if provided name is VM
|
|
|
|
vm = qvm_collection.get_vm_by_name(args[0])
|
|
|
|
if vm is not None:
|
|
|
|
kwargs = {}
|
|
|
|
if options.frontend:
|
|
|
|
kwargs['frontend'] = options.frontend
|
2012-04-13 00:29:13 +02:00
|
|
|
block_detach(vm, **kwargs)
|
|
|
|
else:
|
|
|
|
block_detach_all(vm)
|
2011-09-29 13:52:26 +02:00
|
|
|
else:
|
|
|
|
# Maybe device?
|
|
|
|
dev_list = block_list()
|
|
|
|
if not args[0] in dev_list.keys():
|
|
|
|
parser.error ("Invalid VM or device name: %s" % args[0])
|
|
|
|
dev = dev_list[args[0]]
|
|
|
|
attached_to = block_check_attached(None, dev['device'], backend_xid = dev['xid'])
|
|
|
|
if attached_to is None:
|
|
|
|
print >> sys.stderr, "WARNING: Device not connected to any VM"
|
|
|
|
exit(0)
|
|
|
|
block_detach(None, attached_to['devid'], vm_xid=attached_to['xid'])
|
|
|
|
else:
|
|
|
|
# do_list
|
2012-10-15 11:40:08 +02:00
|
|
|
if len(args) > 0:
|
|
|
|
parser.error ("Too many parameters")
|
2012-03-27 12:59:47 +02:00
|
|
|
kwargs = {}
|
|
|
|
kwargs['system_disks'] = options.system_disks
|
|
|
|
for dev in block_list(**kwargs).values():
|
2011-09-29 13:52:26 +02:00
|
|
|
attached_to = block_check_attached(None, dev['device'], backend_xid = dev['xid'])
|
|
|
|
attached_to_str = ""
|
|
|
|
if attached_to:
|
|
|
|
attached_to_str = " (attached to '%s' as '%s')" % (attached_to['vm'], attached_to['frontend'])
|
2011-10-01 02:55:51 +02:00
|
|
|
size_str = bytes_to_kmg(dev['size'])
|
2011-09-29 13:52:26 +02:00
|
|
|
print "%s\t%s %s%s" % (dev['name'], dev['desc'], size_str, attached_to_str)
|
|
|
|
exit (0)
|
|
|
|
|
|
|
|
main()
|