Skip to contentSkip to conversation

How to properly turn off guest VM on server shutdown?

DISCUSSION André Weidemann 12 participants 16 messages Jan 24, 2010 – Jan 26, 2010 KVM

lore
Share & tools
lore
#0 lore raw

Hi,
is there a mechanism inside qemu-kvm that can shutdown the OS inside a VM when the qemu-kvm process receives a kill signal? I am running Windows7 Pro inside a VM and I would like kvm to shut the Windows system down before the process is killed. I know that VMware can shut down the guest OS when stopping the VMware instance. Is there a similar feature in KVM?

Regards
André

#1 lore raw
Quoted 1 line · André Weidemann

Hi,

Hi, André.

Quoted 6 lines

is there a mechanism inside qemu-kvm that can shutdown the OS inside a VM when the qemu-kvm process receives a kill signal?
I am running Windows7 Pro inside a VM and I would like kvm to shut the Windows system down before the process is killed.
I know that VMware can shut down the guest OS when stopping the VMware instance. Is there a similar feature in KVM?

It is an interesting question :-)

I know that this can be done with libvirt. I had tested it with GNU/Linux and it worked. I don't use Windows, so I cannot help you in this sense.

But also it would interest to me to know if this can be done without using libvirt.

Regards,
Daniel

Signature · 2 lines
Fingerprint: BFB3 08D6 B4D1 31B2 72B9  29CE 6696 BF1B 14E6 1D37
Powered by Debian GNU/Linux Lenny - Linux user #188.598
#2 lore raw
Quoted 3 lines · Daniel Bareiro

But also it would interest to me to know if this can be done without using libvirt.

Well, it can, but you get to duplicate all the libvirt code that does this.

Signature · 1 line
error compiling committee.c: too many arguments to function
#3 lore raw
Quoted 5 lines · Avi Kivity
Quoted 2 lines · Daniel Bareiro

But also it would interest to me to know if this can be done without using libvirt.

Well, it can, but you get to duplicate all the libvirt code that does this.

I don't think it can. The original question was whether QEMU could initiate an ACPI shutdown when it receives a kill signal. It cannot and it's unclear to me if it's a good thing for us to do.

But the origin of the question here is probably, can something automatically shut down guests when a machine shutdowns down via the normal mechanism. AFAIK, libvirt does not support this today although there are some scripts floating around the intertubes that can drive this operation through libvirt.

Regards,

Anthony Liguori

#4 lore raw

Hi, Anthony.

Quoted 5 lines · Anthony Liguori

But the origin of the question here is probably, can something automatically shut down guests when a machine shutdowns down via the normal mechanism. AFAIK, libvirt does not support this today although there are some scripts floating around the intertubes that can drive this operation through libvirt.

Well.. perhaps I'm confused because I did these tests a long time ago. But now that you mention it, I believe that I had tried was that, through virsh commands, the VM shutdown could be done from the VMHost being it always running.

I suppose that scripts that you mention will call to virsh when shutdown becomes of the VMHost.

Regards,
Daniel

Signature · 2 lines
Fingerprint: BFB3 08D6 B4D1 31B2 72B9  29CE 6696 BF1B 14E6 1D37
Powered by Debian GNU/Linux Lenny - Linux user #188.598
#5 lore raw

Hi!

Does anyone have sample scripts for this job?

I know you can send the 'system_powerdown' command to the qemu-monitor, this CAN work for windows guests as far as i know.

What other commands could be used in shutdown scripts?

Regards,
Markus

Am 24.01.2010 18:00, schrieb Daniel Bareiro:

Quoted 21 lines

Hi, Anthony.

Quoted 5 lines · Anthony Liguori

But the origin of the question here is probably, can something automatically shut down guests when a machine shutdowns down via the normal mechanism. AFAIK, libvirt does not support this today although there are some scripts floating around the intertubes that can drive this operation through libvirt.

