For AI agents: the complete documentation index is available at https://docs.us.ovhcloud.com/en/llms.txt, the full documentation bundle is available at https://docs.us.ovhcloud.com/en/llms-full.txt, and this page is available as Markdown at https://docs.us.ovhcloud.com/en/public-cloud/compute/create-image-from-existing-image-with-packer.md.
  • English

Create a custom OpenStack image with Packer

View as Markdown

Requirements

OVHcloud Control Panel Access

 

Instructions

Install Packer

Packer can be downloaded from the official website (currently here), and you'll need to unzip it.

For Linux 64-bit

wget https://releases.hashicorp.com/packer/1.11.2/packer_1.11.2_linux_amd64.zip
unzip packer_1.11.2_linux_amd64.zip

 

Install the OpenStack plugin for Packer

packer plugins install github.com/hashicorp/openstack

 

Install jq

jq is a command line tool for parsing JSON document. It'll be used to automate the configuration file creation.

apt-get install jq

 

Fetch your openrc.sh configuration

You can retrieve the openrc.sh file from the OVHcloud Control Panel using the procedure documented in this guide.

 

Install OpenStack command line client

The easiest way is to use a python virtual environment:

python3 -m venv venv3 # creates a virtualenv named venv3
. ./venv3/bin/activate # enter the virtualenv
pip install --upgrade pip
pip install python-openstackclient

or install your distribution package apt-get install python-openstackclient.

 

Verification

Sourcing the openrc.sh configuration file retrieved before, try your local setup with:

. ./openrc.sh
openstack token issue

 

Packer configuration

First, source your openrc.sh file with:

. ./openrc.sh

Next, you'll need the ID of the image, flavor, and network. We'll build our image from Ubuntu 24.04 on a b2-7 hardware, with an interface connected on a public network Ext-Net.

export SOURCE_ID=`openstack image list -f json | jq -r '.[] | select(.Name == "Ubuntu 24.04") | .ID'`
export FLAVOR_ID=`openstack flavor list -f json | jq -r '.[] | select(.Name == "b2-7") | .ID'`
export NETWORK_ID=`openstack network list -f json | jq -r '.[] | select(.Name == "Ext-Net") | .ID'`
Info

For FLAVOR_ID, you can directly use the name, e.g. b2-7.

Finally, create a packer.json file:

cat > packer.json <<EOF
{
    "variables": {
        "os_region_name": "{{env `OS_REGION_NAME`}}",
        "os_tenant_id": "{{env `OS_TENANT_ID`}}",
        "source_id": "{{env `SOURCE_ID`}}",
        "flavor_id": "{{env `FLAVOR_ID`}}",
        "network_id": "{{env `NETWORK_ID`}}"
    },
    "builders": [
        {
            "type": "openstack",
            "region": "{{user `os_region_name`}}",
            "tenant_id": "{{user `os_tenant_id`}}",
            "image_name": "My Custom Image",
            "ssh_username": "ubuntu",
            "source_image": "{{user `source_id`}}",
            "flavor": "{{user `flavor_id`}}",
            "ssh_ip_version": "4",
            "networks": [
                "{{user `network_id`}}"
            ]
        }
    ],
    "provisioners": [
        {
            "script": "setup_vm.sh",
            "type": "shell"
        }
    ]
}
EOF

In the last selection of the configuration file, we specify a setup_vm.sh shell script to be run:

#!/bin/sh

set -ex

if [ `id -u` -ne 0 ]; then
     sudo $0
    exit 0
fi

## your custom code below
apt-get install git
git clone ...

 

Building the image

Using the configuration file created above, check it and build the image with:

packer validate packer.json
packer build packer.json

You should have a new image available. You can check with:

openstack image list | grep 'My Custom Image'
Info

TIP: To enable debug information, use export PACKER_LOG=1.

 

Go further

Browse all the Compute guides.

Browse the rest of the OVHcloud US documentation for guides on our other products and services.

Learn more about Public Cloud Compute on the OVHcloud US website.