Solved Some help to fix a bug that's inside the script I use to manage automatically my bhyve VMs

Hello.

I've created a script like the one below to be able to manage my bhye VMs without using wrappers. Actually I use the same pattern for every VM I want to use. Every VM has its own number associated. What changes from one to another is only this line :

Code:
-s 1,virtio-blk,/mnt/zroot2/zroot2/bhyve/img/Linux/ubuntu2210.img,bootindex=1 \

That's the name and the location of the raw file. Inside the script I have added a mechanism to kill a ghosted vm if it does not find the associated viewer window active. That's because I don't find it useful to use a ghosted VM. Yes,it is faster,but It causes also some troubles. So,this is the script I use to launch a Windows VM :

Code:
#!/bin/sh

setxkbmap it
vms="$(ls /dev/vmm/*)"
vncs="$(ps ax | awk '/vncviewer [0]/{print $6}')"
echo $vms
echo $vncs

for vm in $vms; do
                session="${vm##*/}"
                echo "bhyve session = $session"
                echo "vnc session = $vncs"    
                                if ! printf '%s\n' "${vncs}" | grep "${session}"; then
                                printf 'VNC session not found,destroying ghost vms\n'
                                bhyvectl --vm=$session --destroy                         
                else
                                printf 'Found VNC session %s\n' "${session},no ghost vms found,not destroying them"
                fi
done

bhyve -S -c sockets=2,cores=2,threads=2 -m 4G -w -H -A \
-s 0,hostbridge \
-s 1,ahci-hd,/mnt/zroot2/zroot2/bhyve/img/Windows/windows11.img,bootindex=1 \
-s 13,virtio-net,tap18 \
-s 29,fbuf,tcp=0.0.0.0:5918,w=1600,h=950,wait \
-s 30,xhci,tablet \
-s 31,lpc \
-l bootrom,/usr/local/share/uefi-firmware/BHYVE_UEFI_CODE.fd \
vm0:18 < /dev/null & sleep 2 && vncviewer 0:18

Unfortunately there is a bug that I'm not able to fix : if I want to boot more than one VM,the first one will be killed. For example let's take this case : the first time that I've launched my script the VNC viewer window hasn't been shown,I don't know why,so I've been forced to press CTRL + C to kill the script. But as you know,when a bhyve VM script is killed pressing CTRL + C it is still there as a ghost,as a running process. At this point I ran my script again and this is what happened :

Code:
# ./vm18
/dev/vmm/vm0:18

bhyve session = vm0:18
vnc session =
VNC session not found,destroying ghost vms
fbuf frame buffer base: 0x317f28e00000 [sz 16777216]

TigerVNC Viewer 64-bit v1.12.0
Built on: 2021-11-23 05:59
Copyright (C) 1999-2021 TigerVNC Team and many others (see README.rst)
See https://www.tigervnc.org for information on TigerVNC.

Wed May 24 16:11:19 2023
 DecodeManager: Detected 16 CPU core(s)
 DecodeManager: Creating 4 decoder thread(s)
 CConn:       Connected to host 0 port 5918
 CConnection: Server supports RFB protocol version 3.8
 CConnection: Using RFB protocol version 3.8
 CConnection: Choosing security type None(1)
 CConn:       Using pixel format depth 24 (32bpp) little-endian rgb888

it worked as expected,because it didn't find the VNC viewer window and it destroyed the VM and it launched a new one,like I want. But,let's assume that now I want to run another VM. The script that I will run is similar to the previous one,but this time I want to virtualize Linux and at the same time I want that the previous Windows VM still runs. So,this is the script I want to run as a second VM :

Code:
#!/bin/sh

setxkbmap it
vms="$(ls /dev/vmm/*)"
vncs="$(ps ax | awk '/vncviewer [0]/{print $6}')"
echo $vms
echo $vncs

for vm in $vms; do
                session="${vm##*/}"
                echo "bhyve session = $session"
                echo "vnc session = $vncs"    
                                if ! printf '%s\n' "${vncs}" | grep "${session}"; then
                                printf 'VNC session not found,destroying ghost vms\n'
                                bhyvectl --vm=$session --destroy                         
                else
                                printf 'Found VNC session %s\n' "${session},no ghost vms found,not destroying them"
                fi
done

bhyve -S -c sockets=2,cores=2,threads=2 -m 8G -w -H -A \
-s 0,hostbridge \
-s 1,virtio-blk,/mnt/zroot2/zroot2/bhyve/img/Linux/ubuntu2210.img,bootindex=1 \
-s 13,virtio-net,tap19 \
-s 14,virtio-9p,sharename=/ \
-s 29,fbuf,tcp=0.0.0.0:5919,w=1600,h=950,wait \
-s 30,xhci,tablet \
-s 31,lpc \
-l bootrom,/usr/local/share/uefi-firmware/BHYVE_UEFI_CODE.fd \
vm0:19 < /dev/null & sleep 2 && vncviewer 0:19

What I expect is that the Windows VM still runs and even the Linux VM should run. Instead,this is what happens :

Code:
# ./vm19
/dev/vmm/vm0:18
0:18
bhyve session = vm0:18
vnc session = 0:18
VNC session not found,destroying ghost vms
fbuf frame buffer base: 0x308d08600000 [sz 16777216]

TigerVNC Viewer 64-bit v1.12.0
Built on: 2021-11-23 05:59
Copyright (C) 1999-2021 TigerVNC Team and many others (see README.rst)
See https://www.tigervnc.org for information on TigerVNC.

Wed May 24 17:28:53 2023
 DecodeManager: Detected 16 CPU core(s)
 DecodeManager: Creating 4 decoder thread(s)
 CConn:       Connected to host 0 port 5919
 CConnection: Server supports RFB protocol version 3.8
 CConnection: Using RFB protocol version 3.8
 CConnection: Choosing security type None(1)
 CConn:       Using pixel format depth 24 (32bpp) little-endian rgb888

This statement is false : VNC session not found,destroying ghost vms,because as I told before,the vm18 + its VNC viewer were active and the script report it,also :

Code:
bhyve session = vm0:18
vnc session = 0:18

There is some logic error inside the script and I'm damaging my brain to understand where it is and how to fix it.
 
Back
Top