Start script for all VMs in /etc/init.d ?

Discussions related to using VirtualBox on Linux hosts.
Post Reply
rowi
Posts: 5
Joined: 4. May 2008, 17:48
Location: Germany
Contact:

Start script for all VMs in /etc/init.d ?

Post by rowi »

Dear all,

since every guy should have the same problem like me, I'd like to ask if there is a start script which starts all my virtual machines when the server comes up and stops them when the server goes down.
I think of a script like /etc/init.d/vboxdrv but one for my VMs.

best regards
Rolf
Sasquatch
Volunteer
Posts: 17798
Joined: 17. Mar 2008, 13:41
Primary OS: Debian other
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Windows XP, Windows 7, Linux
Location: /dev/random

Post by Sasquatch »

Should be fairly simple. All you have to do is catch the parameter (start, stop) and put that in cases or if statements, along the proper parameters for the vboxmanage command. Like '/etc/init.d/vm1 start', would run 'vboxmanage startvm vm1' and stop would send 'vboxmanage control vm vm1 savestate'.
Read the Forum Posting Guide before opening a topic.
VirtualBox FAQ: Check this before asking questions.
Online User Manual: A must read if you want to know what we're talking about.
Howto: Install Linux Guest Additions
Howto: Use Shared Folders on Linux Guest
See the Tutorials and FAQ section at the top of the Forum for more guides.
Try searching the forums first with Google and add the site filter for this forum.
E.g. install guest additions site:forums.virtualbox.org

Retired from this Forum since OSSO introduction.
rowi
Posts: 5
Joined: 4. May 2008, 17:48
Location: Germany
Contact:

Post by rowi »

Thank you for replying. I'd like to start every virtual machine I have in one script, so I don't need to use another script when adding (or removing) a virtual machine. Unfortunately VBoxManage list runningvms doesn't give me a feedback of the names of the running vms. Beside this, I would like to do something like that:

Code: Select all

#! /bin/sh

# Q&D script for starting/stopping vms

start()
{
VBoxManage startvm Debby -type vrdp
VBoxManage startvm Winnie -type vrdp
}

stop()
{
VBoxManage controlvm Debby savestate
VBoxManage controlvm Winnie savestate
}

case "$1" in
start)
    start
    ;;
stop)
    stop
    ;;
*)
    echo "Usage: $0 {start|stop}"
    exit 1
esac

exit 0
I've seen that the vms are obviously already stopped by /etc/init.d/vboxdrv in stop mode (calling stop_vms) when shutting down, so there may be no need to stop them in an own script.

The script above could be enhanced by looking for the available machines and start them all. The I'd be happy :). But I don't know how to get the names "Debby" and "Winnie" out of a vboxmanage-command (or from whatever).

If I stop the vms I can connect to them anyway. Is this ok?

best regards
Rolf
TerryE
Volunteer
Posts: 3572
Joined: 28. May 2008, 08:40
Primary OS: Ubuntu other
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Ubuntu 10.04 & 11.10, both Svr&Wstn, Debian, CentOS
Contact:

Post by TerryE »

do an xargs on `VBoxManage list vms | egrep ^Name:| cut --characters=18-` or ditto ... runningvms ...
JshWright
Volunteer
Posts: 119
Joined: 13. Sep 2007, 00:33
Primary OS: Debian Lenny
VBox Version: OSE self-compiled
Guest OSses: Many...

Post by JshWright »

VBoxManage list runningvms returns the UUIDs of the VMs, which can be used in place of the name for any of the VBox command line tools.

Additionally, you might want to look at using VBoxHeadless rather than VBoxManage -type....
thomasjohansen
Posts: 16
Joined: 9. May 2008, 09:39
Primary OS: Ubuntu other
VBox Version: OSE Debian
Guest OSses: ubuntu, w2000, xp
Location: Denmark, Koege

Post by thomasjohansen »

when running a script like this:

Code: Select all

#!/bin/sh
echo "***setting up network for virtualbox***"
sudo tunctl -t tap1 -u thomas
sudo tunctl -t tap2 -u thomas
sudo brctl addbr br0
sudo ifconfig eth0 0.0.0.0 promisc
sudo brctl addif br0 eth0
sudo ifconfig br0 10.192.67.47 netmask 255.255.255.0
sudo route add default gw 10.192.67.1 br0
sudo brctl addif br0 tap1
sudo brctl addif br0 tap2
sudo ifconfig tap1 up
sudo ifconfig tap2 up 
echo "***done setting up network for virtualbox***"
echo "***starting virtualbox VM-mailserver & VM-oipserver"
VBoxManage startvm Mailserver -type vrdp
VBoxManage startvm oipserver -type vrdp
echo "***done starting virtualbox VM-mailserver & VM-oipserver"
it will only start one of the VM's. It cant handle 2 VBoxManage commands after each other because its not finish with the first before it starts the next one.

