-
Notifications
You must be signed in to change notification settings - Fork 0
/
initenv.subr
530 lines (425 loc) · 11.7 KB
/
initenv.subr
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
#10.0.4
#
init_items_desc()
{
nodename_desc="Short form nodename of this host e.g. like hostname.\n Warning: this operation will recreate the ssh keys in $workdir/.ssh dir"
hostname_desc="Fully qualified domain name (FQDN) of the host"
nodeip_desc="Node managment IP address"
nodedescr_desc="Node info/description"
jnameserver_desc="Jails default DNS nameserver"
nodeippool_desc="Jail pool IP address"
nat_enable_desc="Enable NAT for RFC1918 networks"
natip_desc="NAT IP"
fbsdrepo_desc="Use official FreeBSD repository"
mdtmp_desc="Configure memory disk"
repo_desc="Use repository for images and template"
workdir_desc="work directory"
ipfw_enable_desc="Enable IPFW"
zfsfeat_desc="Enable ZFS feature"
jail_interface_desc="Jails NIC"
parallel_desc="Parallel mode stop/start"
stable_desc="Use STABLE branch"
}
init_items_default()
{
INITIP=$(/sbin/ifconfig `/sbin/route -n get 0.0.0.0 2>/dev/null | awk '/interface/{print $2}'` 2>/dev/null|awk '/inet /{print $2}'|head -n1)
[ -z "${INITIP}" ] && IP="8.8.8.8"
##default area
nodename_default=`hostname`
nodeip_default="${INITIP}"
nodedescr_default="Datacenter #5, Middle of Nowhere"
jnameserver_default="${INITIP},8.8.8.8"
nodeippool_default="10.0.0.0/16 ${INITIP}/24"
nat_enable_default="Enable NAT"
natip_default="${INITIP}"
fbsdrepo_default="1"
mdtmp_default="8"
repo_default="http://bsdstore.ru"
workdir_default="/usr/jails"
ipfw_enable_default="1"
zfsfeat_default="1"
jail_interface_default="auto"
parallel_default="5"
stable_default="0"
}
# install file from $2 to $3 if not equal
# $1 addit. arg for install
installne()
{
if ! cmp -s $2 $3; then
install $1 $2 $3
return 1
fi
return 0
}
make_nat()
{
case "${ok}" in
"pf")
if [ `grep ^pf_load= /boot/loader.conf|wc -l` = 0 ]; then
getyesno "${BOLD}Shall i modify the /boot/loader.conf to sets pf_load=YES ?${NORMAL}" && sysrc -vf /boot/loader.conf pf_load=YES
fi
truncate -s0 ${etcdir}/pfnat.conf
for _net in ${rfc1918}; do
_nm=`echo ${_net} |tr "/" " "`
${workdir}/sbin/netmask ${_nm} ${natip}
if [ $? -ne 1 ]; then
cat >> ${etcdir}/pfnat.conf <<EOF
nat on ${ext_iface} from ${_net} to ! ${_net} -> ${natip}
EOF
fi
done
answ="${ok}"
ok="ok"
${miscdir}/sqlcli ${dbdir}/local.sqlite UPDATE local SET nat_enable=\"${answ}\"
return 0
;;
"ipfw")
if getyesno "${BOLD}Shall i modify the /boot/loader.conf to sets ipfw_load=YES, ipfw_nat_load=YES, libalias_load=YES and net.inet.ip.fw.default_to_accept=1 ?${NORMAL}"; then
sysrc -vf /boot/loader.conf net.inet.ip.fw.default_to_accept=1
sysrc -vf /boot/loader.conf ipfw_nat_load=YES
sysrc -vf /boot/loader.conf libalias_load=YES
fi
truncate -s0 ${etcdir}/ipfw.conf
_nm=`echo ${rfc1918} |tr " " ","`
# ${workdir}/sbin/netmask ${_nm} ${natip}
# if [ $? -ne 1 ]; then
_extiface=`route -n get default | awk '/interface:/{print $2}'`
ifconfig ${_extiface} >/dev/null 2>&1
if [ $? -eq 0 ]; then
cat >> ${etcdir}/ipfw.conf << EOF
/sbin/ipfw -q add ${fwcount_end} nat 123 all from ${_nm} to not ${_nm} any via ${_extiface}
/sbin/ipfw -q nat 123 config ip ${natip}
/sbin/ipfw -q add ${fwcount_end} nat 123 ip from any to ${natip} via ${_extiface}
EOF
fi
answ="${ok}"
ok="ok"
${miscdir}/sqlcli ${dbdir}/local.sqlite UPDATE local SET nat_enable=\"${answ}\"
return 0
;;
esac
return 1
}
configure_cron()
{
local _minutesconf=0 _hourlyconf=0
if [ -f "/var/cron/tabs/root" ]; then
if [ `/usr/bin/grep periodic_minutes /var/cron/tabs/root |wc -l` = 0 ]; then
_minutesconf=1
fi
if [ `/usr/bin/grep periodic_hourly /var/cron/tabs/root |wc -l` = 0 ]; then
_hourlyconf=1
fi
fi
[ ${_minutesconf} -eq 0 -a ${_hourlyconf} -eq 0 ] && return 0
if getyesno "${BOLD}Shall i modify the /var/cron/tabs/root file to enable periodic cbsd tasks?${NORMAL}"; then
[ ${_minutesconf} -eq 1 ] && echo "* * * * * /usr/bin/lockf -s -t0 ${ftmpdir}/periodic_minutes /usr/sbin/periodic minutes > /dev/null 2>&1" >> /var/cron/tabs/root
[ ${_hourlyconf} -eq 1 ] && echo "0 * * * * /usr/bin/lockf -s -t0 ${ftmpdir}/periodic_hourly /usr/sbin/periodic hourly > /dev/null 2>&1" >> /var/cron/tabs/root
fi
}
# if arg then force configure
configure_nat()
{
rfc1918="10.0.0.0/8 172.16.0.0/12 192.168.0.0/16"
ext_iface=`route -n get 0.0.0.0 |awk '/interface/{print $2}'`
local _default
local _net _nm
. ${tools}
if [ "${inter}" = "0" ]; then
[ -z "${natip}" ] && natip=`cbsd -c "cbsdsql local UPDATE local SET natip=\"${natip}\"" 2>/dev/null`
ok="${nat_enable}"
make_nat
return 0
fi
_default=`cbsd -c "cbsdsql local SELECT natip FROM local" 2>/dev/null`
iptype ${_default} >/dev/null 2>&1
if [ $? -ne 1 ]; then
_default=`cbsd -c "cbsdsql local SELECT nodeip FROM local"`
fi
${ECHO} "${BOLD}Set IP address as the aliasing NAT address, e.g: ${GREEN}${_default}${NORMAL}"
read natip
[ -z "${natip}" ] && natip="${_default}"
if [ -z "${natip}" ]; then
$ECHO "${MAGENTA}Error: empty natip value${NORMAL}"
return 1
fi
${miscdir}/sqlcli ${dbdir}/local.sqlite UPDATE local SET natip=\"${natip}\"
_default="pf"
answ=0
ok=
while [ "$ok" != "ok" ]; do
${ECHO} "${BOLD}Which one NAT framework should be use: [${GREEN}${_default}${NORMAL}${BOLD}]${NORMAL}"
${ECHO} "${MAGENTA}(type FW name, eg pf,ipfw or \"exit\" for break)${NORMAL}"
read ok leftover
[ -z "$ok" ] && ok="${_default}"
make_nat && return
[ "${ok}" != "exit" ] || return
done
}
rsyncd_enable()
{
[ ! -f "${inventory}" ] || . ${inventory}
if [ `grep ^cbsdrsyncd_enable="YES" /etc/rc.conf|wc -l` = 0 ]; then
getyesno "${BOLD}Shall i modify the /etc/rc.conf to sets cbsdrsyncd_enable=\"YES\"${NORMAL}" && sysrc -vf /etc/rc.conf cbsdrsyncd_enable="YES"
fi
if [ -n "$nodeip" ]; then
if [ `grep ^cbsdrsyncd_flags= /etc/rc.conf|wc -l` = 0 ]; then
getyesno "${BOLD}Shall i modify the /etc/rc.conf to sets cbsdrsyncd_flags=\"--config=${etcdir}/rsyncd.conf\" ?${NORMAL}" && sysrc -vf /etc/rc.conf cbsdrsyncd_flags="--config=${etcdir}/rsyncd.conf"
fi
fi
[ -f "/usr/local/etc/rc.d/cbsdrsyncd" ] && sysrc -vf /usr/local/etc/rc.d/cbsdrsyncd required_files="${etcdir}/rsyncd.conf"
service cbsdrsyncd onestart
}
rsyncd_disable()
{
service cbsdrsyncd stop > /dev/null 2>&1
sysrc -vf /etc/rc.conf cbsdrsyncd_enable="NO"
}
configure_rsync()
{
[ ! -f "${etcdir}/rsyncd.conf" ] && installne "-o ${cbsduser} -g ${cbsduser} -m 444" ${distdir}/etc/defaults/rsyncd.conf ${etcdir}/rsyncd.conf
if [ `grep -c cbsdrsyncd_enable /etc/rc.conf` = "1" ]; then
return 0
fi
getyesno "${MAGENTA}Configure RSYNC services for jail migration?${NORMAL}" && rsyncd_enable
}
update_hwinfo()
{
for _uninit in ${HWINI}; do
eval answ="\$${_uninit}"
if [ -n "${answ}" ]; then
${miscdir}/sqlcli ${dbdir}/local.sqlite UPDATE local SET ${_uninit}=\"${answ}\"
fi
done
}
# form for $hostname
get_hostname()
{
f_dialog_msgbox "Already set: ${hostname}"
}
# form for $nodename
get_nodename()
{
local _input _retval
f_dialog_input _input "${nodename_desc}" "${nodename}"
_retval=$?
case ${_retval} in
0)
nodename=${_input}
;;
esac
}
# form for $nodeip
get_nodeip()
{
local _input _retval
f_dialog_input _input "${nodeip_desc}" "${nodeip}"
_retval=$?
case ${_retval} in
0)
nodeip=${_input}
;;
esac
}
# form for $nodeip
get_nodedescr()
{
}
# form for $jnameserver
get_jnameserver()
{
local _input _retval
f_dialog_input _input "${jnameserver_desc}" "${jnameserver}"
_retval=$?
case ${_retval} in
0)
jnameserver=${_input}
;;
esac
}
# form for $nodeippool
get_nodeippool()
{
local _input _retval
f_dialog_input _input "${nodeippool_desc}" "${nodeippool}"
_retval=$?
case ${_retval} in
0)
nodeippool=${_input}
;;
esac
}
# form for $nat_enable
get_nat_enable()
{
local _input _retval
f_dialog_input _input "${nat_enable_desc}" "${nat_enable}"
_retval=$?
case ${_retval} in
0)
nat_enable=${_input}
;;
esac
}
# form for $natip
get_natip()
{
local _input _retval
f_dialog_input _input "${natip_desc}" "${natip}"
_retval=$?
case ${_retval} in
0)
natip=${_input}
;;
esac
}
# form for $fbsdrepo
get_fbsdrepo()
{
local _retval
msg_yes="no"
msg_no="yes"
f_dialog_noyes "${fbsdrepo_desc}"
_retval=$?
case ${_retval} in
0|1)
fbsdrepo=${_retval}
;;
esac
}
# form for $repo
get_repo()
{
local _input _retval
f_dialog_input _input "${repo_desc}" "${repo}"
_retval=$?
case ${_retval} in
0)
repo=${_input}
;;
esac
}
# form for $ipfw_enable
get_ipfw_enable()
{
local _retval
msg_yes="no"
msg_no="yes"
f_dialog_noyes "${ipfw_enable_desc}"
_retval=$?
case ${_retval} in
0|1)
ipfw_enable=${_retval}
;;
esac
}
# form for $zfsfeat
get_zfsfeat()
{
local _retval
msg_yes="no"
msg_no="yes"
f_dialog_noyes "${zfsfeat_desc}"
_retval=$?
case ${_retval} in
0|1)
zfsfeat_enable=${_retval}
;;
esac
}
# form for $jail_interface
get_jail_interface()
{
local _input _retval
f_dialog_input _input "${jail_interface_desc}" "${jail_interface}"
_retval=$?
case ${_retval} in
0)
jail_interface=${_input}
;;
esac
}
# form for $parallel
get_parallel()
{
local _input _retval
f_dialog_input _input "${parallel_desc}" "${parallel}"
_retval=$?
case ${_retval} in
0)
parallel=${_input}
;;
esac
}
# form for $stable
get_stable()
{
local _retval
msg_yes="no"
msg_no="yes"
f_dialog_yesno "${stable_desc}"
_retval=$?
case ${_retval} in
0|1)
stable=${_retval}
;;
esac
}
# nodename must be set
# if exist $old_nodename than remove it
install_sshkey()
{
local _md5name _keyfile _pubfile
# first remove old keys if exist old_nodename
if [ -n "${old_nodename}" ]; then
_md5name=`md5 -qs ${old_nodename}`
_keyfile="${sshdir}/${_md5name}.id_rsa"
[ -f ${_keyfile} ] && rm -f ${_keyfile}
[ -f "${_pubkey}" ] && rm -f ${_pubkey}
fi
# keygen
_md5name=`md5 -qs ${nodename}`
_keyfile="${sshdir}/${_md5name}.id_rsa"
_pubfile="${sshdir}/authorized_keys"
if [ ! -f ${_keyfile} ]; then
/usr/bin/ssh-keygen -t rsa -f ${_keyfile} -N ''
mv ${_keyfile}.pub ${_pubfile}
fi
chmod 0444 ${_pubfile}
chmod 0400 ${_keyfile}
chown ${cbsduser}:${cbsduser} ${_keyfile} ${_pubfile}
}
# run install_sshkey for change ssh key
# update nodename in respective places
# $nodename and $old_nodename must be filled
change_nodename()
{
[ -z "${nodename}" -o -z "${old_nodename}" ] && return 0
${miscdir}/sqlcli ${dbdir}/local.sqlite UPDATE local SET nodename=\"${nodename}\"
install_sshkey
echo "${nodename}" > "${nodenamefile}"
env sqlcolnames="1" ${miscdir}/sqlcli ${dbdir}/local.sqlite "SELECT * FROM local" > ${inventory}
}
## MAIN
#user settings
USERINI="\
nodename \
hostname \
nodeip \
nodedescr \
jnameserver \
nodeippool \
nat_enable \
natip \
fbsdrepo \
mdtmp \
repo \
workdir \
ipfw_enable \
zfsfeat \
jail_interface \
parallel \
stable"
#system settins
HWINI="fs ncpu physmem memtype disks cpumodel cpufreq kernhz sched eventtimer nics osrelease arch vnet racct"