aboutsummaryrefslogtreecommitdiffstats
path: root/main/lxc/lxc.initd
blob: d5e45a54077d55d133f3daae41d29da0e7909849 (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
#!/sbin/openrc-run
# Copyright 1999-2012 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/app-emulation/lxc/files/lxc.initd.2,v 1.5 2012/07/21 05:07:15 flameeyes Exp $

CONTAINER=${SVCNAME#*.}
command="/usr/bin/lxc-start"
pidfile="/var/run/lxc/$CONTAINER.pid"
extra_started_commands="reboot"

depend() {
	need localmount
	after firewall net
}

lxc_get_configfile() {
	local i
	for i in /var/lib/lxc/${CONTAINER}/config \
			/etc/lxc/${CONTAINER}.conf \
			/etc/lxc/${CONTAINER}/config; do
		if [ -f "$i" ]; then
			echo "$i"
			return 0
		fi
	done
	eerror "Unable to find a suitable configuration file."
	eerror "If you set up the container in a non-standard"
	eerror "location, please set the CONFIGFILE variable."
	return 1
}

lxc_get_var() {
	awk 'BEGIN { FS="[ \t]*=[ \t]*" } $1 == "'$1'" { print $2; exit }' ${CONFIGFILE}
}

checkconfig() {
	if [ ${CONTAINER} = ${SVCNAME} ]; then
		CONTAINER=
		return 0
	fi
	CONFIGFILE=${CONFIGFILE:-$(lxc_get_configfile)}

	# no need to output anything, the function takes care of that.
	[ -z "${CONFIGFILE}" ] && return 1

	utsname=$(lxc_get_var lxc.utsname)
	if [ "${CONTAINER}" != "${utsname}" ]; then
	    eerror "You should use the same name for the service and the"
	    eerror "lxc.utsname. Right now the lxc.utsname is set to ${utsname}"
	    return 1
	fi
}

_autostart() {
	ebegin "$1 LXC containers"
	shift
	lxc-autostart --group "${LXC_GROUP:-onboot,}" "$@"
	eend $?
}

start() {
	checkconfig || return 1
	if [ -z "$CONTAINER" ]; then
		_autostart "Starting"
		return
	fi

	rm -f /var/log/lxc/${CONTAINER}.log

	rootpath=$(lxc_get_var lxc.rootfs)
	# verify that container is not on tmpfs
	dev=$(df -P "${rootpath}" | awk '{d=$1}; END {print d}')
	type=$(awk -v dev="$dev" '$1 == dev {m=$3}; END {print m}' /proc/mounts)
	if [ "$type" = tmpfs ] && ! yesno "$ALLOW_TMPFS"; then
		eerror "${rootpath} is on tmpfs and ALLOW_TMPFS is not set"
		return 1
	fi

	local logdir=/var/log/lxc
	checkpath -d -m 750 -o root:wheel $logdir

	checkpath -d ${pidfile%/*}
	ebegin "Starting container ${CONTAINER}"
	start-stop-daemon --start $command \
		--pidfile $pidfile \
		-- \
		--daemon \
		--pidfile $pidfile \
		--name ${CONTAINER} \
		--rcfile ${CONFIGFILE} \
		--logpriority WARN \
		--logfile $logdir/${CONTAINER}.log \
		|| eend $? || return $?
	lxc-wait -n ${CONTAINER} -t 5 -s RUNNING
	eend $?
}

stop() {
	checkconfig || return 1
	if [ -z "$CONTAINER" ]; then
		_autostart "Stopping" --shutdown --timeout ${LXC_TIMEOUT:-30}
		return
	fi

	ebegin "Stopping container ${CONTAINER}"
	start-stop-daemon --stop --pidfile ${pidfile} \
		--retry ${POWEROFF_SIGNAL:-SIGUSR2}/${TIMEOUT:-30} \
		--progress
	eend $?
}

reboot() {
	checkconfig || return 1
	if [ -z "$CONTAINER" ]; then
		_autostart "Rebooting" --reboot
		return
	fi

	ebegin "Sending reboot signal to container $CONTAINER"
	start-stop-daemon --signal ${RESTART_SIG:-SIGTERM} \
		--pidfile ${pidfile}
	eend $?
}