Skip to content

Commit

Permalink
Add list command to td_virsh_tool (#55)
Browse files Browse the repository at this point in the history
To be able to list current VMs, this is important to see the forwarding ports
for ssh connection
  • Loading branch information
hector-cao authored Apr 11, 2024
1 parent 73a307b commit 74bd27a
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions guest-tools/td_virsh_tool.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ main() {
create_overlay_image
create_domain_xml
boot_vm
echo_ssh_cmd
echo_ssh_cmd ${domain}
done
}

Expand All @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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"
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 74bd27a

Please sign in to comment.