How to get the session numbers of every running vncviewer and bhyve background processes and store them inside different variables

Hello to everyone.

I like to play with the bhyve virtual machines in FreeBSD a lot. As you probably know,sometime when the OS loaded by the virtual machine is halted,the bhyve virtual machine is not killed. Now,I would like to create a script to kill the "dead" virtual machines that are still listed when I do a "ps ax | grep bhyve",but that can't be used anymore. Let's take the example below :

Code:
mario@marietto:/home/marietto/bhyve # ps ax | grep vnc
14893  3  S+      0:00.00 grep vnc

mario@marietto:/home/marietto/bhyve # ps ax | grep bhyve

14880  -  Is      0:00.00 bhyve: system.pwd (bhyve)
14881  -  Is      0:00.00 bhyve: system.grp (bhyve)
14875  3  SC      0:38.48 bhyve: vm4 (bhyve)
14898  3  S+      0:00.00 grep bhyve

in this case I have one bhyve virtual machine running called vm4 without the corresponding vnc session number,that should be 4,but,instead it is NULL. It means that I've closed the bhyve virtual machine and the vncviewer window but it didn't do properly what I wanted so the bhyve vm is still listed between the running process and it should be killed.

To be clear,I want to show you how is the script that I usually use to launch a bhyve vm :

Code:
bhyve -S -c sockets=2,cores=2,threads=2 -m 8G -w -H -A \
-s 0,hostbridge \
-s 1,nvme,/dev/nvd0,bootindex=1 \
-s 2,virtio-blk,/dev/$vmdisk4 \
-s 3,virtio-blk,/dev/$vmdisk8 \
-s 4,virtio-blk,/dev/$vmdisk11 \
-s 10,virtio-net,tap4 \
-s 11,virtio-9p,sharename=/ \
-s 12,hda,play=/dev/dsp,rec=/dev/dsp \
-s 29,fbuf,tcp=0.0.0.0:5904,w=1600,h=950 \
-s 30,xhci,tablet \
-s 31,lpc \
-l bootrom,/usr/local/share/uefi-firmware/BHYVE_BHF_CODE.fd \
vm4 < /dev/null & sleep 2 && vncviewer 0:4

now,since there isn't any easy method to know if a virtual machine is a ghost or if it is running,I've thought that grepping the vncviewer session number is the most secure method to determine which virtual machine is really running because I never close the vncviewer window but I minimize its window and since I've associated the same number to the virtual machine and to the vncviewer session,if the vnc-viewer-session number is the same as the bhyve-session-number,I'm sure that vm4 is still USABLE. When I will halt the vm4,the corresponding vnc session should be closed for sure. But sometimes it does not happens. Now,what I want to do is to create a script that should assign the correct value to vnc-session and bhyve-session using a string manipulation script that I want to create. In this case the values are :

Code:
$vnc-session-number=NULL
$bhyve-session-number=4

Later I will complete the script like this :

if "vnc-session-number" is different than the "bhyve-session-number",then

Code:
bhyvectl --vm=$bhyve-session-number --force-reset
bhyvectl --vm=$bhyve-session-number --destroy
 
I've created the script I wanted, asking here and there and it seems to work as expected. Here it is :

Code:
#!/bin/sh

setxkbmap it
#set -eux
vms="$(ps ax | awk '/bhyve: [0]/{print $6}')"
vncs="$(ps ax | awk '/vncviewer [0]/{print $6}')"
for vm in $vms; do
                session="${vm##vm}"  
                echo "bhyve session = $vms"
                echo "vnc session = $vncs"
                echo "$session"
                if ! printf '%s\n' "${vncs}" | grep "${session}"; then
                                printf 'VNC session not found,destroying ghost vms\n'
                                bhyvectl --vm=$vms --force-reset
                                bhyvectl --vm=$vms --destroy
                sleep 5
                else
                                printf 'Found VNC session %s\n' "${session},no ghost vms found,not destroying them"
                fi
done

I hope someone finds it useful.
 
Last edited:
It means that I've closed the bhyve virtual machine and the vncviewer window but it didn't do properly what I wanted so the bhyve vm is still listed between the running process and it should be killed.
You seem to think that closing the vncviewer window would shutdown the VM? It won't. Just like a regular PC would still keep running after you disconnect the monitor and keyboard. The VM will only shutdown if you actually tell it to shutdown, i.e. push the power button. Shutting down the VM would cause the vncviewer process to exit (because it lost connection), not the other way around.
 
