-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvm_obtain
executable file
·297 lines (228 loc) · 10.1 KB
/
vm_obtain
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
#!/usr/local/bin/cbsd
#v11.1.19
MYARG="jname vm_size vm_cpus vm_ram vm_os_type"
MYOPTARG="ip4_addr gw authkey host_hostname mask pause_after_err astart bhyve_vnc_tcp_bind"
MYDESC="Obtaing new VM"
ADDHELP="vm_ram - in megabytes\n\
authkey - can be 'node' or fullpath to pubkey\n\
ip4_addr - can be 'DHCP' or 'REALDHCP'\n"
CBSDMODULE="convectix"
. ${subr}
astart=0
bhyve_vnc_tcp_bind=
. ${cbsdinit}
. ${system}
. ${subrdir}/universe.subr
. ${subrdir}/bhyve.subr
oastart="${astart}"
obhyve_vnc_tcp_bind="${bhyve_vnc_tcp_bind}"
readconf freecloud.conf
[ -z "${cbsd_queue_name}" ] && cbsd_queue_name="/clonos/bhyvevms/"
[ -z "${pause_after_err}" ] && pause_after_err=0
[ -z "${template_wait_offline}" ] && template_wait_offline="30" # max seconds to wait when template going to offline when it online
#defines
_MYDIR=$( /usr/bin/dirname `realpath $0` )
### MAIN Route ###
set -e
. ${_MYDIR}/vm_ssh.subr
set +e
if [ -n "${vm_size}" ]; then
orig_vm_size="${vm_size}"
fi
if [ -n "${mask}" ]; then
orig_mask="${mask}"
fi
if [ -n "${vm_cpus}" ]; then
orig_vm_cpus="${vm_cpus}"
fi
if [ -n "${vm_ram}" ]; then
orig_vm_ram="${vm_ram}"
fi
if [ -z "${ip4_addr}" ]; then
orig_ip4_addr="DHCP"
else
orig_ip4_addr="${ip4_addr}"
fi
if [ -z "${gw}" ]; then
orig_gw="10.0.0.1"
else
orig_gw="${gw}"
fi
if [ -z "${host_hostname}" ]; then
orig_host_hostname="${jname}.convectix.com"
else
orig_host_hostname="${host_hostname}"
fi
if [ -z "${authkey}" ]; then
orig_authkey=0
else
orig_authkey="${authkey}"
fi
TRAP=""
if [ "${orig_authkey}" != "0" ]; then
prefix=$( substr --pos=0 --len=1 --str="${orig_authkey}" )
if [ "${prefix}" = "/" ]; then
# file-based authkey
[ ! -r "${orig_authkey}" ] && err 1 "${N1_COLOR}No such pubkey: ${N2_COLOR}${orig_authkey}${N0_COLOR}"
else
${ECHO} "${N1_COLOR}Select authkey from database for: ${N2_COLOR}${orig_authkey}${N0_COLOR}"
authtmp=$( ${MKTEMP_CMD} )
${TRUNCATE_CMD} -s0 ${authtmp}
cbsdsqlro authkey "SELECT authkey FROM authkey WHERE name='${orig_authkey}'" >> ${authtmp}
authkey_file_size=$( /usr/bin/stat -f "%z" ${authtmp} 2>/dev/null )
if [ "${authkey_file_size}" = "0" ]; then
${ECHO} "${N1_COLOR}No such pubkey in database, rollback to FS: ${N2_COLOR}${sshdir}/authorized_keys${N0_COLOR}"
/bin/cp -a ${sshdir}/authorized_keys ${authtmp}
orig_authkey="${authtmp}"
else
${ECHO} "${N1_COLOR}Authkey key found in DB for: ${N2_COLOR}${orig_authkey},${authtmp}${N0_COLOR}"
orig_authkey="${authtmp}"
TRAP="${TRAP} /bin/rm -f ${orig_authkey};"
trap "${TRAP}" HUP INT QUIT ABRT KILL ALRM TERM BUS EXIT
fi
fi
fi
ovm_os_type="${vm_os_type}"
. ${jrcconf}
[ $? -eq 0 ] && err 1 "${N1_COLOR}Already exist: ${N2_COLOR}${jname}${N0_COLOR}"
vm_os_type="${ovm_os_type}"
${ECHO} "${N2_COLOR}READY${N0_COLOR}"
lockfile="${tmpdir}/vm_obtain.lock"
[ -f ${lockfile} ] && err 1 "Already locked $lockfile"
/usr/bin/touch ${lockfile}
trap "/etc/rc.d/ctld onestop > /dev/null 2>&1; /bin/rm -f ${lockfile}" HUP INT ABRT BUS TERM EXIT
readconf freecloud.conf
${ECHO} "${N1_COLOR}OS profile: ${N2_COLOR}${vm_os_type}${N0_COLOR}"
eval service_vm_ip="\$convectix_srv_${vm_os_type}_ip"
[ -z "${service_vm_ip}" ] && err 1 "${N1_COLOR}Empty service_vm_ip: Please set template_vm_${vm_os_type} in freecloud.conf${N0_COLOR}"
[ -z "${template_vm}" ] && eval template_vm="\$template_vm_${vm_os_type}" # source for clone_vm
[ -z "${clone_vm}" ] && eval clone_vm="\$clone_vm_${vm_os_type}" # source for clone_vm
[ -z "${login}" -o "${login}" = "unset" ] && eval login="\$login_${vm_os_type}" # login for VM
[ -z "${pw}" -o "${pw}" = "unset" ] && eval pw="\$pw_${vm_os_type}" # login for VM
[ -z "${resizer_opts}" -o "${resizer_opts}" = "unset" ] && eval resizer_opts="\$resizer_${vm_os_type}_opts" # login for VM
[ -z "${service_vm_ip}" -o "${service_vm_ip}" = "unset" ] && err 1 "${N1_COLOR}Please set convectix_srv_${vm_os_type} in freecloud.conf${N0_COLOR}"
[ -z "${template_vm}" -o "${template_vm}" = "unset" ] && err 1 "${N1_COLOR}Please set template_vm_${vm_os_type} in freecloud.conf${N0_COLOR}"
[ -z "${clone_vm}" -o "${clone_vm}" = "unset" ] && err 1 "${N1_COLOR}Please set clone_vm_${vm_os_type} in freecloud.conf${N0_COLOR}"
[ -z "${template_vm}" ] && err 1 "${N1_COLOR}Empty template_vm${N0_COLOR}"
[ -z "${clone_vm}" ] && err 1 "${N1_COLOR}Empty clone_vm${N0_COLOR}"
[ -z "${service_vm_ip}" ] && err 1 "${N1_COLOR}Empty service_vm_ip${N0_COLOR}"
[ -z "${login}" ] && err 1 "${N1_COLOR}Please set login_${vm_os_type} in freecloud.conf${N0_COLOR}"
[ -z "${pw}" ] && err 1 "${N1_COLOR}Please set pw_${vm_os_type} in freecloud.conf${N0_COLOR}"
echo ":: ${service_vm_ip}"
init_ssh
${ssh_cmd} -C date > /dev/null 2>&1
[ $? -ne 0 ] && err 1 "No ssh connection with resizer service: ${ssh_cmd}"
${ECHO} "${N1_COLOR}Connection to ${service_vm_ip} passed${N0_COLOR}"
vm_size=$( echo ${orig_vm_size} | ${TR_CMD} -d "[:alpha:]" )
# Check if SIZE if valid: can't be smaller then template
if is_number ${vm_size}; then
# not number, try to convert
if conv2bytes "${vm_size}"; then
vm_ram_bytes="${convval}"
else
err 1 "${vm_size} is not number and we can't convert int via conv2bytes"
fi
else
# assume that number in gigabytes
vm_bytes=$(( vm_size * 1024 * 1024 * 1024 ))
# >> MB KB B <<
fi
## Check vm_ram value
if is_number ${orig_vm_ram}; then
# not number, try to convert
if conv2bytes "${orig_vm_ram}"; then
vm_ram_bytes="${convval}"
else
err 1 "${orig_vm_ram} is not number and we can't convert int via conv2bytes"
fi
vm_ram_megabytes=$(( vm_ram_bytes / 1024 / 1024 ))
else
# assume that number in megabytes
vm_ram_megabytes=${orig_vm_ram}
fi
${ECHO} "${N1_COLOR}RAM IN MBytes: ${N2_COLOR}${vm_ram_megabytes}${N0_COLOR}"
[ ${vm_ram_megabytes} -lt 128 ] && err 1 "RAM to small, < 128 MB"
orig_vm_ram=${vm_ram_megabytes}
# get ZVOL size
readconf zfs.conf
. ${zfstool}
#echo "/sbin/zfs get -Hp -o value volsize zroot/ROOT/default/bcbsd-emptyf110a-dsk1.vhd"
tmplink="${jaildatadir}/${template_vm}-data/dsk1.vhd"
if is_getzvol ${tmplink}; then
template_vol="${is_zvol}"
else
err 1 "Can't determine ZVOL for template: ${template_vm}, no such link from ${tmplink}"
fi
${ECHO} "${N1_COLOR}:: Template VOL: ${N2_COLOR}${is_zvol}${N0_COLOR}"
template_bytes=$( /sbin/zfs get -Hp -o value volsize ${is_zvol} )
template_bytes=$(( template_bytes + 1000000000 ))
${ECHO} "${N1_COLOR}:: Template Bytes: ${N2_COLOR}${template_bytes} ( + 1GB buffer)${N0_COLOR}"
${ECHO} "${N1_COLOR}:: VM Bytes request: ${N2_COLOR}${vm_bytes}${N0_COLOR}"
if [ ${template_bytes} -gt ${vm_bytes} ]; then
convval=$( ${miscdir}/conv2human ${template_bytes} |${TR_CMD} '[:upper:]' '[:lower:]' )
echo "Template size: ${convval} (< ${vm_size})"
err 1 "VM size can't be smaller than template size"
fi
# CBSD QUEUE
if [ -x "${moduledir}/cbsd_queue.d/cbsd_queue" ]; then
[ "${cbsd_queue_name}" != "none" ] && cbsd_queue cbsd_queue_name=${cbsd_queue_name} id=${jname} cmd=bcreate vm_ram=${vm_ram} vm_cpus=${vm_cpus} vm_os_type=${vm_os_type} astart=${astart} protected=${protected} status=1
fi
ready_for_process=1
while [ ${ready_for_process} -ne 0 ]; do
### check for online
clone_jid=$( jstatus jname=${clone_vm} )
template_jid=$( jstatus jname=${template_vm} )
ready_for_process=$(( clone_jid + template_jid ))
if [ ${ready_for_process} -gt 0 ]; then
${ECHO} "${N1_COLOR}template/clone not in offline state, waiting ${template_wait_offline}...${N0_COLOR}"
sleep 1
template_wait_offline=$(( template_wait_offline - 1 ))
fi
[ ${template_wait_offline} -lt 1 ] && err 1 "${N1_COLOR}Not in offline mode, please stop then${N0_COLOR}"
done
brename old=${clone_vm} new=${jname} host_hostname=${orig_host_hostname}
task mode=new /usr/local/bin/cbsd bclone old=${template_vm} new=${clone_vm} mac_reinit=1 > /dev/null 2>&1
echo "--"
case "${orig_ip4_addr}" in
[Rr][Ee][Aa][Ll][Dd][Hh][Cc][Pp])
echo "HERE"
gw=0
mask=0
orig_mask=0
;;
esac
echo "vm_resize jname=${jname} size=${vm_size} ip4_addr=${orig_ip4_addr} gw=${orig_gw} authkey=${orig_authkey} vm_os_type=${vm_os_type} host_hostname=${orig_host_hostname} login=${login} pw=${pw} pause_after_err=${pause_after_err} service_vm_ip=${service_vm_ip} resizer_opts=${resizer_opts}"
#### BACK ME
#timeout 60 vm_resize jname=${jname} size=${vm_size} ip4_addr="${orig_ip4_addr}" gw="${orig_gw}" authkey="${orig_authkey}" vm_os_type="${vm_os_type}" host_hostname="${orig_host_hostname}" login="${login} pw=${pw}"
echo "Ready to resizing"
vm_resize jname=${jname} size=${vm_size} ip4_addr="${orig_ip4_addr}" gw="${orig_gw}" authkey="${orig_authkey}" vm_os_type="${vm_os_type}" host_hostname="${orig_host_hostname}" login="${login} pw=${pw} pause_after_err=${pause_after_err}" service_vm_ip="${service_vm_ip}" resizer_opts="${resizer_opts}"
ret=$?
if [ ${ret} -ne 0 ]; then
echo "Error in vm_resize."
bremove ${jname} > /dev/null 2>&1
jremove ${jname} > /dev/null 2>&1
/bin/rm -f ${lockfile}
exit 1
fi
echo "--"
echo "bset jname=${jname} vm_ram=${vm_ram} vm_cpus=${vm_cpus}"
bset jname=${jname} vm_ram="${vm_ram}" vm_cpus="${vm_cpus}" ip4_addr="${orig_ip4_addr}"
bset jname=${jname} protected=0
bset jname=${jname} hidden=0
[ -n "${obhyve_vnc_tcp_bind}" ] && bset jname=${jname} bhyve_vnc_tcp_bind="${obhyve_vnc_tcp_bind}"
/bin/rm -f ${lockfile}
echo "vm_bootcfg jname=${jname} ip4_addr=${orig_ip4_addr} gw=${orig_gw} mask=${orig_mask} authkey=node"
vm_bootcfg jname=${jname} ip4_addr=${orig_ip4_addr} gw=${orig_gw} mask=${orig_mask} authkey=node
# CBSD QUEUE
if [ -x "${moduledir}/cbsd_queue.d/cbsd_queue" ]; then
[ "${cbsd_queue_name}" != "none" ] && cbsd_queue cbsd_queue_name=${cbsd_queue_name} id=${jname} cmd=bcreate vm_ram=${vm_ram} vm_cpus=${vm_cpus} vm_os_type=${vm_os_type} astart=${astart} protected=${protected} status=2
fi
# Re-init MAC address even bclone reset it.
#
cbsdsqlrw ${jailsysdir}/${jname}/local.sqlite "UPDATE bhyvenic SET nic_hwaddr='0'"
[ "${oastart}" = "1" ] && bstart ${jname}
${ECHO} "${N1_COLOR}Credential for new vm${N0_COLOR}"
${ECHO} "${N1_COLOR}IP: ${N2_COLOR}${orig_ip4_addr}${N0_COLOR}"
${ECHO} "${N1_COLOR}User: ${N2_COLOR}${login}${N0_COLOR}"
${ECHO} "${N1_COLOR}Password: ${N2_COLOR}${pw}${N0_COLOR}"
${ECHO} "${N1_COLOR}Key: ${N2_COLOR}${convectix_ssh_privatekey}${N0_COLOR}"