VM Launch Scripts

The following is a script (with GUI interaction via "zenity") to automate the execution of the VDD VMs. The script let a user choose the desired distro (operating system), the desktop environment (obviously not the case for Windows) and, if it's the first time it launches the VM, let a user also choose to get or not an encrypted share folder. Furthermore, if it's the case, it lets the user to enter its own passphrase to encrypt and/or decrypt that shared folder.

#!/bin/sh
#/usr/bin/launchgui
# ***************************************************************
# Copyright notice
#
# (c) 2009 Binario Etico Soc. Coop. info(@)binarioetico.org
# All rights reserved
#
# This script is part of the VDD-Project www.vdd-project.org. This script 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 of the License, or
# (at your option) any later version.
#
# The GNU General Public License can be found at
# http://www.gnu.org/copyleft/gpl.html
#
# This script 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.
#
# This copyright notice MUST APPEAR in all copies of the script!
# Author: Fabrizio Nasti fabrizio.nasti(@)binarioetico.org
# ***************************************************************
#
### This script is intended to be executed by a normal user on a LTSP thin client connected to the
### VDD/LTSP server (XEN Dom0) to launch para- or fully virtualized user Desktop Environments
### on already running Xen DomUs, and more specifically to:
### 1. launch an xnested environment via Xephyr
### 2. access via ssh the desired virtual machine (Xen DomU) and start the desired Desktop
### Environment
### or to:
### 1-2. start a remote desktop connection on a Windows XP virtual machine
### This script is also intended to:
### 3. create and/or activate an lvm-based encrypted or non-encrypted per-user share folder.
###
### The script use "zenity" to provide a graphical user dialog interface to
### enter the desired following parameters: distro, desktop environment, crypted or not crypted share
### folder, passphrase to crypt and de-crypt the share folder.
#
#
## Choose the distro (operating system)
#
VM=$(zenity --width=250 --height=300 --list --title="Operating System" --text="Choose your Virtual Operating System" \
--radiolist --column "Choose" --column "OS" --column "VM" FALSE "Windows XP" winxpvm FALSE "Debian Lenny" \
lennyvm FALSE "Ubuntu Jaunty" jauntyvm FALSE "Centos5" centos5vm FALSE "Fedora11" fedora11vm FALSE \
"Slackware13" slackware13vm --hide-column=3 --print-column=3)
#
if [ $VM == winxpvm ]; then
echo -e "no Desktop Environment to choose";
else
#
## Choose the desktop environment
DE=$(zenity --width=250 --height=300 --list --title="Desktop Environment" --text="Choose your Desktop Environment" \
--radiolist --column "Choose" --column "Desktop" --column "DE" TRUE KDE3/4 startkde FALSE Gnome gnome-session FALSE \
XFCE xfce4-session --hide-column=3 --print-column=3)

fi
#
#
## Create and activate encrypted or non-encrypted per-$USER share
#
VG=serv1
#
if [ -e /dev/$VG/"$USER"_enc ]; then
echo "encrypted device is already existing"
if [ -e /home/$USER/share ]; then
echo "the folder share is already existing"
else
echo "...creating the folder share"
mkdir /home/$USER/share
fi
if [ -e /var/lib/samba/usershares/"$USER"_share ]; then
echo "the share is already active"
else
echo "...activating the share"
net usershare add "$USER"_share /home/$USER/share "$USER share" "$USER":F
fi
if (mount | grep "$USER"_enc 1> /dev/null); then
echo "the encrypted device is already mounted on /home/$USER/share"
else
zenity --entry --title="DE-ENCRYPTION PASSPHRASE" --text="Insert the passphrase to \
decrypt your share folder" --hide-text 1> /home/$USER/passphrase
sudo mount.crypt /dev/$VG/"$USER"_enc /home/$USER/share /dev/null); then
echo "the device is already mounted on /home/$USER/share"
else
zenity --entry --title="DE-ENCRYPTION PASSPHRASE" --text="Insert the passphrase to decrypt your
share folder" --hide-text 1> /home/$USER/passphrase
sudo mount /dev/$VG/$USER /home/$USER/share /home/$USER/passphrase
sudo cryptsetup --verbose -c aes-cbc-essiv:sha256 -q luksFormat /dev/$VG/"$USER"_enc \
/var/www/$USER/vm_$USER$VM
else
sudo mkdir -m 757 /var/www/$USER &&
echo VM=$VM >> /var/www/$USER/vm_$USER$VM
fi
rdesktop -f -x l $VM &> /dev/null
;;
*)
# Set the X channel to be used first by 'Xephyr' and then by 'export DISPLAY'
XCH=$[`cat /root/Xephyr_offset` +1]
#
# Exclude the use of XCH 10 and 11 (i don't know why but they don't work)
if [ `echo $XCH` -eq 10 ]; then
XCH=$[$XCH +2]
fi
if [ `echo $XCH` -eq 11 ]; then
XCH=$[$XCH +1]
fi
# Check if a Xephyr process is using the set X channel (XCH)
while (ps axf | grep "Xephyr" | grep :$XCH 1> /dev/null); do
XCH=$[$XCH +1]
if [ `echo $XCH` -eq 10 ]; then
XCH=$[$XCH +2]
fi
if [ `echo $XCH` -eq 11 ]; then
XCH=$[$XCH +1]
fi
done
#
# Launch Xephyr on $XCH in fullscreen mode and put it in background
Xephyr -ac :$XCH -fullscreen &

#
# Get the Xephyr PID e write it in an apache chroot file
XEPHYR_PID=$!
echo XEPHYR_PID=$XEPHYR_PID
if [ -d /var/www/$USER ]; then
echo XEPHYR_PID=$XEPHYR_PID > /var/www/$USER/vm_$XCH
else
sudo mkdir -m 757 /var/www/$USER &&
echo XEPHYR_PID=$XEPHYR_PID > /var/www/$USER/vm_$XCH
fi
#
# Update the first free X channel to be used (offset)
echo $XCH > /root/Xephyr_offset
#
# When XCH get 100 re-set the offset to 0
if [ "$XCH" -ge "100" ]; then
echo 0 > /root/Xephyr_offset
fi
#
# Export custom env variables. They will be passed through ssh to the desired VM (according with
# server (Dom0) /etc/ssh/ssh_config and virtual machines (DomUs) /etc/ssh/sshd_config)
export XCH=$XCH
export DE=$DE
#
# The USER variable is read from standard local environment
#
# Export desired VM DISPLAY towards the LTSP thin-client (id-est the server) and start the
# desired Desktop Environment (DE)
#
ssh $USER@$VM 'export DISPLAY=192.168.108.21:$XCH && $DE' &
# Write the script variables in a per-$XCH file in the per-$USER Apache root director
echo XCH=$XCH > /var/www/$USER/vm_$XCH
echo VM=$VM >> /var/www/$USER/vm_$XCH
echo DE=$DE >> /var/www/$USER/vm_$XCH
;;
esac
#
#
# If everything went fine exit without errors
exit 0