nooo. I don't think that. I want that when the script does not find the vncviewer session active,it kills the virtual machine attached. I want this because sometime I see that the bhyve vms are listed between the processes,but they don't work anymore. You can see this behavior even because bhyve produces some particular errors when you launch it directly from a script (I don't use vm-bhyve). I dont understand the reasons for the ghosted vms. Anyway,with that script,I don't see that errors anymore and the vms start faster.
 
Instead of looking for a weird workaround force-killing virtual machines, you should better investigate why they don't properly power off. None of my VMs (running FreeBSD, Linux, Windows) ever showed that problem...
 
I don't think that. I want that when the script does not find the vncviewer session active,it kills the virtual machine attached.
Don't kill the VM, shut it down gracefully. Or else the VM's filesystems will be dirty and may get corrupted. You don't pull the plug on a physical machine either, you shut it down gracefully.
 
I've realized that the problems can arise if I kill the process manually with kill -9 "number of bhyve process",but not if I force reset and destroy the vm. I don't know if you have understood that I don't do that usually. Usually I close the Linux / Windows vm internally as everyone does. The brute force is for those vms that became ghosts. Since I saw that it's not a good idea to kill the process,I started to use reset and destroy. If the vm became ghost,it means that I can't close the os internally anymore. How many vms do you use at the same time ? maybe you don't see the ghost problem if you run few vms,but sometimes I run a lot of vms.
 
The brute force is for those vms that became ghosts.
Figure out why they became 'ghosts', because that rarely, if ever, happens to any of my VMs. So there's something not quite right if this happens a lot in your situation.

How many vms do you use at the same time ?
At the moment I have 20, 14 of those run constantly, 6 I turn on when needed.
 
Figure out why they became 'ghosts', because that rarely, if ever, happens to any of my VMs. So there's something not quite right if this happens a lot in your situation.


At the moment I have 20, 14 of those run constantly, 6 I turn on when needed.

I agree. Even if its hard for me to understand in which circumstances it happens since I'm not a developer and I have a modest experience. But do you use the vm-bhyve wrapper ?
 
I've understood one of the reasons why my vms don't close correctly. For example, it happened a little while ago,when I tried to boot a linux vm,but unfortunately,it didn't boot because the grub and the efi partition were damaged. At this point,I've closed the vm by clicking on the upper right corner of the vncviewer window. Do you know another method to close it ? I can't close linux normally because it didn't start at all,but the bhyve process remains enabled and I can't kill the process with kill -9 because it takes time and because I've realized that it can damage the vm more than using the bhyve reset and destroy commands. In this kind of situation my script works like a charm :

Code:
# ./lin-phy-nvd0-vm2.sh 
bhyve session = 0:4
vnc session =
0:4
VNC session not found,destroying ghost vms
vm_open: 0:4 could not be opened: No such file or directory
vm_open: 0:4 could not be opened: No such file or directory
TOSHIBA External USB 3.0 1.8T ; da0
Seagate M3 Portable USB 3.0 1.8T ; da5
CT500MX500SSD4 ; ada0
CT1000P1SSD8 ; nvd0
G-DISK ; da3
Elements ; da2
WDC WD3200AAJS-00L7A0-298GB ; ada1
SanDisk Cruzer-15GB ;
Kingston DataTraveler 2.0 ;
TOSHIBA External USB 3.0 932 GB ; da4
Corsair Force 3 SSD ; da1
MXT-USB Storage Device ; da6
Samsung SSD 860 EVO ;
fbuf frame buffer base: 0x94b400000 [sz 16777216]

as you can see it understood that it was a ghost vm and it killed it.
 
At this point,I've closed the vm by clicking on the upper right corner of the vncviewer window.
You don't close the VM when you close the vncviewer window. Just like your computer will keep running if you turn off the monitor. This might be a language barrier issue but it's an important difference.

The VM is still running. It might be stuck in a BIOS boot screen, or grub, or somewhere else but that doesn't change the fact the VM is still running. It's not a "ghost" VM. It's just a VM that failed to boot (for whatever reason).
 
Back
Top