Skip to content

Commit 3e93f7d

Browse files
churcherschurchers
churchers
authored and
churchers
committed
Big tidy up
Simplify main script and move functions into independent libraries. Add a Makefile to install everything to the correct places. Add an initial man page.
1 parent 43a817b commit 3e93f7d

File tree

10 files changed

+1439
-770
lines changed

10 files changed

+1439
-770
lines changed

Makefile

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#
2+
# Makefile
3+
#
4+
5+
.include <bsd.own.mk>
6+
7+
PREFIX?= /usr/local
8+
MAN=
9+
BINOWN= root
10+
BINGRP= wheel
11+
BINMODE= 0500
12+
BINDIR=$(PREFIX)/sbin
13+
FILESDIR=$(PREFIX)/lib/vm-bhyve
14+
RCDIR=$(PREFIX)/etc/rc.d
15+
MANDIR=$(PREFIX)/man/man8
16+
MKDIR=mkdir
17+
18+
PROG= vm
19+
MAN= $(PROG).8
20+
21+
install:
22+
$(MKDIR) -p $(BINDIR)
23+
$(MKDIR) -p $(FILESDIR)
24+
$(INSTALL) -m $(BINMODE) $(PROG) $(BINDIR)/
25+
$(INSTALL) lib/* $(FILESDIR)/
26+
$(INSTALL) rc.d/* $(RCDIR)/
27+
rm -f $(MAN).gz
28+
gzip -k $(MAN)
29+
$(INSTALL) $(MAN).gz $(MANDIR)/
30+
rm -f $(MAN).gz
31+
32+
.include <bsd.prog.mk>

lib/vm-cmd

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/bin/sh
2+
#-------------------------------------------------------------------------+
3+
# Copyright (C) 2015 Matt Churchyard ([email protected])
4+
# All rights reserved
5+
#
6+
# Redistribution and use in source and binary forms, with or without
7+
# modification, are permitted providing that the following conditions
8+
# are met:
9+
# 1. Redistributions of source code must retain the above copyright
10+
# notice, this list of conditions and the following disclaimer.
11+
# 2. Redistributions in binary form must reproduce the above copyright
12+
# notice, this list of conditions and the following disclaimer in the
13+
# documentation and/or other materials provided with the distribution.
14+
#
15+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16+
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18+
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19+
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21+
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22+
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23+
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24+
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25+
# POSSIBILITY OF SUCH DAMAGE.
26+
27+
# cmd: vm ...
28+
__parse_cmd(){
29+
local _cmd="$1"
30+
shift
31+
32+
case "${_cmd}" in
33+
init)
34+
__setup
35+
__switch_init
36+
;;
37+
switch)
38+
__parse_switch_cmd "$@"
39+
;;
40+
create)
41+
__vm_create "$@"
42+
;;
43+
install)
44+
__vm_install "$@"
45+
;;
46+
start)
47+
__vm_start "$@"
48+
;;
49+
stop)
50+
__vm_stop "$@"
51+
;;
52+
startall)
53+
__vm_startall
54+
;;
55+
stopall)
56+
__vm_stopall
57+
;;
58+
console)
59+
__vm_console "$@"
60+
;;
61+
_run)
62+
__vm_run "$@"
63+
;;
64+
iso)
65+
__vm_iso "$@"
66+
;;
67+
*)
68+
__usage
69+
;;
70+
esac
71+
}
72+
73+
# cmd: vm switch ...
74+
__parse_switch_cmd(){
75+
local _cmd="$1"
76+
shift
77+
78+
case "${_cmd}" in
79+
create)
80+
__switch_create "$@"
81+
;;
82+
list)
83+
__switch_list
84+
;;
85+
destroy)
86+
__switch_remove "$@"
87+
;;
88+
add)
89+
__switch_add_member "$@"
90+
;;
91+
remove)
92+
__switch_remove_member "$@"
93+
;;
94+
vlan)
95+
__switch_vlan "$@"
96+
;;
97+
*)
98+
__usage
99+
;;
100+
esac
101+
}

lib/vm-common

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/bin/sh
2+
#-------------------------------------------------------------------------+
3+
# Copyright (C) 2015 Matt Churchyard ([email protected])
4+
# All rights reserved
5+
#
6+
# Redistribution and use in source and binary forms, with or without
7+
# modification, are permitted providing that the following conditions
8+
# are met:
9+
# 1. Redistributions of source code must retain the above copyright
10+
# notice, this list of conditions and the following disclaimer.
11+
# 2. Redistributions in binary form must reproduce the above copyright
12+
# notice, this list of conditions and the following disclaimer in the
13+
# documentation and/or other materials provided with the distribution.
14+
#
15+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16+
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18+
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19+
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21+
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22+
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23+
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24+
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25+
# POSSIBILITY OF SUCH DAMAGE.
26+
27+
# make sure we have the right environment
28+
__setup(){
29+
__load_module "vmm"
30+
__load_module "nmdm"
31+
__load_module "if_bridge"
32+
__load_module "if_tap"
33+
34+
sysctl net.link.tap.up_on_open=1 >/dev/null 2>&1
35+
}
36+
37+
# load a kernel module
38+
__load_module(){
39+
local _mod="$1"
40+
kldstat -n ${_mod} >/dev/null 2>&1
41+
if [ $? -ne 0 ]; then
42+
kldload ${_mod} >/dev/null 2>&1
43+
if [ $? -ne 0 ]; then
44+
__err "unable to load ${_mod}.ko!"
45+
exit 1
46+
fi
47+
fi
48+
}
49+
50+
# show usage
51+
__usage(){
52+
cat << EOT
53+
vm: Bhyve virtual machine managament v${VERSION}
54+
Usage: vm ...
55+
init
56+
switch list
57+
switch create <name>
58+
switch vlan <name> <vlan|0>
59+
switch add <name> <interface>
60+
switch remove <name> <interface>
61+
switch destroy <name>
62+
create [-t template] [-s size] <name>
63+
install <name> <iso>
64+
start <name>
65+
stop <name>
66+
console <name>
67+
startall
68+
stopall
69+
iso [url]
70+
EOT
71+
exit 1
72+
}
73+
74+
# error
75+
__err(){
76+
echo "${0}: ERROR: $1"
77+
exit 1
78+
}
79+
80+
# warn
81+
__warn(){
82+
echo "${0}: WARNING: $1"
83+
}

0 commit comments

Comments
 (0)