- Fix autostart of VMs, which is no longer handled by the vboxes.service.

A new routine "start_vms" is added to the start section of vboxdrv.sh,
  which is called by vboxdrv.service. Files "vboxes.service", "vboxes.sh",
  and "virtualbox-sysconfig.vbox" are deleted.
  These changes are to satisfy bsc#1107769.

OBS-URL: https://build.opensuse.org/package/show/Virtualization/virtualbox?expand=0&rev=445
This commit is contained in:
Larry Finger 2018-09-20 19:27:52 +00:00 committed by Git OBS Bridge
parent 0947a127a5
commit 75cddad54d
7 changed files with 40 additions and 290 deletions

View File

@ -21,7 +21,7 @@ fi
# Check if virtualbox-host-source is installed, quit if not
if ! rpm -qf "$SOURCE/Makefile" &>/dev/null ; then
echo "Sources for building host modules are not present,"
echo "Use 'sudo zypper install virtualbox-host-source kernel_devel' to install them. Quitting .."
echo "Use 'sudo zypper install virtualbox-host-source kernel-devel kernel-default-devel' to install them. Quitting .."
exit 1
fi
#

View File

@ -344,6 +344,30 @@ stop_vms()
fi
}
start_vms()
{
OLD_IFS=$IFS
IFS=$'\n'
# read config file
[ -r /etc/default/virtualbox ] && . /etc/default/virtualbox
#echo "Symbols are $VBOXAUTOSTART_DB and $VBOXAUTOSTART_CONFIG"
# read autostart config file
if [ -r $VBOXAUTOSTART_CONFIG ]; then
VBoxManage list vms
# find all the files of type user.start
var=$(ls $VBOXAUTOSTART_DB | grep start | grep -v auto)
# process each file of that type
for i in $var; do
# Extract the user name - the first word on the line
user=$(echo $i | head -n1 | cut -d "." -f1)
# autostart the VMs for that user
su $user -c "/usr/lib/virtualbox/VBoxAutostart --start --config $VBOXAUTOSTART_CONFIG"
done
fi
IFS=$OLD_IFS
}
cleanup()
{
for i in /lib/modules/*; do
@ -407,6 +431,7 @@ dmnstatus()
case "$1" in
start)
start
start_vms
;;
stop)
stop_vms

View File

@ -1,19 +0,0 @@
# Autostart configured Virtual Box VMs
# configuration: /etc/sysconfig/vbox
[Unit]
SourcePath=/usr/lib/virtualbox/vboxes.sh
Description=Autostart Headless Virtual Box VMs
Before=multi-user.target graphical.target
After=network-online.target vboxdrv.service
Wants=network-online.target vboxdrv.service
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/lib/virtualbox/vboxes.sh start
ExecStop=/usr/lib/virtualbox/vboxes.sh stop
[Install]
WantedBy=multi-user.target

227
vboxes.sh
View File

@ -1,227 +0,0 @@
#!/bin/sh
#
# description: Starts and stops vbox autostart VMs.
# Based on
# http://www.amiryan.org/2009/11/04/virtualbox-init-d-service-autostart-scriptu
#
# By Richard Bos <rbos at opensuse dot org> - May 2010
#
# Converted to standalone script for systemd environments
#
# By Hans-Peter jansen <hpj at urpla dot net> - July 2017
#
PRG=$(basename $0)
SERVICE="Virtualbox machines"
VBOXMGR_BIN=/usr/lib/virtualbox/VBoxManage
if [[ ! -x $VBOXMGR_BIN ]]; then
echo "$VBOXMGR_BIN does not exist"
if [ "$1" = "stop" ]; then
exit 0
else
exit 6
fi
fi
# read config file
[ -r /etc/sysconfig/vbox ] && . /etc/sysconfig/vbox
start() {
N=1
for VBOX in $VBOX_AUTOSTART; do
if grep -q \; <<< "$VBOX"; then
VBOX_NAME[$N]=$(cut -d\; -f1 <<< "$VBOX")
VBOX_USER[$N]=$(cut -d\; -f2 <<< "$VBOX")
else
VBOX_NAME[$N]="$VBOX"
VBOX_USER[$N]=""
fi
N=$(($N+1))
done
VBOXES=${#VBOX_NAME[*]}
if [ $VBOXES -eq 0 ]; then
# The virtual machines have to be configured in /etc/sysconfig/vbox
echo "Starting $SERVICE: no virtual machines configured"
else
N=1
echo "Starting $SERVICE: "
while [[ $N -le $VBOXES ]]; do
if [[ $N -lt $VBOXES ]]; then
echo -n "${VBOX_NAME[$N]} (user: ${VBOX_USER[$N]}), "
else
echo "${VBOX_NAME[$N]} (user: ${VBOX_USER[$N]})"
fi
N=$(($N+1))
done
N=1
while [[ $N -le $VBOXES ]]; do
if [[ -n "${VBOX_USER[$N]}" ]]; then
if getent passwd ${VBOX_USER[$N]} &>/dev/null; then
# The tag "Name:" occurs in multiple sections. Require at least 7 blanks
# with an additional flexible amount of spaces. At the moment of writing
# 13 spaces are needed.
VBOX_RUNNING=$(su ${VBOX_USER[$N]} -c "$VBOXMGR_BIN list --long runningvms" |
sed -n 's/^Name:[[:blank:]]\{7\} *//p' | grep -w "${VBOX_NAME[$N]}")
if [[ -z "$VBOX_RUNNING" ]]; then
VBOX_PRESENT=$(su ${VBOX_USER[$N]} -c "$VBOXMGR_BIN list --long vms" |
sed -n 's/^Name:[[:blank:]]\{7\} *//p' | grep -w "${VBOX_NAME[$N]}")
if [[ -n "$VBOX_PRESENT" ]]; then
# start VM with VBoxManage in headless mode
# unlike VBoxHeadless, VBoxManage waits until VM is running
su ${VBOX_USER[$N]} -c "$VBOXMGR_BIN -q startvm "${VBOX_NAME[$N]}" -type headless" &> /tmp/$PRG.$$
RETVAL=$?
if [[ $RETVAL != 0 ]]; then
echo "Starting virtual machine: ${VBOX_NAME[$N]} (user: ${VBOX_USER[$N]}) failed: "
cat /tmp/$PRG.$$
fi
rm /tmp/$PRG.$$
else
echo "Virtual machine: ${VBOX_NAME[$N]} (user: ${VBOX_USER[$N]}) does not exist"
fi
else
echo "Virtual machine: ${VBOX_NAME[$N]} (user: ${VBOX_USER[$N]}) is already running"
fi
else
echo "Virtual machine: ${VBOX_NAME[$N]}, VBOX_USER: ${VBOX_USER[$N]} does not exist"
fi
else
echo "Virtual machine: ${VBOX_NAME[$N]}: VBOX_USER not configured"
fi
N=$(($N+1))
done
fi
}
stop() {
for VBOX in $VBOX_AUTOSTART; do
if grep -q \; <<< "$VBOX"; then
VBOX_USER=$(cut -d\; -f2 <<< "$VBOX")
# Only add the user to the list, if not present yet
if ! grep -qw "$VBOX_USER" <<< "$VBOX_USERS"; then
VBOX_USERS="$VBOX_USERS $VBOX_USER"
fi
fi
done
N=1
for VBOX_USER in $VBOX_USERS; do
VBOX_RUNNING=$(su $VBOX_USER -c "$VBOXMGR_BIN list --long runningvms" |
sed -n 's/^Name:[[:blank:]]\{7\} *//p')
for VBOX in $VBOX_RUNNING; do
VBOX_NAME[$N]="$VBOX"
VBOX_USER[$N]="$VBOX_USER"
N=$(($N+1))
done
done
VBOXES=${#VBOX_NAME[*]}
if [[ $VBOXES -eq 0 ]]; then
echo "Shutting down $SERVICE: no virtual machines running."
else
echo "Shutting down $SERVICE: "
N=1
while [[ $N -le $VBOXES ]]; do
echo -n "${VBOX_NAME[$N]} (user: ${VBOX_USER[$N]}): "
su ${VBOX_USER[$N]} -c "$VBOXMGR_BIN -q controlvm ${VBOX_NAME[$N]} savestate"
N=$(($N+1))
done
fi
}
status() {
for VBOX in $VBOX_AUTOSTART; do
if grep -q \; <<< "$VBOX"; then
VBOX_USER=$(cut -d\; -f2 <<< "$VBOX")
# Only add the user to the list, if not present yet
if ! grep -qw "$VBOX_USER" <<< "$VBOX_USERS"; then
VBOX_USERS="$VBOX_USERS $VBOX_USER"
fi
fi
done
N=1
for VBOX_USER in $VBOX_USERS; do
# The tag "Name:" occurs in multiple sections. Require at least 7 blanks
# with an additional flexible amount of spaces. At the moment of writing
# 13 spaces are needed.
VBOX_RUNNING=$(su $VBOX_USER -c "$VBOXMGR_BIN list --long runningvms" |
sed -n 's/^Name:[[:blank:]]\{7\} *//p')
for VBOX in $VBOX_RUNNING; do
VBOX_NAME[$N]="$VBOX"
VBOX_USER[$N]="$VBOX_USER"
N=$(($N+1))
done
done
VBOXES=${#VBOX_NAME[*]}
if [[ $VBOXES -eq 0 ]]; then
echo "$SERVICE: no virtual machines running."
else
N=1
while [[ $N -le $VBOXES ]]; do
# The long sed line changes the output from:
# running (since 2010-04-25T14:51:57.373000000)
# to:
# running (since 2010-04-25 14:51:57)
#
STATE=$(su ${VBOX_USER[$N]} -c "$VBOXMGR_BIN showvminfo "${VBOX_NAME[$N]}"" |
sed -n 's/State: *//p' |
sed 's/\([0-9][0-9]\)\.[0-9]\{9\}/\1/;s/\([0-9][0-9]\)T\([0-9][0-9]\)/\1 \2/')
printf "%-56s %s\n" "${VBOX_NAME[$N]} (user: ${VBOX_USER[$N]}):" "$STATE"
N=$(($N+1))
done
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart|force-reload)
stop
start
;;
status)
status
;;
*)
echo "Usage: $PRG {start|stop|restart|force-reload|status}" >&2
exit 3
;;
esac

