[meta-arm] arm-autonomy/xenguest-manager: Allow guests with substring names

Submitted by Diego Sueiro on Oct. 30, 2020, 10:03 a.m. | Patch ID: 177729

Details

Message ID 20201030100338.2159-1-diego.sueiro@arm.com
State Superseded
Headers show

Commit Message

Diego Sueiro Oct. 30, 2020, 10:03 a.m.
From: Nathan Dunne <Nathan.Dunne@arm.com>

Created new function for determining guest running state such that
two guests with names such as "myguest" and "myguest2" report
correctly, by searching for exact guestname instead of contains.

Also modified the status command to use the same function to avoid
duplication, and added a new nested function for testing status for
a particular guest, instead of recursively calling the whole bash
script.

Using the nested function speeds up "xenguest-manager status" from
~7.5s to ~1.5s my machine.

Change-Id: Ie6fc08cacc55f623c44b08478f76031510a59126
Issue-Id: SCM-1517
Signed-off-by: Nathan Dunne <Nathan.Dunne@arm.com>
---
 .../xenguest/files/xenguest-manager           | 39 ++++++++++++-------
 1 file changed, 26 insertions(+), 13 deletions(-)

Patch hide | download patch | download mbox

diff --git a/meta-arm-autonomy/recipes-extended/xenguest/files/xenguest-manager b/meta-arm-autonomy/recipes-extended/xenguest/files/xenguest-manager
index 78ac55d..4ea3a37 100755
--- a/meta-arm-autonomy/recipes-extended/xenguest/files/xenguest-manager
+++ b/meta-arm-autonomy/recipes-extended/xenguest/files/xenguest-manager
@@ -597,11 +597,22 @@  function check_guest_exist()
     fi
 }
 
+function xl_list_contains()
+{
+    guestname="${1}"
+    # Select first column of xl list, and find guestname exactly using regex
+    running=$(xl list | awk 'NR > 1 {print $1}' | grep "^${guestname}$" || echo)
+    if [ "${running}" = "${guestname}" ]; then
+        return 0
+    fi
+
+    return 1
+}
+
 function check_guest_running()
 {
     guestname="${1}"
-    running=$(xl list | awk 'NR > 1 {print $1}' | grep "${guestname}" || echo)
-    if [ ! "${running}" = "${guestname}" ]; then
+    if ! xl_list_contains $guestname; then
         echo "${PREF} Guest ${guestname} is not running"
         exit 1
     fi
@@ -610,8 +621,7 @@  function check_guest_running()
 function check_guest_not_running()
 {
     guestname="${1}"
-    running=$(xl list | awk 'NR > 1 {print $1}' | grep "${guestname}" || echo)
-    if [ "${running}" = "${guestname}" ]; then
+    if xl_list_contains $guestname; then
         echo "${PREF} Guest ${guestname} is running"
         exit 1
     fi
@@ -668,10 +678,8 @@  case ${cmd} in
         guestname="${arg1:-}"
         check_guest_arg ${cmd} ${guestname}
         check_guest_exist ${guestname}
-        # We need to stop the guest first
-        running=$(xl list | awk 'NR > 1 {print $1}' | grep "${guestname}" \
-            || echo)
-        if [ "${running}" = "${guestname}" ]; then
+        # We need to stop the guest first if it is running
+        if xl_list_contains $guestname; then
             echo "xl destroy ${guestname}" >> ${LOGFILE} 2>&1
             xl destroy ${guestname} >> ${LOGFILE} 2>&1
             if [ $? -ne 0 ]; then
@@ -719,20 +727,25 @@  case ${cmd} in
         fi
         ;;
     status)
-        guestname="${arg1}"
-        if [ -n "${guestname}" ]; then
+
+        single_status() (
+            guestname="${1}"
             check_guest_exist ${guestname}
-            if xl list | awk 'NR > 1 {print $1}' | grep "${guestname}" > \
-                /dev/null 2>&1; then
+            if xl_list_contains $guestname; then
                 echo "${guestname}: Running"
             else
                 echo "${guestname}: Stopped"
             fi
+        )
+
+        guestname="${arg1}"
+        if [ -n "${guestname}" ]; then
+            single_status ${guestname}
         else
             guestlist=$($this list)
             if [ -n "${guestlist}" ]; then
                 for f in ${guestlist}; do
-                    $this status $f
+                    single_status $f
                 done
             fi
         fi

Comments

Jon Mason Nov. 3, 2020, 2:08 a.m.
On Fri, Oct 30, 2020 at 10:03:38AM +0000, Diego Sueiro wrote:
> From: Nathan Dunne <Nathan.Dunne@arm.com>

> 

> Created new function for determining guest running state such that

> two guests with names such as "myguest" and "myguest2" report

> correctly, by searching for exact guestname instead of contains.

> 

> Also modified the status command to use the same function to avoid

> duplication, and added a new nested function for testing status for

> a particular guest, instead of recursively calling the whole bash

> script.

> 

> Using the nested function speeds up "xenguest-manager status" from

> ~7.5s to ~1.5s my machine.

> 

> Change-Id: Ie6fc08cacc55f623c44b08478f76031510a59126

> Issue-Id: SCM-1517

> Signed-off-by: Nathan Dunne <Nathan.Dunne@arm.com>


Applied to master.

Thanks,
Jon

> ---

>  .../xenguest/files/xenguest-manager           | 39 ++++++++++++-------

>  1 file changed, 26 insertions(+), 13 deletions(-)

> 

> diff --git a/meta-arm-autonomy/recipes-extended/xenguest/files/xenguest-manager b/meta-arm-autonomy/recipes-extended/xenguest/files/xenguest-manager

> index 78ac55d..4ea3a37 100755

> --- a/meta-arm-autonomy/recipes-extended/xenguest/files/xenguest-manager

> +++ b/meta-arm-autonomy/recipes-extended/xenguest/files/xenguest-manager

> @@ -597,11 +597,22 @@ function check_guest_exist()

>      fi

>  }

