2013-01-20 21:44:13 +01:00
|
|
|
#!/usr/bin/python2
|
2010-04-05 20:58:57 +02:00
|
|
|
#
|
|
|
|
# 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-08-23 03:48:03 +02:00
|
|
|
from qubes.qubes import QubesException
|
2010-04-05 20:58:57 +02:00
|
|
|
from optparse import OptionParser;
|
|
|
|
import subprocess
|
2011-03-11 01:48:27 +01:00
|
|
|
import re
|
2011-04-06 23:55:16 +02:00
|
|
|
import os
|
2011-10-07 21:56:58 +02:00
|
|
|
import sys
|
2011-03-11 01:48:27 +01:00
|
|
|
|
2010-04-05 20:58:57 +02:00
|
|
|
def main():
|
|
|
|
usage = "usage: %prog [options] <vm-name>"
|
|
|
|
parser = OptionParser (usage)
|
|
|
|
parser.add_option ("-t", "--template", dest="template",
|
|
|
|
help="Specify the TemplateVM to use")
|
|
|
|
parser.add_option ("-l", "--label", dest="label",
|
|
|
|
help="Specify the label to use for the new VM (e.g. red, yellow, green, ...)")
|
2011-03-11 01:48:27 +01:00
|
|
|
parser.add_option ("-p", "--proxy", action="store_true", dest="proxyvm", default=False,
|
|
|
|
help="Create ProxyVM")
|
2012-02-24 04:24:36 +01:00
|
|
|
parser.add_option ("-H", "--hvm", action="store_true", dest="hvm", default=False,
|
2013-11-25 07:14:06 +01:00
|
|
|
help="Create HVM (standalone unless --template option used)")
|
|
|
|
parser.add_option ("--hvm-template", action="store_true", dest="hvm_template", default=False,
|
|
|
|
help="Create HVM template")
|
2011-03-11 01:48:27 +01:00
|
|
|
parser.add_option ("-n", "--net", action="store_true", dest="netvm", default=False,
|
|
|
|
help="Create NetVM")
|
2011-03-16 23:45:02 +01:00
|
|
|
parser.add_option ("-s", "--standalone", action="store_true", dest="standalone", default=False,
|
|
|
|
help="Create standalone VM - independent of template ")
|
2013-11-25 17:15:45 +01:00
|
|
|
parser.add_option ("-R", "--root-move-from", dest="root_move", default=None,
|
2012-02-24 04:24:36 +01:00
|
|
|
help="Use provided root.img instead of default/empty one (file will be MOVED)")
|
2013-11-25 17:15:45 +01:00
|
|
|
parser.add_option ("-r", "--root-copy-from", dest="root_copy", default=None,
|
|
|
|
help="Use provided root.img instead of default/empty one (file will be COPIED)")
|
2011-03-16 18:39:54 +01:00
|
|
|
parser.add_option ("-m", "--mem", dest="mem", default=None,
|
|
|
|
help="Initial memory size (in MB)")
|
|
|
|
parser.add_option ("-c", "--vcpus", dest="vcpus", default=None,
|
|
|
|
help="VCPUs count")
|
2011-04-03 17:47:20 +02:00
|
|
|
parser.add_option ("-i", "--internal", action="store_true", dest="internal", default=False,
|
|
|
|
help="Create VM for internal use only (hidden in qubes-manager, no appmenus)")
|
2011-04-06 23:52:39 +02:00
|
|
|
parser.add_option ("--force-root", action="store_true", dest="force_root", default=False,
|
|
|
|
help="Force to run, even with root privileges")
|
2010-04-05 20:58:57 +02:00
|
|
|
|
|
|
|
parser.add_option ("-q", "--quiet", action="store_false", dest="verbose", default=True)
|
|
|
|
(options, args) = parser.parse_args ()
|
|
|
|
if (len (args) != 1):
|
|
|
|
parser.error ("You must specify VM name!")
|
|
|
|
vmname = args[0]
|
|
|
|
|
2013-11-25 07:14:06 +01:00
|
|
|
if (options.netvm + options.proxyvm + options.hvm + options.hvm_template) > 1:
|
|
|
|
parser.error ("You must specify at most one VM type switch")
|
2011-03-11 01:48:27 +01:00
|
|
|
|
2011-04-06 23:52:39 +02:00
|
|
|
if os.geteuid() == 0:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "*** Running this tool as root is strongly discouraged, this will lead you in permissions problems."
|
2011-04-06 23:52:39 +02:00
|
|
|
if options.force_root:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "Continuing as commanded. You have been warned."
|
2011-04-06 23:52:39 +02:00
|
|
|
else:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "Retry as unprivileged user."
|
|
|
|
print >> sys.stderr, "... or use --force-root to continue anyway."
|
2011-04-06 23:52:39 +02:00
|
|
|
exit(1)
|
|
|
|
|
2011-03-15 18:28:28 +01:00
|
|
|
if options.label is None:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "You must choose a label for the new VM by passing the --label option."
|
|
|
|
print >> sys.stderr, "Possible values are:"
|
2011-03-15 18:28:28 +01:00
|
|
|
for l in QubesVmLabels.values():
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "* {0}".format(l.name)
|
2011-03-15 18:28:28 +01:00
|
|
|
exit (1)
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2011-03-15 18:28:28 +01:00
|
|
|
if options.label not in QubesVmLabels:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "Wrong label name, supported values are the following:"
|
2011-03-15 18:28:28 +01:00
|
|
|
for l in QubesVmLabels.values():
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "* {0}".format(l.name)
|
2011-03-15 18:28:28 +01:00
|
|
|
exit (1)
|
|
|
|
label = QubesVmLabels[options.label]
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2013-11-25 17:15:15 +01:00
|
|
|
if options.hvm and not options.template:
|
|
|
|
options.standalone = True
|
|
|
|
|
|
|
|
if options.hvm_template:
|
|
|
|
options.standalone = True
|
|
|
|
|
2013-11-25 17:15:45 +01:00
|
|
|
if not options.standalone and any([options.root_copy, options.root_move]):
|
2012-02-24 04:24:36 +01:00
|
|
|
print >> sys.stderr, "root.img can be specified only for standalone VMs"
|
|
|
|
exit (1)
|
|
|
|
|
2013-11-25 07:14:06 +01:00
|
|
|
if options.hvm_template and options.template is not None:
|
|
|
|
print >> sys.stderr, "Template VM cannot be based on another template"
|
|
|
|
exit (1)
|
|
|
|
|
2013-11-25 17:15:45 +01:00
|
|
|
if options.root_copy and options.root_move:
|
|
|
|
print >> sys.stderr, "Only one of --root-move-from and --root-copy from can be specified"
|
|
|
|
exit(1)
|
2013-11-25 07:14:06 +01:00
|
|
|
|
2013-11-25 17:15:45 +01:00
|
|
|
if options.root_copy is not None and not os.path.exists(options.root_copy):
|
|
|
|
print >> sys.stderr, "File specified as root.img does not exists"
|
|
|
|
exit (1)
|
2013-11-25 14:27:43 +01:00
|
|
|
|
2013-11-25 17:15:45 +01:00
|
|
|
if options.root_move is not None and not os.path.exists(options.root_move):
|
2012-02-24 04:24:36 +01:00
|
|
|
print >> sys.stderr, "File specified as root.img does not exists"
|
|
|
|
exit (1)
|
|
|
|
|
2010-04-05 20:58:57 +02:00
|
|
|
qvm_collection = QubesVmCollection()
|
|
|
|
qvm_collection.lock_db_for_writing()
|
|
|
|
qvm_collection.load()
|
|
|
|
|
|
|
|
if qvm_collection.get_vm_by_name(vmname) is not None:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "A VM with the name '{0}' already exists in the system.".format(vmname)
|
2010-04-05 20:58:57 +02:00
|
|
|
exit(1)
|
|
|
|
|
2012-03-14 13:34:01 +01:00
|
|
|
template = None
|
2010-04-05 20:58:57 +02:00
|
|
|
if options.template is not None:
|
2012-03-09 11:01:20 +01:00
|
|
|
template = qvm_collection.get_vm_by_name(options.template)
|
|
|
|
if template is None:
|
2012-10-28 03:49:53 +01:00
|
|
|
print >> sys.stderr, "There is no (Template)VM with the name '{0}'".format(options.template)
|
2010-04-05 20:58:57 +02:00
|
|
|
exit (1)
|
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(options.template)
|
2010-04-05 20:58:57 +02:00
|
|
|
exit (1)
|
|
|
|
if (options.verbose):
|
2012-03-09 11:01:20 +01:00
|
|
|
print "--> Using TemplateVM: {0}".format(template.name)
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2013-11-25 07:14:06 +01:00
|
|
|
elif not options.hvm and not options.hvm_template:
|
2012-03-09 11:01:20 +01:00
|
|
|
if qvm_collection.get_default_template() is None:
|
2012-10-28 03:49:53 +01:00
|
|
|
print >> sys.stderr, "No default TemplateVM defined!"
|
2010-04-05 20:58:57 +02:00
|
|
|
exit (1)
|
|
|
|
else:
|
2012-03-09 11:01:20 +01:00
|
|
|
template = qvm_collection.get_default_template()
|
2010-04-05 20:58:57 +02:00
|
|
|
if (options.verbose):
|
2012-03-09 11:01:20 +01:00
|
|
|
print "--> Using default TemplateVM: {0}".format(template.name)
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2011-04-04 18:41:02 +02:00
|
|
|
if options.standalone:
|
|
|
|
new_vm_template = None
|
|
|
|
else:
|
2012-03-09 11:01:20 +01:00
|
|
|
new_vm_template = template
|
2011-04-04 18:41:02 +02:00
|
|
|
|
2011-03-11 01:48:27 +01:00
|
|
|
vm = None
|
2013-01-22 00:33:35 +01:00
|
|
|
if options.netvm:
|
|
|
|
vmtype = "QubesNetVm"
|
|
|
|
elif options.proxyvm:
|
|
|
|
vmtype = "QubesProxyVm"
|
|
|
|
elif options.hvm:
|
|
|
|
vmtype = "QubesHVm"
|
2013-11-25 07:14:06 +01:00
|
|
|
elif options.hvm_template:
|
|
|
|
vmtype = "QubesTemplateHVm"
|
2013-01-22 00:33:35 +01:00
|
|
|
else:
|
|
|
|
vmtype = "QubesAppVm"
|
|
|
|
|
2012-08-23 03:48:03 +02:00
|
|
|
try:
|
2013-01-22 00:33:35 +01:00
|
|
|
vm = qvm_collection.add_new_vm(vmtype, name=vmname, template=new_vm_template, label = label)
|
2012-08-23 03:48:03 +02:00
|
|
|
except QubesException as err:
|
|
|
|
print >> sys.stderr, "ERROR: {0}".format(err)
|
|
|
|
exit (1)
|
2011-03-16 18:39:54 +01:00
|
|
|
|
2011-04-03 17:47:20 +02:00
|
|
|
if options.internal:
|
|
|
|
vm.internal = True
|
|
|
|
|
2011-03-16 18:39:54 +01:00
|
|
|
if options.mem is not None:
|
|
|
|
vm.memory = options.mem
|
|
|
|
|
|
|
|
if options.vcpus is not None:
|
|
|
|
vm.vcpus = options.vcpus
|
|
|
|
|
2010-04-05 20:58:57 +02:00
|
|
|
try:
|
2012-03-09 11:01:20 +01:00
|
|
|
vm.create_on_disk(verbose=options.verbose, source_template=template)
|
2013-11-25 17:15:45 +01:00
|
|
|
if options.root_move:
|
|
|
|
os.unlink(vm.root_img)
|
|
|
|
os.rename(options.root_move, vm.root_img)
|
|
|
|
elif options.root_copy:
|
2012-02-24 04:24:36 +01:00
|
|
|
os.unlink(vm.root_img)
|
2013-11-25 17:15:45 +01:00
|
|
|
# use "cp" to preserve sparse file
|
|
|
|
subprocess.check_call(["cp", options.root_copy, vm.root_img])
|
2010-04-05 20:58:57 +02:00
|
|
|
|
|
|
|
except (IOError, OSError) as err:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "ERROR: {0}".format(err)
|
2010-04-05 20:58:57 +02:00
|
|
|
exit (1)
|
|
|
|
|
|
|
|
|
|
|
|
qvm_collection.save()
|
|
|
|
qvm_collection.unlock_db()
|
|
|
|
|
|
|
|
|
|
|
|
main()
|