scripts

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

vmb (2464B)


      1 #!/bin/sh
      2 
      3 # TODO virtio-blk
      4 # TODO is grub complete?
      5 
      6 cpu="2"
      7 mem="2G"
      8 disk="disk.img"
      9 tap="tap0"
     10 uefifile="/usr/local/share/uefi-firmware/BHYVE_UEFI.fd"
     11 
     12 usage()
     13 {
     14 	echo "usage: ${0##*/} start [-Duv] [-c cpu] [-C com] [-d disksiz]" 1>&2
     15 	echo "               [-g grub_bootdrv] [-G grub_bootdir] [-i img]" 1>&2
     16 	echo "               [-m mem] [-t tap] vmname" 1>&2
     17 	echo "       ${0##*/} stop [-t tap] vmname" 1>&2
     18 	echo "       -D: pause at first instruction and wait for gdb" 1>&2
     19 	echo "       -u: run in uefi mode" 1>&2
     20 	echo "       -v: wait until the vnc client has started" 1>&2
     21 	exit 1
     22 }
     23 
     24 err()
     25 {
     26 	echo "${0##*/}: ${@}" 1>&2
     27 	exit 1
     28 }
     29 
     30 vmb_start()
     31 {
     32 	while getopts "c:C:d:Dg:G:i:m:t:uv" arg; do
     33 	case "${arg}" in
     34 		c) cpu="${OPTARG}" ;;
     35 		C) com="${OPTARG}" ;;
     36 		d) disksiz="${OPTARG}" ;;
     37 		D) dbgw="w" ;;
     38 		g) grubdrv="${OPTARG}" ;;
     39 		G) grubdir="${OPTARG}" ;;
     40 		i) img="${OPTARG}" ;;
     41 		m) mem="${OPTARG}" ;;
     42 		t) tap="${OPTARG}" ;;
     43 		u) uefi="-l bootrom,${uefifile}" ;;
     44 		v) vncwait=",wait" ;;
     45 		*) usage ;;
     46 	esac
     47 	done
     48 	shift $((OPTIND - 1))
     49 
     50 	name="${1}"
     51 	test -z "${name}" && usage
     52 
     53 	if [ -n "${uefi}" ]; then
     54 		test ! -f ${uefifile} && \
     55 		err "uefi file \"${uefifile}\" not found." \
     56 		"Install $(pkg search uefi-edk2 | awk '{print $1}')"
     57 	fi
     58 
     59 	if [ -n "${grubdrv}" ] || [ -n "${grubdir}" ]; then
     60 		test ! "$(pkg info | awk '{print $1}' | grep grub2-bhyve)" &&
     61 		err "grub driver not found." \
     62 		"Install $(pkg search grub2-bhyve | awk '{print $1}')"
     63 	fi
     64 
     65 	ifconfig "${tap}" up || exit 1
     66 
     67 	test -n "${disksiz}" && truncate -s "${disksiz}" ${disk}
     68 
     69 	if [ -n "${grubdrv}" ]; then
     70 		echo "(hd0) disk.img" > device.map
     71 		${img:+(cd0) ${img}} >> device.map
     72 		grub-bhyve \
     73 			-m device.map \
     74 			-r ${grubdrv} \
     75 			${grubdir:+-d ${grubdir}} \
     76 			-M ${mem} \
     77 			${name}
     78 	fi
     79 
     80 	bhyve \
     81 		-wHAP \
     82 		-c ${cpu} \
     83 		-m ${mem} \
     84 		-s 0,hostbridge \
     85 		${img:+-s 3,ahci-cd,${img}} \
     86 		-s 4,ahci-hd,${disk} \
     87 		-s 5,virtio-net,${tap} \
     88 		-s 29,fbuf,tcp=0.0.0.0:5900${vncwait} \
     89 		-s 30,xhci,tablet \
     90 		-s 31,lpc \
     91 		-G ${dbgw}1234 \
     92 		${com:+-l com1,${com}} \
     93 		${uefi:+${uefi}} \
     94 		${name}
     95 }
     96 
     97 vmb_stop()
     98 {
     99 	while getopts "t:" arg; do
    100 	case "${arg}" in
    101 		t) tap="${OPTARG}" ;;
    102 		*) usage ;;
    103 	esac
    104 	done
    105 	shift $((OPTIND - 1))
    106 
    107 	name="${1}"
    108 	test -z "${name}" && usage
    109 
    110 	ifconfig ${tap} down
    111 	bhyvectl --force-poweroff --vm=${name}
    112 	bhyvectl --destroy --vm=${name}
    113 }
    114 
    115 cmd="${1}"
    116 shift 1
    117 case "${cmd}" in
    118 	start) vmb_start $@ ;;
    119 	stop) vmb_stop $@ ;;
    120 	*) usage ;;
    121 esac