Well.. perhaps I'm confused because I did these tests a long time ago. But now that you mention it, I believe that I had tried was that, through virsh commands, the VM shutdown could be done from the VMHost being it always running.

I suppose that scripts that you mention will call to virsh when shutdown becomes of the VMHost.

Regards,
Daniel

#6 lore raw
Quoted 1 line · Markus Breitländer

Hi!

Hello;

Quoted 1 line

Does anyone have sample scripts for this job?

#!/bin/bash
CONNECT_STRING="qemu:///system"
for MACHINE in $(virsh -c "$CONNECT_STRING" list | awk '/running$/ {print $2}') ; do
virsh -c "$CONNECT_STRING" shutdown $MACHINE
done
sleep 600

This code will shutdown all runnning hosts with acpi en enabled. I haven't tested it under windows Seven, but under win 2003, you have modify:

* Using regedit the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows
NT\CurrentVersion\Windows and set the value "ShutdownWarningDialogTimeout" to dword:00000001 (this will force shutdown even if users are connected)

AND

* Goto Control Pannel, admin tools and double-click "Local security settings"
* Expand "Local policies" and click on "Security Options" (left window pan)
* On the right side, locate "Shutdown: Allow system to be shutdown..." and
enable the option(this allows to powerdown on ctr-alt-del screen".

HTH

Signature · 2 lines
http://www.glennie.fr
If the only tool you have is hammer, you tend to see every problem as a nail.
#7 lore raw

Hello Glennie,

Am 26.01.2010 14:46, schrieb Glennie Vignarajah:

Quoted 26 lines
Quoted 1 line · Markus Breitländer

Hi!

Hello;

Quoted 1 line

Does anyone have sample scripts for this job?

#!/bin/bash
CONNECT_STRING="qemu:///system"
for MACHINE in $(virsh -c "$CONNECT_STRING" list | awk '/running$/ {print $2}') ; do
virsh -c "$CONNECT_STRING" shutdown $MACHINE
done
sleep 600

This code will shutdown all runnning hosts with acpi en enabled. I haven't tested it under windows Seven, but under win 2003, you have modify:

* Using regedit the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows
NT\CurrentVersion\Windows and set the value "ShutdownWarningDialogTimeout" to dword:00000001 (this will force shutdown even if users are connected)

Quoted 8 lines

AND

* Goto Control Pannel, admin tools and double-click "Local security settings"
* Expand "Local policies" and click on "Security Options" (left window pan)
* On the right side, locate "Shutdown: Allow system to be shutdown..." and
enable the option(this allows to powerdown on ctr-alt-del screen".

HTH

I was thinking about a script that doesn't use virsh.

I would be intrested in what commands virsh uses in it's 'shutdown' command...

I haven't been working on my own script yet. Up to know i have in mind to use qemu monitor command 'system_powerdown' and maybe ssh into linux boxes to get em down (but the latter is not really nice).
By the way when testing manually, i experianced you might want to use the 'system_powerdown' command twice / execute it two times shortly after another to get windows machines down.

Greets,
Markus

#8 lore raw

2010/1/26 Markus Breitländer <[email protected]>:

Quoted 31 lines

Hello Glennie,

Am 26.01.2010 14:46, schrieb Glennie Vignarajah:

Quoted 28 lines
Quoted 1 line · Markus Breitländer

Hi!

Hello;

Quoted 1 line

Does anyone have sample scripts for this job?

#!/bin/bash
CONNECT_STRING="qemu:///system"
for MACHINE in $(virsh -c "$CONNECT_STRING" list | awk '/running$/ {print $2}') ; do
  virsh -c "$CONNECT_STRING" shutdown $MACHINE
done
sleep 600

This code will shutdown all runnning hosts with acpi en enabled. I haven't tested it under windows Seven, but under win 2003, you have modify:

 * Using regedit the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows and set the value "ShutdownWarningDialogTimeout" to dword:00000001 (this will force shutdown even if users are connected)

AND

Quoted 8 lines
Quoted 6 lines

 * Goto Control Pannel, admin tools and double-click "Local security settings"
 * Expand "Local policies" and click on "Security Options" (left window pan)
 * On the right side, locate "Shutdown: Allow system to be shutdown..." and enable the option(this allows to powerdown on ctr-alt-del screen".

HTH

I was thinking about a script that doesn't use virsh.

I suppose you can start your guest with
-monitor unix:/${SOCKETFILE},server,nowait

and then do something like:
socat - unix-connect:${SOCKETFILE} <<EOF
system_powerdown
EOF

Best Regards
Kenni Lund

#9 lore raw
In reply to Anthony Liguori

[email protected] wrote on 01/24/2010 05:29:08 PM:

Quoted 6 lines

Anthony Liguori <[email protected]>
But the origin of the question here is probably, can something automatically shut down guests when a machine shutdowns down via the normal mechanism. AFAIK, libvirt does not support this today although there are some scripts floating around the intertubes that can drive this operation through libvirt.

Does the 'system_powerdown' monitor command initiate an ACPI shutdown? If so, I guess you can make an rc.d script that calls it during shutdown?

Avishay

#10 lore raw
Quoted 9 lines · Avishay Traeger
Quoted 5 lines

But the origin of the question here is probably, can something automatically shut down guests when a machine shutdowns down via the normal mechanism. AFAIK, libvirt does not support this today although there are some scripts floating around the intertubes that can drive this operation through libvirt.

Does the 'system_powerdown' monitor command initiate an ACPI shutdown? If so, I guess you can make an rc.d script that calls it during shutdown?

Interesting... I didn't know this command of Qemu Monitor.

Doing a test being connected to one of my guest by serial console, I see that when executing the command from Qemu Monitor, guest does a normal shutdown. But for this I believe that acpi package (Debian) must be installed.

Thanks for the information!

Regards,
Daniel

Signature · 2 lines
Fingerprint: BFB3 08D6 B4D1 31B2 72B9  29CE 6696 BF1B 14E6 1D37
Powered by Debian GNU/Linux Lenny - Linux user #188.598
#11 lore raw
Quoted 10 lines · Daniel Bareiro
Quoted 3 lines

Does the 'system_powerdown' monitor command initiate an ACPI shutdown? If so, I guess you can make an rc.d script that calls it during shutdown?

Interesting... I didn't know this command of Qemu Monitor.

Doing a test being connected to one of my guest by serial console, I see that when executing the command from Qemu Monitor, guest does a normal shutdown. But for this I believe that acpi package (Debian) must be installed.

Hmmm... but this does not work in OpenBSD with stock kernel with mpbios disabled. It will be necessary to configure something additional?

Regards,
Daniel

Signature · 2 lines
Fingerprint: BFB3 08D6 B4D1 31B2 72B9  29CE 6696 BF1B 14E6 1D37
Powered by Debian GNU/Linux Lenny - Linux user #188.598
#12 lore raw
In reply to Avishay Traeger
Quoted 13 lines · Avishay Traeger

[email protected] wrote on 01/24/2010 05:29:08 PM:

Quoted 7 lines

Anthony Liguori <[email protected]>
But the origin of the question here is probably, can something automatically shut down guests when a machine shutdowns down via the normal mechanism. AFAIK, libvirt does not support this today although there are some scripts floating around the intertubes that can drive this operation through libvirt.

Does the 'system_powerdown' monitor command initiate an ACPI shutdown? If so, I guess you can make an rc.d script that calls it during shutdown?

Quoted 7 lines

Avishay

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Under a linux guest with the acpi package , it can be done with libvirt, and it's work fine.

http://libvirt.org/formatdomain.html :
"||ACPI is useful for power management, for example, with KVM guests it is required for graceful shutdown to work."

Maybe the same can be done with windows guest.
I've never tested.

Regards

begin:vcard
fn:Jean-Philippe Menil
n:Menil;Jean-Philippe
org;quoted-printable:Universit=C3=A9 de Nantes;IRTS - DSI adr;quoted-printable:;;2, rue de la Houssini=C3=A8re;Nantes;Loire-Atlantique;44322;France email;internet:[email protected] title;quoted-printable:Administrateur r=C3=A9seau
tel;work:02.51.12.53.92
tel;fax:02.51.12.58.60
x-mozilla-html:FALSE
url:http://www.criun.univ-nantes.fr/
version:2.1
end:vcard

#13 lore raw
Quoted 1 line · Jean-Philippe Menil

Maybe the same can be done with windows guest.

Should work with any Windows, and verified to work with Vista x64 guest.

Signature · 4 lines
< Jernej Simončič ><><><><>< http://eternallybored.org/ >

It works better if you plug it in.
       -- Sattinger's Law
#14 lore raw
Quoted 6 lines · Jernej Simončič
Quoted 1 line · Jean-Philippe Menil

Maybe the same can be done with windows guest.

Should work with any Windows, and verified to work with Vista x64 guest.

The only problem is that windows does not want to shut down when you need it.

All versions of windows server requires enabling something in the registry - to notice the "power down" events to start with.

If a windows machine is used by someone else (open files, logged in user etc), it brings a dialog box in response to "power down" event asking if you _really_ want to shut down since this machine is used over network.

And finally, quite often during screensaver "work" windows notices the "power down" event only after some other event such as mouse move or a keypress.

In order to shut down my windows guests I come to this version:

  {
    # moving mouse helps windows (xp) to notice the powerdown event
    echo mouse_move 1 1
    sleep .1
    echo system_powerdown
    sleep 1
    # also for windows, if it asks "ok to shutdown if in use?"
    echo sendkey ret
    sleep .1
  } | \
    nc -U -w2 -q2 $run/$name/mon > /dev/null

That's netcat connecting to the guest's monitor which is a unix socket.

The script performs similar task for all guests in first cycle, next it repeats the procedure but now waits for $max_guest_waittime, which should be sufficient for any guest to shut down. If the guest did not shut down in time, the script simple kills the guest.

Note that "sleep 1" in the above is not necessary sufficient, as (windows) guest might be in swap and might need some time to draw the dialog box.

/mjt

#15 lore raw
Quoted 39 lines · Michael Tokarev
Quoted 5 lines · Jernej Simončič
Quoted 1 line · Jean-Philippe Menil

Maybe the same can be done with windows guest.

Should work with any Windows, and verified to work with Vista x64 guest.

The only problem is that windows does not want to shut down when you need it.

All versions of windows server requires enabling something in the registry - to notice the "power down" events to start with.

If a windows machine is used by someone else (open files, logged in user etc), it brings a dialog box in response to "power down" event asking if you _really_ want to shut down since this machine is used over network.

And finally, quite often during screensaver "work" windows notices the "power down" event only after some other event such as mouse move or a keypress.

In order to shut down my windows guests I come to this version:

  {
    # moving mouse helps windows (xp) to notice the powerdown event
    echo mouse_move 1 1
    sleep .1
    echo system_powerdown
    sleep 1
    # also for windows, if it asks "ok to shutdown if in use?"
    echo sendkey ret
    sleep .1
  } | \
    nc -U -w2 -q2 $run/$name/mon > /dev/null

That's netcat connecting to the guest's monitor which is a unix socket.

Quoted 8 lines

The script performs similar task for all guests in first cycle, next it repeats the procedure but now waits for $max_guest_waittime, which should be sufficient for any guest to shut down. If the guest did not shut down in time, the script simple kills the guest.

Note that "sleep 1" in the above is not necessary sufficient, as (windows) guest might be in swap and might need some time to draw the dialog box.

A cleaner alternative might be emulating the monitoring interface of some standard UPS. It's somehow the same scenario: The virtual power is about to vanish, let's inform the guest to shut down properly. And when choosing a serial link, that should even be possible without modifying QEMU.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux