#! /bin/bash # Copyright (C) 2008 Stephane Levant # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2, or (at your option) # any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program; see the file COPYING. If not, write to the # Free Software Foundation, Inc., 59 Temple Place - Suite 330, # Boston, MA 02111-1307, USA. VERSION=0.1 prog=`basename $0` ### User options # The screen size SCREEN_SIZE=640x480 # Select the uid of the user to use UID_TO_USE=1001 # Select the console to use (use another if already used) CONSOLE=8 # Select the display number to use (use another if already used) DISPLAY_NB=1 # Commande that lauch virtualbox VIRTUALBOX=virtualbox ### Code # Display an error message error() { echo "$prog: $1" >&2 exit 1 } [ $UID = 0 ] || error "Only root can start this script" # Search for the virtualbox command which $VIRTUALBOX &> /dev/null || VIRTUALBOX=Virtualbox which $VIRTUALBOX &> /dev/null || error "Cannot find virtualbox" # Get the name of the user to use USER_TO_USE=$(awk -F: '$3 == '$UID_TO_USE' {print $1}' /etc/passwd) # Use root user if not found if [ "$USER_TO_USE" = "" ]; then echo "Warning: user uid 1001 not found, using root" UID_TO_USE=0 USER_TO_USE=root fi # find a free tty function isFree() { [ "$(lsof /dev/tty$1 | egrep -v 'getty|COMMAND')" = "" ] } if ! isFree $CONSOLE; then # Tests the 12 ttys for ((i=1;i<=12;i++)); do if isFree $i; then CONSOLE=$i break fi done fi # find a free server number if [ -e /tmp/.X11-unix/X$DISPLAY_NB ]; then for ((i=1;i<=12;i++)); do if [ ! -e /tmp/.X11-unix/X$i ]; then DISPLAY_NB=$i break fi done fi # Display user message echo "Start X on :$DISPLAY_NB, size=$SCREEN_SIZE, with user\ uid $UID_TO_USE ($USER_TO_USE) on tty$CONSOLE" # File to modify and trap XWRAPPER=/etc/X11/Xwrapper.config XWRAPPER_SVG=/etc/X11/Xwrapper.config.bak-brood X11_CONF=/etc/X11/xorg.conf X11_CONF_SVG=/etc/X11/xorg.conf.bak-brood HOME_USER=$(awk -F: '$3 == '$UID_TO_USE' {print $6}' /etc/passwd) XSESSION=$HOME_USER/.xsession XSESSION_SVG=$HOME_USER/.xsession.bak-brood TEMP_FILE=$(mktemp) rm $TEMP_FILE function exitProperly() { [ -f "$X11_CONF_SVG" ] && mv "$X11_CONF_SVG" "$X11_CONF" [ -f "$XSESSION_SVG" ] && mv "$XSESSION_SVG" "$XSESSION" [ -f "$XWRAPPER_SVG" ] && mv "$XWRAPPER_SVG" "$XWRAPPER" exit 0 } trap exitProperly 2 3 24 # Allow users to start X and renice Xserver [ -f $XWRAPPER_SVG ] || mv $XWRAPPER $XWRAPPER_SVG cat << EOF > $XWRAPPER # Xwrapper.config (Debian X Window System server wrapper configuration file) # # This file was generated by the post-installation script of the x11-common # package using values from the debconf database. # # See the Xwrapper.config(5) manual page for more information. # # This file is automatically updated on upgrades of the x11-common package # *only* if it has not been modified since the last upgrade of that package. # # If you have edited this file but would like it to be automatically updated # again, run the following command as root: # dpkg-reconfigure x11-common allowed_users=anybody nice_value=-10 EOF # Create a new config file xorg.conf [ -f $X11_CONF_SVG ] || mv $X11_CONF $X11_CONF_SVG awk ' { if ($1=="Modes") print "\tModes\t\"'$SCREEN_SIZE'\"" else print }' $X11_CONF_SVG > $X11_CONF # Create a user .xsession file [ -f $XSESSION_SVG ] || mv $XSESSION $XSESSION_SVG #echo "touch $TEMP_FILE; exec $VIRTUALBOX" > $XSESSION echo "touch $TEMP_FILE; exec $VIRTUALBOX" > $XSESSION chown $USER_TO_USE $XSESSION su -lc "startx -- :$DISPLAY_NB vt$CONSOLE -nolisten tcp \ || touch $TEMP_FILE" $USER_TO_USE & while :; do sleep 1 echo "Waiting for X server to start..." [ -f $TEMP_FILE ] && exitProperly done