how to work around this porblem?
Sasquatch
Volunteer
Posts: 17798
Joined: 17. Mar 2008, 13:41
Primary OS: Debian other
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Windows XP, Windows 7, Linux
Location: /dev/random

Post by Sasquatch »

Add a sleep for it? Time how long it takes for the command to finish, and set that for the sleep timer plus some extra time in case your system is a bit slower.
Read the Forum Posting Guide before opening a topic.
VirtualBox FAQ: Check this before asking questions.
Online User Manual: A must read if you want to know what we're talking about.
Howto: Install Linux Guest Additions
Howto: Use Shared Folders on Linux Guest
See the Tutorials and FAQ section at the top of the Forum for more guides.
Try searching the forums first with Google and add the site filter for this forum.
E.g. install guest additions site:forums.virtualbox.org

Retired from this Forum since OSSO introduction.
TerryE
Volunteer
Posts: 3572
Joined: 28. May 2008, 08:40
Primary OS: Ubuntu other
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Ubuntu 10.04 & 11.10, both Svr&Wstn, Debian, CentOS
Contact:

Post by TerryE »

How about:

Code: Select all

...
VBoxManage startvm Mailserver -type vrdp &
VBoxManage startvm oipserver -type vrdp
...
That is put the first startvm into the bg. Though thinking about it, I'd add & to both startvm's so that the script completes after starting both machines and does not stall the init.d script until the VMs close.
Sasquatch
Volunteer
Posts: 17798
Joined: 17. Mar 2008, 13:41
Primary OS: Debian other
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Windows XP, Windows 7, Linux
Location: /dev/random

Post by Sasquatch »

Then I would actually change it to this:

Code: Select all

VBoxManage startvm Mailserver -type vrdp && VBoxManage startvm oipserver -type vrdp
With that line, the second command is executed when the first one is done. I just realized it. I used a similar method for my networking config. I had to bring some interfaces down and up again, so I added them all on one line and it did what it supposed to do. You might be able to do that with your network config too.

I do wonder one thing. Why do you put your eth0 into promisc mode? I don't do it (or it's set automatically) and it runs just fine with Host Interface.
Read the Forum Posting Guide before opening a topic.
VirtualBox FAQ: Check this before asking questions.
Online User Manual: A must read if you want to know what we're talking about.
Howto: Install Linux Guest Additions
Howto: Use Shared Folders on Linux Guest
See the Tutorials and FAQ section at the top of the Forum for more guides.
Try searching the forums first with Google and add the site filter for this forum.
E.g. install guest additions site:forums.virtualbox.org

Retired from this Forum since OSSO introduction.
thomasjohansen
Posts: 16
Joined: 9. May 2008, 09:39
Primary OS: Ubuntu other
VBox Version: OSE Debian
Guest OSses: ubuntu, w2000, xp
Location: Denmark, Koege

Great worked with &&

Post by thomasjohansen »

Thx, it worked by adding two && after the first VBoxManage command.


About my networksettings:

I have used alot of time figuring out how to make HIF working, and followed tons of guides, and the one which worked for me was one from the ubuntuforum where it suggested "promisc", so im just repeating what it says.


So for now im keeping it like that, maybe I'll change it when I get a little better with ubuntu in generel. (still a newbie)
Sasquatch
Volunteer
Posts: 17798
Joined: 17. Mar 2008, 13:41
Primary OS: Debian other
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Windows XP, Windows 7, Linux
Location: /dev/random

Post by Sasquatch »

I'm pretty green myself, but using the Manual way (that is use VBoxAddIF), I had no problems at all. It works like a charm, no need to change anything, only listed 'bridge_ports lan vbox0 vbox1' in my interfaces file at the brdg interface config.
Read the Forum Posting Guide before opening a topic.
VirtualBox FAQ: Check this before asking questions.
Online User Manual: A must read if you want to know what we're talking about.
Howto: Install Linux Guest Additions
Howto: Use Shared Folders on Linux Guest
See the Tutorials and FAQ section at the top of the Forum for more guides.
Try searching the forums first with Google and add the site filter for this forum.
E.g. install guest additions site:forums.virtualbox.org

Retired from this Forum since OSSO introduction.
Post Reply