View File

@ -1,23 +0,0 @@
## Path: System/Virtual Machines/VBox
## Description: Virtual box machines to autostart during boot
## Type: string
## Default: ""
#
# Configuration file for the script /etc/init.d/vboxes
#
# The variable VBOX_AUTOSTART holds the virtual machines to be started during
# boot time. One entry must contain the virtual machine name and the
# virtual machine owner. They are separated with a ";". Multiple entries
# (virtual machines) are separated with a space.
#
# Examples:
# MachineName1;user1
#
# One virtual machine:
# openSUSE_Factory;vbox
#
# Multiple virtual machines:
# openSUSE_Factory;vbox openSUSE_stable;suse
#
VBOX_AUTOSTART=""

View File

@ -1,3 +1,13 @@
-------------------------------------------------------------------
Thu Sep 20 19:24:48 UTC 2018 - Larry Finger <Larry.Finger@gmail.com>
- Fix autostart of VMs, which is no longer handled by the vboxes.service.
A new routine "start_vms" is added to the start section of vboxdrv.sh,
which is called by vboxdrv.service. Files "vboxes.service", "vboxes.sh",
and "virtualbox-sysconfig.vbox" are deleted.
These changes are to satisfy bsc#1107769.
-------------------------------------------------------------------
Thu Aug 30 16:54:26 UTC 2018 - Larry Finger <Larry.Finger@gmail.com>

