Solved Launching a bhyve VM and the VNC Viewer at the same time prevents the VM to be able to connect to Internet.

Hello.

I find very comfortable to start automatically the vncviewer client as soon as a bhyve vm is created,but it does not work as expected. Let's take this as example :

Code:
bhyve -S -c sockets=1,cores=2,threads=2 -m 4G -w -H -A \

-s 0,hostbridge \

-s 3,ahci-cd,/mnt/$vmdisk'p2'/bhyve/iso/Android/android-x86_64-9.0-r2-k49.iso \

-s 4,virtio-blk,/mnt/$vmdisk'p2'/bhyve/os/Android/android9.img \

-s 8,virtio-net,tap9 \

-s 29,fbuf,tcp=0.0.0.0:5909,w=1440,h=900 \

-s 30,xhci,tablet \

-s 31,lpc -l com1,stdio \

-l bootrom,/usr/local/share/uefi-firmware/BHYVE_BHF_CODE.fd \

-l com1,stdio \

vm9 & vncviewer 0:9

this script is able to create the bhyve vm without problem but when it is ready,I face two problems :

1) the first time that the vncviewer client appears it says : connection failure. Reconnect ? I say yes and it works. This is the minor problem,even if it is a little annoying

2) the most serious problem with that script is that the vm can't connect to internet. I'm sure that the problem is inside this line :

Code:
vm9 & vncviewer 0:9

and more precisely the error happens because I'm using the & character. If I remove that character and I run the vm like this :

Code:
bhyve -S -c sockets=1,cores=2,threads=2 -m 4G -w -H -A \

-s 0,hostbridge \

-s 3,ahci-cd,/mnt/$vmdisk'p2'/bhyve/iso/Android/android-x86_64-9.0-r2-k49.iso \

-s 4,virtio-blk,/mnt/$vmdisk'p2'/bhyve/os/Android/android9.img \

-s 8,virtio-net,tap9 \

-s 29,fbuf,tcp=0.0.0.0:5909,w=1440,h=900 \

-s 30,xhci,tablet \

-s 31,lpc -l com1,stdio \

-l bootrom,/usr/local/share/uefi-firmware/BHYVE_BHF_CODE.fd \

-l com1,stdio \

vm9

it can connect to internet. Do you know why it does not accept that character ? how can I fix it ? it's annoying for me to open manually the vnc viewer every time that I launch a vm. I prefer that the script does it automatically.
 
So you are creating a vm named vm9 and the vncviewer command is trying to connect from the host to that vm?
Is there a vnc server running in vm9 that redirects to a display 0:9 on the host? I'm asking because I've not done stuff with bhyve.

normally if you want to string commands together you would do something like
command1 && command 2 && command3 (assuming you want command1 to finish successfully before running command2 and same for command3)

command1; command2 runs command2 regardless of success/failure of command1

(command1 &); command2 should run command1, put it in background and then run command2

If as you say "reconnect" works and doing vncviewer by hand works, that implies you need to wait for the vm actually complete starting up before starting vncviewer. Something like:
(command1 &); sleep 5 && vncviewer
command1 is your bhyve command; do bhyve, put in background, sleep 5 seconds, then start vncviewer
But:
Does the bhyve command return to a command prompt? I'm guessing the answer is "yes". If so, then basically try:
bhyve options options options vm9 && sleep 1 && vncviewer 0:9
 
Dejavu

Don't start the VM using Ampersand Operator (&) because when you destroy the virtual machine it doesn't release the TAP interface as it continue to run as background job.


 
ziomario : I noticed you do have a tendency to create very long thread titles, and they become not particularly readable. I'd like to suggest that maybe you can think up a shorter thread title, and save the discussion for the body of the post.
 
Do you know why it does not accept that character ?
Do you understand what that character means?

It is not an argument to the program. Usually, programs read the command line and can interpret it as arguments. For example, let's say you have a hypothetical program called "echo", which simply takes whatever arguments it finds on the command line, and prints them. If you say "echo foo", it will print "foo". If you say "echo foo &", it will also print "foo". It is only getting one argument!

