Author: Pekka Helenius Date: Sun, 6 Jun 2021 15:09:21 +0200 Subject: [PATCH] GNS3 Server: QEMU environment variables. Add support for QEMU environment variables for GNS3 Server. --- a/gns3server/schemas/qemu.py +++ b/gns3server/schemas/qemu.py @@ -65,6 +65,10 @@ "description": "Console type", "enum": ["telnet", "vnc", "spice"] }, + "env_vars": { + "description": "Environment variables", + "type": "string", + }, "hda_disk_image": { "description": "QEMU hda disk image path", "type": "string", @@ -250,6 +254,10 @@ "description": "Whether the VM is a linked clone or not", "type": "boolean" }, + "env_vars": { + "description": "Environment variables", + "type": "string", + }, "hda_disk_image": { "description": "QEMU hda disk image path", "type": "string", @@ -434,6 +442,10 @@ "description": "Platform to emulate", "enum": QEMU_PLATFORMS }, + "env_vars": { + "description": "Environment variables", + "type": "string", + }, "hda_disk_image": { "description": "QEMU hda disk image path", "type": "string", @@ -603,6 +615,7 @@ "qemu_path", "platform", "console_type", + "env_vars", "hda_disk_image", "hdb_disk_image", "hdc_disk_image", --- a/gns3server/schemas/qemu_template.py +++ b/gns3server/schemas/qemu_template.py @@ -22,6 +22,11 @@ QEMU_TEMPLATE_PROPERTIES = { + "env_vars": { + "description": "Environment variables", + "type": "string", + "default": "" + }, "qemu_path": { "description": "Path to QEMU", "type": "string", --- a/gns3server/compute/qemu/qemu_vm.py +++ b/gns3server/compute/qemu/qemu_vm.py @@ -93,6 +93,7 @@ else: self.platform = platform + self._env_vars = "" self._hda_disk_image = "" self._hdb_disk_image = "" self._hdc_disk_image = "" @@ -219,6 +220,32 @@ disk_image=value)) @property + def env_vars(self): + """ + Returns the environment variables for this QEMU VM. + + :returns: QEMU env vars + """ + + return self._env_vars + + @env_vars.setter + def env_vars(self, env_vars): + """ + Sets the environment variables for this QEMU VM. + + :param env vars: QEMU env vars + """ + + log.info('QEMU VM "{name}" [{id}] has set the QEMU environment variables to {env_vars}'.format(name=self._name, + id=self._id, + env_vars=env_vars)) + if sys.platform.startswith("linux"): + self._env_vars = env_vars.strip() + else: + self._env_vars = "" + + @property def hda_disk_image(self): """ Returns the hda disk image path for this QEMU VM. @@ -912,7 +939,7 @@ with open(self._stdout_file, "w", encoding="utf-8") as fd: fd.write("Start QEMU with {}\n\nExecution log:\n".format(command_string)) self.command_line = ' '.join(command) - self._process = await asyncio.create_subprocess_exec(*command, + self._process = await asyncio.create_subprocess_shell(command_string, stdout=fd, stderr=subprocess.STDOUT, cwd=self.working_dir) @@ -1651,7 +1678,10 @@ additional_options = additional_options.replace("%vm-id%", self._id) additional_options = additional_options.replace("%project-id%", self.project.id) additional_options = additional_options.replace("%project-path%", '"' + self.project.path.replace('"', '\\"') + '"') - command = [self.qemu_path] + command = [] + if self._env_vars: + command.extend([self._env_vars]) + command.extend([self.qemu_path]) command.extend(["-name", self._name]) command.extend(["-m", "{}M".format(self._ram)]) command.extend(["-smp", "cpus={}".format(self._cpus)])