-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxen.subr
476 lines (387 loc) · 13.6 KB
/
xen.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
if [ ! "$_CBSD_XEN_SUBR" ]; then
_CBSD_XEN_SUBR=1
###
# generic function for xen
# required:
# strings.subr
#
. ${workdir}/virtual.subr
XL_CMD="/usr/local/sbin/xl"
# test environment for xening
# init tmuxcmd
init_xen()
{
local _host_ready=1
local _required_kmods="if_tap if_bridge" _i _res
/sbin/ifconfig bridge0 > /dev/null 2>&1
if [ $? -ne 0 ]; then
${ECHO} "${MAGENTA}init_xen : Interface ${GREEN}bridge0${MAGENTA} not exist${NORMAL}"
_host_ready=0
fi
for _i in ${_required_kmods}; do
if ! /sbin/kldstat -qm ${_i} >/dev/null 2>&1; then
${ECHO} "${MAGENTA}No kldloaded module: ${GREEN}${_i}${NORMAL}"
${ECHO} "${MAGENTA}Put ${GREEN}kld_list=\"if_tap if_bridge\"${MAGENTA} into your ${GREEN}/etc/rc.conf${MAGENTA} and execute: ${GREEN}service kld restart${NORMAL}"
_host_ready=0
fi
done
if [ ! -x "${XL_CMD}" ]; then
${ECHO} "${MAGENTA}No such: ${XL_CMD}${NORMAL}"
_host_ready=0
fi
if [ ${_host_ready} -ne 1 ]; then
echo
${ECHO} "${MAGENTA}Please follow ${LYELLOW}https://wiki.freebsd.org/Xen ${MAGENTA}and/or ${LYELLOW}https://wiki.xen.org/wiki/FreeBSD_Dom0${MAGENTA} instruction to setup FreeBSD host:${NORMAL}"
echo
${ECHO} "${WHITE}*${NORMAL} pkg install ${LYELLOW}xen xen-kernel${NORMAL}"
${ECHO} "${WHITE}*${NORMAL} sysrc xencommons_enable='YES'${NORMAL}"
${ECHO} "${WHITE}*${NORMAL} sysrc cloned_interfaces='bridge0'${NORMAL}"
${ECHO} "${WHITE}*${NORMAL} sysrc ifconfig_bridge0='addm ${LYELLOW}bge0${NORMAL} SYNCDHCP'${NORMAL}"
${ECHO} "${WHITE}*${NORMAL} echo 'vm.max_wired=-1' >> /etc/sysctl.conf${NORMAL}"
${ECHO} "${WHITE}*${NORMAL} echo 'xc0 "/usr/libexec/getty Pc" xterm on secure' >> /etc/ttys${NORMAL}"
${ECHO} "${WHITE}*${NORMAL} mkdir -p /var/lock${NORMAL}"
${ECHO} "${WHITE}*${NORMAL} mkdir -p /var/run/xen${NORMAL}"
${ECHO} "${WHITE}*${NORMAL} echo 'xen_cmdline="dom0_mem=${LYELLOW}2048M${NORMAL} dom0_max_vcpus=${LYELLOW}2${NORMAL} dom0pvh=1 com1=115200,8n1 guest_loglvl=all loglvl=all vga=keep noreboot"' >> /boot/loader.conf${NORMAL}"
${ECHO} "${WHITE}*${NORMAL} echo 'xen_kernel="/boot/xen"' >> /boot/loader.conf${NORMAL}"
echo
${ECHO} "${MAGENTA}Where ${LYELLOW}bge0${MAGENTA} is your uplink interface${NORMAL}"
echo
${ECHO} "${MAGENTA}Also ${GREEN}if_tap(4)${MAGENTA} module should be loaded.${NORMAL}"
exit 0
fi
tmuxcmd=$(which tmux)
[ -z "${tmuxcmd}" ] && err 1 "${MAGENTA}Current version required for ${GREEN}tmux${MAGENTA}\nPlease ${GREEN}pkg install tmux ${MAGENTA} or ${GREEN}make -C /usr/ports/sysutils/tmux install${MAGENTA} it.${NORMAL}"
}
# export arguments for bhyve cd_args and cd_args2 (second boot after empty disk check)
# $iso_img must be present
compile_cd_args()
{
cd_args=
cd_args2=
# Ubuntu has trouble when got two CD
#[ "${vm_boot}" = "net" ] && return 0
if [ -z "${iso_img}" ]; then
${ECHO} "${MAGENTA}iso_img variable is not set${NORMAL}"
return 1
fi
if [ ! -r "${iso_img}" ]; then
iso_img2=$( cbsdsql storage_media SELECT path FROM media WHERE jname=\"${jname}\" AND type=\"iso\" LIMIT 1 )
if [ ! -r ${iso_img2} ]; then
${ECHO} "${MAGENTA}iso_img in not readable: ${GREEN}${iso_img} ${iso_img2}${NORMAL}"
return 1
else
iso_img="${iso_img2}"
fi
fi
if [ -z "${iso_img}" ]; then
${ECHO} "${MAGENTA}iso_img variable is empty: no ISO or image is not readable${NORMAL}"
return 1
fi
## for check via ref
# cd_args="-s ${bhyve_pci_index}:0,ahci-cd,${distdir}/upgrade/patch/efirefd.fd"
#if ! next_pci_id; then
# return 1
#fi
# cd_args="${cd_args} -s ${bhyve_pci_index}:0,ahci-cd,${iso_img}"
if [ "${vm_os_type}" = "openbsd" ]; then
# small hack: openbsd use install.fs as hdd drive, not cd
cd_args="-s ${bhyve_pci_index}:0,ahci-hd,${iso_img},ro"
cd_args2=""
else
cd_args="-s ${bhyve_pci_index}:0,ahci-cd,${iso_img}"
cd_args2="-s ${bhyve_pci_index}:0,ahci-cd,/usr/local/cbsd/upgrade/patch/efirefd.fd"
fi
}
# export arguments for bhyve about dsk vms in $dsk_args variable
# $jname must be set's
# sample:
# jname="debian"
# if compile_dsk_args; then
# echo $dsk_args
# else
# echo "No disks"
# fi
compile_dsk_args()
{
local dsk_id=0
local sqldelimer=" "
local prefix
local full_dsk_path
local XENCFG="${1}"
local dsk_count=0
local iso_count=0
local iso_img2=
local media_count=0
local first_count=0
local _ascii _dev_letter _dev
dsk_count=$( cbsdsql ${jailsysdir}/${jname}/local.sqlite 'SELECT COUNT(dsk_path) FROM xendsk' )
iso_img2=$( cbsdsql storage_media SELECT path FROM media WHERE jname=\"${jname}\" AND type=\"iso\" LIMIT 1 )
[ -r ${iso_img2} ] && iso_count=$(( iso_count + 1 ))
if [ -r ${iso_img} ]; then
if [ "${iso_img}" = "${iso_img2}" ]; then
iso_img2=
else
iso_count=$(( iso_count + 1 ))
fi
else
iso_img=
fi
media_count=$(( iso_count + dsk_count ))
[ ${media_count} -eq 0 ] && return 0
# open disk brackets
echo "disk = [" >> ${XENCFG}
# temporary workaroung for Alex (CBSD Telegram) issue with no disk (old SQL schema) - don't select dsk_conf
# eval $( cbsdsql ${jailsysdir}/${jname}/local.sqlite SELECT dsk_controller,dsk_path,dsk_slot,dsk_conf FROM xendsk WHERE jname=\"${jname}\" AND dsk_type=\"vhd\" |while read dsk_controller dsk_path dsk_slot dsk_conf; do
cbsdsql ${jailsysdir}/${jname}/local.sqlite SELECT dsk_controller,dsk_path,dsk_slot FROM xendsk WHERE dsk_type=\"vhd\" |while read dsk_controller dsk_path dsk_slot; do
# test for full path. If path not started from '/' - append $data dir
full_dsk_path=
prefix=$( substr --pos=0 --len=1 --str="${dsk_path}" )
if [ "${prefix}" != "/" ]; then
full_dsk_path="${data}/${dsk_path}"
else
full_dsk_path="${dsk_path}"
fi
# for zvol
# phy:${full_dsk_path} ?
_ascii=$(( 97 + first_count )) # started by 'a' (97 ascii code)
_dev_letter=$( printf "\\$(printf %o ${_ascii} )" )
_dev="hd${_dev_letter}"
if [ ${first_count} -eq 0 ]; then
# first record
printf "'${full_dsk_path},raw,${_dev},rw'" >> ${XENCFG}
else
printf ", '${full_dsk_path},raw,${_dev},rw'" >> ${XENCFG}
fi
first_count=$(( first_count + 1 ))
media_count=$(( media_count - 1 ))
done || err 1 "${MAGENTA}Error while create disk map${NORMAL}"
_ascii=$(( 97 + dsk_count )) # started by 'a' (97 ascii code), skip all hdd and set next letter for disk id
_dev_letter=$( printf "\\$(printf %o ${_ascii} )" )
_dev="hd${_dev_letter}"
if [ -n "${iso_img2}" ]; then
printf ", '${iso_img2},raw,${_dev}:cdrom,r'" >> ${XENCFG}
first_count=$(( first_count + 1 ))
_ascii=$(( 97 + first_count )) # started by 'a' (97 ascii code)
_dev_letter=$( printf "\\$(printf %o ${_ascii} )" )
_dev="hd${_dev_letter}"
fi
[ -n "${iso_img}" ] && printf ", '${iso_img},raw,${_dev}:cdrom,r'" >> ${XENCFG}
# close disk brackets
printf "\n]\n" >> ${XENCFG}
return 0
}
# print 5c:f9:dd:76:d5:c4 from 5cf9dd76d5c4
normalize_nic_hwaddr()
{
local _hwaddr
[ -z "${1}" ] && return 0
_hwaddr=$( echo "${1}" | /usr/bin/tr -d ":"| /usr/bin/sed -e 's/\([0-9A-Fa-f]\{2\}\)/\1:/g' -e 's/\(.*\):$/\1/' )
printf ${_hwaddr}
}
# export arguments for xen about nic vms in $nic_args variable
# $jname must be set's
# sample:
# jname="debian"
# if compile_nic_args; then
# echo $nic_args
# else
# echo "No nic"
# fi
compile_nic_args()
{
local _id=0 i _res taplist=
local sqldelimer=" "
local mybridge
cbsdsql ${jailsysdir}/${jname}/local.sqlite SELECT id,nic_order,nic_driver,nic_slot,nic_type,nic_parent,nic_hwaddr FROM xennic WHERE jname=\"${jname}\" |while read nic_id nic_order nic_driver nic_slot nic_type nic_parent nic_hwaddr; do
# when ip=0 and interface=auto we must use default interface for upstream
if [ "${nic_parent}" = "0" -o "${nic_parent}" = "auto" ]; then
interface=$( getnics-by-ip ip=0.0.0.0 )
else
interface="${nic_parent}"
fi
# still hard coded
mybridge="bridge0"
# if ! mybridge=$( get_my_device bridge ${interface} ); then
# err 1 "${MAGENTA}Error: Cant get_my_device for ${interface}: ${mybridge}${NORMAL}"
# fi
# mytap=$( get_my_tap ${mybridge} )
# [ $? -eq 1 ] && err 1 "${MAGENTA}Error: Cant get_my_tap by: ${GREEN}${mybridge}${NORMAL}"
echo "M $mybridge a $interface"
_res=$( /sbin/ifconfig ${mybridge} addm ${interface} >/dev/null 2>&1 )
# if [ "${nic_hwaddr}" != "0" ]; then
# unset mymac
# mymac=$( normalize_nic_hwaddr ${nic_hwaddr} )
# echo "export nic${_id}=\"-s ${pcislot}:0,virtio-net,${mytap},mac=${mymac}\" ;"
# else
# echo "export nic${_id}=\"-s ${pcislot}:0,virtio-net,${mytap}\" ;"
# fi
#
# echo "export mytap${_id}=\"${mytap}\";"
#
_id=$(( _id + 1 ))
done || err 1 "${MAGENTA}Error while create nic map${NORMAL}"
nic_args=""
return 0
}
# create new disk image for bhyve VMs
# -f fsfeat ( 1 or 0 ) - use of skip FS features
# -p full path
# -s size (eg: 1g)
# -t type (zvol, md)
# require: /usr/local/bin/cbsd as shell
# require: ${tools}
#
# if ! xen_create_dsk -p /tmp/olala.vhd -s 10g -f 0; then
# echo ";=("
# fi
xen_create_dsk()
{
local newdsk imgsize fsfeat imgtype
while getopts "f:p:s:t:" opt; do
case "${opt}" in
f) feat="${OPTARG}" ;;
p) newdsk="${OPTARG}" ;;
s) imgsize="${OPTARG}" ;;
t) imgtype="${OPTARG}" ;;
esac
shift $(($OPTIND - 1))
done
local _res
if [ "${fsfeat}" = "0" ]; then
zfsfeat=0
hammerfeat=0
fi
local lunname=$( /usr/bin/basename ${newdsk} ) # for ZFS vol
local datadir=$( /usr/bin/dirname ${newdsk} ) # for ZFS vol
local convval imgbytes blockcount
if [ -f "${newdsk}" ]; then
echo "Disk ${newdsk} already exist"
return 1
fi
# test for human
if is_number ${imgsize}; then
conv2bytes ${imgsize} || err 1 "${MAGENTA}conv2bytes from xen.subr error for: ${GREEN}${imgsize}${NORMAL}"
imgbytes="${convval}"
else
imgbytes=$imgsize
fi
if [ "${zfsfeat}" = "1" -a "${imgtype}" = "zvol" ]; then
readconf zfs.conf
. ${zfstool}
DATA=$( /sbin/zfs get -Ho value name ${jaildatadir} )
# ZPOOL=$( /sbin/zfs get -Ho value name ${jaildatadir} )
# if zfsroot $jname; then
# err 1 "$ZPOOL/$jname already in use"
# fi
# /sbin/zfs create ${zfs_create_flags} -o mountpoint=${JAILDIR} ${ZPOOL}/${jname}
/sbin/zfs create -V ${imgbytes} -o volmode=dev ${DATA}/bcbsd-${jname}-${lunname}
_res=$?
[ ${_res} -ne 0 ] && return ${_res}
[ ! -d "${datadir}" ] && /bin/mkdir -p ${datadir}
/bin/ln -sf /dev/zvol/${DATA}/bcbsd-${jname}-${lunname} ${newdsk}
elif [ "${hammerfeat}" = "1" ]; then
err 1 "Hammer not supported yet"
else
blockcount=$(( imgbytes / 1048576 ))
[ ! -d ${datadir} ] && /bin/mkdir -p ${datadir}
/usr/bin/touch "${newdsk}"
/bin/dd if=/dev/zero of="${newdsk}" bs=1m count=0 seek=${blockcount} 1> /dev/null 2>&1 || return 1
# /bin/dd if=/dev/random of="${newdsk}" bs=1m count=${blockcount}
_res=$?
[ ${_res} -ne 0 ] && return ${_res}
fi
media mode=register name="hdd-${lunname}" path="${newdsk}" type="hdd" jname="${jname}"
return 0
}
# detach and remove disk image from xen VMs
# jname must be set
# $1 dsk_path from sql table (eg: dsk1.vhd )
# require: /usr/local/bin/cbsd as shell
xen_remove_dsk()
{
local dsk="${1}"; shift
[ -z "${jname}" -o -z "${dsk}" ] && return 0
[ -r ${jailsysdir}/${jname}/local.sqlite ] && cbsdsql ${jailsysdir}/${jname}/local.sqlite DELETE FROM xendsk WHERE dsk_path=\"${dsk}\" AND jname=\"${jname}\"
if [ "${zfsfeat}" = "1" ]; then
readconf zfs.conf
. ${zfstool}
if is_getzvol ${data}/${dsk}; then
/sbin/zfs destroy ${is_zvol}
fi
elif [ "${hammerfeat}" = "1" ]; then
err 1 "Hammer not supported yet"
else
#date
fi
[ -f "${data}/${dsk}" -o -h "${data}/${dsk}" ] && /bin/rm -f "${data}/${dsk}"
return 0
}
# detach and remove NIC from xen VMs
# jname must be set
# $1 is id of nic in SQL table
# require: /usr/local/bin/cbsd as shell
xen_remove_nic()
{
local id="${1}"; shift
[ -z "${jname}" -o -z "${id}" ] && return 0
cbsdsql ${jailsysdir}/${jname}/local.sqlite DELETE FROM xennic WHERE id=\"${id}\" AND jname=\"${jname}\"
return 0
}
# export vncdisplay
compile_vnc_args()
{
local vm_port
local vnc_bind
local width height
vnc_args=
case "${vm_vnc_port}" in
0)
vm_port=$( get_next_rd_port )
vnc_args="--vrde on --vrdeport ${vm_port}"
vncdisplay=$(( ${vm_port} - 5900 ))
${ECHO} "${MAGENTA}VRDP is enabled. VNC port: ${GREEN}${vm_port}. ${MAGENTA}VNC pass: ${GREEN}cbsd${NORMAL}${NORMAL}"
;;
1)
vm_port=0
vnc_args="--vrde off"
;;
*)
vm_port=${vm_vnc_port}
vnc_args="--vrde on --vrdeport ${vm_port}"
;;
esac
echo "${vm_port}" > ${jailsysdir}/${jname}/vnc_port
if [ -n "${xen_vnc_tcp_bind}" ]; then
vnc_bind="${xen_vnc_tcp_bind}"
else
vnc_bind="${default_vnc_tcp_bind}"
fi
# add xen_vnc_tcp_ipconnect variables, used in
# vncviewer promt and exported hooks variables
if [ "${vnc_bind}" = "0.0.0.0" ]; then
xen_vnc_tcp_ipconnect="${nodeip}"
else
xen_vnc_tcp_ipconnect="${vnc_bind}"
fi
#if [ -n "${xen_vnc_resolution}" ]; then
# width=${xen_vnc_resolution%%x*}
# height=${xen_vnc_resolution##*x}
#else
# width="${default_vnc_width}"
# heigh="${default_vnc_height}"
#fi
${ECHO} "${MAGENTA}VRDP is enabled. VNC bind/port: ${GREEN}${xen_vnc_tcp_ipconnect}:${vm_port}${NORMAL}"
${ECHO} "${MAGENTA}For attach VM console, use: ${GREEN}vncviewer ${xen_vnc_tcp_ipconnect}:${vm_port}${NORMAL}"
${ECHO} "${MAGENTA}Resolution: ${GREEN}${width}x${height}.${NORMAL}"
[ -n "${vnc_password}" ] && ${ECHO} "${MAGENTA}VNC pass: ${GREEN}${vnc_password}${NORMAL}"
if [ "${vnc_bind}" = "0.0.0.0" ]; then
echo
${ECHO} "${MAGENTA}Warning!!! You are running system with open ${GREEN}VNC${MAGENTA} port to all world wich is not to safe${MAGENTA}"
${ECHO} "${MAGENTA}Please use IP filter or balancer with password to restrict ${GREEN}VNC${MAGENTA} port access${NORMAL}"
${ECHO} "${MAGENTA}Or change vnc_bind params to ${GREEN}127.0.0.1${MAGENTA} and reboot VM after maintenance work${NORMAL}"
echo
fi
return 0
}
###
fi