Autostart shell scripts

To manage a virtual machine as a system service or to start it automatically at system startup, the call of bhyve or a shell script can be included in the rc framework.

The GitHub repository: https://github.com/Beckhoff/TCBSD_Hypervisor_Samples/tree/main/vm_autostart includes sample files that demonstrate how to incorporate a simple VM configuration into the rc framework using shell scripts. The sample in the directory vm_autostart contains the appropriate files for this.

If the sample script has been loaded onto the TwinCAT/BSD host as described in chapter Use shell scripts, you can change to the directory with cd /home/Administrator/TCBSD_Hypervisor_Samples-main/vm_autostart.

vm_autostart
├── Makefile
├── rc.d
│   └── samplevm
└── samplevm

The sample script samplevm shown in the chapter Use shell scripts has been extended by start, stop and status parameters to be able to start and stop a VM configuration with VNC access via the command line.

The vm_autostart/rc.d/samplevm shell script is used to integrate the vm_autostart/samplevm shell script into the rc framework.

Controlling the VM system service with the sample script:

1. Navigate to the directory with cd /home/Administrator/TCBSD_Hypervisor_Samples-main/vm_autostart
2. Enter the command doas make to install both files from the vm_autostart directory on the TwinCAT/BSD host.
3. Then enter doas service samplevm enable to enable the VM instance as a system service for autostart via service(8).
4. After including the shell script as a system service, the virtual machine can be started with the doas service samplevm start command.
5. The command doas service samplevm status shows whether the virtual machine has been restarted and with which process ID it is running.
In this sample, the virtual machine can be accessed via a VNC client via TCP port “5900”. From now on, the virtual machine is also restarted after a reboot of the TwinCAT/BSD host and is available for use.
1. The virtual machine can be stopped again with the command doas service samplevm stop.
2. The autostart of the VM is disabled again with the command doas service samplevm disable .

The sample script is a first starting point and shows how virtual machines can be started, managed and automated under TwinCAT/BSD. The sample script vm_autostart/samplevm can be customized and extended as needed to achieve a desired VM configuration. The chapter Advanced VM configuration explains other parameters that can be used to extend the configuration of a virtual machine.

For detailed information about creating rc.d scripts, see the FreeBSD Handbook chapter Practical rc.d scripting in BSD.