Skip to content

Instantly share code, notes, and snippets.

@pr1ntf
Last active September 12, 2023 00:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pr1ntf/4109b59d83800f5c25ab to your computer and use it in GitHub Desktop.
Save pr1ntf/4109b59d83800f5c25ab to your computer and use it in GitHub Desktop.
iocage/bhyve notepad
# Create guest
# iohyve create $name $size $con
__create() {
local name="$2"
local pool="$(zfs list | grep iohyve | head -n1 | cut -d '/' -f 1)"
local size="$3"
local con="$4"
echo "Creating $name..."
# create dataset in case there needs to be files to store in the future. (grub2-bhyve)
zfs create $pool/iohyve/$name
# create zfs volume (note -o volmode=dev os required for ahci-hd slot emulation)
zfs create -V $size -o volmode=dev $pool/iohyve/$name/${name}-0.img
zfs set iohyve:name=$name $pool/iohyve/$name
# disk size
zfs set iohyve:size=$size $pool/iohyve/$name
# guest ram
zfs set iohyve:ram=256M $pool/iohyve/$name
# number of cpu cores
zfs set iohyve:cpu=1 $pool/iohyve/$name
# /dev/tapX interface used for virtio-net networking (need for vnet, too)
zfs set iohyve:tap=tap0 $pool/iohyve/$name
# the /dev/nmdmXA|B console to attach to.
# note: multiple running machines on the same console is bad news
zfs set iohyve:con=$con $pool/iohyve/$name
# persist and boot flags for the start function
zfs set iohyve:persist=0 $pool/iohyve/$name
zfs set iohyve:boot=0 $pool/iohyve/$name
}
# Start guest (combine load and boot)
__start() {
local name="$2"
local flag="$3"
local pool="$(zfs list | grep iohyve | head -n1 | cut -d '/' -f 1)"
local ram="$(zfs get -H -o value iohyve:ram $pool/iohyve/$name)"
local con="$(zfs get -H -o value iohyve:con $pool/iohyve/$name)"
local cpu="$(zfs get -H -o value iohyve:cpu $pool/iohyve/$name)"
local tap="$(zfs get -H -o value iohyve:tap $pool/iohyve/$name)"
if [ -z $flag ]; then
echo "Starting $name... (Takes 15 seconds for FreeBSD guests)"
bhyveload -m $ram -d /dev/zvol/$pool/iohyve/$name/${name}-0.img -c /dev/${con}A ioh-$name > /dev/null
# default creates tap0, useful for starting out with iohyve
if [ $tap = "tap0" ]; then
# no need to create tap0, should be installed already via README
# boot guest with tap0
bhyve -c $cpu -A -H -P -m $ram -s0,hostbridge -s1,lpc -s2,ahci-hd,/dev/zvol/$pool/iohyve/$name/${name}-0.img \
-s3,virtio-net,$tap -lcom1,/dev/${con}A ioh-$name &
else
# check to see if tap is already created before attempting to create new tap interface
local tapif="$(ifconfig -a | grep $tap: | cut -c1-4)"
if [ -z $tapif ]; then
# create tap interface
ifconfig $tap create
ifconfig bridge0 addm $tap
# boot the guest
bhyve -c $cpu -A -H -P -m $ram -s0,hostbridge -s1,lpc \
-s2,ahci-hd,/dev/zvol/$pool/iohyve/$name/${name}-0.img \
-s3,virtio-net,$tap -lcom1,/dev/${con}A ioh-$name &
else
# boot the guest
bhyve -c $cpu -A -H -P -m $ram -s0,hostbridge -s1,lpc \
-s2,ahci-hd,/dev/zvol/$pool/iohyve/$name/${name}-0.img \
-s3,virtio-net,$tap -lcom1,/dev/${con}A ioh-$name &
fi
fi
elif [ $flag = "-p" ]; then
zfs set iohyve:persist=1 $pool/iohyve/$name
local persist="$(zfs get -H -o value iohyve:persist $pool/iohyve/$name)"
# > /dev/null was a bug workaround, may not be needed anymore
bhyveload -m $ram -d /dev/zvol/$pool/iohyve/$name/${name}-0.img -c /dev/${con}A ioh-$name > /dev/null
if [ $tap = "tap0" ]; then
# no need to create tap0, should be installed already via README
# boot guest with tap0
bhyve -c $cpu -A -H -P -m $ram -s0,hostbridge -s1,lpc \
-s2,ahci-hd,/dev/zvol/$pool/iohyve/$name/${name}-0.img \
-s3,virtio-net,$tap -lcom1,/dev/${con}A ioh-$name &
else
# check to see if tap is already created before attempting to create new tap interface
local tapif="$(ifconfig -a | grep $tap: | cut -c1-4)"
if [ -z $tapif ]; then
# create tap interface
ifconfig $tap create
ifconfig bridge0 addm $tap
# boot the guest
bhyve -c $cpu -A -H -P -m $ram -s0,hostbridge -s1,lpc \
-s2,ahci-hd,/dev/zvol/$pool/iohyve/$name/${name}-0.img \
-s3,virtio-net,$tap -lcom1,/dev/${con}A ioh-$name &
else
# boot the guest
bhyve -c $cpu -A -H -P -m $ram -s0,hostbridge -s1,lpc \
-s2,ahci-hd,/dev/zvol/$pool/iohyve/$name/${name}-0.img \
-s3,virtio-net,$tap -lcom1,/dev/${con}A ioh-$name &
fi
fi
# wait for the os to boot
sleep 15
local pid="$(ps | grep ioh-$name | grep -v grep | cut -c1-5)"
while [ $persist = "1" ]
do
persist="$(zfs get -H -o value iohyve:persist $pool/iohyve/$name)"
if [ -z $(ps | grep ioh-$name | grep -v grep | cut -c1-5) ]; then
bhyvectl --force-poweroff --vm=ioh-$name
bhyvectl --destroy --vm=ioh-$name
sleep 5
bhyveload -m $ram -d /dev/zvol/$pool/iohyve/$name/${name}-0.img \
-c /dev/${con}A ioh-$name > /dev/null
if [ $tap = "tap0" ]; then
# no need to create tap0, should be installed already via README
# boot guest with tap0
bhyve -c $cpu -A -H -P -m $ram -s0,hostbridge -s1,lpc \
-s2,ahci-hd,/dev/zvol/$pool/iohyve/$name/${name}-0.img \
-s3,virtio-net,$tap -lcom1,/dev/${con}A ioh-$name &
else
# check to see if tap is already created before attempting to create new tap interface
local tapif="$(ifconfig -a | grep $tap: | cut -c1-4)"
if [ -z $tapif ]; then
# create tap interface
ifconfig $tap create
ifconfig bridge0 addm $tap
# boot the guest
bhyve -c $cpu -A -H -P -m $ram -s0,hostbridge -s1,lpc \
-s2,ahci-hd,/dev/zvol/$pool/iohyve/$name/${name}-0.img \
-s3,virtio-net,$tap -lcom1,/dev/${con}A ioh-$name &
else
# boot the guest
bhyve -c $cpu -A -H -P -m $ram -s0,hostbridge -s1,lpc \
-s2,ahci-hd,/dev/zvol/$pool/iohyve/$name/${name}-0.img \
-s3,virtio-net,$tap -lcom1,/dev/${con}A ioh-$name &
fi
fi
sleep 15
pid="$(ps | grep ioh-$name | grep -v grep | cut -c1-5)"
if [ $persist = "0" ]; then
exit 0
fi
else
sleep 20
if [ $persist = "0" ]; then
exit 0
fi
fi
done
else
echo "If your happy and you know it, SYNTAX ERROR"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment