Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 29 Apr 2011 23:26:53 +0200
From:      =?ISO-8859-1?Q?Stefan_Tollk=FChn?= <stefan.tollkuehn@editura.de>
To:        freebsd-emulation@freebsd.org
Subject:   Re: AW: rc.d script for running multiple VirtualBox VMs from /etc/rc.conf
Message-ID:  <4DBB2D1D.9090106@editura.de>
In-Reply-To: <7eb90bc16d977388d722269e4e05b9b7@bluelife.at>
References:  <634914A010D0B943A035D226786325D422B76F91CA@EXVMBX020-12.exch020.serverdata.net>	<41b7269d367ea0d98c3bd4071358d3ab@bluelife.at>	<634914A010D0B943A035D226786325D422B76F9354@EXVMBX020-12.exch020.serverdata.net>	<0cd75e5b5debaa8d6a20efeb69af90e9@bluelife.at> <7eb90bc16d977388d722269e4e05b9b7@bluelife.at>

next in thread | previous in thread | raw e-mail | index | archive | help
This is a multi-part message in MIME format.
--------------010206050808040903080705
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi everybody,

I tried to install the new vboxheadless script, but there is a problem
if the users default shell is /usr/sbin/nologin. This prevents the
command from running essential parts of the script in line 49, 83 and 106.

First I tried to invoke the su command with -m but this didn't work.

So I replaced su with sudo. This means sudo will be a new dependency for
the virtualbox port, but this might be a minor problem, since sudo is
most likely installed.

At the moment I'm unable to test the script in a real life scenario by
rebooting the host. I'll test it this weekend. ;)

- --
Best regards

Stefan Tollkühn

Editura GmbH & Co. KG
Tempelhofer Damm 2 - 12101 Berlin
Telefon +49 (30) 789594981
Telefax +49 (30) 789594990
E-Mail stefan.tollkuehn@editura.de

Amtsgericht Charlottenburg, HRA 43189 B, USt.Id. DE217180548

Geschäftsführer: Ralf Szymanski, Stefan Krause

On 04/19/2011 07:57 PM, Bernhard Froehlich wrote:
> On Mon, 18 Apr 2011 16:29:33 +0200, Bernhard Froehlich wrote:
>> On Wed, 13 Apr 2011 05:28:44 -0700, Tobias Oberstein wrote:
>>> Hi Bernhard,
>>>
>>>>> I wanted to configure multiple VirtualBox VMs to startup automatically
>>>>> at boot time and generally be a nice citizen to the FreeBSD way of
>>>>> starting up stuff.
>>>>>
>>>>> Using that rc.d script (see attached) you can configure starting up
>>>>> multiple VirtualBox VMs from /etc/rc.conf:
>>>>
>>>> Sorry it's probably my fault because you're the 3rd one who has written such
>>>> a script but we already have one for 4.0.
>>>
>>> Ah, ok;)
>>>
>>>>
>>>> http://svn.bluelife.at/index.cgi/blueports/view/emulators/virtualbox-ose-
>>>> devel/files/vboxheadless.in
>>>>
>>>> It is a bit less complicated and can also manage multiple VMs but it does not
>>>> have separated flags per VM.
>>>
>>> Mine is more complicated, yes .. but it's more or less a direct
>>> adaption of the script for apache22.
>>>
>>> Also, I definitely need flags per VM, since we run each VM
>>>
>>> i) under a dedicated user
>>> ii) on a separate ZFS filesystem
>>> iii) with specific VNC port/password
>>>
>>> i+iii requires flags per VM.
>>>
>>> iii) is for obvious reasons .. need to connect to each VM on a distinct port.
>>>
>>> i) is for a) security and b) management of VMs by different "VM
>>> admins" which don't have to be root or have a user which would manage
>>> all VMs.
>>>
>>> ==
>>>
>>> I there interest to consolidate our scripts?
>>
>> I've merged the features together and got the following:
>>
>> # vboxheadless_enable (bool):         Set to "NO" by default.
>> #                                     Set it to "YES" to enable
>> vboxheadless.
>> # vboxheadless_machines (str):        Space separated list of machines
>> # vboxheadless_user (str):            Default user account to run with
>> # vboxheadless_<machine>_name (str):  VBox machine name or UUID
>> # vboxheadless_<machine>_user (str):  User account to run with
>> # vboxheadless_<machine>_flags (str): Custom flags for VBoxHeadless
>>
>> simple example:
>>
>> vboxheadless_enable="YES"
>> vboxheadless_machines="FreeBSD Ubuntu1 Machine3"
>>
>>
>> complex example:
>> vboxheadless_enable="YES"
>> vboxheadless_machines="vm1 vm2 Machine3"
>> vboxheadless_user="vboxuser"
>>
>> vboxheadless_vm1_name="FreeBSD"
>> vboxheadless_vm1_user="user1"
>>
>> vboxheadless_vm2_name="Ubuntu1"
>> vboxheadless_vm2_user="user2"
>> vboxheadless_vm2_flags="--vnc --vncport 4711 --vncpass <here>"
>>
>>
>> I could not test it yet but will do that in the evening and commit the
>> new script to blueports.
> 
> Done. http://svn.bluelife.at/index.cgi/blueports/revision/?rev=1287
> 
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk27LRwACgkQQYKEyEkJsIMNQACeLu1Fyj2eIvqoz9OVEqtFEsJZ
9akAnR0LybqddmXx9MO9KbEkTQ6tMdg4
=6ZR+
-----END PGP SIGNATURE-----

