[PATCH] Extend libvirt-guests to shutdown only persistent VMs

At the moment, there is no configuration option for the libvirt-guests service that allows users to define that only persistent virtual machines should be shutdown on host shutdown. Currently, the service config allows to choose between two ON_SHUTDOWN actions that are executed on running virtual machines when the host goes down: shutdown, suspend. The ON_SHUTDOWN action should be orthogonal to the type of the virtual machine. However, the existing implementation, does not suspend transient virtual machines. This is the matrix of actions that is executed on virtual machines based on the configured ON_SHUTDOWN action and the type of a virtual machine. | persistent | transient shutdown | shutdown | shutdown (what we want to change) suspend | suspend | nothing Add config option PERSISTENT_ONLY to libvirt-guests config that allows users to define if the ON_SHUTDOWN action should be applied only on persistent virtual machines. PERSISTENT_ONLY can be set to true, false, default. The default option will implement the already existing logic. Case 1: PERSISTENT_ONLY=default | persistent | transient shutdown | shutdown | shutdown suspend | suspend | nothing Case 2: PERSISTENT_ONLY=true | persistent | transient shutdown | shutdown | nothing suspend | suspend | nothing Case 3: PERSISTENT_ONLY=false | persistent | transient shutdown | shutdown | shutdown suspend | suspend | suspend Change-Id: Ib03013d00b3ec60716287dad4743a038cf000763 --- tools/libvirt-guests.sh.in | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/tools/libvirt-guests.sh.in b/tools/libvirt-guests.sh.in index 344b54390a..c3c5954e17 100644 --- a/tools/libvirt-guests.sh.in +++ b/tools/libvirt-guests.sh.in @@ -38,6 +38,7 @@ PARALLEL_SHUTDOWN=0 START_DELAY=0 BYPASS_CACHE=0 SYNC_TIME=0 +PERSISTENT_ONLY="default" test -f "$initconfdir"/libvirt-guests && . "$initconfdir"/libvirt-guests @@ -438,14 +439,16 @@ shutdown_guests_parallel() # stop # Shutdown or save guests on the configured uris stop() { - local suspending="true" local uri= + local action="suspend" + local persistent_only="default" + # last stop was not followed by start [ -f "$LISTFILE" ] && return 0 if [ "x$ON_SHUTDOWN" = xshutdown ]; then - suspending="false" + action="shutdown" if [ $SHUTDOWN_TIMEOUT -lt 0 ]; then gettext "SHUTDOWN_TIMEOUT must be equal or greater than 0" echo @@ -454,6 +457,22 @@ stop() { fi fi + case "x$PERSISTENT_ONLY" in + xtrue) + persistent_only="true" + ;; + xfalse) + persistent_only="false" + ;; + *) + if [ "x$action" = xshutdown ]; then + persistent_only="false" + elif [ "x$action" = xsuspend ]; then + persistent_only="true" + fi + ;; + esac + : >"$LISTFILE" set -f for uri in $URIS; do @@ -478,7 +497,7 @@ stop() { echo fi - if "$suspending"; then + if "$persistent_only"; then local transient="$(list_guests "$uri" "--transient")" if [ $? -eq 0 ]; then local empty="true" @@ -486,7 +505,11 @@ stop() { for uuid in $transient; do if "$empty"; then - eval_gettext "Not suspending transient guests on URI: \$uri: " + if [ "x$action" = xsuspend ]; then + eval_gettext "Not suspending transient guests on URI: \$uri: " + else + eval_gettext "Not shutting down transient guests on URI: \$uri: " + fi empty="false" else printf ", " @@ -520,19 +543,19 @@ stop() { if [ -s "$LISTFILE" ]; then while read uri list; do - if "$suspending"; then + if [ "x$action" = xsuspend ]; then eval_gettext "Suspending guests on \$uri URI..."; echo else eval_gettext "Shutting down guests on \$uri URI..."; echo fi if [ "$PARALLEL_SHUTDOWN" -gt 1 ] && - ! "$suspending"; then + [ "x$action" = xshutdown ]; then shutdown_guests_parallel "$uri" "$list" else local guest= for guest in $list; do - if "$suspending"; then + if [ "x$action" = xsuspend ]; then suspend_guest "$uri" "$guest" else shutdown_guest "$uri" "$guest" -- 2.39.2

