diff mbox series

[2/5] guestfs: bringup: check if domain exists

Message ID 20241015-bringup-guestfs-debug-v1-2-bd74c0c31412@samsung.com (mailing list archive)
State New
Headers show
Series guestfs: bringup: add debug mode | expand

Commit Message

Daniel Gomez via B4 Relay Oct. 15, 2024, 11:34 a.m. UTC
From: Daniel Gomez <da.gomez@samsung.com>

virsh domstate fails (exit 1) if domain checked does not exist. This
conflicts when debugging the script with set -euxo pipefile. Replace the
command with virsh list --all and search for the domain in the list.
Also, capture the state to avoid trying to start a domain that is
already running.

Signed-off-by: Daniel Gomez <da.gomez@samsung.com>
---
 scripts/bringup_guestfs.sh | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/scripts/bringup_guestfs.sh b/scripts/bringup_guestfs.sh
index 32aadba0f4e5a0abc81d247f51cdddd0f2255785..5786975e48647321ecbc71ca9be6fb09c8aa63ff 100755
--- a/scripts/bringup_guestfs.sh
+++ b/scripts/bringup_guestfs.sh
@@ -334,10 +334,12 @@  do
 	# If the guest is already defined, then just stop what we're doing
 	# and plead to the developer to clean things up.
 	#
-	virsh domstate $name 1>/dev/null 2>&1
-	if [ $? -eq 0 ]; then
-		echo "Domain $name is already defined."
-		virsh start $name
+	if virsh list --all | grep --quiet --word-regexp "$name"; then
+		output_domstate=$(virsh domstate $name 2>/dev/null)
+		echo "Domain $name is already defined. (state: $output_domstate)"
+		if [ "$output_domstate" != "running" ]; then
+			virsh start $name
+		fi
 		exit 0
 	fi