From 74bd27a5ad8adaff890870b6d653a7f61bd1367f Mon Sep 17 00:00:00 2001 From: Hector Cao <122458375+hector-cao@users.noreply.github.com> Date: Thu, 11 Apr 2024 05:05:10 +0200 Subject: [PATCH] Add list command to td_virsh_tool (#55) To be able to list current VMs, this is important to see the forwarding ports for ssh connection --- guest-tools/td_virsh_tool.sh | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/guest-tools/td_virsh_tool.sh b/guest-tools/td_virsh_tool.sh index af51189..f4c7bb7 100755 --- a/guest-tools/td_virsh_tool.sh +++ b/guest-tools/td_virsh_tool.sh @@ -44,7 +44,7 @@ main() { create_overlay_image create_domain_xml boot_vm - echo_ssh_cmd + echo_ssh_cmd ${domain} done } @@ -59,6 +59,7 @@ Available options: -h, --help Print this help and exit -c D, --clean D Clean domain with name D (use "all" to clean all) -n N, --n_instances N Launch N instances +-l, --list List VMs Environment variables: @@ -75,6 +76,10 @@ parse_params() { usage exit 0 ;; + -l | --list) + print_all + exit 0 + ;; -n | --n_instances) n_instances="${2-}" if [ -z ${n_instances} ]; then @@ -157,19 +162,20 @@ boot_vm() { } echo_ssh_cmd() { + _domain=$1 local host_port=$( virsh \ - qemu-monitor-command ${domain} \ + qemu-monitor-command ${_domain} \ --hmp info usernet | awk '/HOST_FORWARD/ {print $4}' ) local guest_cid=$( virsh \ - qemu-monitor-command ${domain} \ + qemu-monitor-command ${_domain} \ --hmp info qtree | awk '/guest-cid/ {print $3}' ) - echo "Domain ${domain} running with vsock CID: ${guest_cid}," \ + echo "Domain ${_domain} running with vsock CID: ${guest_cid}," \ "ssh -p ${host_port} root@localhost" } @@ -212,6 +218,12 @@ clean_all() { exit 0 } +print_all() { + for domain_to_print in $(virsh list --state-running --name | grep ${DOMAIN_PREFIX}); do + echo_ssh_cmd ${domain_to_print} + done +} + on_exit() { rc=$? if [ ${rc} -ne 0 ]; then