All Products
Search
Document Center

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

Last Updated:Aug 14, 2026

Packer is a lightweight tool for defining and building identical machine images for multiple platforms, such as Windows, Linux, and macOS, in an efficient and parallel manner. This topic describes how to use Packer to create a CentOS 6.9 custom image on an on-premises server that runs Ubuntu 16.04 and import the image to Alibaba Cloud.

Prerequisites

  • You must have an AccessKey ID and an AccessKey secret. For more information, see Create an AccessKey.

    Note
    • AccessKeys grant extensive permissions. For security purposes, we recommend that you create a RAM user and then use the RAM user's AccessKey pair. For more information, see Create a RAM user.

    • A RAM user's AccessKey secret is displayed only upon creation and cannot be retrieved later. Store it securely.

  • Activate OSS and create a bucket. For more information, see Activate OSS.

    Note

    The image file (disk_size) cannot exceed 5 GB. For more information, 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.

Procedure

The following software versions are used in this example. Your steps may vary depending on your software versions.

  • Operating system of the on-premises server: Ubuntu 16.04

  • Operating system for the image to be created: CentOS 6.9

Step 1: Install KVM

  1. Run the following command to check whether your on-premises server, which runs Ubuntu 16.04 in this example, supports KVM.

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

    If the command returns the following output, your 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 your on-premises server supports KVM, run the following commands to install KVM and related packages.

    sudo apt-get update
    sudo apt-get install qemu-kvm qemu virt-manager virt-viewer  libvirt-bin bridge-utils
  3. Verify the KVM installation.

    1. Run the following command to check whether the KVM kernel modules are loaded.

      sudo lsmod | grep kvm

      Output that contains kvm and either kvm_intel or kvm_amd indicates that the KVM kernel modules are loaded.

      dZ:~# sudo lsmod | grep kvm
      kvm_intel            176128  0
      kvm                  561152  1 kvm_intel
      irqbypass             16384  1 kvm
    2. Run the following command to check whether libvirt is installed.

      sudo dpkg -l libvirt-bin

      If the output contains libvirt-bin, libvirt is installed.

      d0dZ:~# sudo dpkg -l libvirt-bin
      Desired=Unknown/Install/Remove/Purge/Hold
      | Status=Not/Inst/Conf-files/Unpacked/half-conf/Half-inst/trig-aWait/Trig-pend
      |/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
      ||/ Name                    Version             Architecture        Description
      +++-=======================-===================-===================-===============================
      ii  libvirt-bin             1.3.1-1ubuntu10.31  amd64               programs for the libvirt library
    3. Run the following command to check whether QEMU is installed.

      sudo dpkg -l qemu-kvm

      If the output contains qemu-kvm, QEMU is installed.

      @dZ:~# sudo dpkg -l qemu-kvm
      Desired=Unknown/Install/Remove/Purge/Hold
      | Status=Not/Inst/Conf-files/Unpacked/half-conf/Half-inst/trig-await/Trig-pend
      |/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
      ||/ Name                    Version              Architecture         Description
      +++-=======================-====================-====================-============================================
      ii  qemu-kvm                1:2.5+dfsg-5ubuntu10.51    amd64                QEMU Full virtualization

    If all commands produce the expected output, KVM is installed correctly. Otherwise, you may need to reinstall KVM.

Step 2: Install Packer

Install Packer on your on-premises server. For more information, see Install Packer.

Step 3: Define a Packer template

The following example commands are for CentOS 6.9. To create an image for a different operating system, customize the centos.json Packer template. For more information, see Customize the Packer template.

  1. On the on-premises server, run the following command to change the current directory.

    cd /usr/local
  2. Run the following command to download the official centos.json configuration file for Alibaba Cloud.

    sudo wget https://raw.githubusercontent.com/hashicorp/packer-plugin-alicloud/main/builder/examples/local/centos.json
  3. Run the following command to download the official ks.cfg configuration file for Alibaba Cloud.

    sudo wget https://raw.githubusercontent.com/hashicorp/packer-plugin-alicloud/main/builder/examples/local/http/centos-6.8/ks.cfg
  4. Run the following command to create a directory, for example, http/centos-6.9.

    sudo mkdir -p http/centos-6.9
  5. Run the following command to move the ks.cfg configuration file to the http/centos-6.9 directory.

    sudo mv ks.cfg http/centos-6.9/

