Creating a Private Image Using Packer
Packer is an open-source tool that can be used to create custom images. It consists of three components, builder, provisioner, and post-processor. The components can be flexibly combined using a JSON template to automatically create image files. Packer simplifies private image creation by changing the image creation process to a form in which management code can be configured. In this way, users can flexibly customize images and switch images between different cloud platforms.
This section describes how to create a Ubuntu 16.04 Server 64-bit private image from a CentOS 7.4 64-bit ECS using Packer and upload the created image to the public cloud platform.
Constraints
Full-ECS images cannot be used to create private images using Packer.
Procedure

Installing Packer
- Log in to the management console, create an ECS (for example, an ECS running CentOS 7.4 64-bit), and bind an EIP to the ECS.
- Log in to the ECSs.
- On the Packer download page, select the Packer version corresponding to the ECS OS and architecture type. You are advised to select a version ranging from 1.2.3 to 1.4.2.
- Run the following command to install Packer (packer_1.4.2_linux_amd64.zip is used as an example):
wget --no-check-certificate https://releases.hashicorp.com/packer/1.4.2/packer_1.4.2_linux_amd64.zip
- You need to bind an EIP to the ECS in advance so that the ECS can access the Internet.
- If message "command not found" is displayed, the wget tool is not installed. Run the yum install wget command to install the wget tool.
- Run the following command to decompress the Packer installation package:
- Run the following command to move the Packer installation package to the /usr/local/bin directory: mv packer /usr/local/bin
The /usr/local/bin directory has been added to environment variables. You can also move the Packer installation package to another directory that has been added to environment variables.
- Run the following command to query the Packer version number and check whether Packer is installed successfully:
- If the command output contains the Packer version number, Packer is installed successfully.
- If "command not found" is displayed, the Packer installation fails. Check whether the directory where Packer resides has been added to environment variables.
Run the env | grep PATH command to print environment variables and check whether the environment variable PATH contains the Packer installation directory.
If the PATH does not contain the Packer installation directory, run the following commands to add the Packer installation directory to PATH:
- Run the following command to open the profile file:
vim /etc/profile
- Press i to enter editing mode and add export PATH=$PATH:/usr/local/bin to the end of the file.
Replace /usr/local/bin with the actual directory where Packer is installed.
- Press Esc to exit editing mode. Enter :wq and press Enter to save the changes and exit.
- Run the following command to make the change take effect:
- Run the following command to open the profile file:
Creating a Packer Template
To create an image using Packer, you need a template in JSON format. You need to specify a builder, provisioner, and post-processor in the template. In the Builder, you can specify any operation on the source image, specify the installation software, and modify configuration. In this example, a post-processor is used to redirect the output path of manifest. If your Packer template has multiple builders, you can locate the ID of the image created from each builder based on the manifest output. For details about the builder, provisioner, and post-processor, see the official Packer documents.
This section uses the Shell provisioner as an example.
- Run the following command to create the hwcloud.json file:
touch hwcloud.json
- Run the following command to open the hwcloud.json file:
vim hwcloud.json
- Press i to enter editing mode and edit the template based on the site requirements. The following content is for reference only. For the parameter descriptions, see Table 1.
{ "builders": [{ "type": "openstack", "identity_endpoint": "https://iam.xxx.com/v3", "tenant_name": "xxx", "domain_name": "domain_name", "username": "username", "password": "password", "ssh_username": "root", "region": "xxx", "image_name": "Ubuntu-image-updating-powered-by-Packer", "instance_name": "Ubuntu-image-updating-powered-by-Packer", "source_image": "f1dd2272-7041-479e-9663-646632b6ac00", "availability_zone": "xxx", "flavor": "s3.medium.2", "use_blockstorage_volume": true, "networks": ["11d661c4-e41f-487f-a6f6-9b88d623dd5d"], "floating_ip": "8f686f9a-3408-4fdd-be75-ea768065800c" }], "provisioners": [{ "inline": [ "apt-get update -y" ], "inline_shebang": "/bin/sh -x", "type": "shell", "skip_clean": true }], "post-processors": [{ "strip_path": true, "output": "packer-template-ubuntu-updating-result.log", "type": "manifest" }] }
In Table 1, tenant_name, region, availability_zone, flavor, networks, and floating_ip are the attributes of the ECS used to create the private image.
Table 1 Packer template parameters Parameter
Description
Mandatory
type
Specifies the type. Retain the default value openstack.
Yes
identity_endpoint
Specifies the address of the identity authentication node. The format is https://IAM endpoint/v3.
Obtain the IAM endpoint from Regions and Endpoints.
Yes
tenant_name
Specifies the project name. To obtain the project name, perform the following operations:
- On the management console, move the cursor to the username in the upper right corner and choose My Credentials.
- On the API Credentials page, obtain the project name (value in the Project Name column).
Yes
domain_name
Specifies the domain name. To obtain the domain name, perform the following operations:
- On the management console, move the cursor to the username in the upper right corner and choose My Credentials.
- On the API Credentials page, obtain the domain name.
Yes
username
Specifies the IAM username. To obtain the IAM username, perform the following operations:
- On the management console, move the cursor to the username in the upper right corner and choose My Credentials.
- On the API Credentials page, obtain the IAM username.
NOTE:If you use an account to log in to the HUAWEI CLOUD console, the IAM username and account name are the same.
Yes
password
Specifies the password for logging in to the management console.
Yes
ssh_username
Specifies the SSH login username of the private image to be created.
Yes
region
Specifies the region name. Obtain the region name from Regions and Endpoints.
Yes
image_name
Specifies the name of the private image to be created.
Yes
instance_name
Specifies the name of the temporary instance generated during the private image creation. If you do not set this parameter, the system uses a random value.
No
source_image
Specifies the ID of the source image, which can be obtained from the public image list on the IMS console. If you already have a Ubuntu 16.04 Server 64-bit private image and want to reconstruct the image using Packer, you can enter the ID of the private image.
Yes
availability_zone
Specifies the AZ. Obtain the AZ from Regions and Endpoints.
Yes
flavor
Specifies the ECS flavor.
Yes
use_blockstorage_volume
Specifies whether the system disk rather than the whole ECS is used to create an image.
Yes. The value must be true.
networks
Specifies the ID of the VPC subnet.
Yes
floating_ip
Specifies the EIP ID.
This parameter is mandatory if the image instance created using Packer needs to access the Internet.
provisioners
Specifies the type of the Packer provisioner used to create the private image. For details, see Packer Provisioners.
Yes
post-processors
Specifies the type of the Packer post-processor used to create the private image.
No
Creating a Private Image Using Packer
- After the Packer template is created, run the following command to create an image:
The command output is as follows:
openstack output will be in this color. ==> openstack: Loading flavor: s3.small.1 openstack: Verified flavor. ID: s3.small.1 ==> openstack: Creating temporary keypair: packer_5be8d358-2cc6-66a4-f1b5-31e8587c7bfa ... ==> openstack: Created temporary keypair: packer_5be8d358-2cc6-66a4-f1b5-31e8587c7bfa ==> openstack: Launching server... ==> openstack: Launching server... openstack: Server ID: fcf2014e-2f70-46c5-80d5-870ae0d1e659 ==> openstack: Waiting for server to become ready... openstack: Selected floating IP: '8f686f9a-3408-4fdd-be75-ea768065800c' (119.3.67.11) ==> openstack: Associating floating IP '8f686f9a-3408-4fdd-be75-ea768065800c' (119.3.67.11) with instance port... openstack: Added floating IP '8f686f9a-3408-4fdd-be75-ea768065800c' (119.3.67.11) to instance! ==> openstack: Using ssh communicator to connect: 119.3.67.11 ==> openstack: Waiting for SSH to become available... ==> openstack: Connected to SSH! ==> openstack: Provisioning with shell script: /tmp/packer-shell133419321 openstack: + apt-get update -y openstack: Hit:1 http://archive.ubuntu.com/ubuntu xenial InRelease ...... openstack: Fetched 7,088 kB in 9s (778 kB/s) openstack: Reading package lists... ==> openstack: Stopping server: fcf2014e-2f70-46c5-80d5-870ae0d1e659 ... openstack: Waiting for server to stop: fcf2014e-2f70-46c5-80d5-870ae0d1e659 ... ==> openstack: Creating the image: CentOS-image-updating-powered-by-Packer openstack: Image: 9eccbb17-9aed-4beb-bf44-1e8c80448ba3 ==> openstack: Waiting for image CentOS-image-updating-powered-by-Packer (image id: 9eccbb17-9aed-4beb-bf44-1e8c80448ba3) to become ready... ==> openstack: Terminating the source server: fcf2014e-2f70-46c5-80d5-870ae0d1e659 ... ==> openstack: Deleting temporary keypair: packer_5be8d358-2cc6-66a4-f1b5-31e8587c7bfa ... ==> openstack: Running post-processor: manifest Build 'openstack' finished. ==> Builds finished. The artifacts of successful builds are: --> openstack: An image was created: 9eccbb17-9aed-4beb-bf44-1e8c80448ba3 --> openstack: - Log in to the management console and click Image Management Service under Computing.
- Click the Private Images tab and view the image created using Packer. Figure 1 shows the created image.
Helpful Links
Packer official guide: https://www.packer.io/intro/getting-started/install.html
Last Article: Converting the Image Format Using qemu-img-hw
Next Article: Configuring an ISO File as a Local Image Source

Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.