Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the case of ssh proxy for vsock #6224

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions libvirt/tests/cfg/virtual_device/vsock.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
60 changes: 60 additions & 0 deletions libvirt/tests/src/virtual_device/vsock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"])
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
Loading