forked from cbsd/cbsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbhyve.subr
173 lines (139 loc) · 4.33 KB
/
bhyve.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
#v10.0.4
# generic function for bhyve
# required:
# strings.subr
#
# test environment for bhyveing
# init tmuxcmd
init_bhyve()
{
local _required_kmods="vmm if_tap if_bridge" _i _res
# /usr/bin/grep -qw POPCNT /var/run/dmesg.boot || err 1 "${MAGENTA}Your CPU does not appear to support ${GREEN}EPT${NORMAL}"
res=$( /usr/bin/su -m cbsd -c ${miscdir}/popcnttest > /dev/null 2>&1 )
[ $? -ne 0 ] && err 1 "${MAGENTA}Your CPU does not appear to support ${GREEN}EPT${NORMAL}"
for _i in ${_required_kmods}; do
if ! kldstat -qm ${_i} >/dev/null 2>&1; then
${ECHO} "${MAGENTA}No kldloaded module: ${GREEN}${_i}${NORMAL}"
err 1 "${MAGENTA}Put ${GREEN}kld_list=\"if_bridge if_tap vmm\"${MAGENTA} into your ${GREEN}/etc/rc.conf${MAGENTA} and execute: ${GREEN}service kld restart${MAGENTA} or load modules via loader.conf and try again.${NORMAL}"
fi
done
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}"
}
# autoincement for $bhyve_pci_index variable which symbolizes pci id bus
next_pci_id()
{
[ -z "${bhyve_pci_index}" ] && bhyve_pci_index="0"
bhyve_pci_index=$(( bhyve_pci_index + 1 ))
[ ${bhyve_pci_index} -gt 31 ] && ${ECHO} "${MAGENTA}No free available bhyve_pci_index, 31 is max${NORMAL}" && return 1
return 0
}
# 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=" "
eval $( cbsdsql local SELECT dsk_controller,dsk_path,dsk_slot FROM bhyvedsk WHERE jname=\"${jname}\" AND dsk_type=\"vhd\" |while read dsk_controller dsk_path dsk_slot; do
if ! next_pci_id; then
# no free pci bus
exit 0
fi
if [ ${dsk_slot} -eq 0 ]; then
pcislot=${bhyve_pci_index}
else
pcislot=${dsk_slot}
fi
echo "export dsk${dsk_id}=\"-s ${pcislot}:0,${dsk_controller},${data}/${dsk_path}\" ;"
dsk_id=$(( dsk_id + 1 ))
done ) || err 1 "${MAGENTA}Error while create disk map${NORMAL}"
dsk_args=""
for i in $(seq 0 31); do
eval T="\$dsk$i"
[ -z "${T}" ] && break
# make custom argument for bhyve and add count to bhive_pci_index cause increment in while loop we lose
if ! next_pci_id; then
# no free pci bus
fi
dsk_args="${dsk_args} ${T}"
done
[ -z "${dsk_args}" ] && return 1
return 0
}
# export arguments for bhyve hostbridge_args
compile_hostbridge_args()
{
# This is usually configured at slot 0
hostbridge_args="-s 0:0,${vm_hostbridge}"
}
# export arguments for bhyve lpc_args
compile_lpc_args()
{
if ! next_pci_id; then
return 1
fi
lpc_args="-s ${bhyve_pci_index}:0,lpc"
}
# export arguments for bhyve nic_args
compile_nic_args()
{
if ! next_pci_id; then
return 1
fi
nic_args="-s ${bhyve_pci_index}:0,virtio-net,${mytap}"
}
# export arguments for bhyve cd_args
# $iso_img must be present
compile_cd_args()
{
if ! next_pci_id; then
return 1
fi
[ -z "${iso_img}" -o ! -f "${iso_img}" ] && return 1
cd_args="-s ${bhyve_pci_index}:0,ahci-cd,${iso_img}"
}
# create new disk image for bhyve VMs
# $1 full path
# $2 size (eg: 1g)
# require: /usr/local/bin/cbsd as shell
# require: ${tools}
#
# if ! bhyve_create_dsk /tmp/olala.vhd 10g; then
# echo ";=("
# fi
bhyve_create_dsk()
{
local newdsk="${1}"; shift
local freesize="${1}"; shift
local convval imgbytes blockcount
# test for human
if is_number ${freesize}; then
conv2bytes ${freesize} || err 1 "${MAGENTA}conv2bytes from bhyve.subr error for: ${GREEN}${freesize}${NORMAL}"
imgbytes="${convval}"
else
imgbytes=$freesize
fi
blockcount=$(( imgbytes / 1048576 ))
touch "${newdsk}"
dd if="/dev/zero" of="${newdsk}" bs=1m count=0 seek=${blockcount} 1> /dev/null 2>&1 || return 1
return 0
}
# detach and remove disk image from bhyve VMs
# jname must be set
# $1 dsk_path from sql table (eg: dsk1.vhd )
# require: /usr/local/bin/cbsd as shell
bhyve_remove_dsk()
{
local dsk="${1}"; shift
[ -z "${jname}" -o -z "${dsk}" ] && return 0
cbsdsql local DELETE FROM bhyvedsk WHERE dsk_path=\"${dsk}\" AND jname=\"${jname}\"
[ -f "${data}/${dsk}" ] && rm -f "${data}/${dsk}"
return 0
}