From 638038decb4c221845234793a9824782745cc985 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 5 Mar 2025 00:51:12 -0500 Subject: [PATCH] Add the case of ssh proxy for vsock Signed-off-by: Lili Zhu --- libvirt/tests/cfg/virtual_device/vsock.cfg | 5 ++ libvirt/tests/src/virtual_device/vsock.py | 60 ++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/libvirt/tests/cfg/virtual_device/vsock.cfg b/libvirt/tests/cfg/virtual_device/vsock.cfg index a2f01b4ba9..9e7ff1821b 100644 --- a/libvirt/tests/cfg/virtual_device/vsock.cfg +++ b/libvirt/tests/cfg/virtual_device/vsock.cfg @@ -31,6 +31,11 @@ - edit_start: start_vm = "no" edit_xml = "yes" + - ssh_proxy: + start_vm = "no" + guest_user = "guest_user" + guest_user_passwd = "123456" + systemd_version_cmd = "rpm -q systemd | awk -F'-' '{print $2}'" - negative_test: status_error = no variants: diff --git a/libvirt/tests/src/virtual_device/vsock.py b/libvirt/tests/src/virtual_device/vsock.py index e8df713ac8..39edb9377e 100644 --- a/libvirt/tests/src/virtual_device/vsock.py +++ b/libvirt/tests/src/virtual_device/vsock.py @@ -9,6 +9,8 @@ from avocado.utils import process +from virttest import libvirt_version +from virttest import ssh_key from virttest import virsh from virttest import utils_test from virttest import utils_misc @@ -183,6 +185,43 @@ def managedsave_restore(): result = virsh.start(vm_name) utils_test.libvirt.check_exit_status(result, expect_error=False) + def add_guest_user(name, passwd): + """ + Added a user account in guest + + :param name: user name + :param passwd: password of user account + """ + try: + session = vm.wait_for_login() + session.cmd_output('useradd %s' % name) + finally: + session.close() + virsh.set_user_password(vm_name, name, passwd, debug=True) + + def forward_vsock_to_sshd(): + """ + Run a service that forwards VSOCK <=> SSHD communication + """ + + session = vm.wait_for_login() + try: + session.cmd("socat VSOCK-LISTEN:22,reuseaddr,fork TCP:localhost:22 &") + except Exception as e: + test.fail("Forwarding vsock to sshd communication failed: %s" % e) + + def check_systemd_version(): + """ + Check the version of systemd inside the guest + """ + try: + session = vm.wait_for_login() + systemd_version = session.cmd_output(systemd_version_cmd) + logging.debug("systemd version of guest is %s" % systemd_version) + finally: + session.close() + return systemd_version + start_vm = params.get("start_vm", "no") vm_name = params.get("main_vm", "avocado-vt-vm1") vm = env.get_vm(params["main_vm"]) @@ -197,6 +236,9 @@ def managedsave_restore(): vsock_num = params.get("num") communication = params.get("communication", "no") == "yes" detach_device_alias = params.get("detach_device_alias", "no") == "yes" + guest_user = params.get("guest_user") + guest_user_passwd = params.get("guest_user_passwd") + systemd_version_cmd = params.get("systemd_version_cmd") # Backup xml file vmxml = vm_xml.VMXML.new_from_inactive_dumpxml(vm_name) backup_xml = vmxml.copy() @@ -244,6 +286,24 @@ def managedsave_restore(): else: result = virsh.start(vm_name, debug=True) utils_test.libvirt.check_exit_status(result, expect_error=False) + elif guest_user: + if not libvirt_version.version_compare(10, 4, 0): + test.cancel("SSH proxy for vsock is not supported in this version.") + else: + vmxml.add_device(vsock_dev) + vmxml.sync() + logging.debug(vmxml) + virsh.start(vm_name, ignore_status=False) + vm_ip = vm.wait_for_get_address(0, timeout=240) + add_guest_user(guest_user, guest_user_passwd) + ssh_key.setup_ssh_key(vm_ip, guest_user, guest_user_passwd) + + if int(check_systemd_version()) < 256: + forward_vsock_to_sshd() + + ssh_proxy_cmd = "ssh %s@qemu/%s hostname" % (guest_user, vm_name) + if process.run(ssh_proxy_cmd, shell=True).exit_status != 0: + test.fail("ssh proxy for vsock is not working.") else: session = vm.wait_for_login() session.close()