summaryrefslogtreecommitdiffstats
path: root/main/xen/xendomains.initd
blob: 5077d82e03950b615df90717c0a9bbc44154e971 (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
#!/sbin/runscript

# See docs/misc/distro_mapping.txt
if [ -d /var/lock/subsys ]; then
	LOCKFILE=/var/lock/subsys/xendomains
else
	LOCKFILE=/var/lock/xendomains
fi

if [ -d /etc/sysconfig ]; then
	XENDOM_CONFIG=/etc/sysconfig/xendomains
else
	XENDOM_CONFIG=/etc/default/xendomains
fi

if ! [ -r $XENDOM_CONFIG ]; then
	eend 1;
	echo "$XENDOM_CONFIG not existing";
	return 1;
fi

. $XENDOM_CONFIG

depend() {
	need xencommons
	after net xend
}

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/$CMD list "${1}" >/dev/null 2>&1
}

check_xen() {
	CMD=xm
	/usr/sbin/$CMD list &> /dev/null
	if test $? -ne 0
	then
        	CMD=xl
	fi

	/usr/sbin/$CMD list &> /dev/null
	if test $? -ne 0
	then
        	eend 1;
        	echo "Could not find xl or xm";
        	return 1;
	fi

	# Correct exit code would probably be 5, but it's enough
	# if xend complains if we're not running as privileged domain
	if ! [ -e /proc/xen/privcmd ]; then
        	eend 1;
        	echo "Could not find /proc/xen/privcmd";
        	return 1;
	fi
}

start() {
	einfo "Starting Xen domains in ${AUTODIR:=/etc/xen/auto}"

	check_xen

	for dom in $(ls "${AUTODIR:=/etc/xen/auto}/"* 2>/dev/null | sort); do
		name=$(get_domname ${dom})
		if ! is_running ${name} ; then
			ebegin "  Starting domain ${name}"
			/usr/sbin/$CMD create --quiet --defconfig ${dom} >/dev/null 2>&1
			eend $?
		else
			einfo "  Not starting ${name} - already running."
		fi
	done
}

stop() {
	einfo "Stopping Xen domains in ${AUTODIR:=/etc/xen/auto}"

	check_xen

	for dom in $(ls "${AUTODIR:=/etc/xen/auto}/"* 2>/dev/null | sort); do
		name=$(get_domname ${dom})
		if is_running ${name} ; then
			ebegin "  Sending shutdown signal to ${name}"
			/usr/sbin/$CMD shutdown ${name} >/dev/null 2>&1
			eend $?
		fi
	done

	for dom in $(ls "${AUTODIR:=/etc/xen/auto}/"* 2>/dev/null | sort); do
		name=$(get_domname ${dom})
		if is_running ${name} ; then
			ebegin "  Waiting for domain ${name} to shutdown"
			/usr/sbin/$CMD shutdown --wait ${name} >/dev/null 2>&1
			eend $?
		else
			einfo "  Not stopping ${name} - not running."
		fi
	done
}

status() {
	check_xen
	/usr/sbin/$CMD list
}