View File

@ -12,7 +12,7 @@
# license that conforms to the Open Source Definition (Version 1.9)
# published by the Open Source Initiative.
# Please submit bugfixes or comments via http://bugs.opensuse.org/
# Please submit bugfixes or comments via https://bugs.opensuse.org/
#
@ -62,16 +62,12 @@ Source8: %{name}-guest-preamble
Source9: %{name}-wrapper.sh
Source10: %{name}-LocalConfig.kmk
Source11: %{name}-60-vboxdrv.rules
Source13: %{name}-sysconfig.vbox
Source14: vboxdrv.service
Source15: vboxadd-service.service
Source16: vboxconfig.sh
Source17: vboxguestconfig.sh
Source18: fix_usb_rules.sh
Source19: vboxdrv.sh
# init script to start virtual boxes during boot, to be configured via /etc/sysconfig/vbox bnc#582398
Source20: vboxes.sh
Source21: vboxes.service
Source98: %{name}-rpmlintrc
Source99: %{name}-patch-source.sh
#rework init scripts to fit suse needs
@ -173,6 +169,7 @@ BuildRequires: libqt5-qtbase-devel
BuildRequires: libqt5-qtx11extras-devel
BuildRequires: libvpx-devel
BuildRequires: libxslt-devel
BuildRequires: libzio-devel
BuildRequires: module-init-tools
BuildRequires: pam-devel
BuildRequires: pulseaudio-devel
@ -696,21 +693,13 @@ install -m 0755 %{SOURCE16} %{buildroot}/sbin/vboxconfig
install -m 0755 %{SOURCE17} %{buildroot}/sbin/vboxguestconfig
install -m 0755 %{SOURCE18} %{buildroot}/sbin/vbox-fix-usb-rules.sh
install -m 0755 %{SOURCE19} %{buildroot}%{_vbox_instdir}/vboxdrv.sh
install -m 0755 %{SOURCE20} %{buildroot}%{_vbox_instdir}/vboxes.sh
install -m 0644 %{SOURCE21} %{buildroot}%{_unitdir}/vboxes.service
ln -s %{_vbox_instdir}/vboxes.sh %{buildroot}%{_sbindir}/rcvboxes
ln -s %{_vbox_instdir}/vboxdrv.sh %{buildroot}%{_sbindir}/rcvboxdrv
# Init script to start virtual boxes during boot
ln -sf %{_unitdir}/vboxdrv.service %{buildroot}%{_unitdir}/multi-user.target.wants/vboxdrv.service
ln -sf %{_unitdir}/vboxadd-service.service %{buildroot}%{_unitdir}/multi-user.target.wants/vboxadd-service.service
ln -sf %{_unitdir}/vboxes.service %{buildroot}%{_unitdir}/multi-user.target.wants/vboxes.service
# sysconfig file intended for vboxes script
install -d -m 755 %{buildroot}%{_fillupdir}
install -m 640 %{SOURCE13} %{buildroot}%{_fillupdir}/sysconfig.vbox
# config file for vboxdrv script and vboxweb
install -d -m 755 %{buildroot}%{_sysconfdir}/vbox
install -d -m 1775 %{buildroot}%{_sysconfdir}/vbox
echo -e "#settings for vboxwebsrn\nVBOXWEB_USER=root" > %{buildroot}%{_sysconfdir}/vbox/vbox.cfg
# install udev helper script for creating usb devices
install -m 0755 -D src/VBox/Installer/linux/VBoxCreateUSBNode.sh %{buildroot}%{_vbox_instdir}/VBoxCreateUSBNode.sh
@ -923,16 +912,11 @@ export DISABLE_RESTART_ON_UPDATE=yes
%dir %{_unitdir}
%dir %{_unitdir}/multi-user.target.wants
/usr/lib/virtualbox/vboxdrv.sh
/usr/lib/virtualbox/vboxes.sh
%{_unitdir}/vboxdrv.service
%{_unitdir}/vboxes.service
%{_unitdir}/multi-user.target.wants/vboxdrv.service
%{_unitdir}/multi-user.target.wants/vboxes.service
%dir %{_sysconfdir}/vbox
%attr(1775,root,vboxusers) %{_sysconfdir}/vbox
%config %{_sysconfdir}/vbox/vbox.cfg
%{_fillupdir}/sysconfig.vbox
%{_sbindir}/rcvboxes
%{_sbindir}/rcvboxdrv
/sbin/vboxconfig
%{_vbox_instdir}/VBoxCreateUSBNode.sh
%verify(not mode) %attr(0755,root,vboxusers) %{_vbox_instdir}/VBoxNetNAT