Skip to content

Examples

Some examples are provided here for demonstration and convenience.

Provisioning script

If you have a lot of run-command directives in your customize section, you might prefer to put your commands in a script and use virt-builder's run directive instead. In this example, 'setup.sh' exists in the same directory as the virtfile. The run directive copies it to the VM and executes it.

Or, if you want a script to run during first-boot when the VM is actually running, you can use the firstboot option. Both are demonstrated below:

Example: using scripts for customisation
virtfile
source: virt-builder
image: fedora-42

customize:
- install: openssh,nginx
- run: provision.sh
- firstboot: firstboot.sh

machine:
- vcpu: 2
- memory: 2048
provision.sh
#!/bin/bash
# This script is run during the image setup by the provisioner,
# before the VM is booted.

systemctl enable sshd.service nginx.service

# firewall-offline-cmd --add-service=http
firstboot.sh
#!/bin/bash
# This script is run inside the VM during the first boot.

firewall-cmd --add-service=http

# alternatively you could use firewall-offline-cmd in setup.sh, but this is just
# for demonstration.