--------------010206050808040903080705
Content-Type: text/plain;
 name="vboxheadless.in"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename="vboxheadless.in"

#!/bin/sh

# $FreeBSD$
#
# PROVIDE: vboxheadless
# REQUIRE: LOGIN vboxnet
# KEYWORD: shutdown
#
# Add the following line to /etc/rc.conf[.local] to enable vboxheadless
#
# vboxheadless_enable (bool):         Set to "NO" by default.
#                                     Set it to "YES" to enable vboxheadless.
# vboxheadless_machines (str):        Space separated list of machines 
# vboxheadless_user (str):            Default user account to run with.
#                                     (default: %%VBOXUSER%%)
# vboxheadless_<machine>_name (str):  Virtualbox machine name or UUID.
# vboxheadless_<machine>_user (str):  User account to run with.
# vboxheadless_<machine>_flags (str): Additional flags for VBoxHeadless.

. /etc/rc.subr

name="vboxheadless"
rcvar=`set_rcvar`

command="%%PREFIX%%/bin/VBoxHeadless"
pidbase="/var/run/${name}"
   
start_cmd="${name}_start"
stop_cmd="${name}_stop"
status_cmd="${name}_status"

vboxheadless_start()
{
    local pid
    local mpidfile

    echo "Starting Virtual Machines:"
    for machine in ${vboxheadless_machines}; do
        mpidfile="${pidbase}_${machine}.pid"
        pid=$(check_pidfile $mpidfile $command)
        eval vmname="\${vboxheadless_${machine}_name:-${machine}}"
        eval vmuser="\${vboxheadless_${machine}_user:-${vboxheadless_user}}"
        eval vmflags="\${vboxheadless_${machine}_flags:-}"

        HOME=$(/usr/sbin/pw usershow -7 -n "${vmuser}" | /usr/bin/cut -d: -f6)

        /usr/bin/printf "%25s " "${vmname}"

        %%PREFIX%%/bin/sudo -u ${vmuser} %%PREFIX%%/bin/VBoxManage showvminfo "${vmname}" >/dev/null

        if [ $? != 0 ]; then
            echo "Unknown machine"
            continue
        fi

        if [ -n "${pid}" ]; then 
            echo "Already running? (pid=${pid})"
            continue
        fi

        /usr/bin/touch ${mpidfile}
        /usr/sbin/chown ${vmuser} ${mpidfile}
        /usr/sbin/daemon -f -p ${mpidfile} -u ${vmuser} ${command} --startvm "${vmname}" ${vmflags}
        echo "Started"
    done
}

vboxheadless_stop()
{
    local pid 
    local mpidfile

    echo "Saving states for Virtual Machines:"
    for machine in ${vboxheadless_machines}; do
        mpidfile="${pidbase}_${machine}.pid"
        pid=$(check_pidfile $mpidfile $command)
        eval vmname="\${vboxheadless_${machine}_name:-${machine}}"
        eval vmuser="\${vboxheadless_${machine}_user:-${vboxheadless_user}}"

        /usr/bin/printf "%25s " "${vmname}"

        if [ -n "${pid}" ]; then 
            %%PREFIX%%/bin/sudo -u ${vmuser} %%PREFIX%%/bin/VBoxManage controlvm "${vmname}" savestate >/dev/null
            wait_for_pids $pid >/dev/null
            echo "Stopped"
        else
            echo "Not running?"
        fi      
    done
}

vboxheadless_status()
{
    local pid 
    local mpidfile

    /usr/bin/printf "%25s %s\n" "Machine" "Status"
    /usr/bin/printf "%25s %s\n" "-------------------------" "------------"

    for machine in ${vboxheadless_machines}; do
        mpidfile="${pidbase}_${machine}.pid"
        pid=$(check_pidfile $mpidfile $command)
        eval vmname="\${vboxheadless_${machine}_name:-${machine}}"
        eval vmuser="\${vboxheadless_${machine}_user:-${vboxheadless_user}}"

        %%PREFIX%%/bin/sudo -u ${vmuser} %%PREFIX%%/bin/VBoxManage showvminfo "${vmname}" >/dev/null

        if [ $? != 0 ]; then
            /usr/bin/printf "%20s %s\n" "${vmname}" "Unknown Machine"
        elif [ -n "${pid}" ]; then 
            /usr/bin/printf "%25s %s\n" "${vmname}" "Running"
        else
            /usr/bin/printf "%25s %s\n" "${vmname}" "Powered Off"
        fi
    done
}

load_rc_config $name

: ${vboxheadless_enable="NO"}
: ${vboxheadless_machines=""}
: ${vboxheadless_user="%%VBOXUSER%%"}

cmd_arg="$1"
if [ $# -gt 0 ]; then
    shift
fi
if [ -n "$*" ]; then
    vboxheadless_machines="$*"
fi

run_rc_command "${cmd_arg}"

--------------010206050808040903080705--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4DBB2D1D.9090106>