What really happens is that "&" is a shell metacharacter. It is closely related to ";". Both metacharacters separate shell commands (typically programs getting started) from each other. If you say "echo foo ; echo bar", the shell will start the program echo once with the argument foo (which will print "foo"), WAIT FOR IT TO FINISH, then start echo again with argument bar (which will print "bar"). On the other hand, if you say "echo foo & echo bar", the shell will start echo with argument foo, NOT WAIT FOR IT, immediately start a second process running the echo program again with argument bar, and wait for the second copy (the "echo bar") to finish. The really important part is that you have two programs running simultaneously! This parallelism, and how it can be super easily be operated from the shell, was one of the breakthroughs of Unix. At the time this became available in the 70s, no other production OSes had stuff like that.

The really important thing is this: Your question "does not accept that character" shows that you don't understand how command lines and job control in the shell work. I suggest you read up on that a little bit.

With short-running programs like echo, this is not particularly spectacular. But you could easily create a slow version of echo which dribbles out its printout one character at a time, with a one second gap in between. If you use that and ran "slowecho foo & slowecho bar", the printout might be "fboaor" (or some similar scrambling). Which is probably not what you intended.

Unfortunately, with this power comes a price. Which is several things. First, you are now running two programs in parallel. Parallelism is the root of most evil in computing, since it is REALLY HARD to understand, control, and administer. For example, I'm going to bet that bhyve is a little bit slow to start up (unlike echo), and you see that effect: bhyve takes a while to connect the display connection; vncviewer starts, gets there faster, and finds that there is nobody there yet listening on the port, and reports "connection failure". What this really means is that you have to either retry vncviewer, or set up some synchronization mechanism. That could be as easy as "bhyve ... & sleep 10 ; vncviewer ...", so vncviewer starts after a 10 second delay (might work, unreliable, but what the heck).

Here is another important distinction: If you say "echo ... &", then the echo program runs in the background. It can print to stdout, but it can't read from stdin, until and unless you attach the controlling terminal to it again (with fg and bg commands). And you don't know when it is done running, until the next time the shell gets control.

Your second problem is likely more insiduous. Because with all this parallelism and detaching, you have another problem: bhyve may be looking for input from stdin (which has been disconnected, because it is running detached), or it may run forever until it gets reaped by the shell. Since I don't know exactly how bhyve works or what it does, this is on you to debug.
 
Code:
 vm9 & sleep 5 && vncviewer 0:9

the vncviewer appears,but the VM can't connect ;

Code:
 vm9 & sleep 5 & vncviewer 0:9

the vncviewer appears,but the VM can't connect ;

Code:
 vm9 && sleep 5 && vncviewer 0:9

the vncviewer does not appears at all ;

Code:
 vm9 && sleep 5 & vncviewer 0:9

the vncviewer appears,but the VM can't connect ;

I don't see any solution for this problem.
 
vncviewer 0:8 is that a typo? in your OP it says vncviewer 0:9

I've changed VM and I fixed the typo. Reload the page.

It changes nothing if now I'm trying this ? right ?

Code:
bhyve -S -c sockets=2,cores=2,threads=2 -m 8G -w -H -A \
-s 0,hostbridge \
-s 1,ahci-cd,/mnt/$vmdisk'p2'/bhyve/iso/Linux/No-SystemD/void-live-x86_64-musl-20210930-xfce.iso \
-s 2,virtio-blk,/mnt/$vmdisk'p2'/bhyve/os/Linux/void.img \
#-s 2:0,passthru,1/0/0 \
#-s 3:0,passthru,2/0/0 \
#-s 3:1,passthru,2/0/1 \
#-s 3:2,passthru,2/0/2 \
#-s 3:3,passthru,2/0/3 \
-s 9,virtio-net,tap9 \
-s 10,virtio-9p,sharename=/ \
#-s 9,hda,play=/dev/dsp,rec=/dev/dsp \
-s 29,fbuf,tcp=0.0.0.0:5909,w=1440,h=900 \
-s 30,xhci,tablet \
-s 31,lpc \
-l bootrom,/usr/local/share/uefi-firmware/BHYVE_BHF_CODE.fd \
-l com1,stdio \
 
