-
Notifications
You must be signed in to change notification settings - Fork 0
/
jcreate.subr
333 lines (268 loc) · 9.57 KB
/
jcreate.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
if [ ! "$_CBSD_JCREATE_SUBR" ]; then
_CBSD_JCREATE_SUBR=1
###
# postaction for bsdconf-related module action
# pkg, useradd, password etc
# $1 - set $jname
# $2 - jconf (for fromfile)
postcreate_module_action()
{
local jname jconf
[ -n "${1}" ] && jname="${1}"
[ -n "${2}" ] && jconf="${2}"
[ -n "${orig_pkg_bootstrap}" ] && pkg_bootstrap="${orig_pkg_bootstrap}"
if [ -n "${user_pw_root_crypt}" ]; then
passwd jname=${jname} user="root" epw="${user_pw_root_crypt}"
elif [ -n "${user_pw_root}" ]; then
passwd jname=${jname} user="root" pw="${user_pw_root}"
fi
if [ -n "${sysrc_enable}" ]; then
for i in ${sysrc_enable}; do
sysrc jname=${jname} ${i}_enable="YES"
done
fi
if [ -n "${user_add}" ]; then
if [ -n "${jconf}" ]; then
${ECHO} "${MAGENTA}Adding users: ${GREEN}${user_add}${NORMAL}"
adduser jname=${jname} mode=add fromfile="${jconf}"
fi
fi
[ -n "${odelpkglist}" ] && delpkglist="${odelpkglist}"
[ -n "${delpkglist}" ] && delpkglist=1
#[ ${applytpl} -eq 1 ] && ${miscdir}/config_pkgrepo -t ${etcdir}/pkg.conf -v ${ver} -a ${arch} ${data}
# pkg area
if [ "${pkg_bootstrap}" = "1" ]; then
[ "${ver}" != "empty" ] && pkg mode=bootstrap jname=${jname} ||true
if [ -n "${pkglist}" -a "${pkglist}" != "NO" -a "${ver}" != "empty" ]; then
if [ ! -f "${pkglist}" ]; then
pkg mode=install jname=${jname} ${pkglist}
else
pkg mode=install pkglist="${pkglist}" jname=${jname}
[ ${delpkglist} -eq 1 ] && /bin/rm -f ${pkglist}
fi
fi
fi
}
# users custom stop script
# $1 - is source dir
external_exec_script()
{
local _res
local _dir="${1}"
[ -z "${_dir}" ] && return 1
[ ! -d "${jailsysdir}/${jname}/${_dir}" ] && return 0
[ -z "$( /bin/ls ${jailsysdir}/${jname}/${_dir}/ )" ] && return 0
[ -d "${data}/tmp/${_dir}" ] && /bin/rm -rf "${data}/tmp/${_dir}"
/bin/cp -a "${jailsysdir}/${jname}/${_dir}" ${data}/tmp/${_dir}
/usr/bin/find "${data}/tmp/${_dir}" \( -type l -or -type f \) -and \( -perm +111 \) -depth 1 -maxdepth 1 -exec /usr/bin/basename {} \; | while read _file; do
${ECHO} "${MAGENTA}Execute script: ${GREEN}${_file}${NORMAL}"
jexec jname="${jname}" /tmp/${_dir}/${_file}
done
/bin/rm -rf "${data}/tmp/${_dir}"
}
# users custom stop for executing in master host
# $1 - is source dir name ( master_poststart.d master_prestart.d )
external_exec_master_script()
{
local _res
local _dir="${1}"
local _i
[ -z "${_dir}" ] && return 1
local _srcdir="${jailsysdir}/${jname}/${_dir}"
[ ! -d "${_srcdir}" ] && return 0
[ -z "$( /bin/ls ${_srcdir}/ )" ] && return 0
local CBSDPATH="${PATH}"
# reset CBSD PATH
# it is necessary that the calling of any commands from external hooks
# does not conflict with the same CBSD commands that the user does not expect
export PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"
/usr/bin/find "${_srcdir}" \( -type l -or -type f \) -and \( -perm +111 \) -depth 1 -maxdepth 1 -exec /usr/bin/basename {} \; |while read _file; do
${ECHO} "${MAGENTA}Execute master script: ${GREEN}${_file}${NORMAL}"
${ECHO} "${MAGENTA} :: ${_srcdir}/${_file}${NORMAL}"
${_srcdir}/${_file}
done
# restore CBSD PATH
export PATH="${CBSDPATH}"
}
# users custom stop script
# $1 - is source dir (stop.d start.d)
external_exec_script()
{
local _res
local _dir="${1}"
[ -z "${_dir}" ] && return 1
[ ! -d "${jailsysdir}/${jname}/${_dir}" ] && return 0
[ -z "$( /bin/ls ${jailsysdir}/${jname}/${_dir}/ )" ] && return 0
[ -d "${data}/tmp/${_dir}" ] && rm -rf "${data}/tmp/${_dir}"
/bin/cp -a "${jailsysdir}/${jname}/${_dir}" ${data}/tmp/${_dir}
/usr/bin/find "${data}/tmp/${_dir}" \( -type l -or -type f \) -and \( -perm +111 \) -depth 1 -maxdepth 1 -exec /usr/bin/basename {} \; | while read _file; do
${ECHO} "${MAGENTA}Execute script: ${GREEN}${_file}${NORMAL}"
jexec jname="${jname}" /tmp/${_dir}/${_file}
done
/bin/rm -rf "${data}/tmp/${_dir}"
}
# users custom stop for executing in master host
# $1 - is source dir name ( master_poststop.d master_prestop.d )
external_exec_master_script()
{
local _res
local _dir="${1}"
[ -z "${_dir}" ] && return 1
local _srcdir="${jailsysdir}/${jname}/${_dir}"
[ ! -d "${_srcdir}" ] && return 0
[ -z "$( /bin/ls ${_srcdir}/ )" ] && return 0
local CBSDPATH="${PATH}"
# reset CBSD PATH
# it is necessary that the calling of any commands from external hooks
# does not conflict with the same CBSD commands that the user does not expect
export PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"
/usr/bin/find "${_srcdir}" \( -type l -or -type f \) -and \( -perm +111 \) -depth 1 -maxdepth 1 -exec /usr/bin/basename {} \; |while read _file; do
${ECHO} "${MAGENTA}Execute master script: ${GREEN}${_file}${NORMAL}"
${ECHO} "${MAGENTA} :: ${_srcdir}/${_file}${NORMAL}"
${_srcdir}/${_file}
done
# restore CBSD PATH
export PATH="${CBSDPATH}"
}
exec_master_prestop()
{
eval CMD=\${exec_master_prestop}
[ -z "${CMD}" -o "${CMD}" = "0" ] && return 0
local CBSDPATH="${PATH}"
# reset CBSD PATH
# it is necessary that the calling of any commands from external hooks
# does not conflict with the same CBSD commands that the user does not expect
export PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"
${ECHO} "${MAGENTA}${jname} Master exec prestop: ${GREEN}${CMD}${NORMAL}"
${CMD}
# restore CBSD PATH
export PATH="${CBSDPATH}"
}
exec_prestop()
{
eval CMD=\${exec_prestop}
[ -z "${CMD}" -o "${CMD}" = "0" ] && return 0
local CBSDPATH="${PATH}"
# reset CBSD PATH
# it is necessary that the calling of any commands from external hooks
# does not conflict with the same CBSD commands that the user does not expect
export PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"
${ECHO} "${MAGENTA}${jname} exec exec_prestop: ${GREEN}${CMD}${NORMAL}"
/usr/sbin/chroot ${path} ${CMD}
# restore CBSD PATH
export PATH="${CBSDPATH}"
}
exec_master_poststop()
{
eval CMD=\${exec_master_poststop}
[ -z "${CMD}" -o "${CMD}" = "0" ] && return 0
local CBSDPATH="${PATH}"
# reset CBSD PATH
# it is necessary that the calling of any commands from external hooks
# does not conflict with the same CBSD commands that the user does not expect
export PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"
${ECHO} "${MAGENTA}${jname} Master exec afterstop: ${GREEN}${CMD}${NORMAL}"
${CMD}
# restore CBSD PATH
export PATH="${CBSDPATH}"
}
exec_poststop()
{
eval CMD=\${exec_poststop}
[ -z "${CMD}" -o "${CMD}" = "0" ] && return 0
local CBSDPATH="${PATH}"
# reset CBSD PATH
# it is necessary that the calling of any commands from external hooks
# does not conflict with the same CBSD commands that the user does not expect
export PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"
${ECHO} "${MAGENTA}${jname} exec exec_poststop: ${GREEN}${CMD}${NORMAL}"
/usr/sbin/chroot ${path} ${CMD}
# restore CBSD PATH
export PATH="${CBSDPATH}"
i=$((i + 1))
}
exec_master_poststart()
{
eval CMD=\${exec_master_poststart}
[ -z "${CMD}" -o "${CMD}" = "0" ] && return 0
local CBSDPATH="${PATH}"
# reset CBSD PATH
# it is necessary that the calling of any commands from external hooks
# does not conflict with the same CBSD commands that the user does not expect
export PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"
${ECHO} "${MAGENTA}${jname} Master exec afterstart: ${GREEN}${CMD}${NORMAL}"
${CMD}
# restore CBSD PATH
export PATH="${CBSDPATH}"
}
exec_poststart()
{
eval CMD=\${exec_poststart}
[ -z "${CMD}" -o "${CMD}" = "0" ] && return 0
${ECHO} "${MAGENTA}${jname} exec exec_poststart: ${GREEN}${CMD}${NORMAL}"
jexec jname=${jname} ${CMD}
}
exec_cbsdjail_first_boot()
{
if [ -f ${path}/etc/rc.cbsdjail_first_boot ]; then
jexec jname=${jname} /bin/sh /etc/rc.cbsdjail_first_boot
/bin/rm -f ${path}/etc/rc.cbsdjail_first_boot
fi
}
exec_master_prestart()
{
eval CMD=\${exec_master_prestart}
[ -z "${CMD}" -o "${CMD}" = "0" ] && return 0
local CBSDPATH="${PATH}"
# reset CBSD PATH
# it is necessary that the calling of any commands from external hooks
# does not conflict with the same CBSD commands that the user does not expect
export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
${ECHO} "${MAGENTA}${jname} master exec prestart: ${GREEN}${CMD}${NORMAL}"
${CMD}
# restore CBSD PATH
export PATH="${CBSDPATH}"
}
exec_prestart()
{
eval CMD=\${exec_prestart}
[ -z "${CMD}" -o "${CMD}" = "0" ] && return 0
${ECHO} "${MAGENTA}${jname} exec exec_prestart: ${GREEN}${CMD}${NORMAL}"
jexec jname=${jname} ${CMD}
}
export_bhyve_data_for_external_hook()
{
. ${workdir}/vnet.subr # get_vm_uplink_interface
# export variables for external hooks
export jname=${jname}
for _i in ${JARG} ${MYCOL} ipv4_first_public ipv4_first_private ipv4_first ipv6_first_public ipv6_first_private ipv6_first bhyve_vnc_tcp_ipconnect; do
T=
eval T="\$$_i"
export ${_i}="${T}"
done
# Export additional data to hooks
if [ -r "${jailsysdir}/${jname}/vnc_port" ]; then
vm_vnc_port=$( /bin/cat ${jailsysdir}/${jname}/vnc_port )
export vm_vnc_port="${vm_vnc_port}"
fi
nic_id=0
local sqldelimer=" "
eval $( cbsdsql ${jailsysdir}/${jname}/local.sqlite "SELECT nic_hwaddr,nic_parent FROM bhyvenic"| while read nic_hwaddr nic_parent; do
echo "export nic_hwaddr${nic_id}=\"${nic_hwaddr}\""
get_vm_uplink_interface ${nic_parent}
echo "export nic_parent${nic_id}=\"${interface}\""
done )
}
export_jail_data_for_external_hook()
{
# export variables for external hooks
# todo: first_ipv4_addr + first_ipv6_addr
export jname=${jname}
for _i in ${JARG} ipv4_first_public ipv4_first_private ipv4_first ipv6_first_public ipv6_first_private ipv6_first; do
T=
eval T="\$$_i"
export ${_i}="${T}"
done
}
###
fi