2010-04-05 20:58:57 +02:00
|
|
|
#!/usr/bin/python2.6
|
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2010 Joanna Rutkowska <joanna@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.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
from qubes.qubes import QubesVmCollection
|
|
|
|
from qubes.qubes import QubesVmLabels
|
2012-01-12 01:27:27 +01:00
|
|
|
from qubes.qubes import QubesHost
|
2011-06-30 01:07:47 +02:00
|
|
|
from qubes.qubes import qubes_kernels_base_dir
|
2010-04-05 20:58:57 +02:00
|
|
|
from optparse import OptionParser
|
|
|
|
import subprocess
|
2011-06-30 01:07:47 +02:00
|
|
|
import os
|
2011-10-07 21:56:58 +02:00
|
|
|
import sys
|
2012-03-02 16:17:18 +01:00
|
|
|
import re
|
2010-04-05 20:58:57 +02:00
|
|
|
|
|
|
|
def do_list(vm):
|
|
|
|
label_width = 18
|
|
|
|
fmt="{{0:<{0}}}: {{1}}".format(label_width)
|
|
|
|
|
|
|
|
print fmt.format ("name", vm.name)
|
|
|
|
print fmt.format ("label", vm.label.name)
|
|
|
|
print fmt.format ("type", vm.type)
|
2012-03-09 11:01:20 +01:00
|
|
|
if vm.template is not None:
|
|
|
|
print fmt.format ("template", vm.template.name)
|
2012-03-04 21:59:02 +01:00
|
|
|
if vm.netvm is not None:
|
|
|
|
print fmt.format ("netvm", vm.netvm.name)
|
2012-03-09 11:28:06 +01:00
|
|
|
print fmt.format ("updateable?", vm.updateable)
|
2010-04-05 20:58:57 +02:00
|
|
|
print fmt.format ("installed by RPM?", vm.installed_by_rpm)
|
2012-03-07 19:03:55 +01:00
|
|
|
print fmt.format ("include in backups", vm.include_in_backups)
|
2010-04-05 20:58:57 +02:00
|
|
|
print fmt.format ("dir", vm.dir_path)
|
|
|
|
print fmt.format ("config", vm.conf_file)
|
2011-06-10 19:08:47 +02:00
|
|
|
print fmt.format ("pcidevs", vm.pcidevs)
|
2012-03-09 11:01:20 +01:00
|
|
|
if vm.template is None:
|
2010-04-05 20:58:57 +02:00
|
|
|
print fmt.format ("root img", vm.root_img)
|
2011-03-05 15:13:31 +01:00
|
|
|
if vm.is_template():
|
2011-02-27 00:06:46 +01:00
|
|
|
print fmt.format ("root COW img", vm.rootcow_img)
|
2012-03-09 11:01:20 +01:00
|
|
|
if vm.template is not None:
|
|
|
|
print fmt.format ("root img", vm.template.root_img)
|
2012-02-24 04:22:14 +01:00
|
|
|
if hasattr(vm, 'volatile_img'):
|
|
|
|
print fmt.format ("root volatile img", vm.volatile_img)
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2012-02-24 04:22:14 +01:00
|
|
|
if hasattr(vm, 'private_img'):
|
|
|
|
print fmt.format ("private img", vm.private_img)
|
2011-08-31 19:41:36 +02:00
|
|
|
print fmt.format ("vcpus", str(vm.vcpus))
|
2011-04-29 01:38:43 +02:00
|
|
|
print fmt.format ("memory", vm.memory)
|
2012-02-24 04:22:14 +01:00
|
|
|
if hasattr(vm, 'maxmem'):
|
|
|
|
print fmt.format ("maxmem", vm.maxmem)
|
2012-03-02 16:17:18 +01:00
|
|
|
print fmt.format ("MAC", "%s%s" % (vm.mac, " (auto)" if vm._mac is None else ""))
|
2012-02-24 04:22:14 +01:00
|
|
|
if hasattr(vm, 'kernel'):
|
|
|
|
if vm.uses_default_kernel:
|
|
|
|
print fmt.format ("kernel", "%s (default)" % vm.kernel)
|
|
|
|
else:
|
|
|
|
print fmt.format ("kernel", vm.kernel)
|
|
|
|
if hasattr(vm, 'kernelopts'):
|
|
|
|
if vm.uses_default_kernelopts:
|
|
|
|
print fmt.format ("kernelopts", "%s (default)" % vm.kernelopts)
|
|
|
|
else:
|
|
|
|
print fmt.format ("kernelopts", vm.kernelopts)
|
2012-04-23 12:19:54 +02:00
|
|
|
if hasattr(vm, 'debug'):
|
|
|
|
print fmt.format("debug", "on" if vm.debug else "off")
|
2010-04-05 20:58:57 +02:00
|
|
|
|
|
|
|
def set_label(vms, vm, args):
|
|
|
|
if len (args) != 1:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "Missing label name argument!"
|
2012-02-26 00:12:54 +01:00
|
|
|
exit (1)
|
2010-04-05 20:58:57 +02:00
|
|
|
|
|
|
|
label = args[0]
|
|
|
|
if label not in QubesVmLabels:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "Wrong label name, supported values are the following:"
|
2010-04-05 20:58:57 +02:00
|
|
|
for l in QubesVmLabels.values():
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "* {0}".format(l.name)
|
2010-04-05 20:58:57 +02:00
|
|
|
exit (1)
|
|
|
|
|
|
|
|
vm.label = QubesVmLabels[label]
|
|
|
|
|
2011-04-29 01:38:43 +02:00
|
|
|
def set_memory(vms, vm, args):
|
|
|
|
if len (args) != 1:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "Missing memory argument!"
|
2012-03-02 16:17:18 +01:00
|
|
|
exit (1)
|
2011-04-29 01:38:43 +02:00
|
|
|
|
2012-04-18 12:31:24 +02:00
|
|
|
new_memory = int(args[0])
|
|
|
|
|
|
|
|
if new_memory <= 0:
|
|
|
|
print >>sys.stderr, "Memory size must be positive"
|
|
|
|
return False
|
|
|
|
|
2012-04-23 22:01:13 +02:00
|
|
|
qubes_host = QubesHost()
|
|
|
|
if new_memory > qubes_host.memory_total/1024:
|
|
|
|
print >> sys.stderr, "This host has only {0} MB of RAM".format(qubes_host.memory_total/1024)
|
2012-04-18 12:31:24 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
vm.memory = new_memory
|
2011-04-29 01:38:43 +02:00
|
|
|
|
|
|
|
def set_maxmem(vms, vm, args):
|
|
|
|
if len (args) != 1:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "Missing maxmem argument!"
|
2012-03-02 16:17:18 +01:00
|
|
|
exit (1)
|
2011-04-29 01:38:43 +02:00
|
|
|
|
2012-04-18 12:31:24 +02:00
|
|
|
new_maxmem = int(args[0])
|
|
|
|
|
|
|
|
if new_maxmem <= 0:
|
|
|
|
print >>sys.stderr, "Memory size must be positive"
|
|
|
|
return False
|
|
|
|
|
|
|
|
qubes_host = QubesHost()
|
|
|
|
if new_maxmem > qubes_host.memory_total/1024:
|
|
|
|
print >> sys.stderr, "This host has only {0} MB of RAM".format(qubes_host.memory_total/1024)
|
|
|
|
return False
|
|
|
|
|
|
|
|
if new_maxmem < vm.memory:
|
2012-04-23 12:15:50 +02:00
|
|
|
print >> sys.stderr, "WARNING: new maxmem smaller than memory property - VM will be able to use only 'maxmem' memory amount"
|
2012-04-18 12:31:24 +02:00
|
|
|
|
|
|
|
vm.maxmem = new_maxmem
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2012-03-02 16:17:18 +01:00
|
|
|
def set_mac(vms, vm, args):
|
|
|
|
if len (args) != 1:
|
|
|
|
print >> sys.stderr, "Missing MAC argument!"
|
|
|
|
exit (1)
|
|
|
|
|
|
|
|
if not re.match("[0-9a-fA-F:]{17}|auto", args[0]):
|
|
|
|
print >> sys.stderr, "Invalid MAC argument!"
|
|
|
|
print >> sys.stderr, "Possible values:"
|
|
|
|
print >> sys.stderr, "1) auto"
|
|
|
|
print >> sys.stderr, "2) MAC in format: XX:XX:XX:XX:XX:XX"
|
|
|
|
exit (1)
|
|
|
|
|
|
|
|
mac = args[0]
|
|
|
|
if mac == "auto":
|
|
|
|
mac = None
|
|
|
|
vm.mac = mac
|
|
|
|
|
2011-06-10 19:08:47 +02:00
|
|
|
def set_pcidevs(vms, vm, args):
|
|
|
|
if len (args) != 1:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "Missing pcidevs argument!"
|
2012-03-02 16:17:18 +01:00
|
|
|
exit (1)
|
2011-06-10 19:08:47 +02:00
|
|
|
|
2011-07-05 22:01:28 +02:00
|
|
|
vm.pcidevs = list(eval(args[0]))
|
2011-06-10 19:08:47 +02:00
|
|
|
|
2010-04-05 20:58:57 +02:00
|
|
|
def set_netvm(vms, vm, args):
|
|
|
|
if len (args) != 1:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "Missing netvm name argument!"
|
|
|
|
print >> sys.stderr, "Possible values:"
|
|
|
|
print >> sys.stderr, "1) default"
|
|
|
|
print >> sys.stderr, "2) none"
|
|
|
|
print >> sys.stderr, "3) <vmaname>"
|
2010-04-05 20:58:57 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
netvm = args[0]
|
|
|
|
if netvm == "none":
|
2012-03-04 21:59:02 +01:00
|
|
|
netvm = None
|
2010-04-05 20:58:57 +02:00
|
|
|
vm.uses_default_netvm = False
|
|
|
|
elif netvm == "default":
|
2012-03-04 21:59:02 +01:00
|
|
|
netvm = vms.get_default_netvm()
|
2010-04-05 20:58:57 +02:00
|
|
|
vm.uses_default_netvm = True
|
|
|
|
else:
|
2012-03-04 21:59:02 +01:00
|
|
|
netvm = vms.get_vm_by_name (netvm)
|
|
|
|
if netvm is None:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "A VM with the name '{0}' does not exist in the system.".format(netvm)
|
2010-04-05 20:58:57 +02:00
|
|
|
exit(1)
|
2012-03-04 21:59:02 +01:00
|
|
|
if not netvm.is_netvm():
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "VM '{0}' is not a NetVM".format(netvm)
|
2010-04-05 20:58:57 +02:00
|
|
|
exit (1)
|
|
|
|
vm.uses_default_netvm = False
|
|
|
|
|
2012-03-09 01:52:28 +01:00
|
|
|
vm.netvm = netvm
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2011-06-30 01:07:47 +02:00
|
|
|
def set_kernel(vms, vm, args):
|
|
|
|
if len (args) != 1:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "Missing kernel version argument!"
|
|
|
|
print >> sys.stderr, "Possible values:"
|
|
|
|
print >> sys.stderr, "1) default"
|
|
|
|
print >> sys.stderr, "2) none (kernels subdir in VM)"
|
|
|
|
print >> sys.stderr, "3) <kernel version>, one of:"
|
2011-06-30 01:07:47 +02:00
|
|
|
for k in os.listdir(qubes_kernels_base_dir):
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, " -", k
|
2011-06-30 01:07:47 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
kernel = args[0]
|
|
|
|
if kernel == "default":
|
|
|
|
kernel = vms.get_default_kernel()
|
|
|
|
vm.uses_default_kernel = True
|
|
|
|
elif kernel == "none":
|
|
|
|
kernel = None
|
|
|
|
vm.uses_default_kernel = False
|
|
|
|
else:
|
|
|
|
if not os.path.exists(qubes_kernels_base_dir + '/' + kernel):
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "Kernel version {0} not installed.".format(kernel)
|
2011-06-30 01:07:47 +02:00
|
|
|
exit(1)
|
|
|
|
vm.uses_default_kernel = False
|
|
|
|
|
|
|
|
vm.kernel = kernel
|
|
|
|
|
2011-07-24 23:24:45 +02:00
|
|
|
def set_template(vms, vm, args):
|
|
|
|
if len (args) != 1:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "Missing template name argument!"
|
2011-07-24 23:24:45 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
template_name = args[0];
|
2012-03-09 11:01:20 +01:00
|
|
|
template = vms.get_vm_by_name(template_name)
|
|
|
|
if template is None or template.qid not in vms:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "A VM with the name '{0}' does not exist in the system.".format(template_name)
|
2011-07-24 23:24:45 +02:00
|
|
|
return False
|
|
|
|
|
2012-03-09 11:01:20 +01:00
|
|
|
if not template.is_template():
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "VM '{0}' is not a TemplateVM".format(template_name)
|
2011-07-24 23:24:45 +02:00
|
|
|
return False
|
|
|
|
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "Setting template for VM '{0}' to '{1}'...".format (vm.name, template_name)
|
2012-03-09 11:01:20 +01:00
|
|
|
vm.template = template
|
2011-07-24 23:24:45 +02:00
|
|
|
return True
|
2011-06-30 01:07:47 +02:00
|
|
|
|
2011-08-31 19:41:36 +02:00
|
|
|
def set_vcpus(vms, vm, args):
|
|
|
|
if len (args) != 1:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "Missing vcpus count argument!"
|
2011-08-31 19:41:36 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
vcpus = int(args[0])
|
|
|
|
if vcpus <= 0:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "A vcpus count must be positive."
|
2011-08-31 19:41:36 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
qubes_host = QubesHost()
|
|
|
|
if vcpus > qubes_host.no_cpus:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "This host has only {0} cpus".format(ubes_host.no_cpus)
|
2011-08-31 19:41:36 +02:00
|
|
|
return False
|
|
|
|
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "Setting vcpus count for VM '{0}' to '{1}'...".format (vm.name, vcpus)
|
2011-08-31 19:41:36 +02:00
|
|
|
vm.vcpus = vcpus
|
|
|
|
return True
|
|
|
|
|
2011-08-31 20:34:56 +02:00
|
|
|
def set_kernelopts(vms, vm, args):
|
|
|
|
if len (args) != 1:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "Missing kernel opts argument!"
|
|
|
|
print >> sys.stderr, "Possible values:"
|
|
|
|
print >> sys.stderr, "1) default"
|
|
|
|
print >> sys.stderr, "2) <opts>"
|
2011-08-31 20:34:56 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
if args[0] == 'default':
|
|
|
|
vm.uses_default_kernelopts = True
|
|
|
|
else:
|
|
|
|
vm.uses_default_kernelopts = False
|
|
|
|
vm.kernelopts = args[0]
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2011-11-02 00:39:23 +01:00
|
|
|
def set_name(vms, vm, args):
|
|
|
|
if len (args) != 1:
|
|
|
|
print >> sys.stderr, "Missing new name!"
|
|
|
|
return False
|
|
|
|
|
|
|
|
if args[0] == vm.name:
|
|
|
|
return False
|
|
|
|
vm.set_name(args[0])
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2012-04-23 12:19:54 +02:00
|
|
|
def set_debug(vms, vm, args):
|
|
|
|
if len (args) != 1:
|
|
|
|
print >> sys.stderr, "Missing value (True/False or on/off)!"
|
|
|
|
return False
|
|
|
|
|
|
|
|
if args[0].lower() == "on":
|
|
|
|
vm.debug = True
|
|
|
|
elif args[0].lower() == "off":
|
|
|
|
vm.debug = False
|
|
|
|
else:
|
|
|
|
vm.debug = bool(eval(args[0].capitalize()))
|
|
|
|
return True
|
|
|
|
|
2010-04-05 20:58:57 +02:00
|
|
|
properties = {
|
2012-03-07 19:03:55 +01:00
|
|
|
"include_in_backups": set_include_in_backups,
|
2011-06-10 19:08:47 +02:00
|
|
|
"pcidevs": set_pcidevs,
|
2010-04-05 20:58:57 +02:00
|
|
|
"label" : set_label,
|
|
|
|
"netvm" : set_netvm,
|
2011-04-29 01:38:43 +02:00
|
|
|
"maxmem" : set_maxmem,
|
|
|
|
"memory" : set_memory,
|
2011-06-30 01:07:47 +02:00
|
|
|
"kernel" : set_kernel,
|
2011-07-24 23:24:45 +02:00
|
|
|
"template" : set_template,
|
2011-08-31 19:41:36 +02:00
|
|
|
"vcpus" : set_vcpus,
|
2011-08-31 20:34:56 +02:00
|
|
|
"kernelopts": set_kernelopts,
|
2011-11-02 00:39:23 +01:00
|
|
|
"name": set_name,
|
2012-03-02 16:17:18 +01:00
|
|
|
"mac": set_mac,
|
2012-04-23 12:19:54 +02:00
|
|
|
"debug": set_debug,
|
2010-04-05 20:58:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def do_set(vms, vm, property, args):
|
|
|
|
if property not in properties.keys():
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "ERROR: Wrong property name: '{0}'".format(property)
|
2010-04-05 20:58:57 +02:00
|
|
|
return False
|
|
|
|
|
2012-02-24 04:22:14 +01:00
|
|
|
if not hasattr(vm, property):
|
|
|
|
print >> sys.stderr, "ERROR: Property '{0}' not available for this VM".format(property)
|
|
|
|
return False
|
|
|
|
|
2010-04-05 20:58:57 +02:00
|
|
|
return properties[property](vms, vm, args)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
usage = "usage: %prog -l [options] <vm-name>\n"\
|
|
|
|
"usage: %prog -s [options] <vm-name> <property> [...]\n"\
|
|
|
|
"List/set various per-VM properties."
|
|
|
|
|
|
|
|
parser = OptionParser (usage)
|
|
|
|
parser.add_option ("-l", "--list", action="store_true", dest="do_list", default=False)
|
|
|
|
parser.add_option ("-s", "--set", action="store_true", dest="do_set", default=False)
|
|
|
|
|
|
|
|
(options, args) = parser.parse_args ()
|
|
|
|
if (len (args) < 1):
|
|
|
|
parser.error ("You must provide at least the vmname!")
|
|
|
|
|
|
|
|
vmname = args[0]
|
|
|
|
|
|
|
|
if options.do_list and options.do_set:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "You cannot provide -l and -s at the same time!"
|
2010-04-05 20:58:57 +02:00
|
|
|
exit (1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if options.do_set:
|
|
|
|
qvm_collection = QubesVmCollection()
|
|
|
|
qvm_collection.lock_db_for_writing()
|
|
|
|
qvm_collection.load()
|
|
|
|
else:
|
|
|
|
qvm_collection = QubesVmCollection()
|
|
|
|
qvm_collection.lock_db_for_reading()
|
|
|
|
qvm_collection.load()
|
|
|
|
qvm_collection.unlock_db()
|
|
|
|
|
|
|
|
vm = qvm_collection.get_vm_by_name(vmname)
|
|
|
|
if vm is None or vm.qid not in qvm_collection:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "A VM with the name '{0}' does not exist in the system.".format(vmname)
|
2010-04-05 20:58:57 +02:00
|
|
|
exit(1)
|
|
|
|
|
|
|
|
if options.do_set:
|
|
|
|
if len (args) < 2:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "You must specify the property you wish to set..."
|
|
|
|
print >> sys.stderr, "Available properties:"
|
2010-04-05 20:58:57 +02:00
|
|
|
for p in properties.keys():
|
2012-02-24 04:22:14 +01:00
|
|
|
if hasattr(vm, p):
|
|
|
|
print >> sys.stderr, "--> '{0}'".format(p)
|
2010-04-05 20:58:57 +02:00
|
|
|
exit (1)
|
|
|
|
|
|
|
|
property = args[1]
|
|
|
|
do_set(qvm_collection, vm, property, args[2:])
|
|
|
|
qvm_collection.save()
|
|
|
|
qvm_collection.unlock_db()
|
|
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
# do_list
|
|
|
|
do_list(vm)
|
|
|
|
|
|
|
|
main()
|