Shell bhyve VM while loop

amr

Member

Reaction score: 2
Messages: 43

Dear All,

I am trying to use the following while loop to achieve the following goals:
1. First command inside the loop is to run the installation one time only.
2. Second command is to run the VM normally without the ISO file.
Can you please show me the right way to achieve that.

Code:
# The VM loop

  while true; do # Start the installation

    bhyve -Hw -c ${cpu} -m ${ram}G \

      -s 0,hostbridge \

      -s 1,virtio-net,${tap} \

      -s 2,virtio-blk,/dev/zvol/zroot/VMs/${name}/disk0 \

      -s 3,ahci-cd,${FreeBSD_ISO} \

      -s 29,fbuf,tcp=0.0.0.0:${vnc},w=1024,h=768,wait \

      -s 30,xhci,tablet \

      -s 31,lpc \

      -l bootrom,/zroot/VMs/efi.fd \

      ${name}

 

exit=$?

  if [ $exit -eq 0 ]; then

    bhyve -Hw -c ${cpu} -m ${ram}G \

      -s 0,hostbridge \

      -s 1,virtio-net,${tap} \

      -s 2,virtio-blk,/dev/zvol/zroot/VMs/${name}/disk0 \

      -s 29,fbuf,tcp=0.0.0.0:${vnc},w=1024,h=768 \

      -s 30,xhci,tablet \

      -s 31,lpc \

      -l bootrom,/zroot/VMs/efi.fd \

      ${name}

    

 elif [ $exit -ne 0 ]; then

        break;

      fi

  done > /dev/null 2>&1 &
 

usdmatt

Daemon

Reaction score: 571
Messages: 1,498

Would be helpful if you listed the problems you are having with it.

The first issue I can see is that every time it goes round the loop it will run the “install” command again. It may make more sense to take the install version of the command out of the loop and just exit afterwards if the status isn’t valid. Then just have the second version of the command in the loop so it just re-runs the same thing on reboot.
 
OP
amr

amr

Member

Reaction score: 2
Messages: 43

I made it by removing the first command (Installation) from the loop and starting the loop after completion of the Installation command as follows:

Code:
Installation()
  {
    bhyve -Hw -c ${cpu} -m ${ram}G \
      -s 0,hostbridge \
      -s 1,virtio-net,${tap} \
      -s 2,virtio-blk,/dev/zvol/zroot/VMs/${name}/disk0 \
      -s 3,ahci-cd,${FreeBSD_ISO} \
      -s 29,fbuf,tcp=0.0.0.0:${vnc},w=1024,h=768,wait \
      -s 30,xhci,tablet \
      -s 31,lpc \
      -l bootrom,/zroot/VMs/efi.fd \
      ${name}
   }
  Starting()
  {
    bhyve -Hw -c ${cpu} -m ${ram}G \
      -s 0,hostbridge \
      -s 1,virtio-net,${tap} \
      -s 2,virtio-blk,/dev/zvol/zroot/VMs/${name}/disk0 \
      -s 29,fbuf,tcp=0.0.0.0:${vnc},w=1024,h=768 \
      -s 30,xhci,tablet \
      -s 31,lpc \
      -l bootrom,/zroot/VMs/efi.fd \
      ${name}
   } 
  # Start the installation
  Installation &&
  # Starting the loop after completion of installation
  while true; do
    Starting;
    es=$?
    if [ ${es} -ne 0 ]; then
    break;
    fi
  done &
 

usdmatt

Daemon

Reaction score: 571
Messages: 1,498

Glad you got it working.

I like the use of “Installation &&” to go into the while loop on successful return of the first bhyve command.

Not that it matters but you could probably reduce the loop to just “Starting || break”
 
OP
amr

amr

Member

Reaction score: 2
Messages: 43

Thanks, do you mean by "Starting || break" it will break the loop if the exist status not "0"
 

usdmatt

Daemon

Reaction score: 571
Messages: 1,498

Yes, just like “cmd1 && cmd2” will run cmd2 if cmd1 succeeds (returns 0 exit code), using an “or” instead will run cmd2 if it doesn’t succeed
 
Top