All Products
Search
Document Center

Elastic Compute Service:Create and import an on-premises image with Packer

Last Updated:Apr 27, 2026

Build a CentOS 6.9 image on an Ubuntu 16.04 server with Packer and KVM, then import it to Alibaba Cloud as a custom image.

Prerequisites

  • An AccessKey pair (AccessKey ID and AccessKey secret) is obtained.

    Note
    • To prevent AccessKey leaks, create a RAM user and use the RAM user's credentials. See Create a RAM user.

    • The AccessKey secret is displayed only when the AccessKey pair is created. Record and keep it confidential.

  • OSS is activated and an OSS bucket is created.

    Note

    The image disk size cannot exceed 5 GB. See PutObject.

Background

Packer uses Builders, Provisioners, and Post-Processors to turn HCL or JSON templates into custom images, replacing manual image creation with configuration as code. See the Packer documentation for details.

Build and import the image

The following software versions are used in this example. Operations may vary with your software version.

  • On-premises server OS: Ubuntu 16.04

  • Target image OS: CentOS 6.9

Step 1: Install KVM

  1. Check whether the on-premises server supports KVM:

    egrep "(svm|vmx)" /proc/cpuinfo

    If the output contains vmx or svm, the server supports KVM.

    pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch epb intel_pt tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx smap clflushopt xsaveopt xsavec xgetbv1 xsaves dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp
    flags        : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov
  2. If the server supports KVM, install KVM and related tools:

    sudo apt-get update
    sudo apt-get install qemu-kvm qemu virt-manager virt-viewer  libvirt-bin bridge-utils
  3. Verify that KVM is installed.

    1. Verify that the KVM kernel modules are loaded:

      sudo lsmod | grep kvm

      If the modules are loaded, the output contains kvm and kvm_intel or kvm_amd.KVM内核

    2. Verify that libvirt is installed:

      sudo dpkg -l libvirt-bin

      If installed, the output contains libvirt-bin.libvirt

    3. Verify that QEMU is installed:

      sudo dpkg -l qemu-kvm

      If installed, the output contains qemu-kvm.QEMU

    If any output is unexpected, reinstall KVM.

Step 2: Install Packer

Install Packer on the on-premises server. See the "Step 1: Install Packer" section in Create a custom image with Packer.

Step 3: Define a Packer template

This example creates a CentOS 6.9 image. To create images for other operating systems, modify the centos.json template. See Create a Packer template.

  1. Change the working directory to /usr/local:

    cd /usr/local
  2. Download the centos.json template:

    sudo wget https://raw.githubusercontent.com/hashicorp/packer-plugin-alicloud/main/builder/examples/local/centos.json
  3. Download the ks.cfg kickstart file:

    sudo wget https://raw.githubusercontent.com/hashicorp/packer-plugin-alicloud/main/builder/examples/local/http/centos-6.8/ks.cfg
  4. Create the http/centos-6.9 directory:

    sudo mkdir -p http/centos-6.9
  5. Move the ks.cfg configuration file to the http/centos-6.9 directory.

    sudo mv ks.cfg http/centos-6.9/

Step 4: Create and import the image

  1. Export your AccessKey ID:

    export ALICLOUD_ACCESS_KEY=<AccessKey ID>

    Replace <AccessKey ID> with your actual AccessKey ID.

  2. Export your AccessKey secret:

    export ALICLOUD_SECRET_KEY=<AccessKey Secret>

    Replace <AccessKey Secret> with your actual AccessKey secret.

  3. Build the on-premises image and import it to Alibaba Cloud:

    sudo packer build centos.json

    The following output indicates that the image is imported as a custom image to the China (Beijing) region.

    qemu output will be in this color.
     ==> qemu: Downloading or copying ISO
         qemu: Downloading or copying: http://mirrors.aliyun.com/centos/6.9/isos/x86_64/CentOS-6.9-x86_64-minimal.iso
     ................................................
     ==> qemu: Running post-processor: alicloud-import
         qemu (alicloud-import): Deleting import source https://oss-cn-beijing.aliyuncs.com/packer/centos_x86_64
     Build 'qemu' finished.
     ==> Builds finished. The artifacts of successful builds are:
     --> qemu: Alicloud images were created:
     cn-beijing: XXXXXXXX
  4. Go to ECS console - Images.

  5. Select the region specified in centos.json. In this example, select China (Beijing).

  6. On the Custom Images tab, verify that the CentOS 6.9 image appears.

Packer template reference

The following JSON template creates a CentOS 6.9 image. Modify it for other operating systems as needed.

{"variables": {
        "box_basename": "centos-6.9",
        "build_timestamp": "{{isotime \"20060102150405\"}}",
        "cpus": "1",
        "disk_size": "4096",
        "git_revision": "__unknown_git_revision__",
        "headless": "",
        "http_proxy": "{{env `http_proxy`}}",
        "https_proxy": "{{env `https_proxy`}}",
        "iso_checksum_type": "md5",
        "iso_checksum": "af4axxxxxxxxxxxxxxxxx192a2",
        "iso_name": "CentOS-6.9-x86_64-minimal.iso",
        "ks_path": "centos-6.9/ks.cfg",
        "memory": "512",
        "metadata": "floppy/dummy_metadata.json",
        "mirror": "http://mirrors.aliyun.com/centos",
        "mirror_directory": "6.9/isos/x86_64",
        "name": "centos-6.9",
        "no_proxy": "{{env `no_proxy`}}",
        "template": "centos-6.9-x86_64",
        "version": "2.1.TIMESTAMP"
      },
    "builders":[
        {
          "boot_command": [
            "<tab> text ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/{{user `ks_path`}}<enter><wait>"
          ],
          "boot_wait": "10s",
          "disk_size": "{{user `disk_size`}}",
          "headless": "{{ user `headless` }}",
          "http_directory": "http",
          "iso_checksum": "{{user `iso_checksum`}}",
          "iso_checksum_type": "{{user `iso_checksum_type`}}",
          "iso_url": "{{user `mirror`}}/{{user `mirror_directory`}}/{{user `iso_name`}}",
          "output_directory": "packer-{{user `template`}}-qemu",
          "shutdown_command": "echo 'vagrant'|sudo -S /sbin/halt -h -p",
          "ssh_password": "vagrant",
          "ssh_port": 22,
          "ssh_username": "root",
          "ssh_wait_timeout": "10000s",
          "type": "qemu",
          "vm_name": "{{ user `template` }}.raw",
          "net_device": "virtio-net",
          "disk_interface": "virtio",
          "format": "raw"
        }
      ],
    "provisioners": [{
          "type": "shell",
          "inline": [
                "sleep 30",
                "yum install cloud-util cloud-init -y"
              ]
        }],
      "post-processors":[
        {
          "type":"alicloud-import",
          "oss_bucket_name": "packer",
          "image_name": "packer_import",
          "image_os_type": "linux",
          "image_platform": "CentOS",
          "image_architecture": "x86_64",
          "image_system_size": "40",
          "region":"cn-beijing"
        }
      ]
}

QEMU builder parameters

The QEMU builder creates KVM virtual machine images. The following table lists the required parameters. For optional parameters, see QEMU Builder.

Parameter

Type

Description

iso_checksum

string

The checksum for the OS ISO file. Packer verifies this before starting the virtual machine. Specify at least one of iso_checksum and iso_checksum_url. If iso_checksum is specified, iso_checksum_url is ignored.

iso_checksum_type

string

The checksum type for the OS ISO file. Valid values:

  • none: skips checksum verification. Not recommended.

  • md5

  • sha1

  • sha256

  • sha512

iso_checksum_url

string

A URL pointing to a GNU- or BSD-style checksum file for the OS ISO. Specify at least one of iso_checksum and iso_checksum_url. If iso_checksum is specified, iso_checksum_url is ignored.

iso_url

string

A URL or file path to the ISO image:

  • HTTP URL: Packer downloads and caches the file.

  • File path to an IMG or QCOW2 file: Set disk_image to true.

headless

boolean

By default, Packer builds a QEMU virtual machine with a GUI. Set headless to True to start a headless virtual machine.

Packer post-processor parameters

The Post-Processor module uploads on-premises images to Alibaba Cloud. The following table lists the required parameters. For optional parameters, see Post-Processors.

Parameter

Type

Description

access_key

string

The AccessKey ID for importing the image. See View AccessKey pair details.

secret_key

string

The AccessKey secret for importing the image. The secret is shown only at creation time. See Create an AccessKey pair.

region

string

The target region for the image. Example: cn-beijing. See Regions and zones.

image_name

string

The name of the image to upload.

  • 2 to 128 characters in length.

  • Must start with a letter.

  • Can contain letters, digits, underscores (_), and hyphens (-).

  • Cannot start with http:// or https://.

oss_bucket_name

string

The OSS bucket for uploading the image. If the bucket does not exist, Packer creates it automatically.

image_os_type

string

The image OS type. Valid values:

  • linux

  • windows

image_platform

string

The OS distribution. Example: CentOS.

image_architecture

string

The image architecture. Valid values:

  • i386

  • x86_64

format

string

The image format. Valid values:

  • RAW

  • VHD