Packer is a convenient open-source tool to create on-premises image files. It runs on the most major operating systems.

By using Packer, you can create identical on-premises images for multiple platforms from a single source configuration. This topic details steps to create an on-premises image for CentOS 6.9 on an Ubuntu 16.04 server and to upload it to Alibaba Cloud. For actual scenarios, you can customize your Packer templates as required.


Example of creating and importing an on-premises image

  1. Run egrep "(svm|vmx)" /proc/cpuinfo to check whether your on-premises server or virtual machine supports KVM. If the following output returns, KVM is supported.
    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. Run the following commands to install the KVM:
    sudo apt-get install qemu-kvm qemu virt-manager virt-viewer libvirt-bin bridge-utils # Install KVM and related dependencies.
    sudo virt-manager # Enable virt-manager.

    If a GUI runs in the VM console window, you have successfully installed the KVM.

  3. Install Packer.

    To install Packer, see use Packer to create a custom image.

  4. Run the following commands to define a Packer template.
    Note The on-premises image created in the following configuration is for the CentOS 6.9 operating system only. To create images for other operating systems, customize the configuration file centos.json as required.
    cd /user/local # Switch the directory.
    wget # Download file centos.json that is released by Alibaba Cloud.
    wget # Download file ks.cfg that is released by Alibaba Cloud.
    mkdir -p http/centos-6.9 # Create a directory.
    mv ks.cfg http/centos-6.9/ # Move file ks.cfg to the http/centos-6.9 directory.
  5. Run the following commands to create an on-premises image.
    export ALICLOUD_ACCESS_KEY= SpecifyYourAccessKeyIDHere # Import your AccessKeyID,
    export ALICLOUD_SECRET_KEY= SpecifyYourAccessKeySecretHere # Import your AccessKeySecret.
    packer build centos.json # Create an on-premises image.

    An example result is as follows.

    qemu output will be in this color.
     ==> qemu: Downloading or copying ISO
         qemu: Downloading or copying:
     ==> qemu: Running post-processor: alicloud-import
         qemu (alicloud-import): Deleting import source
     Build 'qemu' finished.
     ==> Builds finished. The artifacts of successful builds are:
     --> qemu: Alicloud images were created:
     cn-beijing: XXXXXXXX
  6. Wait for a few minutes, log on to the ECS console and check your custom image in the image list that is in the corresponding region. In this example, the region is cn-beijing, i.e. China (Beijing).

Customize a Packer template

In this example, the following JSON file is customized based on the template used to create an image for the 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": "",
        "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"
          "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"
          "oss_bucket_name": "packer",
          "image_name": "packer_import",
          "image_os_type": "linux",
          "image_platform": "CentOS",
          "image_architecture": "x86_64",
          "image_system_size": "40",

Parameters in a Packer builder

QEMU builder is used in the preceding example to create a virtual machine image. Required parameters for the builder are as follows.

Parameter Type Description
iso_checksum String The checksum for the OS ISO file. Packer verifies this parameter before starting a virtual machine with the ISO attached. Make sure you specify at least one of the iso_checksum or iso_checksum_url parameters. If you have specified the iso_checksum parameter, the iso_checksum_url parameter is automatically ignored.
iso_checksum_type String The type of the checksum specified in iso_checksum. Optional values:
  • none: If you specify none for iso_checksum_type, the checksuming is ignored. This value is not recommended.
  • md5
  • sha1
  • sha256
  • sha512
iso_checksum_url String A URL that points to a GNU or BSD style checksum file that contains the ISO file checksum of an operating system. It may come in either the GNU or BSD pattern. Make sure you specify either the iso_checksum or the iso_checksum_url parameter. If you specify the iso_checksum parameter, the iso_checksum_url parameter is automatically ignored.
iso_url String A URL that points to the ISO file, and contains the installation image. This URL may be an HTTP URL or a file path:
  • If it is an HTTP URL, Packer downloads the file from the HTTP link and caches the file for later.
  • If it is a file path to the IMG or QCOW2 file, QEMU directly starts the file. If you have the file path specified, set parameter disk_image to true.
headless boolean By default, Packer starts the virtual machine GUI to build a QEMU virtual machine. If you set headless to True, a virtual machine without any console is started.

For more information, see Packer QEMU Builder.

Parameters in a Packer provisioner

The provisioner in the preceding example contains a Post-Processor module that enables automated upload of on-premises images to Alibaba Cloud. Required parameters for the provisioner are as follows:

Parameter Type Description
access_key String Your AccessKeyID. The AccessKey has a high privilege. We recommend that you first create a RAM user and use the RAM account to create an AccessKey to maintain security of your Alibaba Cloud account.
secret_key String Your AccessKeySecret. The AccessKey has a high privilege. We recommend that you first create a RAM user and use the RAM account to create an AccessKey to maintain security of your Alibaba Cloud account.
region String Select the region where you want to upload your on-premises image. In this example, the region is cn-beijing. For more information, see regions and zones.
image_name String The name of your on-premises image. The name is a string of 2 to 128 characters. It must begin with an English or a Chinese character. It can contain A-Z, a-z, Chinese characters, numbers, periods (.), colons (:), underscores (_), and hyphens (-).
oss_bucket_name String The OSS bucket name. If you specify a bucket name that does not exist, Packer creates a bucket automatically with the specified oss bucket name when uploading the image.
image_os_type String Image type. Optional values:
  • linux
  • windows
image_platform String Distribution of the image. For example, CentOS.
image_architecture String The instruction set architecture of the image. Optional values:
  • i386
  • x86_64
format String Image format. Optional values:
  • RAW
  • VHD

For more information, see Packer Alicloud Post-Processor.

Next step

You can use the created image to create an ECS instance. For more information, see create an instance from a custom image.


  • For more information about how to use Packer, see Packer documentation.
  • For more information about release information, visit the Packer repository on GitHub packer.
  • For more information about Alibaba Cloud open source tools, visit the Alibaba Cloud repository on GitHub opstools.
  • For more information about Alibaba Cloud and Packer project, visit the Alibaba Cloud & Packer repositories on GitHub packer-provider.
  • For more information about configuration file ks.cfg, see Anaconda Kickstart .