public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Brian Harring" <ferringb@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/kvm-tools:master commit in: /
Date: Sat, 14 Apr 2012 06:26:45 +0000 (UTC)	[thread overview]
Message-ID: <1334384386.80a64bd925e3d97e22f185545a8a8cc650339955.ferringb@gentoo> (raw)

commit:     80a64bd925e3d97e22f185545a8a8cc650339955
Author:     Brian Harring <ferringb <AT> chromium <DOT> org>
AuthorDate: Sat Apr 14 03:49:16 2012 +0000
Commit:     Brian Harring <ferringb <AT> gentoo <DOT> org>
CommitDate: Sat Apr 14 06:19:46 2012 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/kvm-tools.git;a=commit;h=80a64bd9

kvm-init-script: Fix breakages induced in eb70edb7, up the error checking, cleanup.

---
 kvm-init-script |   74 +++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 48 insertions(+), 26 deletions(-)

diff --git a/kvm-init-script b/kvm-init-script
index e618ad9..7bfeb9d 100644
--- a/kvm-init-script
+++ b/kvm-init-script
@@ -7,8 +7,12 @@ PIDFILE=/var/run/vm/${VMNAME}.pid
 MONITOR=/var/run/vm/${VMNAME}.monitor
 QTAP_FILE=/var/run/vm/${VMNAME}.qtap
 
-# modify this
-VMSOFTWARE=${VMSOFTWARE:-qemu-kvm}
+# Default to qemu-kvm in the absense of a setting, failing back
+# to kvm if no binary is found, finally, back to qemu.
+[ -z "$VMSOFTWARE" ] && VMSOFTWARE=$(type -p qemu-kvm)
+[ -z "$VMSOFTWARE" ] && VMSOFTWARE=$(type -p kvm)
+[ -z "$VMSOFTWARE" ] && VMSOFTWARE=$(type -p qemu)
+
 DROP_USER=${DROP_USER:-nobody}
 MEMORY=${MEMORY:-512M}
 TIMEOUT=${TIMEOUT:-300}
@@ -23,8 +27,8 @@ depend() {
 
 send_command() {
 	local command="socat -u - UNIX-CONNECT:${MONITOR}"
-	which nc6 2> /dev/null > /dev/null && command="nc6 -U ${MONITOR} --send-only"
-	echo "$@" | ${command} >/dev/null 2>&1
+	type -p nc6 > /dev/null && command="nc6 -U ${MONITOR} --send-only"
+	echo "$@" | ${command} > /dev/null 2>&1
 }
 
 sanity_check() {
@@ -47,19 +51,22 @@ start() {
 		return 1;
 	}
 
-	mkdir -p $(dirname ${PIDFILE}) $(dirname ${MONITOR})
+	mkdir -p "${PIDFILE%/*}" "${MONITOR%/*}"
 	ebegin "creating qtap ${QTAP:-(auto allocating one)}"
-	QTAP_RET=$(qtap-manipulate create ${QTAP} -u "${DROP_USER}");
-	if [ 0 != $? ]; then
-		eerror "failed to create qtap interface"
-		return 1
+	if [ -n "$QTAP" ]; then
+		qtap-manipulate create_specific "${QTAP}" -u "${DROP_USER}"
+	else
+		QTAP=$(qtap-manipulate create -u "${DROP_USER}")
+		if [ 0 != $? ]; then
+			eerror "failed to create qtap interface"
+			return 1
+		fi
 	fi
-	QTAP=${QTAP:-${QTAP_RET}}
 	echo "${QTAP}" > ${QTAP_FILE}
 	eend $?
 
-	ebegin "Starting ${VMSOFTWARE-qemu} for ${VMNAME} at VNC port${VNC}"
-	start-stop-daemon --start /usr/bin/${VMSOFTWARE-qemu} \
+	ebegin "Starting ${VMSOFTWARE##*/} for ${VMNAME} at VNC port${VNC}"
+	start-stop-daemon --start "${VMSOFTWARE}" \
 		--pidfile ${PIDFILE} \
 		-- -daemonize -pidfile ${PIDFILE} -monitor unix:${MONITOR},server,nowait \
 		-runas ${DROP_USER} -name ${VMNAME} \
@@ -94,21 +101,36 @@ stop() {
 	eend $?
 
 	ebegin "waiting up to ${TIMEOUT:-60} seconds for it to die"
-	ret=1
-	for x in $(seq 0 ${TIMEOUT:-60}); do
-		kill -0 $(cat $PIDFILE) 2>&1 > /dev/null || { ret=0; break; }
-		sleep 1s
-	done
-	eend ${ret}
+	local pid
+	[ -s "${PIDFILE}" ] && pid=$(cat "${PIDFILE}")
+	if [ -z "$pid" ]; then
+		eerror "Couldn't find stored pid at '$PIDFILE'; user will have to manually kill kvm"
+		eerror "Will attempt to destroy qtap despite."
+		eend 1
+	else
+		local ret=1
+		for x in $(seq 0 ${TIMEOUT:-60}); do
+			if kill -0 "${pid}" > /dev/null 2>&1; then
+				sleep 1s
+				continue
+			fi
+			ret=0
+			break
+		done
+		eend $ret
+	fi
 
-	ebegin "Stopping ${VMSOFTWARE-qemu} for ${VMNAME}"
-	start-stop-daemon --stop /usr/bin/${VMSOFTWARE-qemu} \
-		--user ${DROP_USER} \
-		--pidfile ${PIDFILE} \
+	ebegin "Stopping ${VMSOFTWARE##*/} for ${VMNAME}"
+	start-stop-daemon --stop "${VMSOFTWARE}" \
+		--user "${DROP_USER}" \
+		--pidfile "${PIDFILE}" \
 		--quiet
 	eend $?
-	QTAP=$(cat ${QTAP_FILE})
-	ebegin "destroying qtap ${QTAP}"
-	qtap-manipulate destroy ${QTAP}
-	eend $?
+	local qtap
+	[ -s "${QTAP_FILE}" ] && qtap=$(cat "${QTAP_FILE}")
+	if [ -n "$qtap" ]; then
+		ebegin "destroying qtap ${qtap}"
+		qtap-manipulate destroy ${qtap}
+		eend $?
+	fi
 }



             reply	other threads:[~2012-04-14  6:27 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-14  6:26 Brian Harring [this message]
  -- strict thread matches above, loose matches on Subject: below --
2012-04-14  6:26 [gentoo-commits] proj/kvm-tools:master commit in: / Brian Harring
2012-04-14  6:26 Brian Harring
2012-04-14  6:26 Brian Harring
2012-04-14  6:26 Brian Harring
2012-04-14  4:47 Richard Yao
2012-03-17 20:46 Richard Yao
2012-03-17 20:44 Richard Yao

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1334384386.80a64bd925e3d97e22f185545a8a8cc650339955.ferringb@gentoo \
    --to=ferringb@gentoo.org \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox