Skip to content

Commit

Permalink
Add a lot of fixes and style consistency changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
bsdlme committed May 27, 2016
1 parent a13b1f9 commit 22e0e05
Show file tree
Hide file tree
Showing 10 changed files with 378 additions and 331 deletions.
19 changes: 7 additions & 12 deletions iohyve
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
# iohyve v0.7.5 2016/04/14 "Tennessee Cherry Moonshine Edition"

# Set proper path thanks to iocage
PATH=${PATH}:/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin
PATH="${PATH}:/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin"

# Check to see if CPU is compatible with bhyve
if [ $( cat /var/run/dmesg.boot | grep 'POPCNT' ) ]; then
if [ -n "$( cat /var/run/dmesg.boot | grep 'CPU:' | grep 'Intel' )" ] && [ -z "$( cat /var/run/dmesg.boot | grep 'VT-x:' | grep 'UG' )" ]; then
bootmsgs="/var/run/dmesg.boot"
if [ $(grep POPCNT ${bootmsgs}) ]; then
if [ -n "$(grep 'CPU.*Intel' ${bootmsgs})" ] && [ -z "$(grep 'VT-x:.*UG' ${bootmsgs})" ]; then
echo "Your CPU does not seem to fully support bhyve. Missing UG feature on Intel CPU, please upgrade to 5600 series."
exit 1
fi
Expand All @@ -29,15 +30,9 @@ fi

. /etc/rc.subr

. "${LIB}/ioh-cmd"
. "${LIB}/ioh-setup"
. "${LIB}/ioh-zfs"
. "${LIB}/ioh-console"
. "${LIB}/ioh-iso"
. "${LIB}/ioh-firmware"
. "${LIB}/ioh-guest"
. "${LIB}/ioh-disk"
. "${LIB}/ioh-tap"
for lib in ${LIB}/ioh-*; do
. "${lib}"
done

# Show version
__version() {
Expand Down
56 changes: 44 additions & 12 deletions lib/ioh-cmd
Original file line number Diff line number Diff line change
@@ -1,21 +1,53 @@
#!/bin/sh

# Check whether a given command requires root
__root_req_cmd () {
case "$1" in
setup|fetchiso|fetch|cpiso|renameiso|rmiso|fetchfw|cpfw|renamefw|rmfw|create|install|load\
|boot|start|uefi|stop|forcekill|scram|destroy|rename|delete|set|rmpci|fix-bargs|add|remove\
|resize|disks|snap|roll|clone|export|console|conreset)
echo "1"
;;
*)
echo "0"
;;
esac
__root_req_cmd() {
case "$1" in
add|\
boot|\
clone|\
conreset|\
console|\
cpfw|\
cpiso|\
create|\
delete|\
destroy|\
disks|\
export|\
fetch|\
fetchfw|\
fetchiso|\
fix-bargs|\
forcekill|\
install|\
load|\
remove|\
rename|\
renamefw|\
renameiso|\
resize|\
rmfw|\
rmiso|\
rmpci|\
roll|\
scram|\
set|\
setup|\
snap|\
start|\
stop|\
uefi)
echo "1"
;;
*)
echo "0"
;;
esac
}

