How to build Multipass images with Packer

Custom images are supported by Multipass only on Linux.

Packer is a utility that lets you (re)build images to run in a variety of environments. Multipass can run those images too, provided some requirements are met (namely, the image has to boot on the hypervisor in use, and cloud-init needs to be available).


Setting up the build directory

The easiest way is to start from an existing Ubuntu Cloud Image, and the base project setup follows (you can click on the filenames to see their contents, meta-data is empty on purpose):

├── cloud-data
│   ├── meta-data
│   └── user-data
└── template.json

1 directory, 3 files

To specify a different image or image type, modify the iso_checksum and iso_url fields within template.json

Building the image

You will need to install QEMU and Packer (e.g. sudo apt install qemu packer), and from there you can run the following commands:

$ packer build template.json
$ multipass launch file://$PWD/output-qemu/packer-qemu --disk 5G
Launched: tolerant-hammerhead
$ multipass shell tolerant-hammerhead
ubuntu@tolerant-hammerhead ~:$ 

Customizing the image

Now the above works for you, delete the test instance with multipass delete --purge tolerant-hammerhead and edit the following section in the template.json file:

            "type": "shell",
            "inline": ["echo Your steps go here."]

Anything you do here will be reflected in the resulting image. You can install packages, configure services, anything you can do on a running system. You’ll need sudo (passwordless) for anything requiring admin privileges, or you could add this to the above provisioner to run the whole script privileged:

            "execute_command": "sudo sh -c '{{ .Vars }} {{ .Path }}'",

Next steps

Go to Packer’s documentation to learn about the QEMU builder, other provisioners and their configurations as well as everything else that might come in handy. Alternatively, you could extend user-data with other cloud-init modules to provision your image.

Please come to our discourse space and let us know about your images!

Last updated 12 days ago.