summaryrefslogtreecommitdiffstats
path: root/scripts/init.d/wlm
blob: 65d51bd56a823b71dd48e7c545857b4bdd49aa0a (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#
# Start/Stop the workload manager
#
# Copyright IBM Corporation. 2008
#  
# Authors:     Balbir Singh <balbir@linux.vnet.ibm.com>
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2.1 of the GNU Lesser General Public License
# as published by the Free Software Foundation.
# 
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
#
# TODO: Make this code more robust, add error checking and recovery
# for invalid configuration or interrupted execution
#
# Make the script LSB compliant
#

CONF_FILE=/etc/wlm.conf
PROC_CGROUPS_FILE=/proc/cgroups
CGROUP_FS=cgroup
IGNORE_OPTS="ns cpuset"

# support multiple mount points

declare -a MOUNTPOINT
declare -a MOUNTOPTS
maxindex=0

parse_controller_file() {
	ctlr=$1
	file=$2
	tuid="root"
	tgid="root"
	cuid="root"
	cgid="root"

	pushd $PWD 2>&1 > /dev/null
	while read name opts
	do
		case $name in
		'mount')
			;;
		*)
			if ! echo $name | grep -q ^#
			then
				echo "Creating class $name"
				class=$name
				index=1
				for i in `seq 1 $maxindex`
				do 
					echo "mount pt ${MOUNTPOINT[$i]}"
					mkdir -p ${MOUNTPOINT[$i]}/$class
					cd ${MOUNTPOINT[$i]}/$class

					if echo $opts | grep -q "="
					then
						for single_opt in $opts
						do
							cf=`echo $single_opt | cut -d '=' -f1`
							co=`echo $single_opt | cut -d '=' -f2`
							case $cf in
							"tuid")
								tuid=$co
								;;
							"cuid")
								cuid=$co
								;;
							"tgid")
								tgid=$co
								;;
							"cgid")
								cgid=$co
								;;
							*)
								echo -n $co > $cf
								;;
							esac
						done
						chown -R $cuid:$cgid ${MOUNTPOINT[$i]}/$class
						chown -R $tuid:$tgid ${MOUNTPOINT[$i]}/$class/tasks
					fi
				done
			fi
		esac
	done < $file
	popd 2>&1 > /dev/null
}

parse_controller_opts() {
	name=$1;
	file=$2;

	if [ ! -r $PROC_CGROUPS_FILE ]
	then
		echo "$PROC_CGROUPS_FILE does not exist, please compile"
		echo "cgroups into the kernel"
		exit 1
	else
		line=`grep -w $name $PROC_CGROUPS_FILE`
		if [ $? -ne 0 ]
		then
			echo "$name controller not enabled"
			exit 1
		fi

		active=`echo $line | awk '{print $2}'`
		if [[ $active -eq 0 ]]
		then
			echo "$name controller not mounted"
		else
			parse_controller_file $name $file
		fi
	fi
}

parse_conf_file() {
	while read name conf
	do
		# skip all comments
		if  ! echo $name | grep -q ^#
		then
			case $name in
			'mount')
				maxindex=$(($maxindex+1))
				MOUNTPOINT[$maxindex]=`echo $conf | cut -d ' ' -f1`;
				MOUNTOPTS[$maxindex]=`echo $conf | cut -d ' ' -f2`;
				;;
			esac
		fi
	done < $CONF_FILE

	return 0;
}

mount_fs() {
	if [ ! -r $PROC_CGROUPS_FILE ]
	then
		echo "$PROC_CGROUPS_FILE does not exist, please compile"
		echo "cgroups into the kernel"
		exit 1
	#else
		#while read name hierarchy num_cgroups
		#do
		#	if  ! echo $name | grep -q ^#
		#	then
		#		echo $IGNORE_OPTS | grep -wq $name
		#		if [[ $? -ne 0 ]]
		#		then
		#			MOUNT_OPTS=$name","$MOUNT_OPTS
		#		fi
		#	fi
		#done < $PROC_CGROUPS_FILE
		#MOUNT_OPTS=${MOUNT_OPTS%%","}
		#line=`grep -w $MOUNT_OPTS $PROC_CGROUPS_FILE`
		#if [ $? -ne 0 ]
		#then
		#	echo "$name controller not enabled"
		#	exit 1
		#fi
	fi
	for i in `seq 1 $maxindex`
	do
		mkdir -p ${MOUNTPOINT[$i]}
		echo "mounting ${MOUNTPOINT[$i]} with ${MOUNTOPTS[$i]}"
		mount -t $CGROUP_FS $CGROUP_FS -o ${MOUNTOPTS[$i]} ${MOUNTPOINT[$i]}
		#
		# Give root tasks read/write permission to all, since tasks
		# tasks will be moved to root frequently
		#
		chmod ago+rwx ${MOUNTPOINT[$i]}/tasks
		chmod ago+rwx ${MOUNTPOINT[$i]}
		chmod +t      ${MOUNTPOINT[$i]}
	done
	return $?
}

umount_fs() {
	for i in `seq 1 $maxindex`
	do
		umount ${MOUNTPOINT[$i]}
		rmdir ${MOUNTPOINT[$i]}
	done
}
	
create_classes() {
	while read name conf
	do
		# skip all comments
		if  ! echo $name | grep -q ^#
		then
			case $name in
			'mount')
				;;
			*)
				parse_controller_opts $name $conf
			esac
		fi

	done < $CONF_FILE
}

start() {
	echo "Starting wlm service: "
	mount_fs
	if [ $? -eq 0 ]
	then
		create_classes
	fi
	[ $? == 0 ] && touch /var/lock/subsys/wlm
	return $?
}

move_all_to_init_class() {
	for i in `seq 1 $maxindex`
	do
		cd ${MOUNTPOINT[$i]}
		cat /proc/mounts | grep -wq ${MOUNTPOINT[$i]}
		if [ $? -ne 0 ]
		then
			echo "resource control filesystem not mounted"
			exit 1
		fi

		for i in `find . -type d`
		do
			case $i in
			'.')
				;;
			*)
				class=${i#./*}
				echo "Removing class $class"
				sed -nu p < ./$i/tasks > tasks
				rmdir $i
				;;
			esac
		done
		cd - > /dev/null
	done
}


stop() {
	move_all_to_init_class
	umount_fs
}

trapped() {
	#
	# Do nothing
	#
	true
}

usage() {
	echo "$0 <start|stop>"
	exit 1
}

common() {
	#
	# main script work done here
	#
	trap "trapped ABRT" ABRT
	trap "trapped QUIT" QUIT
	trap "trapped TERM" TERM
	trap "trapped INT"   INT

	parse_conf_file
}

case $1 in
	'start')
		common
		start;
		;;
	'stop')
		common
		stop;
		;;
	*)
		usage
		;;
esac