aboutsummaryrefslogtreecommitdiffstats
path: root/main/xen/xendomains.initd
blob: 27464c68562dafb97407a648afcf37a5a003cb98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/sbin/runscript
# Copyright 1999-2012 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/app-emulation/xen-tools/files/xendomains.initd-r2,v 1.4 2012/01/12 13:40:01 alexxy Exp $

extra_commands="status"

depend() {
	need xenstored
	after dhcp xend xenconsoled
}

get_domname() {
	local name_from_file=$(sed -rn 's/^name\W*=\W*\"?([[:alnum:]_\.-]+)\"?\W*;?/\1/p' "${1}" | tail -n 1)

	if [ -z ${name_from_file} ]; then
		basename "${1}"
	else
		echo ${name_from_file}	
	fi
}

is_running() {
	/usr/sbin/xl list "${1}" >/dev/null 2>&1
}

using_screen() {
	yesno "${SCREEN}"
}

set_screen_cmd() {
	export SCREENDIR=${SCREENDIR:-/tmp/screens/S-root}
	# create the parent dirs
	mkdir -p "$SCREENDIR"
	# set permissions
	checkpath -d -m 700 "$SCREENDIR"
	screen_cmd="screen -c ${SCREENRC:-/dev/null} -q -r ${SCREEN_NAME:=xen} -X"
}

dir_is_empty() {
	local dir=$1
	set -- "$dir"/*
	test "$1" = "$dir/*"
}

wait_screen() {
	while dir_is_empty "$SCREENDIR"; do
		sleep 0.1
	done
}

start() {
	set_screen_cmd

	einfo "Starting Xen domains from ${AUTODIR:=/etc/xen/auto}"
	if using_screen ; then
		ebegin "Creating screen session to hold domain consoles"
		(  screen -c ${SCREENRC:-/dev/null} -d -m -S ${SCREEN_NAME} -t dom0 \
		&& wait_screen \
		&& ${screen_cmd} zombie dr \
		&& logrotate -f /etc/xen/xen-consoles.logrotate \
		&& ${screen_cmd} logfile  /var/log/xen-consoles/%t.log \
		&& ${screen_cmd} logfile flush ${SCREEN_LOG_INTERVAL:-1} \
		&& ${screen_cmd} log on \
		&& ${screen_cmd} deflog on ) >/dev/null
		if [ $? -ne 0 ] ; then
			eend 1
			return 1
		else
			eend
		fi
	fi
	# Create all domains with config files in AUTODIR.
	want_usleep=
	for dom in $(ls "${AUTODIR:=/etc/xen/auto}/"* 2>/dev/null | sort); do
		name=$(get_domname ${dom})
		if ! is_running ${name} ; then
			if [ -n "$want_usleep" ]; then
				usleep ${XENDOMAINS_CREATE_USLEEP:=5000000}
			else
				want_usleep=1
			fi
			ebegin "  Starting domain ${name}"
			if using_screen ; then
				${screen_cmd} screen -t ${name} xl create ${dom} -c
			else
				xl create --quiet ${dom}
			fi
			eend $?
		else
			einfo "  Not starting domain ${name} - already running"
		fi
	done
}

stop() {
	set_screen_cmd

	einfo "Shutting down Xen domains from ${AUTODIR:=/etc/xen/auto}"
	# Stop all domains with config files in AUTODIR.
	DOMAINS="$(ls "${AUTODIR:=/etc/xen/auto}/"* 2>/dev/null | sort -r)"

	if yesno "$PARALLEL_SHUTDOWN"; then
		for dom in $DOMAINS ; do
			name=$(get_domname ${dom})
			if is_running ${name} ; then
				ebegin "  Asking domain ${name} to shutdown in the background..."
				xl shutdown -w ${name} >/dev/null &
			else
				einfo "  Not stopping domain ${name} - not running"
			fi
		done
		einfo "  Waiting for shutdown of domains that are still running"
		wait
		eend $?
	else
		for dom in $DOMAINS ; do
			name=$(get_domname ${dom})
			if is_running ${name} ; then
				ebegin "  Waiting for domain ${name} to shutdown"
				xl shutdown -w ${name} >/dev/null
				eend $?
			else
				einfo "  Not stopping domain ${name} - not running"
			fi
		done
	fi
	if using_screen ; then
		if ${screen_cmd} sleep 0 >/dev/null 2>&1 ; then
			ebegin "Closing screen session ${SCREEN_NAME}"
			${screen_cmd} quit
			eend $?
		else
			eend 0
		fi
	fi
}

status() {
	/usr/sbin/xl list
}