-
Notifications
You must be signed in to change notification settings - Fork 1
/
bvmctl
executable file
·247 lines (175 loc) · 4.77 KB
/
bvmctl
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
#!/bin/sh
set -e -u
BVM_PATH=$(dirname $(realpath $0))
DEFAULT_NETWORK=bridge0
DEFAULT_CORES=1
DEFAULT_MEMORY=1024
DEFAULT_OSTYPE=freebsd
error()
{
echo "bvmctl: $@"
exit 1
}
usage()
{
cat <<EOF
Usage: bvmctl [ACTION] [OPTION...]
Actions:
activate [FS] activate ZFS filesystem for bvm management
list list virtual machines
create [VM] create virtual machine
destroy [VM] destroy virtual machine
set [VM] [KEY] [VALUE] set property
disk [VM] [SIZE] add disk to virtual machine (size in GBs)
svinstall [VM] [SVDIR] install runit service in SVDIR
exists [VM] check if virtual machine exists
Properties:
network list of network bridges (bridge0,bridge1/00:00:00:00:00:00,...)
cores number of assigned cores
memory assigned memory in MBs
ostype freebsd | centos7 | debian7 | debian8 | debian9
EOF
}
get_vm_root_fs()
{
zfs get -H -o name -s local bvm:active | head -n 1
}
check_property()
{
[ "$1" = "network" ] && return 0
[ "$1" = "cores" ] && return 0
[ "$1" = "memory" ] && return 0
[ "$1" = "ostype" ] && return 0
error "invalid property $1."
}
perform_activate()
{
local _vm_root_fs=$(get_vm_root_fs)
if [ -n "${_vm_root_fs}" ]; then
error "file system already active (${_vm_root_fs})."
return 1
fi
_vm_root_fs="$1"
zfs create -o canmount=off "${_vm_root_fs}"
zfs set bvm:active=on "${_vm_root_fs}"
}
perform_set()
{
local _vm_name="$1"
local _property="$2"
local _value="$3"
check_property ${_property}
if [ $(get_vm_root_fs | wc -l) -ne 1 ]; then
error "filesystem not properly configured."
fi
local _vm_root_fs=$(get_vm_root_fs)
local _vm_fs="${_vm_root_fs}/${_vm_name}"
zfs set bvm:${_property}=${_value} ${_vm_fs}
}
perform_get()
{
local _vm_name="$1"
local _property="$2"
check_property ${_property}
if [ $(get_vm_root_fs | wc -l) -ne 1 ]; then
error "filesystem not properly configured."
fi
local _vm_root_fs=$(get_vm_root_fs)
local _vm_fs="${_vm_root_fs}/${_vm_name}"
zfs get bvm:${_property} ${_vm_fs}
}
perform_list()
{
if [ $(get_vm_root_fs | wc -l) -ne 1 ]; then
error "filesystem not properly configured."
fi
local _vm_root_fs=$(get_vm_root_fs)
zfs list -d 1 -H -o name ${_vm_root_fs} | sed -e "s@${_vm_root_fs}/@@" | grep -v "/"
}
perform_create()
{
local _vm_name="$1"
if [ $(get_vm_root_fs | wc -l) -ne 1 ]; then
error "filesystem not properly configured."
fi
local _vm_root_fs=$(get_vm_root_fs)
local _vm_fs="${_vm_root_fs}/${_vm_name}"
zfs create -o canmount=off ${_vm_root_fs}/${_vm_name}
zfs set bvm:network=${DEFAULT_NETWORK} ${_vm_fs}
zfs set bvm:cores=${DEFAULT_CORES} ${_vm_fs}
zfs set bvm:memory=${DEFAULT_MEMORY} ${_vm_fs}
zfs set bvm:ostype=${DEFAULT_OSTYPE} ${_vm_fs}
}
perform_destroy()
{
local _vm_name="$1"
if [ $(get_vm_root_fs | wc -l) -ne 1 ]; then
error "filesystem not properly configured."
fi
local _vm_root_fs=$(get_vm_root_fs)
local _vm_fs="${_vm_root_fs}/${_vm_name}"
zfs destroy -r ${_vm_fs} > /dev/null 2>&1
}
perform_disk()
{
local _vm_name="$1"
local _vol_size="$2"
if [ $(get_vm_root_fs | wc -l) -ne 1 ]; then
error "filesystem not properly configured."
fi
local _vm_root_fs=$(get_vm_root_fs)
local _vm_fs="${_vm_root_fs}/${_vm_name}"
[ -n "${_vol_size}" ] || error "invalid volume size."
local _index=0
while true; do
if [ -e /dev/zvol/${_vm_fs}/disk${_index} ]; then
_index=$((${_index} + 1))
else
break
fi
done
zfs create -b 16K -o volmode=dev -V ${_vol_size}G -o compression=lz4 ${_vm_fs}/disk${_index}
}
perform_svinstall()
{
local _vm_name="$1"
local _svdir="$2"
local _service=${_svdir}/${_vm_name}
mkdir -p ${_service}
cat << EOF > ${_service}/run
#!/bin/sh
exec 2>&1
exec ${BVM_PATH}/bvm exec -n \$(basename \$(pwd))
EOF
chmod 755 ${_service}/run
mkdir -p ${_service}/log
cat << EOF > ${_service}/log/run
#!/bin/sh
mkdir -p main
exec chpst svlogd -tt ./main
EOF
chmod 755 ${_service}/log/run
cat << EOF > ${_service}/finish
#!/bin/sh
exec 2>&1
exec ${BVM_PATH}/bvm finish -n \$(basename \$(pwd))
EOF
chmod 755 ${_service}/finish
}
perform_exists()
{
local _vm_name="$1"
if [ $(get_vm_root_fs | wc -l) -ne 1 ]; then
error "filesystem not properly configured."
fi
local _vm_root_fs=$(get_vm_root_fs)
local _vm_fs="${_vm_root_fs}/${_vm_name}"
zfs get -H -o name name ${_vm_fs} > /dev/null 2>&1
}
if [ $# -eq 0 ]; then
usage
exit 1
fi
action="$1"
shift
perform_${action} "$@"