tools: add qvm-create

The tool and tests.
This commit is contained in:
Marek Marczykowski-Górecki 2017-04-28 02:10:00 +02:00
parent 04593b273d
commit b61844ae5d
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724
2 changed files with 243 additions and 0 deletions

View File

@ -0,0 +1,91 @@
# -*- encoding: utf8 -*-
#
# The Qubes OS Project, http://www.qubes-os.org
#
# Copyright (C) 2017 Marek Marczykowski-Górecki
# <marmarek@invisiblethingslab.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with this program; if not, see <http://www.gnu.org/licenses/>.
import qubesmgmt.tests
import qubesmgmt.tests.tools
import qubesmgmt.tools.qvm_create
class TC_00_qvm_create(qubesmgmt.tests.QubesTestCase):
def test_000_just_appvm(self):
self.app.expected_calls[('dom0', 'mgmt.vm.Create.AppVM', None,
b'name=new-vm label=red')] = b'0\x00'
self.app.expected_calls[('dom0', 'mgmt.label.List', None, None)] = \
b'0\x00red\nblue\n'
self.app.expected_calls[('dom0', 'mgmt.vm.List', None, None)] = \
b'0\x00new-vm class=AppVM state=Halted\n'
qubesmgmt.tools.qvm_create.main(['-l', 'red', 'new-vm'], app=self.app)
self.assertAllCalled()
def test_001_missing_vm(self):
with self.assertRaises(SystemExit):
with qubesmgmt.tests.tools.StderrBuffer() as stderr:
qubesmgmt.tools.qvm_create.main(['-l', 'red'], app=self.app)
self.assertIn('NAME', stderr.getvalue())
self.assertAllCalled()
def test_002_custom_template(self):
self.app.expected_calls[('dom0', 'mgmt.vm.Create.AppVM',
'some-template', b'name=new-vm label=red')] = b'0\x00'
self.app.expected_calls[('dom0', 'mgmt.label.List', None, None)] = \
b'0\x00red\nblue\n'
self.app.expected_calls[('dom0', 'mgmt.vm.List', None, None)] = \
b'0\x00new-vm class=AppVM state=Halted\n'
qubesmgmt.tools.qvm_create.main(['-l', 'red', '-t',
'some-template', 'new-vm'], app=self.app)
self.assertAllCalled()
def test_003_properties(self):
self.app.expected_calls[('dom0', 'mgmt.vm.Create.AppVM',
None, b'name=new-vm label=red')] = b'0\x00'
self.app.expected_calls[('dom0', 'mgmt.label.List', None, None)] = \
b'0\x00red\nblue\n'
self.app.expected_calls[('dom0', 'mgmt.vm.List', None, None)] = \
b'0\x00new-vm class=AppVM state=Halted\n'
self.app.expected_calls[('new-vm', 'mgmt.vm.property.Set',
'netvm', b'sys-whonix')] = b'0\x00'
qubesmgmt.tools.qvm_create.main(['-l', 'red', '--prop',
'netvm=sys-whonix', 'new-vm'],
app=self.app)
self.assertAllCalled()
def test_004_pool(self):
self.app.expected_calls[('dom0', 'mgmt.vm.CreateInPool.AppVM',
None, b'name=new-vm label=red pool=some-pool')] = b'0\x00'
self.app.expected_calls[('dom0', 'mgmt.label.List', None, None)] = \
b'0\x00red\nblue\n'
self.app.expected_calls[('dom0', 'mgmt.vm.List', None, None)] = \
b'0\x00new-vm class=AppVM state=Halted\n'
qubesmgmt.tools.qvm_create.main(['-l', 'red', '-P', 'some-pool',
'new-vm'],
app=self.app)
self.assertAllCalled()
def test_005_pools(self):
self.app.expected_calls[('dom0', 'mgmt.vm.CreateInPool.AppVM',
None, b'name=new-vm label=red pool:private=some-pool '
b'pool:volatile=other-pool')] = b'0\x00'
self.app.expected_calls[('dom0', 'mgmt.label.List', None, None)] = \
b'0\x00red\nblue\n'
self.app.expected_calls[('dom0', 'mgmt.vm.List', None, None)] = \
b'0\x00new-vm class=AppVM state=Halted\n'
qubesmgmt.tools.qvm_create.main(['-l', 'red', '--pool',
'private=some-pool', '--pool', 'volatile=other-pool', 'new-vm'],
app=self.app)
self.assertAllCalled()

View File

@ -0,0 +1,152 @@
#
# 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>
# Copyright (C) 2017 Marek Marczykowski-Górecki
# <marmarek@invisiblethingslab.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser 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.
#
'''qvm-create tool'''
# TODO list available classes
# TODO list labels (maybe in qvm-prefs)
# TODO features, devices, tags
from __future__ import print_function
import argparse
import sys
import qubesmgmt
import qubesmgmt.tools
parser = qubesmgmt.tools.QubesArgumentParser()
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',
action=qubesmgmt.tools.PropertyAction,
help='set domain\'s property, like "internal", "memory" or "vcpus"')
parser.add_argument('--pool', '-p',
action='append',
metavar='VOLUME_NAME=POOL_NAME',
help='specify the pool to use for a volume')
parser.add_argument('-P',
metavar='POOL_NAME',
dest='one_pool',
default='',
help='change all volume pools to specified pool')
parser.add_argument('--template', '-t',
action=qubesmgmt.tools.SinglePropertyAction,
help='specify the TemplateVM to use')
parser.add_argument('--label', '-l',
action=qubesmgmt.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)')
# silently ignored
parser_root.add_argument('--no-root',
action='store_true', default=False,
help=argparse.SUPPRESS)
parser.add_argument('name', metavar='VMNAME',
action=qubesmgmt.tools.SinglePropertyAction,
nargs='?',
help='name of the domain to create')
def main(args=None, app=None):
'''Main function of qvm-create tool'''
args = parser.parse_args(args, app=app)
pools = {}
pool = None
if hasattr(args, 'pool') and args.pool:
for pool_vol in args.pool:
try:
volume_name, pool_name = pool_vol.split('=')
pools[volume_name] = pool_name
except ValueError:
parser.error(
'Pool argument must be of form: -P volume_name=pool_name')
if args.one_pool:
pool = args.one_pool
if 'label' not in args.properties:
parser.error('--label option is mandatory')
if 'name' not in args.properties:
parser.error('VMNAME is mandatory')
if args.root_copy_from or args.root_move_from:
parser.error(
'--root-copy-from and --root-move-from not implemented yet')
try:
args.app.get_label(args.properties['label'])
except KeyError:
parser.error('no such label: {!r}; available: {}'.format(
args.properties['label'],
', '.join(repr(l.name) for l in args.app.labels)))
try:
args.app.get_vm_class(args.cls)
except KeyError:
parser.error('no such domain class: {!r}'.format(args.cls))
try:
vm = args.app.add_new_vm(args.cls,
name=args.properties.pop('name'),
label=args.properties.pop('label'),
template=args.properties.pop('template', None),
pool=pool,
pools=pools)
except qubesmgmt.exc.QubesException as e:
args.app.log.error('Error creating VM: {!s}'.format(e))
return 1
retcode = 0
for prop, value in args.properties.items():
try:
setattr(vm, prop, value)
except qubesmgmt.exc.QubesException as e:
args.app.log.error(
'Error setting property {} (but VM created): {!s}'.
format(prop, e))
retcode = 2
return retcode
if __name__ == '__main__':
sys.exit(main())