Packer is a lightweight open source tool for creating images and runs on commonly used operating systems such as Windows, Linux, and macOS. This topic describes how to create an on-premises image for CentOS 6.9 and upload the image to Alibaba Cloud. You can create a Packer template to create images for other operating systems.

Prerequisites

  • An AccessKey pair is created. For more information, see Obtain an AccessKey pair.
    Note To prevent disclosing the AccessKey pair of your Alibaba Cloud account, we recommend that you create a RAM user and use the credentials of the RAM user to create an AccessKey pair. For more information, see Create a RAM user.
  • The Object Storage Service (OSS) service is activated. For more information, see Activate OSS.

Background information

In this topic, the following software versions are used. Operations may vary based on your software version.
  • Operating system of the on-premises server: Ubuntu 16.04
  • Operating system for which to create an image: CentOS 6.9

Example

In this example, CentOS 6.9 is used. Perform the following operations to create an on-premises image:

  1. Run the egrep "(svm|vmx)" /proc/cpuinfo command to check whether your on-premises server or virtual machine supports Kernel-based Virtual Machine (KVM).
    If KVM is supported, a command output similar to the following one is returned:
    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 in sequence to install KVM if your on-premises server or virtual machine supports KVM:
    1. sudo apt-get install qemu-kvm qemu virt-manager virt-viewer  libvirt-bin bridge-utils  #Install KVM and related controls.
    2. sudo virt-manager  #Enable virt-manager.

    If KVM is installed, you can create a virtual machine on the Graphical User Interface (GUI).

  3. Install Packer.
    For more information, see Use Packer to create a custom image.
  4. Run the following commands in sequence to create a Packer template.
    Note The following sample commands are based on the CentOS 6.9 operating system. If you want to create on-premises images for other operating systems, create the centos.json configuration file. For more information, see the "Create a Packer template" in this topic.
    1. cd /usr/local  #Switch the directory.
    2. wget https://raw.githubusercontent.com/alibaba/packer-provider/master/examples/alicloud/local/centos.json  #Download the centos.json configuration file that is released by Alibaba Cloud.
    3. wget https://raw.githubusercontent.com/alibaba/packer-provider/master/examples/alicloud/local/http/centos-6.9/ks.cfg  #Download the ks.cfg configuration file that is released by Alibaba Cloud.
    4. mkdir -p http/centos-6.9  #Create a directory.
    5. mv ks.cfg http/centos-6.9/  #Move the ks.cfg file to the http/centos-6.9 directory.
  5. Run the following commands in sequence to create and import an on-premises image:
    1. export ALICLOUD_ACCESS_KEY=<Your AccessKey ID>      #Import your AccessKey ID.
    2. export ALICLOUD_SECRET_KEY=<Your AccessKey secret>  #Import your AccessKey secret.
    3. packer build centos.json                          #Create an on-premises image and import it to Alibaba Cloud.
    A command output similar to the following one is returned:
    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
  6. Wait a few minutes and go to the Images page in the ECS console to view the custom image in the specified region. In this example, the image is created in the China (Beijing) region.

Create a Packer template

In the preceding example, CentOS 6.9 is used to create an on-premises image. For more information, see the "Example" in this topic. The following JSON file shows how to create a Packer template based on 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"
        }
      ]
}
In this example, the QEMU Packer builder is used to create a virtual machine image. The following table describes the required parameters for the QEMU Packer builder. For more information about optional parameters, visit QEMU Builder.
Table 1. Required parameters for the QEMU Packer builder
Parameter Type Description
iso_checksum string The checksum for the ISO file of the operating system. Packer verifies this parameter before a virtual machine to which the ISO file is mounted is started. Make sure that you specify at least one of the iso_checksum and iso_checksum_url parameters. If you specify the iso_checksum parameter, the value of iso_checksum_url is ignored.
iso_checksum_type string The checksum type of the ISO file of the specified operating system. Valid values:
  • none: If you set iso_checksum_type to none, the checksum process 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. Make sure that you specify at least one of the iso_checksum and iso_checksum_url parameters. If you specify the iso_checksum parameter, the value of iso_checksum_url is ignored.
iso_url string A URL that points to the ISO file and contains the installation image. This URL can be an HTTP URL or a file path:
  • If the URL is an HTTP URL, Packer downloads the ISO file from the HTTP URL and caches the file for running it later.
  • If the URL is a file path of an IMG or QCOW2 file, the QEMU Packer builder uses the file to create a custom image. If you specify the file path, set disk_image to true.
headless boolean By default, Packer starts the GUI to build a QEMU virtual machine. If you set headless to True, a virtual machine without a console is started.
The provisioner used in the preceding example contains a Post-Processor module that enables automated upload of on-premises images to Alibaba Cloud. The following table describes the required parameters for the Packer provisioner. For more information about optional parameters, visit Alicloud Import Post-Processor.
Table 2. Required parameters for the Packer provisioner
Parameter Type Description
access_key string Your AccessKey ID.
secret_key string Your AccessKey secret.
region string The ID of the region to which you want to upload your on-premises image. In this example, the region ID is cn-beijing. For more information about regions, see Regions and zones.
image_name string The name of your on-premises image.
  • The name must be 2 to 128 characters in length.
  • The name must start with a letter.
  • The name can contain letters, 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 bucket name that does not exist, Packer creates a bucket with the specified name when Packer uploads the image.
image_os_type string The type of the image. Valid values:
  • linux
  • windows
image_platform string The distribution of the image. In this example, CentOS is used.
image_architecture string The architecture of the image. Valid values:
  • i386
  • x86_64
format string The format of the image. Valid values:
  • RAW
  • VHD

What to do next

You can use the custom image uploaded to Alibaba Cloud to create Elastic Compute Service (ECS) instances. For more information, see Create an ECS instance by using a custom image.