LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Virtualization and Cloud
User Name
Password
Linux - Virtualization and Cloud This forum is for the discussion of all topics relating to Linux Virtualization and Linux Cloud platforms. Xen, KVM, OpenVZ, VirtualBox, VMware, Linux-VServer and all other Linux Virtualization platforms are welcome. OpenStack, CloudStack, ownCloud, Cloud Foundry, Eucalyptus, Nimbus, OpenNebula and all other Linux Cloud platforms are welcome. Note that questions relating solely to non-Linux OS's should be asked in the General forum.

Notices



Reply
 
Search this Thread
Old 06-03-2011, 04:53 PM   #1
alphy
LQ Newbie
 
Registered: Nov 2006
Posts: 16

Rep: Reputation: 0
How does QEMU shuts down a Guest OS.


Hi,

I am trying to understand the internals of the shutdown procedure in a virtual machine.

The aim is to understand:
1) How does the QEMU process, shuts down the Guest OS gracefully.

Any pointers in this regard would be useful

Thanks,
Alphonse
 
Old 06-03-2011, 05:07 PM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Main: Gentoo Others: What fits the task
Posts: 15,650
Blog Entries: 2

Rep: Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095
It doesn't. The OS shuts down itself, like it would do on a physical machine.
 
Old 06-03-2011, 05:37 PM   #3
jefro
Guru
 
Registered: Mar 2008
Posts: 12,082

Rep: Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520
It might be more of the way you pose the question.

If you shutdown qemu it would not allow the guest os time to shutdown properly. Depending on the VM you may have choices as to how that process happens also. Some allow you to choose. One choice may be to save state. One next start it resumes from where you left it. One one choice it is a hard power down. Other choices may exist too.

If you shutdown the guest from the normal OS shutdown then the end would be that qemu shuts down.
 
Old 06-03-2011, 08:17 PM   #4
alphy
LQ Newbie
 
Registered: Nov 2006
Posts: 16

Original Poster
Rep: Reputation: 0
Ok. Let me rephrase my question to be specific.

* Assume that I do not have control over Guest OS, but still I would like to signal the OS to
shutdown gracefully. Via QEMU-Monitor, I am able to issue a shutdown command to the VM.

I would like to understand the execution steps in this case.

Thanks for the information.

-Alphonse
 
Old 06-03-2011, 09:17 PM   #5
jefro
Guru
 
Registered: Mar 2008
Posts: 12,082

Rep: Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520
Then I think this is the correct answer. If you shutdown qemu it would not allow the guest os time to shutdown properly. UNLESS this works but you'd have to test it.


"system_powerdown

This has an effect similar to the physical power button on a modern PC. The VM will get an ACPI shutdown request and usually shutdown cleanly."

If you had access to the qemu monitor you may be able to save or stop or take a snapshot. See this page for the commands in monitor that may help. (assumes some level and compile and arch too)

http://en.wikibooks.org/wiki/QEMU/Monitor

In no case that I know of can qemu send a shutdown command to the client os. ssh, vnc, telnet or such is used if acpi issue.

Last edited by jefro; 06-03-2011 at 09:21 PM.
 
1 members found this post helpful.
Old 06-04-2011, 07:15 AM   #6
dyasny
Member
 
Registered: Dec 2007
Location: Canada
Distribution: RHEL,Fedora
Posts: 848

Rep: Reputation: 91
in very simple terms, QEMU sends the VM an ACPI shutdown command, same as what would happen when you click the power button on a physical machine. If that fails, you can issue the "destroy" command which will act as if you've pulled the plug on the VM

---------- Post added 06-04-11 at 11:16 AM ----------

note that for this to work, the VM has to be running with ACPI enabled and using an ACPI enabled HAL
 
Old 06-06-2011, 11:22 AM   #7
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Slackware, CentOS, Ubuntu, Fedora, Timesys, Linux From Scratch
Posts: 1,777
Blog Entries: 20

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by dyasny View Post
in very simple terms, QEMU sends the VM an ACPI shutdown command, same as what would happen when you click the power button on a physical machine. If that fails, you can issue the "destroy" command which will act as if you've pulled the plug on the VM

---------- Post added 06-04-11 at 11:16 AM ----------

note that for this to work, the VM has to be running with ACPI enabled and using an ACPI enabled HAL
Is there a signal that could be sent in lieu of a command, to QEMU's process, so that rc scripts shutting the host down can get QEMU to send that power button ACPI signal to the guest OS?
 
Old 06-06-2011, 01:14 PM   #8
dyasny
Member
 
Registered: Dec 2007
Location: Canada
Distribution: RHEL,Fedora
Posts: 848

Rep: Reputation: 91
system_powerdown is the command to send to the VM's monitor of course, it has already been mentioned.
 
Old 06-06-2011, 02:11 PM   #9
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Slackware, CentOS, Ubuntu, Fedora, Timesys, Linux From Scratch
Posts: 1,777
Blog Entries: 20

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by dyasny View Post
system_powerdown is the command to send to the VM's monitor of course, it has already been mentioned.
But for host system rc scripts, a signal is the usual means. Entering a command line is not readily done, as a script would have to hunt down the appropriate pty and take over. Normally rc scripts send a SIGTERM signal to processes to have them do a graceful end. QEMU just exits when it gets SIGTERM. Maybe some other signal will make it do the guest ACPI thing?
 
Old 06-07-2011, 03:01 AM   #10
dyasny
Member
 
Registered: Dec 2007
Location: Canada
Distribution: RHEL,Fedora
Posts: 848