The commands are cut off. I went back to your original post, and took the part that you said worked. Then I modified it how I think it should work (bolded part).
I'm assuming that the bhyve command returns to the command prompt if there is nothing after vm9. Is that correct?
What should happen is the bhyve command runs, starts vm9, if bhyve command returns successfully, sleeps for 5 seconds and then starts vncviewer to the local display 0:9.
bhyve -S -c sockets=1,cores=2,threads=2 -m 4G -w -H -A \ -s 0,hostbridge \ -s 3,ahci-cd,/mnt/$vmdisk'p2'/bhyve/iso/Android/android-x86_64-9.0-r2-k49.iso \ -s 4,virtio-blk,/mnt/$vmdisk'p2'/bhyve/os/Android/android9.img \ -s 8,virtio-net,tap9 \ -s 29,fbuf,tcp=0.0.0.0:5909,w=1440,h=900 \ -s 30,xhci,tablet \ -s 31,lpc -l com1,stdio \ -l bootrom,/usr/local/share/uefi-firmware/BHYVE_BHF_CODE.fd \ -l com1,stdio \ vm9 [B]&& sleep 5 && vncviewer 0:9[/B]
 
You say to do like this ?

Code:
vm9 && sleep 5 && vncviewer 0:9

if yes,it does not work : the VM can connect to internet,but the vncviewer does not appears at all. So,it does not make sense to do like this.

----> bhyve command returns to the command prompt if there is nothing after vm9. Is that correct?

no. bhyve never returns to the command prompt. It does not care if there is or not something after vm9.
 
Yes that is what I said. Even if you wait longer than 5 secs it does not appear?
what happens if you make it say:

vm9 && echo "VM9 Started"

Does the message VM9 Started get printed? If not then something is not reporting success completion.
I would then try putting the bhyve command in parens:
(bhyve ...... vm9) && sleep 5 && vncviewer 0:9
 
in parens ? how ? I have a script. In parens you say like this ?

Code:
(bhyve -S -c sockets=2,cores=2,threads=2 -m 8G -w -H -A \
-s 0,hostbridge \
-s 1,ahci-cd,/mnt/$vmdisk'p2'/bhyve/iso/Linux/No-SystemD/void-live-x86_64-musl-20210930-xfce.iso \
-s 2,virtio-blk,/mnt/$vmdisk'p2'/bhyve/os/Linux/void.img \
#-s 2:0,passthru,1/0/0 \
#-s 3:0,passthru,2/0/0 \
#-s 3:1,passthru,2/0/1 \
#-s 3:2,passthru,2/0/2 \
#-s 3:3,passthru,2/0/3 \
-s 9,virtio-net,tap9 \
-s 10,virtio-9p,sharename=/ \
#-s 9,hda,play=/dev/dsp,rec=/dev/dsp \
-s 29,fbuf,tcp=0.0.0.0:5909,w=1440,h=900 \
-s 30,xhci,tablet \
-s 31,lpc \
-l bootrom,/usr/local/share/uefi-firmware/BHYVE_BHF_CODE.fd \
-l com1,stdio \) && sleep 5 && vncviewer 0:9
 
Code:
bhyve -S -c sockets=2,cores=2,threads=2 -m 8G -w -H -A \
-s 0,hostbridge \
-s 1,ahci-cd,/mnt/$vmdisk'p2'/bhyve/iso/Linux/No-SystemD/void-live-x86_64-musl-20210930-xfce.iso \
-s 2,virtio-blk,/mnt/$vmdisk'p2'/bhyve/os/Linux/void.img \
#-s 2:0,passthru,1/0/0 \
#-s 3:0,passthru,2/0/0 \
#-s 3:1,passthru,2/0/1 \
#-s 3:2,passthru,2/0/2 \
#-s 3:3,passthru,2/0/3 \
-s 9,virtio-net,tap9 \
-s 10,virtio-9p,sharename=/ \
#-s 9,hda,play=/dev/dsp,rec=/dev/dsp \
-s 29,fbuf,tcp=0.0.0.0:5909,w=1440,h=900 \
-s 30,xhci,tablet \
-s 31,lpc \
-l bootrom,/usr/local/share/uefi-firmware/BHYVE_BHF_CODE.fd \
-l com1,stdio \ < /dev/null
vm9 && sleep 5 && vncviewer 0:9

vm9: Command not found.
 