ping
On 13. Nov 2023, at 11:22, Benjamin Taubmann <benjamin.taubmann@nutanix.com> wrote:
At the moment, there is no configuration option for the libvirt-guests service that allows users to define that only persistent virtual machines should be shutdown on host shutdown.
Currently, the service config allows to choose between two ON_SHUTDOWN actions that are executed on running virtual machines when the host goes down: shutdown, suspend. The ON_SHUTDOWN action should be orthogonal to the type of the virtual machine. However, the existing implementation, does not suspend transient virtual machines. This is the matrix of actions that is executed on virtual machines based on the configured ON_SHUTDOWN action and the type of a virtual machine.
| persistent | transient shutdown | shutdown | shutdown (what we want to change) suspend | suspend | nothing
Add config option PERSISTENT_ONLY to libvirt-guests config that allows users to define if the ON_SHUTDOWN action should be applied only on persistent virtual machines. PERSISTENT_ONLY can be set to true, false, default. The default option will implement the already existing logic.
Case 1: PERSISTENT_ONLY=default | persistent | transient shutdown | shutdown | shutdown suspend | suspend | nothing
Case 2: PERSISTENT_ONLY=true | persistent | transient shutdown | shutdown | nothing suspend | suspend | nothing
Case 3: PERSISTENT_ONLY=false | persistent | transient shutdown | shutdown | shutdown suspend | suspend | suspend
Change-Id: Ib03013d00b3ec60716287dad4743a038cf000763 --- tools/libvirt-guests.sh.in | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-)
diff --git a/tools/libvirt-guests.sh.in b/tools/libvirt-guests.sh.in index 344b54390a..c3c5954e17 100644 --- a/tools/libvirt-guests.sh.in +++ b/tools/libvirt-guests.sh.in @@ -38,6 +38,7 @@ PARALLEL_SHUTDOWN=0 START_DELAY=0 BYPASS_CACHE=0 SYNC_TIME=0 +PERSISTENT_ONLY="default"
test -f "$initconfdir"/libvirt-guests && . "$initconfdir"/libvirt-guests @@ -438,14 +439,16 @@ shutdown_guests_parallel() # stop # Shutdown or save guests on the configured uris stop() { - local suspending="true" local uri= + local action="suspend" + local persistent_only="default" +
# last stop was not followed by start [ -f "$LISTFILE" ] && return 0
if [ "x$ON_SHUTDOWN" = xshutdown ]; then - suspending="false" + action="shutdown" if [ $SHUTDOWN_TIMEOUT -lt 0 ]; then gettext "SHUTDOWN_TIMEOUT must be equal or greater than 0" echo @@ -454,6 +457,22 @@ stop() { fi fi
+ case "x$PERSISTENT_ONLY" in + xtrue) + persistent_only="true" + ;; + xfalse) + persistent_only="false" + ;; + *) + if [ "x$action" = xshutdown ]; then + persistent_only="false" + elif [ "x$action" = xsuspend ]; then + persistent_only="true" + fi + ;; + esac + : >"$LISTFILE" set -f for uri in $URIS; do @@ -478,7 +497,7 @@ stop() { echo fi
- if "$suspending"; then + if "$persistent_only"; then local transient="$(list_guests "$uri" "--transient")" if [ $? -eq 0 ]; then local empty="true" @@ -486,7 +505,11 @@ stop() {
for uuid in $transient; do if "$empty"; then - eval_gettext "Not suspending transient guests on URI: \$uri: " + if [ "x$action" = xsuspend ]; then + eval_gettext "Not suspending transient guests on URI: \$uri: " + else + eval_gettext "Not shutting down transient guests on URI: \$uri: " + fi empty="false" else printf ", " @@ -520,19 +543,19 @@ stop() {
if [ -s "$LISTFILE" ]; then while read uri list; do - if "$suspending"; then + if [ "x$action" = xsuspend ]; then eval_gettext "Suspending guests on \$uri URI..."; echo else eval_gettext "Shutting down guests on \$uri URI..."; echo fi
if [ "$PARALLEL_SHUTDOWN" -gt 1 ] && - ! "$suspending"; then + [ "x$action" = xshutdown ]; then shutdown_guests_parallel "$uri" "$list" else local guest= for guest in $list; do - if "$suspending"; then + if [ "x$action" = xsuspend ]; then suspend_guest "$uri" "$guest" else shutdown_guest "$uri" "$guest" -- 2.39.2

On Mon, Nov 13, 2023 at 10:22:59AM +0000, Benjamin Taubmann wrote:
At the moment, there is no configuration option for the libvirt-guests service that allows users to define that only persistent virtual machines should be shutdown on host shutdown.
Currently, the service config allows to choose between two ON_SHUTDOWN actions that are executed on running virtual machines when the host goes down: shutdown, suspend. The ON_SHUTDOWN action should be orthogonal to the type of the virtual machine. However, the existing implementation, does not suspend transient virtual machines. This is the matrix of actions that is executed on virtual machines based on the configured ON_SHUTDOWN action and the type of a virtual machine.
| persistent | transient shutdown | shutdown | shutdown (what we want to change) suspend | suspend | nothing
Add config option PERSISTENT_ONLY to libvirt-guests config that allows users to define if the ON_SHUTDOWN action should be applied only on persistent virtual machines. PERSISTENT_ONLY can be set to true, false, default. The default option will implement the already existing logic.
Case 1: PERSISTENT_ONLY=default | persistent | transient shutdown | shutdown | shutdown suspend | suspend | nothing
Case 2: PERSISTENT_ONLY=true | persistent | transient shutdown | shutdown | nothing suspend | suspend | nothing
Case 3: PERSISTENT_ONLY=false | persistent | transient shutdown | shutdown | shutdown suspend | suspend | suspend
Change-Id: Ib03013d00b3ec60716287dad4743a038cf000763
Sorry for the delay. I'm not sure what this Change-Id is or what it tracks, we don't use anything like it. What we need, however, is your sign-off for every patch, see https://libvirt.org/hacking.html#developer-certificate-of-origin and git history. With that added this is Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
--- tools/libvirt-guests.sh.in | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-)
diff --git a/tools/libvirt-guests.sh.in b/tools/libvirt-guests.sh.in index 344b54390a..c3c5954e17 100644 --- a/tools/libvirt-guests.sh.in +++ b/tools/libvirt-guests.sh.in @@ -38,6 +38,7 @@ PARALLEL_SHUTDOWN=0 START_DELAY=0 BYPASS_CACHE=0 SYNC_TIME=0 +PERSISTENT_ONLY="default"
test -f "$initconfdir"/libvirt-guests && . "$initconfdir"/libvirt-guests @@ -438,14 +439,16 @@ shutdown_guests_parallel() # stop # Shutdown or save guests on the configured uris stop() { - local suspending="true" local uri= + local action="suspend" + local persistent_only="default" +
# last stop was not followed by start [ -f "$LISTFILE" ] && return 0
if [ "x$ON_SHUTDOWN" = xshutdown ]; then - suspending="false" + action="shutdown" if [ $SHUTDOWN_TIMEOUT -lt 0 ]; then gettext "SHUTDOWN_TIMEOUT must be equal or greater than 0" echo @@ -454,6 +457,22 @@ stop() { fi fi
+ case "x$PERSISTENT_ONLY" in + xtrue) + persistent_only="true" + ;; + xfalse) + persistent_only="false" + ;; + *) + if [ "x$action" = xshutdown ]; then + persistent_only="false" + elif [ "x$action" = xsuspend ]; then + persistent_only="true" + fi + ;; + esac + : >"$LISTFILE" set -f for uri in $URIS; do @@ -478,7 +497,7 @@ stop() { echo fi
- if "$suspending"; then + if "$persistent_only"; then local transient="$(list_guests "$uri" "--transient")" if [ $? -eq 0 ]; then local empty="true" @@ -486,7 +505,11 @@ stop() {
for uuid in $transient; do if "$empty"; then - eval_gettext "Not suspending transient guests on URI: \$uri: " + if [ "x$action" = xsuspend ]; then + eval_gettext "Not suspending transient guests on URI: \$uri: " + else + eval_gettext "Not shutting down transient guests on URI: \$uri: " + fi empty="false" else printf ", " @@ -520,19 +543,19 @@ stop() {
if [ -s "$LISTFILE" ]; then while read uri list; do - if "$suspending"; then + if [ "x$action" = xsuspend ]; then eval_gettext "Suspending guests on \$uri URI..."; echo else eval_gettext "Shutting down guests on \$uri URI..."; echo fi
if [ "$PARALLEL_SHUTDOWN" -gt 1 ] && - ! "$suspending"; then + [ "x$action" = xshutdown ]; then shutdown_guests_parallel "$uri" "$list" else local guest= for guest in $list; do - if "$suspending"; then + if [ "x$action" = xsuspend ]; then suspend_guest "$uri" "$guest" else shutdown_guest "$uri" "$guest" -- 2.39.2 _______________________________________________ Devel mailing list -- devel@lists.libvirt.org To unsubscribe send an email to devel-leave@lists.libvirt.org

On Thu, Feb 29, 2024 at 04:00:37PM +0100, Martin Kletzander wrote:
On Mon, Nov 13, 2023 at 10:22:59AM +0000, Benjamin Taubmann wrote:
At the moment, there is no configuration option for the libvirt-guests service that allows users to define that only persistent virtual machines should be shutdown on host shutdown.
Currently, the service config allows to choose between two ON_SHUTDOWN actions that are executed on running virtual machines when the host goes down: shutdown, suspend. The ON_SHUTDOWN action should be orthogonal to the type of the virtual machine. However, the existing implementation, does not suspend transient virtual machines. This is the matrix of actions that is executed on virtual machines based on the configured ON_SHUTDOWN action and the type of a virtual machine.
| persistent | transient shutdown | shutdown | shutdown (what we want to change) suspend | suspend | nothing
Add config option PERSISTENT_ONLY to libvirt-guests config that allows users to define if the ON_SHUTDOWN action should be applied only on persistent virtual machines. PERSISTENT_ONLY can be set to true, false, default. The default option will implement the already existing logic.
Case 1: PERSISTENT_ONLY=default | persistent | transient shutdown | shutdown | shutdown suspend | suspend | nothing
Case 2: PERSISTENT_ONLY=true | persistent | transient shutdown | shutdown | nothing suspend | suspend | nothing
Case 3: PERSISTENT_ONLY=false | persistent | transient shutdown | shutdown | shutdown suspend | suspend | suspend
Change-Id: Ib03013d00b3ec60716287dad4743a038cf000763
Sorry for the delay.
I'm not sure what this Change-Id is or what it tracks, we don't use anything like it.
What we need, however, is your sign-off for every patch, see
https://libvirt.org/hacking.html#developer-certificate-of-origin
and git history.
With that added this is
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
Please don't forget to update docs/manpages/libvirt-guests.rst with information about this feature too. -- Andrea Bolognani / Red Hat / Virtualization

At the moment, there is no configuration option for the libvirt-guests service that allows users to define that only persistent virtual machines should be shutdown on host shutdown. Currently, the service config allows to choose between two ON_SHUTDOWN actions that are executed on running virtual machines when the host goes down: shutdown, suspend. The ON_SHUTDOWN action should be orthogonal to the type of the virtual machine. However, the existing implementation, does not suspend transient virtual machines. This is the matrix of actions that is executed on virtual machines based on the configured ON_SHUTDOWN action and the type of a virtual machine. | persistent | transient shutdown | shutdown | shutdown (what we want to change) suspend | suspend | nothing Add config option PERSISTENT_ONLY to libvirt-guests config that allows users to define if the ON_SHUTDOWN action should be applied only on persistent virtual machines. PERSISTENT_ONLY can be set to true, false, default. The default option will implement the already existing logic. Case 1: PERSISTENT_ONLY=default | persistent | transient shutdown | shutdown | shutdown suspend | suspend | nothing Case 2: PERSISTENT_ONLY=true | persistent | transient shutdown | shutdown | nothing suspend | suspend | nothing Case 3: PERSISTENT_ONLY=false | persistent | transient shutdown | shutdown | shutdown suspend | suspend | suspend Signed-off-by: Benjamin Taubmann <benjamin.taubmann@nutanix.com> Reviewed-by: Martin Kletzander <mkletzan@redhat.com> --- v2: Update libvirt-guests' man page and git commit message --- docs/manpages/libvirt-guests.rst | 22 +++++++++++++++++++ tools/libvirt-guests.sh.in | 37 ++++++++++++++++++++++++++------ 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/docs/manpages/libvirt-guests.rst b/docs/manpages/libvirt-guests.rst index f27eaad6e4..42dae283e3 100644 --- a/docs/manpages/libvirt-guests.rst +++ b/docs/manpages/libvirt-guests.rst @@ -113,6 +113,28 @@ The following variables are supported: this requires guest agent with support for time synchronization running in the guest. By default, this functionality is turned off. +- PERSISTENT_ONLY=default + + Defines what type of guest virtual machine ON_SHUTDOWN action is applied to + + * default + + This implements the already existing default behavior. + If ON_SHUTDOWN action is shutdown, transient and persistent guest virtual + machines are asked to shutdown. + If ON_SHUTDOWN action is suspend, only persistent guest virtual machines + are asked to suspend. + + * true + + ON_SHUTDOWN action is executed only on persistent guest virtual machines. + Transient guest virtual machines are not affected. + + * false + + ON_SHUTDOWN action is executed on persistent and transient guest virtual + machines. + BUGS ==== diff --git a/tools/libvirt-guests.sh.in b/tools/libvirt-guests.sh.in index 344b54390a..c3c5954e17 100644 --- a/tools/libvirt-guests.sh.in +++ b/tools/libvirt-guests.sh.in @@ -38,6 +38,7 @@ PARALLEL_SHUTDOWN=0 START_DELAY=0 BYPASS_CACHE=0 SYNC_TIME=0 +PERSISTENT_ONLY="default" test -f "$initconfdir"/libvirt-guests && . "$initconfdir"/libvirt-guests @@ -438,14 +439,16 @@ shutdown_guests_parallel() # stop # Shutdown or save guests on the configured uris stop() { - local suspending="true" local uri= + local action="suspend" + local persistent_only="default" + # last stop was not followed by start [ -f "$LISTFILE" ] && return 0 if [ "x$ON_SHUTDOWN" = xshutdown ]; then - suspending="false" + action="shutdown" if [ $SHUTDOWN_TIMEOUT -lt 0 ]; then gettext "SHUTDOWN_TIMEOUT must be equal or greater than 0" echo @@ -454,6 +457,22 @@ stop() { fi fi + case "x$PERSISTENT_ONLY" in + xtrue) + persistent_only="true" + ;; + xfalse) + persistent_only="false" + ;; + *) + if [ "x$action" = xshutdown ]; then + persistent_only="false" + elif [ "x$action" = xsuspend ]; then + persistent_only="true" + fi + ;; + esac + : >"$LISTFILE" set -f for uri in $URIS; do @@ -478,7 +497,7 @@ stop() { echo fi - if "$suspending"; then + if "$persistent_only"; then local transient="$(list_guests "$uri" "--transient")" if [ $? -eq 0 ]; then local empty="true" @@ -486,7 +505,11 @@ stop() { for uuid in $transient; do if "$empty"; then - eval_gettext "Not suspending transient guests on URI: \$uri: " + if [ "x$action" = xsuspend ]; then + eval_gettext "Not suspending transient guests on URI: \$uri: " + else + eval_gettext "Not shutting down transient guests on URI: \$uri: " + fi empty="false" else printf ", " @@ -520,19 +543,19 @@ stop() { if [ -s "$LISTFILE" ]; then while read uri list; do - if "$suspending"; then + if [ "x$action" = xsuspend ]; then eval_gettext "Suspending guests on \$uri URI..."; echo else eval_gettext "Shutting down guests on \$uri URI..."; echo fi if [ "$PARALLEL_SHUTDOWN" -gt 1 ] && - ! "$suspending"; then + [ "x$action" = xshutdown ]; then shutdown_guests_parallel "$uri" "$list" else local guest= for guest in $list; do - if "$suspending"; then + if [ "x$action" = xsuspend ]; then suspend_guest "$uri" "$guest" else shutdown_guest "$uri" "$guest" -- 2.39.2

On 3/26/24 12:37, Benjamin Taubmann wrote:
At the moment, there is no configuration option for the libvirt-guests service that allows users to define that only persistent virtual machines should be shutdown on host shutdown.
Currently, the service config allows to choose between two ON_SHUTDOWN actions that are executed on running virtual machines when the host goes down: shutdown, suspend. The ON_SHUTDOWN action should be orthogonal to the type of the virtual machine. However, the existing implementation, does not suspend transient virtual machines. This is the matrix of actions that is executed on virtual machines based on the configured ON_SHUTDOWN action and the type of a virtual machine.
| persistent | transient shutdown | shutdown | shutdown (what we want to change) suspend | suspend | nothing
Add config option PERSISTENT_ONLY to libvirt-guests config that allows users to define if the ON_SHUTDOWN action should be applied only on persistent virtual machines. PERSISTENT_ONLY can be set to true, false, default. The default option will implement the already existing logic.
Case 1: PERSISTENT_ONLY=default | persistent | transient shutdown | shutdown | shutdown suspend | suspend | nothing
Case 2: PERSISTENT_ONLY=true | persistent | transient shutdown | shutdown | nothing suspend | suspend | nothing
Case 3: PERSISTENT_ONLY=false | persistent | transient shutdown | shutdown | shutdown suspend | suspend | suspend
Signed-off-by: Benjamin Taubmann <benjamin.taubmann@nutanix.com> Reviewed-by: Martin Kletzander <mkletzan@redhat.com> --- v2: Update libvirt-guests' man page and git commit message --- docs/manpages/libvirt-guests.rst | 22 +++++++++++++++++++ tools/libvirt-guests.sh.in | 37 ++++++++++++++++++++++++++------ 2 files changed, 52 insertions(+), 7 deletions(-)
Merged now. Michal
participants (4)
-
Andrea Bolognani
-
Benjamin Taubmann
-
Martin Kletzander
-
Michal Prívozník