scripts

Random scripts
git clone git://git.margiolis.net/scripts.git
Log | Files | Refs | README | LICENSE

commit 1a88044d505a272e77aec851aabed3bfb208949f
parent 5cd272e0e21d778036b275dfe728e2284259e8af
Author: Christos Margiolis <christos@margiolis.net>
Date:   Sat,  4 Mar 2023 14:52:17 +0200

merge vmstart and vmstop

Diffstat:
MMakefile | 3+--
Mbookmark | 2+-
Mvdq | 2+-
Mvds | 2+-
Avmb | 114+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dvmstart | 79-------------------------------------------------------------------------------
Dvmstop | 24------------------------
7 files changed, 118 insertions(+), 108 deletions(-)

diff --git a/Makefile b/Makefile @@ -28,8 +28,7 @@ TARGS = audl \ upd \ vdq \ vds \ - vmstart \ - vmstop \ + vmb \ walset \ wttr diff --git a/bookmark b/bookmark @@ -1,7 +1,7 @@ #!/bin/sh # XXX: make this an env variable? -BMK_DIR="${HOME}/shit/bookmarks/" +BMK_DIR="${HOME}/n/bookmarks/" bmk_getfile() { diff --git a/vdq b/vdq @@ -1,4 +1,4 @@ #!/bin/sh -QUEUE="${HOME}/shit/queue" +QUEUE="${HOME}/n/queue" mpv $(awk '{print $1}' ${QUEUE}) diff --git a/vds b/vds @@ -1,6 +1,6 @@ #!/bin/sh -PLAYLIST="${HOME}/shit/playlist" +PLAYLIST="${HOME}/n/playlist" while getopts "p:" arg; do case "${arg}" in diff --git a/vmb b/vmb @@ -0,0 +1,114 @@ +#!/bin/sh + +# TODO virtio-blk +# TODO is grub complete? + +cpu="2" +mem="2G" +disk="disk.img" +tap="tap0" +uefifile="/usr/local/share/uefi-firmware/BHYVE_UEFI.fd" + +usage() +{ + echo "usage: ${0##*/} start [-uv] [-c cpu] [-C com] [-d disksiz]" 1>&2 + echo " [-g grub_bootdrv] [-G grub_bootdir] [-i img]" 1>&2 + echo " [-m mem] [-t tap] vmname" 1>&2 + echo " ${0##*/} stop [-t tap] vmname" 1>&2 + echo " -u: run in uefi mode" 1>&2 + echo " -v: start a vnc server" 1>&2 + exit 1 +} + +err() +{ + echo "${0##*/}: ${@}" 1>&2 + exit 1 +} + +vmb_start() +{ + while getopts "c:C:d:g:G:i:m:t:uv" arg; do + case "${arg}" in + c) cpu="${OPTARG}" ;; + C) com="${OPTARG}" ;; + d) disksiz="${OPTARG}" ;; + g) grubdrv="${OPTARG}" ;; + G) grubdir="${OPTARG}" ;; + i) img="${OPTARG}" ;; + m) mem="${OPTARG}" ;; + t) tap="${OPTARG}" ;; + u) uefi="-l bootrom,${uefifile}" ;; + v) vnc="-s 29,fbuf,tcp=0.0.0.0:5900,wait" ;; + *) usage ;; + esac + done + shift $((OPTIND - 1)) + + name="${1}" + test -z "${name}" && usage + + test ! -f ${uefifile} && \ + err "uefi file \"${uefifile}\" not found." \ + "Install $(pkg search uefi-edk2 | awk '{print $1}')" + + test ! -f ${grubdrv} && \ + err "grub drive \"${grubdrv}\" not found." \ + "Install $(pkg search grub2-bhyve | awk '{print $1}')" + + ifconfig "${tap}" up || exit 1 + + test -n "${disksiz}" && truncate -s "${disksiz}" ${disk} + + if [ -n "${grubdrv}" ]; then + echo "(hd0) disk.img" > device.map + ${img:+(cd0) ${img}} >> device.map + grub-bhyve \ + -m device.map \ + -r ${grubdrv} \ + ${grubdir:+-d ${grubdir}} \ + -M ${mem} \ + ${name} + fi + + bhyve \ + -wHAP \ + -c ${cpu} \ + -m ${mem} \ + -s 0,hostbridge \ + ${img:+-s 3,ahci-cd,${img}} \ + -s 4,ahci-hd,${disk} \ + -s 5,virtio-net,${tap} \ + ${vnc:+${vnc}} \ + -s 30,xhci,tablet \ + -s 31,lpc \ + ${com:+-l com1,${com}} \ + ${uefi:+${uefi}} \ + ${name} +} + +vmb_stop() +{ + while getopts "t:" arg; do + case "${arg}" in + t) tap="${OPTARG}" ;; + *) usage ;; + esac + done + shift $((OPTIND - 1)) + + name="${1}" + test -z "${name}" && usage + + ifconfig ${tap} down + bhyvectl --force-poweroff --vm=${name} + bhyvectl --destroy --vm=${name} +} + +cmd="${1}" +shift 1 +case "${cmd}" in + start) vmb_start $@ ;; + stop) vmb_stop $@ ;; + *) usage ;; +esac diff --git a/vmstart b/vmstart @@ -1,79 +0,0 @@ -#!/bin/sh - -usage() -{ - echo "usage: ${0##*/} [-uv] [-c cpu] [-C com] [-d disksiz] [-g grub_bootdrv]" 1>&2 - echo " [-G grub_bootdir] [-i img] [-m mem] [-t tap] name" 1>&2 - exit 1 -} - -err() -{ - echo "${0##*/} ${@}" 1>&2 - exit 1 -} - -cpu="2" -mem="2G" -disk="disk.img" -tap="tap0" -vnc="no" -uefifile="/usr/local/share/uefi-firmware/BHYVE_UEFI.fd" - -# TODO man page? -# TODO merge vmstart and vmstop to `vm <start/stop> ...`? (find new name in this case) -# TODO virtio-blk -# TODO does grub work properly? - -while getopts "c:C:d:g:G:i:m:t:uv" arg; do -case "${arg}" in - c) cpu="${OPTARG}" ;; - C) com="${OPTARG}" ;; - d) disksiz="${OPTARG}" ;; - g) grubdrv="${OPTARG}" ;; - G) grubdir="${OPTARG}" ;; - i) img="${OPTARG}" ;; - m) mem="${OPTARG}" ;; - t) tap="${OPTARG}" ;; - u) uefi="-l bootrom,${uefifile}" ;; - v) vnc="-s 29,fbuf,tcp=0.0.0.0:5900,wait" ;; - *) usage ;; -esac -done -shift $((OPTIND - 1)) - -name="${1}" -test -z "${name}" && usage - -test ! -f ${uefifile} && \ -err "${uefifile} not found. Install $(pkg search uefi-edk | awk '{print $1}')" - -ifconfig "${tap}" up || exit 1 - -test -n "${disksiz}" && truncate -s "${disksiz}" ${disk} - -if [ -n "${grubdrv}" ]; then - echo "(hd0) disk.img" > device.map - ${img:+(cd0) ${img}} >> device.map - grub-bhyve \ - -m device.map \ - -r ${grubdrv} \ - ${grubdir:+-d ${grubdir}} \ - -M ${mem} \ - ${name} -fi - -bhyve \ - -wHAP \ - -c ${cpu} \ - -m ${mem} \ - -s 0,hostbridge \ - ${img:+-s 3,ahci-cd,${img}} \ - -s 4,ahci-hd,${disk} \ - -s 5,virtio-net,${tap} \ - ${vnc:+${vnc}} \ - -s 30,xhci,tablet \ - -s 31,lpc \ - ${com:+-l com1,${com}} \ - ${uefi:+${uefi}} \ - ${name} diff --git a/vmstop b/vmstop @@ -1,24 +0,0 @@ -#!/bin/sh - -usage() -{ - echo "usage: ${0##*/} [-t tap] name" 1>&2 - exit 1 -} - -tap="tap0" - -while getopts "t:" arg; do -case "${arg}" in - t) tap="${OPTARG}" ;; - *) usage ;; -esac -done -shift $((OPTIND - 1)) - -name="${1}" -test -z "${name}" && usage - -ifconfig ${tap} down -bhyvectl --force-poweroff --vm=${name} -bhyvectl --destroy --vm=${name}