Step 4: Build and import the image

  1. On the on-premises server, run the following command to export your AccessKey ID.

    export ALICLOUD_ACCESS_KEY=<AccessKey ID>

    Replace <AccessKey ID> with your actual AccessKey ID.

  2. Run the following command to export your AccessKey secret.

    export ALICLOUD_SECRET_KEY=<AccessKey secret>

    Replace <AccessKey secret> with your actual AccessKey secret.

  3. Run the following command to build the on-premises image and import it to Alibaba Cloud.

    sudo packer build centos.json

    The following sample output shows a custom image being created in 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 the Images page in the ECS console.

  5. In the top navigation bar, select the region that is specified in the centos.json file, for example, China (Beijing).

  6. On the Custom images tab, find the generated CentOS 6.9 image in the image list.

Customize the Packer template

If you want to create an on-premises image for another operating system, you can customize the Packer template by referring to the following JSON file for CentOS 6.9.

{"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

Packer uses the QEMU builder to create virtual machine images. The following table describes the required parameters for the QEMU builder. For information about optional parameters, see Packer QEMU Builder.

Parameter

Type

Description

iso_checksum

string

The checksum of the operating system ISO file. Packer verifies this checksum before starting the VM to mount the ISO. You must specify at least one of the iso_checksum and iso_checksum_url parameters. If you specify the iso_checksum parameter, the value of the iso_checksum_url parameter is ignored.

iso_checksum_type

string

The checksum algorithm to use for the ISO file. Valid values:

  • none: The checksum verification is skipped. We do not recommend that you specify this value.

  • md5

  • sha1

  • sha256

  • sha512

iso_checksum_url

string

A URL to a file that contains the checksums of the operating system ISO file, in GNU or BSD style. You must specify at least one of the iso_checksum and iso_checksum_url parameters. If you specify the iso_checksum parameter, the value of the iso_checksum_url parameter is ignored.

iso_url

string

A URL to the OS installation ISO file. This URL can be an HTTP URL or a file path:

  • If you specify an HTTP URL, Packer downloads the file from the URL and caches it.

  • If it is a file path to an IMG or QCOW2 file, QEMU uses the file directly to create a custom image. If you specify a file path, set the disk_image parameter to true.

headless

boolean

By default, Packer builds a QEMU virtual machine by starting a GUI. If you set headless to true, Packer starts the virtual machine without a console.

Post-processor parameters

The post-processor automates uploading the on-premises image to Alibaba Cloud. The following table describes the required parameters for the alicloud-import post-processor. For information about optional parameters, see Post-Processors.

Parameter

Type

Description

access_key

string

Your AccessKey ID. For more information about how to view the AccessKey ID of a RAM user, see View the AccessKey information of a RAM user.

secret_key

string

Your AccessKey secret. The AccessKey secret of a RAM user is displayed only when the AccessKey pair is created and cannot be retrieved later. For more information, see Create an AccessKey.

region

string

The ID of the destination Alibaba Cloud region for the on-premises image, for example, cn-beijing in this topic. For more information about regions, see Regions and zones.

image_name

string

The name for the imported custom image.

  • The name must be 2 to 128 characters in length.

  • The name must start with a letter or a Chinese character.

  • The name can contain digits, underscores (_), and hyphens (-).

  • The name cannot start with http:// or https://.

oss_bucket_name

string

The name of your OSS bucket. If you specify a non-existent bucket, Packer automatically creates it during the upload.

image_os_type

string

The type of the operating system. Valid values:

  • linux

  • windows

image_platform

string

The operating system distribution. Example: CentOS.

image_architecture

string

The architecture of the operating system. Valid values:

  • i386

  • x86_64

format

string

The format of the image. Valid values:

  • RAW

  • VHD