Skip to content

Commit

Permalink
Add input checks to functions
Browse files Browse the repository at this point in the history
Because missing arguments could led to undefined behaviour or
partly created machines, input checks were added for all methods which
require input parameters.
  • Loading branch information
moogle19 committed Apr 30, 2016
1 parent 2f97c0b commit b5c997b
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 4 deletions.
6 changes: 6 additions & 0 deletions lib/ioh-console
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ __conlist() {
# Run console
__console() {
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 con="$(zfs get -H -o value iohyve:con $pool)"
echo "Starting console on $name..."
Expand Down
27 changes: 27 additions & 0 deletions lib/ioh-disk
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
__add() {
local name="$2"
local size="$3"

if [ -z $size ]; then
printf "missing argument\nusage:\n"
printf "\tadd <name> <size>\n"
exit 1
fi

local pool="$(zfs list -H -t volume | grep iohyve/$name | cut -d '/' -f 1 | head -n1)"
# optionally allow the pool for the new disk to be specified. If not set, its set to $pool
local newpool="${4-$pool}"
Expand Down Expand Up @@ -31,6 +38,11 @@ __remove() {
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 )"
Expand All @@ -42,6 +54,11 @@ __remove() {
zfs destroy $pool/iohyve/$flagtwo/$flagthree
fi
else
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 )"
# Make sure it's a valid pcidev property as to not shoot foot
Expand All @@ -65,6 +82,11 @@ __resize(){
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)"
# Check if guest exists
echo "Resizing $disk to $size"
Expand All @@ -85,6 +107,11 @@ __resize(){
# List disks for a guest
__disks() {
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
echo "You must enter a guest name."
Expand Down
20 changes: 20 additions & 0 deletions lib/ioh-firmware
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ __fwlist() {
# Fetch Firmware
__fetchfw() {
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)"
echo "Fetching $url..."
Expand All @@ -19,6 +24,11 @@ __fetchfw() {
# Copy Firmware from local machine
__cpfw() {
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)"
echo "Copying $name from $loc..."
Expand All @@ -31,6 +41,11 @@ __renamefw() {
local pool="$(zfs list -H | grep iohyve | cut -d '/' -f 1 | head -n1)"
local fw="$2"
local name="$3"
if [ -z $name ]; then
printf "missing argument\nusage:\n"
printf "\trenamefw <firmware> <newname>\n"
exit 1
fi
echo "Renaming Firmware $2 to $3..."
mv /iohyve/Firmware/$fw/$fw /iohyve/Firmware/$fw/$name
zfs rename $pool/iohyve/Firmware/$fw $pool/iohyve/Firmware/$name
Expand All @@ -39,6 +54,11 @@ __renamefw() {
# Delete Firmware
__deletefw() {
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)"
echo "Deleting $name..."
zfs destroy -rR $pool/iohyve/Firmware/$name
Expand Down
64 changes: 60 additions & 4 deletions lib/ioh-guest
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ __create() {
local name="$2"
local pool="${4-$(zfs list -H | grep iohyve | cut -d '/' -f 1 | head -n1)}"
local size="$3"
if [ -z $size ]; then
printf "missing argument\nusage:\n"
printf "\tcreate <name> <size> [pool]\n"
exit 1
fi
local description="$(date)"
local guestlist="$(zfs list -H -o name -t volume | grep iohyve | cut -d'/' -f1-3)"
listtaps(){
Expand Down Expand Up @@ -197,6 +202,11 @@ __create() {
__install() {
local name="$2"
local iso="$3"
if [ -z $iso ]; then
printf "missing argument\nusage:\n"
printf "\tinstall <name> <ISO>\n"
exit 1
fi
local pool="$(zfs list -H -t volume -o name | grep iohyve/$name | grep disk0 | cut -d '/' -f1)"
local dataset="$(zfs list -H -t volume | grep iohyve/$name | cut -d '/' -f 1-3 | head -n1)"
# Check if guest exists
Expand Down Expand Up @@ -230,6 +240,11 @@ __install() {
__load() {
local name="$1"
local media="$2"
if [ -z $media ]; then
printf "missing argument\nusage:\n"
printf "\tload <name> <path/to/bootdisk>\n"
exit 1
fi
local disk="${3-$(zfs list -H -t volume -o name | grep iohyve/$name | grep disk0 | head -n1)}"
local dataset="$(zfs list -H -t volume -o name | grep iohyve/$name | grep disk0 | cut -d '/' -f 1-3 | head -n1)"
local ram="$(zfs get -H -o value iohyve:ram $dataset)"
Expand Down Expand Up @@ -348,6 +363,11 @@ __load() {
# Boot guest
__boot() {
local name="$1"
if [ -z $name ]; then
printf "missing argument\nusage:\n"
printf "\tboot <name> [runmode] [pcidevices]\n"
exit 1
fi
# runmode (runonce/persist)
# 0 = once
# 1 = persist regular (stop if guest is powering off)
Expand Down Expand Up @@ -431,6 +451,11 @@ __prepare_guest() {
# Start guest (combine load and boot)
__start() {
local name="$2"
if [ -z $name ]; then
printf "missing argument\nusage:\n"
printf "\tstart <name> [-s | -a]\n"
exit 1
fi
local flag="$3"
local pci=""
local runmode="1"
Expand Down Expand Up @@ -471,6 +496,11 @@ __start() {
__uefi() {
local name="$2"
local media="$3"
if [ -z $media ]; then
printf "missing argument\nusage:\n"
printf "\tuefi <name> <ISO>\n"
exit 1
fi
local dataset="$(zfs list -H -t volume | grep iohyve/$name | cut -d '/' -f 1-3 | head -n1)"
local ram="$(zfs get -H -o value iohyve:ram $dataset)"
local con="$(zfs get -H -o value iohyve:con $dataset)"
Expand All @@ -491,10 +521,6 @@ __uefi() {
if [ $fw = '-' ]; then
echo "You must set a firmware file property to use UEFI..."
fi
if [ -z $media ]; then
echo "You must enter at least a zero byte ISO for some OSs..."
echo "EX: iohyve uefi winguest null.iso"
fi
# Check if guest exists
if [ -d /iohyve/$pool/$name ] || [ -d /iohyve/$name ]; then
# Check to make sure guest isn't running
Expand All @@ -521,6 +547,11 @@ __uefi() {
# Gracefully stop a guest
__stop() {
local name="$2"
if [ -z $name ]; then
printf "missing argument\nusage:\n"
printf "\tstop <name>\n"
exit 1
fi
local pool="$(zfs list -H | grep iohyve | cut -d '/' -f 1 | head -n1)"
local pid=$(pgrep -fx "bhyve: ioh-$name")
echo "Stopping $name..."
Expand All @@ -534,6 +565,11 @@ __stop() {
# THIS WILL KILL EVERYTHING MATCHING $NAME
__forcekill() {
local name="$2"
if [ -z $name ]; then
printf "missing argument\nusage:\n"
printf "\tforcekill <name>\n"
exit 1
fi
local pids="$(pgrep -f $name)"
for apid in "$pids"; do
kill -9 $apid
Expand All @@ -554,6 +590,11 @@ __scram() {
# Destroy guest
__destroy() {
local name="$2"
if [ -z $name ]; then
printf "missing argument\nusage:\n"
printf "\tdestroy <name>\n"
exit 1
fi
local pool="$(zfs list -H | grep iohyve | cut -d '/' -f 1 | head -n1)"
echo "Destroying $name..."
# zfs set iohyve:persist=0 $pool/iohyve/$name
Expand All @@ -565,6 +606,11 @@ __destroy() {
__rename() {
local name="$2"
local newname="$3"
if [ -z $newname ]; then
printf "missing argument\nusage:\n"
printf "\trename <name> <newname>\n"
exit 1
fi
local pool="$(zfs list -H -t volume | grep iohyve/$name | cut -d '/' -f 1 | head -n1)"
echo "Renaming $name to $newname..."
zfs rename -f $pool/iohyve/$name $pool/iohyve/$newname
Expand All @@ -576,6 +622,11 @@ __delete() {
local flagone="$2"
local flagtwo="$3"
if [ $flagone = "-f" ]; then
if [ -z $flagtwo ]; then
printf "missing argument\nusage:\n"
printf "\tdelete [-f] <name>\n"
exit 1
fi
local target_dataset="$(zfs list -H -t filesystem -o name | grep iohyve | grep $flagtwo | head -n1)"
echo ""
echo "[WARNING] Forcing permanent deletion of $flagtwo"
Expand All @@ -586,6 +637,11 @@ __delete() {
echo "Deleting $flagtwo at $target_dataset..."
zfs destroy -rR $target_dataset
else
if [ -z $flagone ]; then
printf "missing argument\nusage:\n"
printf "\tdelete [-f] <name>\n"
exit 1
fi
local target_dataset="$(zfs list -H -t filesystem -o name | grep iohyve | grep $flagone | head -n1)"
echo ""
echo "[WARNING] Are you sure you want to permanently delete $flagone and all child datasets?"
Expand Down
20 changes: 20 additions & 0 deletions lib/ioh-iso
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ __isolist() {
# Fetch ISO
__fetchiso() {
local url="$2"
if [ -z $url ]; then
printf "missing argument\nusage:\n"
printf "\tfetchiso <URL>\n"
exit 1
fi
local pool="$(zfs list -H | grep iohyve | cut -d '/' -f 1 | head -n1)"
local name="$(basename $2)"
echo "Fetching $url..."
Expand All @@ -20,6 +25,11 @@ __fetchiso() {
# Copy ISO from local machine
__cpiso() {
local loc="$2"
if [ -z $loc ]; then
printf "missing argument\nusage:\n"
printf "\tcpiso <path>\n"
exit 1
fi
local pool="$(zfs list -H | grep iohyve | cut -d '/' -f 1 | head -n1)"
local name="$(basename $loc)"
echo "Copying $name from $loc..."
Expand All @@ -32,6 +42,11 @@ __renameiso() {
local pool="$(zfs list -H | grep iohyve | cut -d '/' -f 1 | head -n1)"
local iso="$2"
local name="$3"
if [ -z $name ]; then
printf "missing argument\nusage:\n"
printf "\trenameiso <ISO> <newname>\n"
exit 1
fi
echo "Renaming ISO $2 to $3..."
mv /iohyve/ISO/$iso/$iso /iohyve/ISO/$iso/$name
zfs rename $pool/iohyve/ISO/$iso $pool/iohyve/ISO/$name
Expand All @@ -40,6 +55,11 @@ __renameiso() {
# Delete ISO
__deleteiso() {
local name="$2"
if [ -z $name ]; then
printf "missing argument\nusage:\n"
printf "\trmiso <name>\n"
exit 1
fi
local pool="$(zfs list -H | grep iohyve | cut -d '/' -f 1 | head -n1)"
echo "Deleting $name..."
zfs destroy -rR $pool/iohyve/ISO/$name
Expand Down
Loading

0 comments on commit b5c997b

Please sign in to comment.