Rep: Reputation: 91
you have it completely wrong.
Every VM running with QEMU maintains a socket, through which the host communicates with the QEMU process.

This socket is called qemu monitor, and can be accessed. When you access it, you do not access the VM itself, but the actual process that keeps it running, so when you issue "system_powerdown" to that process, it will emulate an ACPI shutdown in the guest space.

If the VM itself supports ACPI, it will detect the ACPI shutdown request, and that will trigger the normal init scripts and the rest of the typical sequence.
 
Old 06-07-2011, 03:00 PM   #11
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Slackware, CentOS, Ubuntu, Fedora, Timesys, Linux From Scratch
Posts: 1,777
Blog Entries: 20

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by dyasny View Post
you have it completely wrong.
Every VM running with QEMU maintains a socket, through which the host communicates with the QEMU process.
I don't see anything in the documentation where a socket can be accessed to send the emulator a monitor command. Doing "lsof" on a running qemu emulator shows 2 pipes and 2 unit domain sockets without names (hence, there is no target to make a connection to).

Quote:
Originally Posted by dyasny View Post
This socket is called qemu monitor, and can be accessed. When you access it, you do not access the VM itself, but the actual process that keeps it running, so when you issue "system_powerdown" to that process, it will emulate an ACPI shutdown in the guest space.
There is a monitor already in the manual command line session. Is that what you are referring to? That's where I can type in "system_powerdown" and watch the guest OS either do the right thing or not (depending on whose opinion of the right thing we are referring to).

Last edited by Skaperen; 06-07-2011 at 03:27 PM.
 
Old 06-07-2011, 04:02 PM   #12
jefro
Guru
 
Registered: Mar 2008
Posts: 12,082

Rep: Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520
I think it would boil down to a proper test on a test platform to be sure. ACPI has failed me before on qemu.
 
Old 06-08-2011, 06:39 AM   #13
dyasny
Member
 
Registered: Dec 2007
Location: Canada
Distribution: RHEL,Fedora
Posts: 848

Rep: Reputation: 91
Quote:
Originally Posted by Skaperen View Post
There is a monitor already in the manual command line session. Is that what you are referring to? That's where I can type in "system_powerdown" and watch the guest OS either do the right thing or not (depending on whose opinion of the right thing we are referring to).
that's the monitor exactly. what happens when it doesn't work for you?
 
Old 06-08-2011, 10:09 AM   #14
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Slackware, CentOS, Ubuntu, Fedora, Timesys, Linux From Scratch
Posts: 1,777
Blog Entries: 20

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by dyasny View Post
that's the monitor exactly. what happens when it doesn't work for you?
Desktop systems put up a prompt asking to shutdown, just like selecting the shutdown menu item. There's usually a 60 second wait. That means I'd have to be sure the host has an extra 60 seconds shutdown process time before doing an aggressive process kill.

Server systems work in some cases (Slackware) and not at all in other (Ubuntu server just stays up).

The real issue is getting that signal to the QEMU process at the time init is sending SIGTERM to other processes. If QEMU were to handle SIGTERM by doing the power button emulation, the issue would be gone. If it could make made to handle it on another signal like SIGHUP, that would be easy to work around. But, getting init to start a script that will hunt down the QEMU monitor sockets to feed in the command line, I have no clue where to start with that.
 
Old 06-08-2011, 10:12 AM   #15
Slax-Dude
Member
 
Registered: Mar 2006
Location: Valadares, Portugal
Distribution: Slackware
Posts: 273

Rep: Reputation: 74
@Skaperen

I have a script, which is called from rc.local_shutdown, on my slackware host which will issue a ACPI shutdown command to all running guests, then wait for 60 secs for them to cleanly shutdown.
If all the guests cleanly shutdown before the timeout, the script will exit and the host will continue shutting down, if not, it will force shutdown to all running guests and the host will continue shutting down.

I use libvirt, one of the available management tools you are not inspired to use


Code:
#!/bin/bash

# number of seconds to wait for VMs to shutdown, before killing them
MAX_TIMEOUT=60

RUNNING_VM_LIST=$(/usr/sbin/virsh list|grep running|cut -d " " -f 4-4)

for RUNNING_VM in $RUNNING_VM_LIST; do
  /usr/sbin/virsh shutdown $RUNNING_VM
done

TIMEOUT_COUNTER=0
while true; do
  RUNNING_VM_LIST=$(/usr/sbin/virsh list|grep running|cut -d " " -f 4-4)
  if [ -z "$RUNNING_VM_LIST" ]; then
    exit 0
  else
    if [ $TIMEOUT_COUNTER -lt $MAX_TIMEOUT ]; then
      sleep 1
      TIMEOUT_COUNTER=$[$TIMEOUT_COUNTER+1]
    else
      for RUNNING_VM in $RUNNING_VM_LIST; do
        /usr/sbin/virsh destroy RUNNING_VM
      done
    fi
  fi
done
 
  


Reply

Tags
kvm, qemu


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
QEMU guest paused after starting on boot dhamilton Linux - Virtualization and Cloud 1 01-02-2011 12:43 AM
[SOLVED] qemu ping guest os Squall90 Linux - Virtualization and Cloud 1 12-07-2010 09:31 AM
Share between Qemu-Kvm host and guest Alexvader General 1 06-13-2010 04:07 PM
Solaris 10 guest endless reboot under KVM/Qemu green_dood Linux - Software 6 12-13-2009 02:20 AM
sound in qemu guest os on debian? zero79 Debian 2 10-16-2005 07:42 PM


All times are GMT -5. The time now is 06:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration