From 03b21369e35713509d1c6d044f976b5ae1b6f65f Mon Sep 17 00:00:00 2001 From: gryf Date: Fri, 13 Aug 2021 11:12:12 +0200 Subject: [PATCH] Added new option for disabling nested virtualization. By default, nested virtualization is enabled. It can be disabled by passing --disable-nested. --- README.rst | 4 ++-- box.py | 23 ++++++++++++++++++----- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/README.rst b/README.rst index 79a3567..4ad8016 100644 --- a/README.rst +++ b/README.rst @@ -98,8 +98,8 @@ Currently, following commands are available: - ``ssh`` - connect to the VM using ssh - ``completion`` - as described above -All of the commands have a range of options, and can be examined it by using -``--help``. +All of the commands have a range of options, and can be examined by using +``--help`` option. What is more interesting though, is the fact, that you can pass your own `cloud-init`_ yaml file, so that VM can be provisioned in easy way. diff --git a/box.py b/box.py index 055e63f..4a0e285 100755 --- a/box.py +++ b/box.py @@ -133,8 +133,8 @@ _boxpy() { fi ;; create|rebuild) - items=(--cpus --disk-size --distro --forwarding --key --memory - --hostname --port --config --version) + items=(--cpus --disable-nested --disk-size --distro --forwarding + --key --memory --hostname --port --config --version) if [[ ${prev} == ${cmd} ]]; then if [[ ${cmd} = "rebuild" ]]; then _vms_comp vms @@ -332,14 +332,16 @@ class FakeLogger: class Config: - ATTRS = ('cpus', 'config', 'creator', 'disk_size', 'distro', 'forwarding', - 'hostname', 'key', 'memory', 'name', 'port', 'version') + ATTRS = ('cpus', 'config', 'creator', 'disable_nested', 'disk_size', + 'distro', 'forwarding', 'hostname', 'key', 'memory', 'name', + 'port', 'version') def __init__(self, args, vbox=None): self.advanced = None self.distro = None self.cpus = None self.creator = None + self.disable_nested = 'False' self.disk_size = None self.forwarding = {} self.hostname = None @@ -686,6 +688,13 @@ class VBoxManage: LOG.fatal(f'Cannot modify VM "{self.name_or_uuid}"') raise BoxVBoxFailure() + if conf.disable_nested == 'False': + if Run(['vboxmanage', 'modifyvm', self.name_or_uuid, + '--nested-hw-virt', 'on']).returncode != 0: + LOG.fatal(f'Cannot set nested virtualization for VM ' + f'"{self.name_or_uuid}"') + raise BoxVBoxFailure() + return self.uuid def convertfromraw(self, src, dst): @@ -1069,7 +1078,7 @@ def vmcreate(args, conf=None): if not vbox.create_controller('SATA', 'sata'): return 4 - for key in ('distro', 'key', 'hostname', 'version'): + for key in ('distro', 'hostname', 'key', 'version'): if not vbox.setextradata(key, getattr(conf, key)): return 5 @@ -1315,6 +1324,8 @@ def main(): help="VM hostname. Default same as vm name") create.add_argument('-p', '--port', help="set ssh port for VM, default " "random port from range 2000-2999") + create.add_argument('-r', '--disable-nested', action='store_true', + help="disable nested virtualization") create.add_argument('-s', '--disk-size', help="disk size to be expanded " "to. By default to 10GB") create.add_argument('-u', '--cpus', type=int, help="amount of CPUs to be " @@ -1353,6 +1364,8 @@ def main(): 'Megabytes') rebuild.add_argument('-n', '--hostname', help="set VM hostname") rebuild.add_argument('-p', '--port', help="set ssh port for VM") + rebuild.add_argument('-r', '--disable-nested', action="store_true", + help="disable nested virtualization") rebuild.add_argument('-s', '--disk-size', help='disk size to be expanded to') rebuild.add_argument('-u', '--cpus', type=int,