Code:
bhyve -S -c sockets=2,cores=2,threads=2 -m 8G -w -H -A \
-s 0,hostbridge \
-s 1,ahci-cd,/mnt/$vmdisk'p2'/bhyve/iso/Linux/No-SystemD/void-live-x86_64-musl-20210930-xfce.iso \
-s 2,virtio-blk,/mnt/$vmdisk'p2'/bhyve/os/Linux/void.img \
#-s 2:0,passthru,1/0/0 \
#-s 3:0,passthru,2/0/0 \
#-s 3:1,passthru,2/0/1 \
#-s 3:2,passthru,2/0/2 \
#-s 3:3,passthru,2/0/3 \
-s 9,virtio-net,tap9 \
-s 10,virtio-9p,sharename=/ \
#-s 9,hda,play=/dev/dsp,rec=/dev/dsp \
-s 29,fbuf,tcp=0.0.0.0:5909,w=1440,h=900 \
-s 30,xhci,tablet \
-s 31,lpc \
-l bootrom,/usr/local/share/uefi-firmware/BHYVE_BHF_CODE.fd \
vm9 && sleep 5 && vncviewer 0:9

fbuf frame buffer base: 0xa43800000 [sz 16777216]

it waits here that I fire up the vnc viewer manually.
 
That almost sounds like the bhyve command is not actually returning after starting the vm.
I'm out of ideas, have nothing more to add.
 
put redirection at the end (after vm9)
dont use && after vm9 that will wait until the process exits
... vm9 < /dev/null &
try the above with and without the stdio (if you remove the -l com1, stdio you may remove < /dev/null)
see if the internet connection works
 
This works. I want to write it here before I forget what it worked,even if it is not complete. Infact vncviewer starts too early.

Code:
bhyve -S -c sockets=2,cores=2,threads=2 -m 8G -w -H -A \
-s 0,hostbridge \
-s 1,ahci-cd,/mnt/$vmdisk'p2'/bhyve/iso/Linux/No-SystemD/void-live-x86_64-musl-20210930-xfce.iso \
-s 2,virtio-blk,/mnt/$vmdisk'p2'/bhyve/os/Linux/void.img \
#-s 2:0,passthru,1/0/0 \
#-s 3:0,passthru,2/0/0 \
#-s 3:1,passthru,2/0/1 \
#-s 3:2,passthru,2/0/2 \
#-s 3:3,passthru,2/0/3 \
-s 9,virtio-net,tap9 \
-s 10,virtio-9p,sharename=/ \
#-s 9,hda,play=/dev/dsp,rec=/dev/dsp \
-s 29,fbuf,tcp=0.0.0.0:5909,w=1440,h=900 \
-s 30,xhci,tablet \
-s 31,lpc \
-l bootrom,/usr/local/share/uefi-firmware/BHYVE_BHF_CODE.fd \
#-l com1,stdio \
vm9 < /dev/null & vncviewer 0:9
 
If I add a delay of 5s,it does not care at all. Vncviewer starts too early anyway.

Code:
bhyve -S -c sockets=2,cores=2,threads=2 -m 8G -w -H -A \
-s 0,hostbridge \
-s 1,ahci-cd,/mnt/$vmdisk'p2'/bhyve/iso/Linux/No-SystemD/void-live-x86_64-musl-20210930-xfce.iso \
-s 2,virtio-blk,/mnt/$vmdisk'p2'/bhyve/os/Linux/void.img \
#-s 2:0,passthru,1/0/0 \
#-s 3:0,passthru,2/0/0 \
#-s 3:1,passthru,2/0/1 \
#-s 3:2,passthru,2/0/2 \
#-s 3:3,passthru,2/0/3 \
-s 9,virtio-net,tap9 \
-s 10,virtio-9p,sharename=/ \
#-s 9,hda,play=/dev/dsp,rec=/dev/dsp \
-s 29,fbuf,tcp=0.0.0.0:5909,w=1440,h=900 \
-s 30,xhci,tablet \
-s 31,lpc \
-l bootrom,/usr/local/share/uefi-firmware/BHYVE_BHF_CODE.fd \
#-l com1,stdio \
vm9 < /dev/null & sleep 5 & vncviewer 0:9
 
this is the solution :

Code:
#-l com1,stdio \
vm9 < /dev/null & sleep 2 && vncviewer 0:9

thanks to everyone.
 
Unfortunately if I add the argument "-l com1,stdio \" like this :

Code:
-l com1,stdio \
vm9 < /dev/null & sleep 2 && vncviewer 0:9

it won't connect to internet. So,the solution is not complete,since I prefer to add "-l com1,stdio" to bhyve.
 
Back
Top