>  

> +function xl_list_contains()

> +{

> +    guestname="${1}"

> +    # Select first column of xl list, and find guestname exactly using regex

> +    running=$(xl list | awk 'NR > 1 {print $1}' | grep "^${guestname}$" || echo)

> +    if [ "${running}" = "${guestname}" ]; then

> +        return 0

> +    fi

> +

> +    return 1

> +}

> +

>  function check_guest_running()

>  {

>      guestname="${1}"

> -    running=$(xl list | awk 'NR > 1 {print $1}' | grep "${guestname}" || echo)

> -    if [ ! "${running}" = "${guestname}" ]; then

> +    if ! xl_list_contains $guestname; then

>          echo "${PREF} Guest ${guestname} is not running"

>          exit 1

>      fi

> @@ -610,8 +621,7 @@ function check_guest_running()

>  function check_guest_not_running()

>  {

>      guestname="${1}"

> -    running=$(xl list | awk 'NR > 1 {print $1}' | grep "${guestname}" || echo)

> -    if [ "${running}" = "${guestname}" ]; then

> +    if xl_list_contains $guestname; then

>          echo "${PREF} Guest ${guestname} is running"

>          exit 1

>      fi

> @@ -668,10 +678,8 @@ case ${cmd} in

>          guestname="${arg1:-}"

>          check_guest_arg ${cmd} ${guestname}

>          check_guest_exist ${guestname}

> -        # We need to stop the guest first

> -        running=$(xl list | awk 'NR > 1 {print $1}' | grep "${guestname}" \

> -            || echo)

> -        if [ "${running}" = "${guestname}" ]; then

> +        # We need to stop the guest first if it is running

> +        if xl_list_contains $guestname; then

>              echo "xl destroy ${guestname}" >> ${LOGFILE} 2>&1

>              xl destroy ${guestname} >> ${LOGFILE} 2>&1

>              if [ $? -ne 0 ]; then

> @@ -719,20 +727,25 @@ case ${cmd} in

>          fi

>          ;;

>      status)

> -        guestname="${arg1}"

> -        if [ -n "${guestname}" ]; then

> +

> +        single_status() (

> +            guestname="${1}"

>              check_guest_exist ${guestname}

> -            if xl list | awk 'NR > 1 {print $1}' | grep "${guestname}" > \

> -                /dev/null 2>&1; then

> +            if xl_list_contains $guestname; then

>                  echo "${guestname}: Running"

>              else

>                  echo "${guestname}: Stopped"

>              fi

> +        )

> +

> +        guestname="${arg1}"

> +        if [ -n "${guestname}" ]; then

> +            single_status ${guestname}

>          else

>              guestlist=$($this list)

>              if [ -n "${guestlist}" ]; then

>                  for f in ${guestlist}; do

> -                    $this status $f

> +                    single_status $f

>                  done

>              fi

>          fi

> -- 

> 2.17.1

> 


> 

> 

>
-=-=-=-=-=-=-=-=-=-=-=-
Links: You receive all messages sent to this group.
View/Reply Online (#1315): https://lists.yoctoproject.org/g/meta-arm/message/1315
Mute This Topic: https://lists.yoctoproject.org/mt/77908559/1003190
Group Owner: meta-arm+owner@lists.yoctoproject.org
Unsubscribe: https://lists.yoctoproject.org/g/meta-arm/unsub [mhalstead@linuxfoundation.org]
-=-=-=-=-=-=-=-=-=-=-=-
Diego Sueiro Nov. 24, 2020, 4:25 a.m.
This is a system generated Comment: Patch 177729 was automatically marked as superseded by patch 178107.