1

I've deployed 2 kvm guests( db and app) on a Debian7 host.

I can set them autostart by

root@mhost:~# virsh autostart db
root@mhost:~# virsh autostart app

This actually creates 2 symbolic links at the /etc/libvirt/qemu/autostart/ :

root@mhost:~# ls /etc/libvirt/qemu/autostart/
db.xml app.xml

Then I thought I can ensure the two guests starting order by simply changing the links name:

root@mhost:/etc/libvirt/qemu/autostart# mv db.xml 10db.xml
root@mhost:/etc/libvirt/qemu/autostart# mv app.xml 20app.xml

but it turns out this doesn't work. Actually after changing the links name, they even don't autostart anymore. What's the problem?

There is a similiar question with answer, but it didn't explain why the symbolic links method not work.

Also, I've tried to use init start script to ensure the order, but it didn't work either. I'm not a shell script guy, so there might be some problem in my script, i.e., the following:

#! /bin/sh
# /etc/init.d/kvmguests

case "$1" in
    start)
        echo "Starting all kvm guests ..."
        virsh start db
        virsh start app
        echo "Done."
        ;;
    stop)
        echo "Stopping all kvm guests ..."
        virsh shutdown app
        virsh shutdown db
        echo "Done."
        ;;
    *)
        echo "Usage: /etc/init.d/kvmguests {start|stop}"
        exit 1
        ;;
esac
exit 0
2
  • 3
    I think I can see what you're trying to do: You want your database to be up before your application, which depends on the database, comes up. The problem with your solution is that simply starting the VMs in a particular order does not guarantee anything about when the services they provide will actually become available. You could start the db VM well before the app VM, and it could still happen that the database is not yet ready when the application attempts to connect to it. A better solution would be to design the application to wait for the database to become available. Oct 13, 2013 at 18:37
  • Thanks @Steven, However I kinda think that just shun the problem. I agree we should design the application that way. But there are situtions we have to ensure the starting order of VMs due to some uncontrollable/stupid reasons
    – John Wang
    Oct 14, 2013 at 2:02

2 Answers 2

1

IMHO I don't think you can control the boot order by changing the names of the symlinks. but there is a workaround

In /etc/sysconfig/libvirt-guests option START_DELAY you can set the delay between boots (so not all vms will start at the same time)

To (sort of) control the order you can change <emulator> tag in domain XML and point to your script, which will start domain after some delay

e.g. something like this:

<domain type='kvm'>
  <name>test</name>
  ...
  <devices>
    <emulator>/usr/local/bin/qemu-kvm-delay</emulator>
  <devices>
</domain>

the content of /usr/local/bin/qemu-kvm-delay could be as simple as:

sleep 1000
exec /usr/bin/qemu-kvm $*

or something fancy as testing for some service availability and calculating the delay for each vm

0

It's an application level problem, not in infrastructure level.

Actually boot up 2 KVM instance means you power them on, there's no guarantee on the boot up sequence of the 2 operation systems inside those 2 KVM instance in the KVM perspective.

However, you can make it on your own. here's the logic:

  1. boot up VM named db, make sure the database application inside this vm works
  2. then boot up VM named app. Maybe you need to make sure the apps connected to your database.

Take mysql for example, here's a simple bash script:

#!/bin/bash

# start the database VM
sudo virsh start db

# make sure the database is started
IS_DB_DOWN=true
while $IS_DB_DOWN; do
  # test if the DB is up
  nc -z <your-db-host> 3306 > /dev/null 2>&1
  if [ $? -eq 0 ]; then
    IS_DB_DOWN=true
  fi
  sleep 5 # use 5 sec as internal
done

# start your VM that runs application
sudo virsh start app
3
  • Thanks Shawn, my orgininal idea is similiar with yours, i.e., using shell script to control the order. However my init script doesn't work, it doesn't start up the VMs at all. Could you please help me check the script in my question?
    – John Wang
    Oct 14, 2013 at 2:12
  • the virsh tool needs the libvirtd daemon running. would u please make sure it works?
    – shawnzhu
    Oct 14, 2013 at 4:44
  • I've also suspected that, but the libvirtd init script is indeed put at a higher starting index. i.e., in my /etc/rc2.d/ folder(my default runlevel is 2), I explicitly set my script run after the libvirtd as ... S20libvirt-bin , S30my-kvm-guests, ...
    – John Wang
    Oct 14, 2013 at 5:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .