Creating images with libguestfs
The first step in creating a secure and regularly updated app for VM Squared is automating a repeatable build. Automated app builds enable you to easily ship security fixes, which ensure that your new VMs have a secure and fully-featured image.
In order to create an automated app for VM Squared we will describe the process for building the disk image that will be used to deploy the VM running your app.
We will be using libguestfs to build our disk image for the app.
Basics of libguestfs
Libguestfs enables us to customize existing Linux disk images to add packages, scripts and configuration settings.
The final image will contain an operating system, customized to include the following:
A contextualization package (
one-context
)When a VM is instantiated, it will be contextualized by VM Squared. This is when the VM’s individual configuration is applied, such as networking and SSH keys.
We will use the latest one-context package from the addon-context-linux repo.
A
firstboot
scriptThis script will be executed when the VM is first booted up after being instantiated in VM Squared.
This can be used to perform any configuration or setup that cannot be applied at build time, such as editing VM’s config files with any input provided by the user during setup.
App files
Anything needed for your app, such as packages, code, templates, configuration files etc.
Requirements
You will need:
- Linux system capable of running libguestfs.
- Packages are available for Fedora, Red Hat, Debian and Ubuntu.
- Target OS VM image in
RAW
orQCOW2
format. - Disk space to store and manipulate your chosen image.
Image preparation
To keep things simple, we’ll create a Debian image with one-context and Grafana installed. The Grafana admin username & password will be set in VM Squared.
Writing a build.sh script
It’s not really a script
It’s mostly a virt-builder
command broken into multiple lines for readability , convenience and reproducible results.
Contents of build.sh
:
Running scripts at boot
This script refers to some files that don’t exist yet, so we’ll make them:
The file firstboot.sh
was specified with --firstboot
which runs this script when the image first boots up, after being instantiated in VM Squared.
With the user’s inputs during setup, we can finish configuring the app.
The output of this script will be written to /root/virt-sysprep-firstboot.log
Contents of firstboot.sh
:
Uploading files to image
We can also upload files to the image with the --upload
parameter.
The file local-config/grafana.ini
was specified as an upload.
Build the image
With all our files in place, we can run the build script:
Successful output:
You can now upload the built image to VM Squared as an Operating System image, and Create your VM Template