From 524fff814b3892c0c7304709d8efb5279087d4de Mon Sep 17 00:00:00 2001 From: grossmj Date: Sun, 1 Nov 2026 18:48:41 +0700 Subject: [PATCH] feat: add support for running aarch64 Qemu VMs --- gns3server/compute/qemu/qemu_vm.py | 102 +++++++++------- gns3server/config_samples/gns3_server.conf | 3 + gns3server/schemas/config.py | 1 + gns3server/schemas/controller/settings.py | 1 + tests/compute/qemu/test_qemu_vm.py | 129 +++++++++++++++++++++ 5 files changed, 191 insertions(+), 45 deletions(-) diff --git a/gns3server/compute/qemu/qemu_vm.py b/gns3server/compute/qemu/qemu_vm.py index d3fa493af..23540e8c1 100644 --- a/gns3server/compute/qemu/qemu_vm.py +++ b/gns3server/compute/qemu/qemu_vm.py @@ -2049,6 +2049,23 @@ class QemuVM(KernelDatapathMixin, BaseNode): super(QemuVM, QemuVM).console_type.__set__(self, new_console_type) + def _arm64_options(self, additional_options): + + if self.platform == "aarch64": + arm64_machine = platform.machine() in ("aarch64", "arm64") + if arm64_machine: + arm64_options = ["-machine", "virt,gic-version=host"] + else: + arm64_options = ["-machine", "virt,gic-version=3"] + if "-cpu" not in additional_options: + if arm64_machine: + arm64_options.extend(["-cpu", "max"]) + else: + arm64_options.extend(["-cpu", "neoverse-n1"]) + return arm64_options + else: + return [] + def _serial_options(self, internal_console_port, external_console_port): if external_console_port: @@ -2512,54 +2529,48 @@ class QemuVM(KernelDatapathMixin, BaseNode): options.extend(["-bios", self._bios_image.replace(",", ",,")]) elif self._uefi: - system_ovmf_firmware_dir = Path(self.manager.config.settings.Qemu.ovmf_firmware_dir) - log.debug(f"Using OVMF firmware directory: {system_ovmf_firmware_dir}") - old_ovmf_vars_path = os.path.join(self.working_dir, "OVMF_VARS.fd") - if os.path.exists(old_ovmf_vars_path): - # the node has its own UEFI variables store already, we must also use the old UEFI firmware - ovmf_firmware_path = self.manager.get_abs_image_path("OVMF_CODE.fd") + if self.platform == "aarch64": + firmware_dir = Path(self.manager.config.settings.Qemu.aavmf_firmware_dir) + firmware_name = "AAVMF_CODE.fd" + vars_name = "AAVMF_VARS.fd" else: - # Use a manual case-insensitive search instead - try: - system_ovmf_firmware_path = next( - (f for f in system_ovmf_firmware_dir.glob("*.fd") if f.name.lower() == "ovmf_code_4m.fd"), None - ) - except (FileNotFoundError, StopIteration): - system_ovmf_firmware_path = None - - if system_ovmf_firmware_path: - ovmf_firmware_path = str(system_ovmf_firmware_path) + firmware_dir = Path(self.manager.config.settings.Qemu.ovmf_firmware_dir) + firmware_name = "OVMF_CODE_4M.fd" + vars_name = "OVMF_VARS_4M.fd" + log.debug(f"Using UEFI firmware directory: {firmware_dir}") + old_ovmf_vars_path = os.path.join(self.working_dir, "OVMF_VARS.fd") + if self.platform != "aarch64" and os.path.exists(old_ovmf_vars_path): + # the node has its own UEFI variables store already, we must also use the old UEFI firmware + firmware_path = self.manager.get_abs_image_path("OVMF_CODE.fd") + vars_node_path = old_ovmf_vars_path + else: + system_firmware_path = next( + (f for f in firmware_dir.glob("*.fd") if f.name.lower() == firmware_name.lower()), None + ) + if system_firmware_path: + firmware_path = str(system_firmware_path) else: # otherwise, get the UEFI firmware from the images directory - ovmf_firmware_path = self.manager.get_abs_image_path("OVMF_CODE_4M.fd") + firmware_path = self.manager.get_abs_image_path(firmware_name) - log.debug(f"Configuring UEFI boot mode using OVMF file: '{ovmf_firmware_path}'") - options.extend(["-drive", f"if=pflash,format=raw,readonly,file={ovmf_firmware_path}"]) - - # try to use the UEFI variables store from the system first - try: - system_ovmf_vars_path = next( - (f for f in system_ovmf_firmware_dir.glob("*.fd") if f.name.lower() == "ovmf_vars_4m.fd"), None - ) - except (FileNotFoundError, StopIteration): - system_ovmf_vars_path = None - if system_ovmf_vars_path: - ovmf_vars_path = str(system_ovmf_vars_path) - else: - # otherwise, get the UEFI variables store from the images directory - ovmf_vars_path = self.manager.get_abs_image_path("OVMF_VARS_4M.fd") - - # the node should have its own copy of OVMF_VARS.fd (the UEFI variables store) - if os.path.exists(old_ovmf_vars_path): - ovmf_vars_node_path = old_ovmf_vars_path - else: - ovmf_vars_node_path = os.path.join(self.working_dir, "OVMF_VARS_4M.fd") - if not os.path.exists(ovmf_vars_node_path): + # Each node must have its own writable UEFI variables store. + vars_node_path = os.path.join(self.working_dir, vars_name) + if not os.path.exists(vars_node_path): + system_vars_path = next( + (f for f in firmware_dir.glob("*.fd") if f.name.lower() == vars_name.lower()), None + ) + if system_vars_path: + vars_path = str(system_vars_path) + else: + vars_path = self.manager.get_abs_image_path(vars_name) try: - shutil.copyfile(ovmf_vars_path, ovmf_vars_node_path) + shutil.copyfile(vars_path, vars_node_path) except OSError as e: - raise QemuError(f"Cannot copy OVMF_VARS_4M.fd file to the node working directory: {e}") - options.extend(["-drive", f"if=pflash,format=raw,file={ovmf_vars_node_path}"]) + raise QemuError(f"Cannot copy {vars_name} file to the node working directory: {e}") + + log.debug(f"Configuring UEFI boot mode using firmware file: '{firmware_path}'") + options.extend(["-drive", f"if=pflash,format=raw,readonly,file={firmware_path}"]) + options.extend(["-drive", f"if=pflash,format=raw,file={vars_node_path}"]) # edk2 firmware requires a Random Number Generator (RNG) device in order to turn network adapters on options.extend(["-object", "rng-random,filename=/dev/urandom,id=rng0"]) @@ -2910,10 +2921,10 @@ class QemuVM(KernelDatapathMixin, BaseNode): if self._cpus > maxcpus: maxcpus = self._cpus command.extend(["-smp", f"cpus={self._cpus},maxcpus={maxcpus},sockets=1"]) - kvm = False + kvm_enabled = False if await self._run_with_hardware_acceleration(self.qemu_path, self._options): command.extend(["-machine", "accel=kvm"]) - kvm = True + kvm_enabled = True command.extend(["-boot", f"order={self._boot_priority}"]) command.extend(self._cdrom_option()) command.extend(await self._disk_options()) @@ -2945,7 +2956,8 @@ class QemuVM(KernelDatapathMixin, BaseNode): for opt in additional_opt_list: if opt in FORBIDDEN_OPTIONS: raise QemuError(f"Forbidden additional option: {opt}") - if kvm: + command.extend(self._arm64_options(additional_options)) + if kvm_enabled: kvmclock_off_reason = self._kvmclock_off_reason() if kvmclock_off_reason: log.info( diff --git a/gns3server/config_samples/gns3_server.conf b/gns3server/config_samples/gns3_server.conf index 357940199..670adc51d 100644 --- a/gns3server/config_samples/gns3_server.conf +++ b/gns3server/config_samples/gns3_server.conf @@ -191,6 +191,9 @@ kvmclock = auto allow_unsafe_options = False ; Path to the OVMF firmware directory ovmf_firmware_dir = "/usr/share/OVMF" +; Path to the aarch64 UEFI firmware directory (AAVMF_CODE.fd and AAVMF_VARS.fd) +; Firmware files are also searched for in the QEMU images directory +aavmf_firmware_dir = "/usr/share/AAVMF" [WebWireshark] ; Enable Web Wireshark feature (container-based Wireshark in browser) diff --git a/gns3server/schemas/config.py b/gns3server/schemas/config.py index a2c513ddd..15e9813bd 100644 --- a/gns3server/schemas/config.py +++ b/gns3server/schemas/config.py @@ -101,6 +101,7 @@ class QemuSettings(BaseModel): ) allow_unsafe_options: bool = Field(False, description="Allow unsafe additional command line options") ovmf_firmware_dir: str = Field("/usr/share/OVMF", description="Path to the OVMF firmware directory") + aavmf_firmware_dir: str = Field("/usr/share/AAVMF", description="Path to the AAVMF firmware directory for aarch64") model_config = ConfigDict(validate_assignment=True, str_strip_whitespace=True) diff --git a/gns3server/schemas/controller/settings.py b/gns3server/schemas/controller/settings.py index 6bfd1e4a2..ab94f3309 100644 --- a/gns3server/schemas/controller/settings.py +++ b/gns3server/schemas/controller/settings.py @@ -198,6 +198,7 @@ class QemuSettingsUpdate(BaseModel): kvmclock: Optional[QemuKvmclock] = None allow_unsafe_options: Optional[bool] = None ovmf_firmware_dir: Optional[str] = None + aavmf_firmware_dir: Optional[str] = None class WebWiresharkSettingsUpdate(BaseModel): diff --git a/tests/compute/qemu/test_qemu_vm.py b/tests/compute/qemu/test_qemu_vm.py index e1fcd2179..96b1d5e4f 100644 --- a/tests/compute/qemu/test_qemu_vm.py +++ b/tests/compute/qemu/test_qemu_vm.py @@ -78,6 +78,15 @@ async def vm(compute_project, manager, fake_qemu_binary, fake_qemu_img_binary): return vm +@pytest.fixture(scope="function") +def uefi_vm(vm, tmp_path, monkeypatch): + + working_dir = tmp_path / "node" + working_dir.mkdir() + monkeypatch.setattr(vm.project, "node_working_directory", lambda node: str(working_dir)) + return vm + + @pytest.fixture(scope="function") def running_subprocess_mock(): @@ -452,6 +461,101 @@ async def test_uefi_boot_mode_option(vm, tmpdir, images_dir, fake_qemu_img_binar ) in " ".join(options) +@pytest.mark.asyncio +@pytest.mark.parametrize( + "guest_platform,dir_setting,code_name,vars_name", + [ + ("x86_64", "ovmf_firmware_dir", "OVMF_CODE_4M.fd", "OVMF_VARS_4M.fd"), + ("aarch64", "aavmf_firmware_dir", "AAVMF_CODE.fd", "AAVMF_VARS.fd"), + ], +) +@pytest.mark.parametrize("system_code,system_vars", [(False, False), (True, True), (True, False), (False, True)]) +async def test_uefi_firmware_selection( + uefi_vm, tmp_path, images_dir, guest_platform, dir_setting, code_name, vars_name, system_code, system_vars +): + + vm = uefi_vm + vm._uefi = True + vm._platform = guest_platform + firmware_dir = tmp_path / "firmware" + firmware_dir.mkdir() + setattr(vm.manager.config.settings.Qemu, dir_setting, str(firmware_dir)) + code_path = firmware_dir / code_name.lower() if system_code else Path(images_dir) / code_name + vars_path = firmware_dir / vars_name.lower() if system_vars else Path(images_dir) / vars_name + code_path.write_bytes(b"firmware") + vars_path.write_bytes(b"initial variables") + if guest_platform == "aarch64": + # An old x86 variables store must not select x86 firmware for an ARM guest. + (Path(vm.working_dir) / "OVMF_VARS.fd").write_bytes(b"x86 variables") + + options = vm._bios_option() + vars_node_path = Path(vm.working_dir) / vars_name + assert options == [ + "-drive", + f"if=pflash,format=raw,readonly,file={code_path}", + "-drive", + f"if=pflash,format=raw,file={vars_node_path}", + "-object", + "rng-random,filename=/dev/urandom,id=rng0", + "-device", + "virtio-rng-pci,rng=rng0", + ] + assert vars_node_path.read_bytes() == b"initial variables" + command = await vm._build_command() + assert " ".join(options) in " ".join(command) + vars_node_path.write_bytes(b"updated variables") + assert vm._bios_option() == options + assert vars_node_path.read_bytes() == b"updated variables" + assert vars_path.read_bytes() == b"initial variables" + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "guest_platform,dir_setting,code_name,vars_name", + [ + ("x86_64", "ovmf_firmware_dir", "OVMF_CODE.fd", "OVMF_VARS.fd"), + ("x86_64", "ovmf_firmware_dir", "OVMF_CODE_4M.fd", "OVMF_VARS_4M.fd"), + ("aarch64", "aavmf_firmware_dir", "AAVMF_CODE.fd", "AAVMF_VARS.fd"), + ], +) +async def test_uefi_existing_variables_store( + uefi_vm, tmp_path, images_dir, guest_platform, dir_setting, code_name, vars_name +): + + vm = uefi_vm + vm._uefi = True + vm._platform = guest_platform + setattr(vm.manager.config.settings.Qemu, dir_setting, str(tmp_path / "missing")) + code_path = Path(images_dir) / code_name + code_path.write_bytes(b"firmware") + vars_node_path = Path(vm.working_dir) / vars_name + vars_node_path.write_bytes(b"existing variables") + + options = vm._bios_option() + assert options[:4] == [ + "-drive", + f"if=pflash,format=raw,readonly,file={code_path}", + "-drive", + f"if=pflash,format=raw,file={vars_node_path}", + ] + assert vars_node_path.read_bytes() == b"existing variables" + + +@pytest.mark.asyncio +async def test_aarch64_uefi_variables_copy_error(uefi_vm, tmp_path): + + vm = uefi_vm + vm._uefi = True + vm._platform = "aarch64" + (tmp_path / "AAVMF_CODE.fd").write_bytes(b"firmware") + (tmp_path / "AAVMF_VARS.fd").write_bytes(b"variables") + vm.manager.config.settings.Qemu.aavmf_firmware_dir = str(tmp_path) + + with patch("gns3server.compute.qemu.qemu_vm.shutil.copyfile", side_effect=OSError("Permission denied")): + with pytest.raises(QemuError, match=r"Cannot copy AAVMF_VARS\.fd file"): + vm._bios_option() + + @pytest.mark.asyncio async def test_uefi_with_bios_image_already_configured(vm, tmpdir, fake_qemu_img_binary): @@ -547,6 +651,31 @@ async def test_set_process_priority_normal(vm, fake_qemu_img_binary): assert not process.called +@pytest.mark.parametrize( + ("host_machine", "expected"), + [ + ("x86_64", ["-machine", "virt,gic-version=3", "-cpu", "neoverse-n1"]), + ("aarch64", ["-machine", "virt,gic-version=host", "-cpu", "max"]), + ("arm64", ["-machine", "virt,gic-version=host", "-cpu", "max"]), + ], +) +def test_arm64_options(vm, host_machine, expected): + vm._platform = "aarch64" + with patch("gns3server.compute.qemu.qemu_vm.platform.machine", return_value=host_machine): + assert vm._arm64_options("") == expected + + +def test_arm64_options_with_custom_cpu(vm): + vm._platform = "aarch64" + with patch("gns3server.compute.qemu.qemu_vm.platform.machine", return_value="x86_64"): + assert vm._arm64_options("-cpu cortex-a72") == ["-machine", "virt,gic-version=3"] + + +def test_arm64_options_for_other_platform(vm): + vm._platform = "x86_64" + assert vm._arm64_options("") == [] + + def test_json(vm, compute_project): json = vm.asdict()