2015-06-26 11:08:26 +02:00
|
|
|
#!/usr/bin/python2
|
|
|
|
# -*- encoding: utf8 -*-
|
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2010-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2015 Wojtek Porczyk <woju@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.
|
|
|
|
#
|
|
|
|
|
|
|
|
# TODO list available classes
|
|
|
|
# TODO list labels (maybe in qvm-prefs)
|
2016-03-03 13:03:27 +01:00
|
|
|
# TODO features, devices, tags
|
2015-06-26 11:08:26 +02:00
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import qubes
|
|
|
|
import qubes.tools
|
|
|
|
|
|
|
|
|
2015-10-05 13:46:06 +02:00
|
|
|
parser = qubes.tools.QubesArgumentParser(want_force_root=True)
|
2015-06-26 11:08:26 +02:00
|
|
|
|
|
|
|
parser.add_argument('--class', '-C', dest='cls',
|
|
|
|
default='AppVM',
|
|
|
|
help='specify the class of the new domain (default: %(default)s)')
|
|
|
|
|
|
|
|
parser.add_argument('--property', '--prop', '-p',
|
|
|
|
action=qubes.tools.PropertyAction,
|
|
|
|
help='set domain\'s property, like "internal", "memory" or "vcpus"')
|
|
|
|
|
2016-04-01 20:02:39 +02:00
|
|
|
parser.add_argument('--pool', '-P',
|
|
|
|
action='append',
|
|
|
|
metavar='POOL_NAME:VOLUME_NAME',
|
|
|
|
help='specify the pool to use for a volume')
|
2016-03-03 01:05:23 +01:00
|
|
|
|
2015-06-26 11:08:26 +02:00
|
|
|
parser.add_argument('--template', '-t',
|
|
|
|
action=qubes.tools.SinglePropertyAction,
|
|
|
|
help='specify the TemplateVM to use')
|
|
|
|
|
|
|
|
parser.add_argument('--label', '-l',
|
|
|
|
action=qubes.tools.SinglePropertyAction,
|
|
|
|
help='specify the label to use for the new domain'
|
|
|
|
' (e.g. red, yellow, green, ...)')
|
|
|
|
|
|
|
|
parser_root = parser.add_mutually_exclusive_group()
|
|
|
|
parser_root.add_argument('--root-copy-from', '-r', metavar='FILENAME',
|
|
|
|
help='use provided root.img instead of default/empty one'
|
|
|
|
' (file will be COPIED)')
|
|
|
|
parser_root.add_argument('--root-move-from', '-R', metavar='FILENAME',
|
|
|
|
help='use provided root.img instead of default/empty one'
|
|
|
|
' (file will be MOVED)')
|
2015-07-03 14:31:48 +02:00
|
|
|
parser_root.add_argument('--no-root',
|
|
|
|
action='store_true', default=False,
|
|
|
|
help=argparse.SUPPRESS)
|
2015-06-26 11:08:26 +02:00
|
|
|
|
|
|
|
parser.add_argument('name', metavar='VMNAME',
|
|
|
|
action=qubes.tools.SinglePropertyAction,
|
|
|
|
nargs='?',
|
|
|
|
help='name of the domain to create')
|
|
|
|
|
|
|
|
|
2015-10-05 17:34:13 +02:00
|
|
|
def main(args=None):
|
|
|
|
args = parser.parse_args(args)
|
2015-06-26 11:08:26 +02:00
|
|
|
|
2016-04-01 20:02:39 +02:00
|
|
|
if args.pool:
|
|
|
|
args.properties['volume_config'] = {}
|
|
|
|
for pool_vol in args.pool:
|
|
|
|
try:
|
|
|
|
pool_name, volume_name = pool_vol.split(':')
|
|
|
|
config = {'pool': pool_name, 'name': volume_name}
|
|
|
|
args.properties['volume_config'][volume_name] = config
|
|
|
|
except ValueError:
|
|
|
|
parser.error(
|
|
|
|
'Pool argument must be of form: -P pool_name:volume_name')
|
|
|
|
|
2015-06-26 11:08:26 +02:00
|
|
|
if 'label' not in args.properties:
|
|
|
|
parser.error('--label option is mandatory')
|
|
|
|
|
|
|
|
if 'name' not in args.properties:
|
|
|
|
parser.error('VMNAME is mandatory')
|
|
|
|
|
|
|
|
try:
|
2015-10-05 23:46:25 +02:00
|
|
|
args.app.get_label(args.properties['label'])
|
2015-06-26 11:08:26 +02:00
|
|
|
except KeyError:
|
2015-10-05 23:46:25 +02:00
|
|
|
parser.error('no such label: {!r}; available: {}'.format(
|
|
|
|
args.properties['label'],
|
2015-10-05 17:34:13 +02:00
|
|
|
', '.join(repr(l.name) for l in args.app.labels)))
|
2015-06-26 11:08:26 +02:00
|
|
|
|
|
|
|
try:
|
2016-03-04 13:03:43 +01:00
|
|
|
cls = args.app.get_vm_class(args.cls)
|
2015-06-26 11:08:26 +02:00
|
|
|
except KeyError:
|
|
|
|
parser.error('no such domain class: {!r}'.format(args.cls))
|
|
|
|
|
2015-09-23 15:41:58 +02:00
|
|
|
if 'template' in args.properties and \
|
|
|
|
'template' not in (prop.__name__ for prop in cls.property_list()):
|
2015-06-26 11:08:26 +02:00
|
|
|
parser.error('this domain class does not support template')
|
|
|
|
|
2015-10-05 17:34:13 +02:00
|
|
|
vm = args.app.add_new_vm(cls, **args.properties)
|
2015-06-26 11:08:26 +02:00
|
|
|
|
2015-10-05 23:46:25 +02:00
|
|
|
# pylint: disable=line-too-long
|
|
|
|
|
2015-06-26 11:08:26 +02:00
|
|
|
# if not options.standalone and any([options.root_copy_from, options.root_move_from]):
|
|
|
|
# print >> sys.stderr, "root.img can be specified only for standalone VMs"
|
|
|
|
# exit (1)
|
|
|
|
|
|
|
|
# if options.hvm_template and options.template is not None:
|
|
|
|
# print >> sys.stderr, "Template VM cannot be based on another template"
|
|
|
|
# exit (1)
|
|
|
|
|
|
|
|
# if options.root_copy_from is not None and not os.path.exists(options.root_copy_from):
|
|
|
|
# print >> sys.stderr, "File specified as root.img does not exists"
|
|
|
|
# exit (1)
|
|
|
|
|
|
|
|
# if options.root_move_from is not None and not os.path.exists(options.root_move_from):
|
|
|
|
# print >> sys.stderr, "File specified as root.img does not exists"
|
|
|
|
# exit (1)
|
|
|
|
|
|
|
|
# elif not options.hvm and not options.hvm_template:
|
|
|
|
# if qvm_collection.get_default_template() is None:
|
|
|
|
# print >> sys.stderr, "No default TemplateVM defined!"
|
|
|
|
# exit (1)
|
|
|
|
# else:
|
|
|
|
# template = qvm_collection.get_default_template()
|
|
|
|
# if (options.verbose):
|
|
|
|
# print('--> Using default TemplateVM: {0}'.format(template.name))
|
|
|
|
|
2015-07-03 14:31:48 +02:00
|
|
|
if not args.no_root:
|
|
|
|
try:
|
|
|
|
vm.create_on_disk()
|
|
|
|
|
|
|
|
if args.root_move_from is not None:
|
|
|
|
# if (options.verbose):
|
|
|
|
# print "--> Replacing root.img with provided file"
|
|
|
|
os.unlink(vm.root_img)
|
2015-10-05 23:46:25 +02:00
|
|
|
os.rename(args.root_move_from, vm.root_img)
|
2015-07-03 14:31:48 +02:00
|
|
|
elif args.root_copy_from is not None:
|
|
|
|
# if (options.verbose):
|
|
|
|
# print "--> Replacing root.img with provided file"
|
|
|
|
os.unlink(vm.root_img)
|
|
|
|
# use 'cp' to preserve sparse file
|
2015-10-05 23:46:25 +02:00
|
|
|
subprocess.check_call(['cp', args.root_copy_from, vm.root_img])
|
2015-07-03 14:31:48 +02:00
|
|
|
|
|
|
|
except (IOError, OSError) as err:
|
|
|
|
parser.error(str(err))
|
2015-06-26 11:08:26 +02:00
|
|
|
|
2015-10-05 17:34:13 +02:00
|
|
|
args.app.save()
|
2015-06-26 11:08:26 +02:00
|
|
|
|
2015-12-28 18:14:37 +01:00
|
|
|
return 0
|
2015-06-26 11:08:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2015-12-28 18:14:37 +01:00
|
|
|
sys.exit(main())
|