1#!/bin/bash
2#
3# Copyright 2007-2008 Samuel Thibault <samuel.thibault@eu.citrix.net>
4#
5# dm script around stubdomains.
6#
7
8DIR=`dirname $0`
9. "${DIR}/stubdompath.sh"
10stubdom_configdir="${XEN_CONFIG_DIR}/stubdoms"
11
12# To fit xterms nicely
13height=339
14
15# Parse arguments
16
17domid=
18domname=
19vncviewer=0
20vncpid=
21extra=
22sdl=0
23opengl=1
24vnc=0
25vncunused=0
26while [ "$#" -gt 0 ];
27do
28    if [ "$#" -ge 2 ];
29    then
30	case "$1" in
31	    -d)
32                domid=$2;
33                extra="$extra -d $domid";
34                shift
35                ;;
36	    -domain-name)
37                domname=$2;
38                shift
39                ;;
40	    -vnc)
41                vnc=1
42                op=${2%,*}
43		ip=${op%:*};
44		vnc_port=${op#*:};
45		shift
46		;;
47            -vncunused)
48                vncunused=1
49                shift
50                ;;
51            -loadvm)
52                extra="$extra -loadvm $2";
53                shift
54                ;;
55            -k)
56                keymap=$2
57                shift
58                ;;
59	    -serial)
60		serial="$2"
61		shift
62		;;
63            -monitor)
64		monitor="$2"
65		shift
66		;;
67	esac
68    fi
69    case "$1" in
70	-vncviewer) vncviewer=1 ;;
71        -sdl) sdl=1 ;;
72        -disable-opengl) opengl=0 ;;
73    esac
74    shift
75done
76
77[ -z "$domid"   ] && { echo "couldn't find domain ID" ; exit 1; }
78[ -z "$domname" ] && { echo "couldn't find domain name" ; exit 1; }
79
80# Termination handler
81
82term() {
83    [ -n "$vncpid" ] && kill -9 $vncpid
84    rm -f /tmp/domname-dm
85    rm ${stubdom_configdir}/$domname-dm
86    exit 0
87}
88
89trap term SIGHUP
90
91############
92# stubdomain
93# Wait for any previous stubdom to terminate
94while xm list | grep -w $domname-dm
95do
96	sleep 1
97done
98
99# Generate stubdom config file
100mkdir -p ${stubdom_configdir} &>/dev/null
101echo "#This file is autogenerated, edit $domname instead!" > ${stubdom_configdir}/$domname-dm
102echo "kernel = '${XENFIRMWAREDIR}/ioemu-stubdom.gz'" >> ${stubdom_configdir}/$domname-dm
103
104vfb="sdl=$sdl, opengl=$opengl"
105test "$DISPLAY" && vfb="$vfb, display=$DISPLAY"
106test "$XAUTHORITY" && vfb="$vfb, xauthority=$XAUTHORITY"
107test $vnc != 0 && vfb="$vfb, vnc=$vnc, vncdisplay=$vnc_port, vnclisten=$ip, vncunused=$vncunused"
108vncpasswd=`xenstore-read /local/domain/0/backend/vfb/$domid/0/vncpasswd 2>/dev/null`
109test "$vncpasswd" && vfb="$vfb, vncpasswd=$vncpasswd"
110test "$keymap" && vfb="$vfb, keymap=$keymap"
111test "$monitor" && vfb="$vfb, monitor=$monitor"
112test "$serial" && vfb="$vfb, serial=$serial"
113echo "vfb = ['$vfb']" >> ${stubdom_configdir}/$domname-dm
114
115echo -n "disk = [ " >> ${stubdom_configdir}/$domname-dm
116j=0
117for i in `xenstore-ls /local/domain/$domid/device/vbd | grep 'backend =' | awk '{print $3}'`
118do
119    i=${i%\"}
120    i=${i#\"}
121    vbd_mode=`xenstore-read $i/mode`
122    vbd_disk=`xenstore-read $i/params`
123    vbd_type=`xenstore-read $i/type`
124    vbd_dev=`xenstore-read $i/dev`
125    vbd_front=`xenstore-read $i/frontend`
126    vbd_devtype=`xenstore-read $vbd_front/device-type`
127    if [ $j -ne 0 ]
128    then
129        echo -n "," >> ${stubdom_configdir}/$domname-dm
130    fi
131    echo -n "'$vbd_type:$vbd_disk,$vbd_dev:$vbd_devtype,$vbd_mode'" >> ${stubdom_configdir}/$domname-dm
132    j=$(( $j + 1 ))
133done
134echo " ] " >> ${stubdom_configdir}/$domname-dm
135echo -n "vif = [ " >> ${stubdom_configdir}/$domname-dm
136j=0
137for i in `xenstore-ls /local/domain/$domid/device/vif | grep 'backend =' | awk '{print $3}'`
138do
139    i=${i%\"}
140    i=${i#\"}
141    vif_mac=`xenstore-read $i/mac`
142    vif_bridge=`xenstore-read $i/bridge`
143    if [ $j -ne 0 ]
144    then
145        echo -n "," >> ${stubdom_configdir}/$domname-dm
146    fi
147    echo -n "'mac=$vif_mac" >> ${stubdom_configdir}/$domname-dm
148    if [ "$vif_bridge" ]
149    then
150        echo -n ",bridge=$vif_bridge'" >> ${stubdom_configdir}/$domname-dm
151    else
152        echo -n "'" >> ${stubdom_configdir}/$domname-dm
153    fi
154    j=$(( $j + 1 ))
155done
156echo " ] " >> ${stubdom_configdir}/$domname-dm
157
158mkfifo /tmp/$domname-dm
159xm create -c ${stubdom_configdir}/$domname-dm target=$domid memory=32 extra="$extra" < /tmp/$domname-dm &
160exec 4>/tmp/$domname-dm
161
162
163###########
164# vncviewer
165if [ "$vncviewer" = 1 ]
166then
167    # Wait for vnc server to appear
168    while ! vnc_port=`xenstore-read /local/domain/$domid/console/vnc-port`
169    do
170        # Check that the stubdom job is still alive
171        kill -0 $consolepid || term
172	sleep 1
173    done
174
175    vncviewer $ip:$vnc_port &
176    vncpid=$!
177fi
178
179# wait for SIGHUP or stubdom termination
180wait
181
182term
183