/
rc.freenas
485 lines (393 loc) · 8.78 KB
/
rc.freenas
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#!/bin/sh
#-
# Copyright (c) 2010 iXsystems, Inc., All rights reserved.
# Written by: Xin LI
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL Jordan Hubbard OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
. /etc/rc.subr
#
# FreeNAS settings
#
: ${FREENAS_CONFIG:="/data/freenas-v1.db"}
: ${FREENAS_CONFIG_MD5:="/var/tmp/freenas_config.md5"}
: ${FREENAS_SQLITE_CMD:="/usr/local/bin/sqlite3fn"}
: ${FREENAS_RC:="/var/tmp/rc.conf.freenas"}
: ${FREENAS_VERSIONFILE:="/etc/version"}
: ${FREENAS_SYSTEMDATASET:="/var/db/system"}
: ${FREENAS_MW_IFACES_FAILED:="/tmp/.middleware_interface_failed"}
#
# Updater specific variables.
#
: ${CD_UPGRADE_SENTINEL="/data/cd-upgrade"}
: ${NEED_UPDATE_SENTINEL="/data/need-update"}
#
# Installer specific variables.
#
: ${FIRST_INSTALL_SENTINEL="/data/first-boot"}
#
# Misc settings
#
: ${FREENAS_CACHEDIR:="/var/tmp/.cache"}
: ${FREENAS_CACHESIZE:="2g"}
: ${FREENAS_CACHEEXPIRE:="60"}
#
# LDAP settings
#
: ${LDAP_CONF:="/usr/local/etc/openldap/ldap.conf"}
: ${CERT_FILE:="/usr/local/etc/certs/cacert.crt"}
: ${LDAP_TIMEOUT:="0"}
#
# PAM settings
#
: ${PAM_TEMPLATE_DIR:="/etc/ix/templates/pam.d"}
: ${PAM_DIR:="/etc/pam.d"}
#
# NSS settings
#
: ${PATH_NS_CONF:="/etc/nsswitch.conf"}
#
# Kerberos settings
#
: ${PATH_KRB5_TEMPLATE:="/etc/ix/templates/kerberos/krb5.conf"}
: ${PATH_KRB5_CONFIG:="/etc/krb5.conf"}
: ${PATH_AD_KEYTAB:="/etc/AD.keytab"}
#
# Samba settings
#
: ${SAMBA_CONF:="/usr/local/etc/smb4.conf"}
#
# TrueNAS
#
: ${HA_MODE_FILE="/tmp/.ha_mode"}
__escape()
{
local val delim
val="${1}"
delim="${2}"
if [ -n "${val}" -a -z "${delim}" ]
then
echo -n "${val}" | sed -Ee 's|\\|\\\\|g' -Ee 's|[^a-zA-Z0-9]|\\&|g'
elif [ -n "${val}" -a -n "${delim}" ]
then
echo -n "${val}" | awk -v delim="${delim}" '{
newstr = "";
split(delim, delims, "");
dlength = length(delims);
split($0, chars, "");
clength = length(chars);
for (i = 1;i <= clength;i++) {
for (j = 1;j <= dlength;j++) {
if (chars[i] == delims[j]) {
newstr = newstr "\\";
break;
}
}
newstr = newstr chars[i];
}
printf("%s", newstr);
}'
fi
}
__unescape()
{
local delim
val="${1}"
delim="${2}"
if [ -n "${val}" -a -z "${delim}" ]
then
echo -n "${1}" | sed -Ee 's|\\([^a-zA-Z0-9])|\1|g' -Ee 's|\\\\|\\|g'
elif [ -n "${val}" -a -n "${delim}" ]
then
echo -n "${val}" | awk -v delim="${delim}" '{
newstr = "";
split(delim, delims, "");
dlength = length(delims);
split($0, chars, "");
clength = length(chars);
for (i = 1;i <= clength;i++) {
for (j = 1;j <= dlength;j++) {
if (chars[i] == "\\" && i + 1 <= clength &&
chars[i + 1] == delims[j]) {
i += 1;
break;
}
}
newstr = newstr chars[i];
}
printf("%s", newstr);
}'
fi
}
for dsfile in $(ls /etc/directoryservice/rc.*)
do
. "${dsfile}"
done
# Create a cache filesystem on ${FREENAS_CACHEDIR} .
#
# Used by ix-activedirectory and ix-ldap.
#
# Returns 0 if successful; 1 if unsuccessful.
#
# NOTE: This function is intentionally noisy to catch potential logic errors
# with the boot process.
#
# TODO: add permanent store capability; using the mdconfig* rc.d scripts would
# be a good idea as it features pre-populating / fsck functionality for memory
# disk images, whereas mdmfs doesn't.
# TODO: employ something similar for etc collectd plugin, but only after the permanent
# store capability has been added.
create_cache_filesystem()
{
local dev
# test to see if /var is an md, if not, presumably it's been moved to
# a persistant volume, so skip creating a separate in memory
# filesystem for it.
mount | awk '$3 == "/var" {print $1}' | grep -qE "/dev/md[[:digit:]]+"
if [ $? -ne 0 ] ; then
if [ ! -d "${FREENAS_CACHEDIR}" ]; then
mkdir -p "${FREENAS_CACHEDIR}"
fi
return 0
fi
if [ -d "${FREENAS_CACHEDIR}" ]; then
dev=$(mount | awk -v "dir=$FREENAS_CACHEDIR" \
'$3 == dir { print $1 }')
if [ -n "$dev" ]; then
if ! umount $dev; then
return 1
fi
if ! mdconfig -d -u ${dev} ; then
return 1
fi
fi
else
mkdir -p "${FREENAS_CACHEDIR}"
fi
mdmfs -i 4096 -b 4096 -f 512 -s ${FREENAS_CACHESIZE} \
md "${FREENAS_CACHEDIR}"
if [ $? -eq 0 ]; then
return 0
fi
return 1
}
#
# General purpose utility functions.
#
# 'var to SQL Fields'
#
# sh vars don't work with ',', but SQL fields require use them as required separators.
var_to_sf()
{
echo $* | sed -e 's/ /, /g'
}
# Is a service enabled?
#
# Parameters:
# 1 - service name, e.g. afp, cifs, etc.
#
# Returns:
# 0 - enabled
# 1 - disabled
# 2 - not found
srv_enabled()
{
enabled=$(${FREENAS_SQLITE_CMD} ${FREENAS_CONFIG} \
"SELECT srv_enable from services_services where srv_service = '$1'" \
2>/dev/null)
if [ "$enabled" = 1 ]; then
return 0
elif [ "$enabled" = 0 ]; then
return 1
fi
return 2
}
dirsrv_enabled()
{
local dir="${1}"
if [ -z "${dir}" ]
then
return 0
fi
case "${dir}" in
activedirectory) activedirectory_enabled; return $? ;;
domaincontroller) domaincontroller_enabled; return $? ;;
ldap) ldap_enabled; return $? ;;
nis) nis_enabled; return $? ;;
esac
return 2
}
dirsrv_set()
{
local dir="${1}"
local enable="${2}"
if [ -z "${dir}" -o -z "${enable}" ]
then
return 0
fi
case "${dir}" in
activedirectory) activedirectory_set "${enable}"; return $? ;;
domaincontroller) domaincontroller_set "${enable}"; return $? ;;
ldap) ldap_enabled "${enable}"; return $? ;;
nis) nis_enabled "${enable}"; return $? ;;
esac
return 2
}
srv_set()
{
local service="${1}"
local enable="${2}"
if [ -z "${service}" -o -z "${enable}" ]
then
return 0
fi
${FREENAS_SQLITE_CMD} ${FREENAS_CONFIG} "
UPDATE
services_services
SET
srv_enable = ${enable}
WHERE
srv_service = '${service}'
"
return $?
}
is_freenas()
{
if cat ${FREENAS_VERSIONFILE} | grep -qi ^freenas
then
return 0
else
return 1
fi
}
system_dataset_enabled()
{
test -n "$(find "${FREENAS_SYSTEMDATASET}" -prune -type d ! -empty 2>/dev/null)"
}
ha_mode()
{
local encstat manual node serial
if [ -f ${HA_MODE_FILE} ]; then
cat ${HA_MODE_FILE}
return 0
fi
/usr/local/bin/python /usr/local/www/freenasUI/failover/detect.py 2> /dev/null
return 0
}
ha_hardware() {
ha_mode | cut -d ":" -f 1
}
ha_node()
{
if ! ha_mode | grep -q ":"; then
return 1
fi
ha_mode | cut -d ":" -f 2
return 0
}
ro_sqlite()
{
local name ret
name=$1
ret=$(mktemp /tmp/${name}.XXXXX 2> /tmp/${name}.rcfail && rm /tmp/${name}.rcfail)
cp ${FREENAS_CONFIG} ${ret}
echo ${ret}
}
fncheck_process()
{
ps -p "$1" > /dev/null 2>&1
return $?
}
kill_process()
{
local pid=$1
local ret=0
kill ${pid} >/dev/null 2>&1
ret=$?
fncheck_process "${pid}"
if [ "$?" != "0" ]
then
kill -9 "${pid}" > /dev/null 2>&1
ret=$?
fi
return ${ret}
}
do_timeout_wait()
{
local res=0
local timeout=$1
shift
local args="$*"
${args} &
pid_args=$!
trap "kill_process ${pid_args}" SIGINT
(
local i=0
local ret=0
while [ "${i}" -lt "${timeout}" ]
do
fncheck_process "${pid_args}"
if [ "$?" != "0" ]
then
ret=0
break
fi
sleep 1
$((i+=1)) >/dev/null 2>&1
done
fncheck_process "${pid_args}"
if [ "$?" = "0" ]
then
kill_process ${pid_args}
ret=$?
fi
exit ${ret}
) &
pid_timeout=$!
wait ${pid_args}
args_wait_ret=$?
wait ${pid_timeout}
timeout_wait_ret=$?
if [ "${args_wait_ret}" != 0 ] || [ "${timeout_wait_ret}" != "0" ]
then
res=1
fi
rm -f "${lf}"
return ${res}
}
timeout_wait()
{
local ret=0
local timeout=$1
shift
local args="$*"
local lf="/tmp/$(echo "${args}"|sha256 2>/dev/null)"
trap "rm -f ${lf}" 2
lockf -t 0 -k -s "${lf}" /usr/bin/true
if [ "$?" != 0 ]
then
return 1
fi
do_timeout_wait ${timeout} ${args}
ret=$?
rm -f "${lf}"
return ${ret}
}