# Process command line
__parse_cmd () {
__parse_cmd() {
while [ $# -gt 0 ] ; do
case "$1" in
version)
Expand Down
11 changes: 6 additions & 5 deletions lib/ioh-console
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

# List consoles in use
__conlist() {
local pool="$(zfs list -H | grep iohyve | cut -d '/' -f 1 | head -n1)"
local guestlist="$(zfs list -H | grep iohyve | grep -v ISO | grep -v disk | grep -v Firmware | sed 1d | cut -f1 | cut -d '/' -f3)"
local pool="$(zfs list -H | grep iohyve | cut -d '/' -f 1 | head -n 1)"
local guestlist="$(zfs list -H | grep iohyve | grep -v -e ISO -e disk -e Firmware | sed 1d | cut -f 1 | cut -d '/' -f 3)"
(
printf 'Guest\tConsole\n'
for guest in $guestlist; do
Expand All @@ -15,14 +15,14 @@ __conlist() {

# Run console
__console() {
local name="$2"
if [ -z $name ]; then
local name=$2
if [ -z "$name" ]; then
printf "missing argument\nusage:\n"
printf "\tconsole <name>\n"
exit 1
fi

local pool="$(zfs list -H -t volume | grep iohyve/$name | grep disk0 | cut -d '/' -f 1-3 | head -n1)"
local pool="$(zfs list -H -t volume | grep iohyve/$name.*disk0 | cut -d '/' -f 1-3 | head -n 1)"
local con="$(zfs get -H -o value iohyve:con $pool)"
echo "Starting console on $name..."
echo "~~. to escape console [uses cu(1) for console]"
Expand All @@ -31,5 +31,6 @@ __console() {

# Reset all consoles. This kills the cu(1)
__conreset() {
# XXX This also kills cu(1) processes not started by iohyve.
killall cu
}
72 changes: 36 additions & 36 deletions lib/ioh-disk
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

# Add a new disk to guest
__add() {
local name="$2"
local size="$3"
local name=$2
local size=$3

if [ -z $size ]; then
if [ -z "$size" ]; then
printf "missing argument\nusage:\n"
printf "\tadd <name> <size>\n"
exit 1
Expand All @@ -15,59 +15,59 @@ __add() {
# optionally allow the pool for the new disk to be specified. If not set, its set to $pool
local newpool="${4-$pool}"
# Find the last disk number and increment one
local lastdisk="$(zfs list -H | grep iohyve/$name | grep disk | cut -d '/' -f4 | cut -f1 | \
sort -V | cut -c5- | tail -n1)"
local newdisk="$(expr $lastdisk + 1)"
local lastdisk="$(zfs list -H | grep iohyve/$name.*disk | cut -d '/' -f 4 | cut -f 1 |
sort -V | cut -c 5- | tail -n 1)"
local newdisk="$((lastdisk + 1))"
echo "Creating new zvol for $name..."
zfs create $newpool/iohyve/$name 2> /dev/null
zfs create -V $size -o volmode=dev $newpool/iohyve/$name/disk$newdisk
# Find the last pcidev and increment by one
local lastpci="$(zfs get -H all | grep iohyve/$name | grep pcidev | cut -f2 | \
cut -d ':' -f3 | sort -V | tail -n1)"
if [ -z $lastpci ]; then
local newpci='1'
local lastpci="$(zfs get -H all | grep iohyve/$name.*pcidev | cut -f 2 |
cut -d ':' -f 3 | sort -V | tail -n 1)"
if [ -z "$lastpci" ]; then
local newpci=1
else
local newpci="$(expr $lastpci + 1)"
local newpci="$((lastpci + 1))"
fi
zfs set iohyve:pcidev:$newpci=ahci-hd,/dev/zvol/$newpool/iohyve/$name/disk$newdisk $pool/iohyve/$name
}

# Remove disk from guest
__remove() {
local flagone="$2"
local flagtwo="$3"
local flagthree="$4"
if [ $flagone = "-f" ]; then
if [ -z $flagthree ]; then
local flagone=$2
local flagtwo=$3
local flagthree=$4
if [ "$flagone" = "-f" ]; then
if [ -z "$flagthree" ]; then
printf "missing argument\nusage:\n"
printf "\tremove [-f] <name> <diskN>\n"
exit 1
fi
echo "Removing $flagthree from $flagtwo"
local pool="$(zfs list -H -o name | grep $flagtwo | grep $flagthree | cut -d '/' -f 1)"
local pciprop="$(zfs get -H all $pool/iohyve/$flagtwo | grep pcidev | grep $flagthree | cut -f2 )"
local pool="$(zfs list -H -o name | grep $flagtwo.*$flagthree | cut -d '/' -f 1)"
local pciprop="$(zfs get -H all $pool/iohyve/$flagtwo | grep pcidev.*$flagthree | cut -f 2)"
# Make sure it's a valid pcidev property as to not shoot foot
if [ -z $pciprop ]; then
if [ -z "$pciprop" ]; then
echo "Not a valid PCIDEV property"
else
zfs inherit -r $pciprop $pool/iohyve/$flagtwo
zfs inherit -r $pciprop $pool/iohyve/$flagtwo
zfs destroy $pool/iohyve/$flagtwo/$flagthree
fi
else
if [ -z $flagtwo ]; then
if [ -z "$flagtwo" ]; then
printf "missing argument\nusage:\n"
printf "\tremove [-f] <name> <diskN>\n"
exit 1
fi
local pool="$(zfs list -H -o name | grep $flagone | grep $flagtwo | cut -d '/' -f 1)"
local pciprop="$(zfs get -H all $pool/iohyve/$flagone | grep pcidev | grep $flagtwo | cut -f2 )"
local pool="$(zfs list -H -o name | grep $flagone.*$flagtwo | cut -d '/' -f 1)"
local pciprop="$(zfs get -H all $pool/iohyve/$flagone | grep pcidev.*$flagtwo | cut -f 2)"
# Make sure it's a valid pcidev property as to not shoot foot
if [ -z $pciprop ]; then
if [ -z "$pciprop" ]; then
echo "Not a valid PCIDEV property"
else
read -p "Are you sure you want to remove $flagtwo from $flagone [Y/N]? " an </dev/tty
case "$an" in
y|Y) zfs inherit -r $pciprop $pool/iohyve/$flagone
y|Y) zfs inherit -r $pciprop $pool/iohyve/$flagone
zfs destroy $pool/iohyve/$flagone/$flagtwo
;;
*) echo "Not removed..."
Expand All @@ -79,21 +79,21 @@ __remove() {

# Resize a disk
__resize(){
local name="$2"
local disk="$3"
local size="$4"
if [ -z $size ]; then
local name=$2
local disk=$3
local size=$4
if [ -z "$size" ]; then
printf "missing argument\nusage:\n"
printf "\tresize <name> <diskN> <size>\n"
exit 1
fi
local pool="$(zfs list -H -o name | grep iohyve/$name | grep $disk | cut -d '/' -f 1)"
local pool="$(zfs list -H -o name | grep iohyve/$name.*$disk | cut -d '/' -f 1)"
# Check if guest exists
echo "Resizing $disk to $size"
if [ -d /iohyve/$pool/$name ] || [ -d /iohyve/$name ]; then
# Check to make sure guest isn't running
local running=$(pgrep -fx "bhyve: ioh-$name")
if [ -z $running ]; then
if [ -z "$running" ]; then
zfs set volsize=$size $pool/iohyve/$name/$disk
zfs set iohyve:size=$size $pool/iohyve/$name
else
Expand All @@ -106,20 +106,20 @@ __resize(){

# List disks for a guest
__disks() {
local name="$2"
if [ -z $name ]; then
local name=$2
if [ -z "$name" ]; then
printf "missing argument\nusage:\n"
printf "\tdisks <name>\n"
exit 1
fi
local pool="$(zfs list -H | grep iohyve | cut -d '/' -f 1 | head -n1)"
if [ -z $name ]; then
local pool="$(zfs list -H | grep iohyve | cut -d '/' -f 1 | head -n 1)"
if [ -z "$name" ]; then
echo "You must enter a guest name."
elif [ -d /iohyve/$pool/$name ] || [ -d /iohyve/$name ]; then
(
echo "Listing disks for $name..."
printf "diskN\tSize\n"
zfs list -H -o name,volsize | grep -E "iohyve.*$name.*disk" | \
zfs list -H -o name,volsize | grep -E iohyve.*$name.*disk |
cut -d '/' -f4
) | column -t
else
Expand Down
32 changes: 16 additions & 16 deletions lib/ioh-firmware
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,45 @@
# List Firmware
__fwlist() {
echo "Listing Firmware..."
zfs list -H | grep iohyve/Firmware | cut -f1 | cut -d '/' -f 4 | sed 1d
zfs list -H | grep iohyve/Firmware | cut -f 1 | cut -d '/' -f 4 | sed 1d
}

# Fetch Firmware
__fetchfw() {
local url="$2"
if [ -z $url ]; then
local url=$2
if [ -z "$url" ]; then
printf "missing argument\nusage:\n"
printf "\tfetchfw <URL>\n"
exit 1
fi
local pool="$(zfs list -H | grep iohyve | cut -d '/' -f 1 | head -n1)"
local name="$(basename $2)"
local pool="$(zfs list -H | grep iohyve | cut -d '/' -f 1 | head -n 1)"
local name="${2##*/}"
echo "Fetching $url..."
zfs create $pool/iohyve/Firmware/$name
fetch $url -o /iohyve/Firmware/$name
}

# Copy Firmware from local machine
__cpfw() {
local loc="$2"
if [ -z $loc ]; then
local loc=$2
if [ -z "$loc" ]; then
printf "missing argument\nusage:\n"
printf "\tcpfw <path>\n"
exit 1
fi
local pool="$(zfs list -H | grep iohyve | cut -d '/' -f 1 | head -n1)"
local name="$(basename $loc)"
local pool="$(zfs list -H | grep iohyve | cut -d '/' -f 1 | head -n 1)"
local name="${loc##*/}"
echo "Copying $name from $loc..."
zfs create $pool/iohyve/Firmware/$name
cp $loc /iohyve/Firmware/$name
}

# Rename Firmware
__renamefw() {
local pool="$(zfs list -H | grep iohyve | cut -d '/' -f 1 | head -n1)"
local fw="$2"
local name="$3"
if [ -z $name ]; then
local pool="$(zfs list -H | grep iohyve | cut -d '/' -f 1 | head -n 1)"
local fw=$2
local name=$3
if [ -z "$name" ]; then
printf "missing argument\nusage:\n"
printf "\trenamefw <firmware> <newname>\n"
exit 1
Expand All @@ -53,13 +53,13 @@ __renamefw() {

# Delete Firmware
__deletefw() {
local name="$2"
if [ -z $name ]; then
local name=$2
if [ -z "$name" ]; then
printf "missing argument\nusage:\n"
printf "\trmfw <firmware> <newname>\n"
exit 1
fi
local pool="$(zfs list -H | grep iohyve | cut -d '/' -f 1 | head -n1)"
local pool="$(zfs list -H | grep iohyve | cut -d '/' -f 1 | head -n 1)"
echo "Deleting $name..."
zfs destroy -rR $pool/iohyve/Firmware/$name
}
Loading

0 comments on commit 22e0e05

Please sign in to comment.