All Products
Search
Document Center

Elastic Compute Service:Use Packer to create and import an on-premises image

Last Updated:Apr 01, 2024

Packer is a lightweight open source tool that is used to create images in parallel for mainstream operating systems such as Windows, Linux, and macOS. This topic describes how to use Packer to create an on-premises image and upload the image to Alibaba Cloud. In this topic, a CentOS 6.9 image is created on an on-premises server that runs Ubuntu 16.04.

Prerequisite

  • An AccessKey pair that consists of an AccessKey ID and an AccessKey secret is obtained. For more information, see Obtain an AccessKey pair.

    Note
    • To prevent leaks of the AccessKey pair of your Alibaba Cloud account, we recommend that you create a Resource Access Management (RAM) user and use the credentials of the RAM user to create an AccessKey pair. For information about how to create a RAM user, see Create a RAM user.

    • An AccessKey secret for a RAM user is displayed only when you create the AccessKey pair for the RAM user. You cannot query the AccessKey secret after the AccessKey pair is created. Record and keep your AccessKey secret confidential.

  • Object Storage Service (OSS) is activated. For more information, see Activate OSS.

Background information

Packer consists of various components, such as Builders, Provisioners, and Post-Processors. Packer uses templates in the Hashicorp Configuration Language (HCL) or JSON format to simplify the process of creating custom images. In addition, Packer standardizes and automates the image creation process and allows you to define images as code to facilitate your cloud migration.

Procedure

In this example, 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 you want to create an image: CentOS 6.9

Step 1: Install Kernel-based Virtual Machine (KVM)

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

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

    The following sample command output indicates that the source 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 on-premises server supports KVM, run the following command to install KVM and relevant controls:

    sudo apt-get update
    sudo apt-get install qemu-kvm qemu virt-manager virt-viewer  libvirt-bin bridge-utils
  3. Check whether KVM is installed as expected.

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

      sudo lsmod | grep kvm

      If the KVM kernel modules are loaded, a command output that contains kvm and kvm_intel or kvm and kvm_amd is returned.KVM内核

    2. Run the following command to check whether libvirt is installed:

      sudo dpkg -l libvirt-bin

      If libvirt is installed, a command output that contains libvirt-bin is returned.libvirt

    3. Run the following command to check whether QEMU is installed:

      sudo dpkg -l qemu-kvm

      If QEMU is installed, a command output that contains qemu-kvm is returned.QEMU

    If the command outputs are as expected, KVM is installed. If one of the command outputs is not as expected, you may need to re-install KVM.

Step 2: Install Packer

Install Packer on the on-premises server. For more information, see the "Step 1: Install Packer" section in the Use Packer to create a custom image topic.

Step 3: Define a Packer template

In this example, a CentOS 6.9 image is created. To create images for other operating systems, modify the Packer template configuration file centos.json based on your needs. For more information, see Create a Packer template.

  1. Run the following command on the source server to change the working directory to the /usr/local directory:

    cd /usr/local
  2. Run the following command to download the centos.json configuration file that is provided by 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 ks.cfg configuration file that is provided by 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. In this example, the http/centos-6.9 directory is created.

    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: Use Packer to create an on-premises image and import the image

  1. Run the following command on the on-premises server to import your AccessKey ID:

    export ALICLOUD_ACCESS_KEY=<AccessKey ID>

    In the preceding command, replace <AccessKey ID> with your actual AccessKey ID.

  2. Run the following command to import your AccessKey secret:

    export ALICLOUD_SECRET_KEY=<AccessKey Secret>

    In the preceding command, replace <AccessKey Secret> with your actual AccessKey secret.

  3. Run the following command to create an on-premises image and import the image to Alibaba Cloud:

    sudo packer build centos.json

    The following sample command output indicates that the on-premises 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. Log on to the ECS console.

  5. In the left-side navigation pane, choose Instances & Images > Images.

  6. In the upper-left corner of the top navigation bar, select the region where the centos.json configuration file is located. In this example, China (Beijing) is selected.

  7. On the Custom Images tab, view the CentOS 6.9 image.

Create a Packer template

The following JSON code describes how to create a Packer template that is used to create a CentOS 6.9 image. To create an image for another operating system, modify the code based on your needs.

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

Required parameters for the QEMU Packer builder

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

Required 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 attached 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 automatically ignored.

iso_checksum_type

string

The checksum type of the ISO file of the specified operating system. Valid values:

  • none: ignores the checksum process. We recommend that you do not use this value.

  • md5

  • sha1

  • sha256

  • sha512

iso_checksum_url

string

A URL that points to a GNU- or BSD- style checksum file that contains the checksum for the ISO file 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 automatically ignored.

iso_url

string

A URL that points to the ISO file and contains the image. The URL can be an HTTP URL or a file path:

  • If the URL is an HTTP URL, Packer downloads the file from the HTTP URL and caches the file to run the file at a later time.

  • If the URL is a file path to the IMG or QCOW2 file, QEMU Packer builder uses the file to create a custom image. If you specify a file path, set disk_image to true.

headless

boolean

By default, Packer uses the GUI to build a QEMU virtual machine. If you set headless to True, a virtual machine that does not have a console is started.

Required parameters for the Packer provisioner

The Packer provisioner 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 information about the optional parameters, see Post-Processors.

Required parameter

Type

Description

access_key

string

The AccessKey ID that is used to import the on-premises image. For information about how to obtain the AccessKey ID of a RAM user, see View the information about AccessKey pairs of a RAM user.

secret_key

string

The AccessKey secret that is used to import the on-premises image. The AccessKey secret of a RAM user is displayed only when you create the AccessKey pair for the RAM user. You cannot query the AccessKey secret after the AccessKey pair is created. For more information, see Obtain an AccessKey pair.

region

string

The ID of the region where you want to upload the on-premises image. In this example, cn-beijing is used. For more information, see Regions and zones.

image_name

string

The name of the on-premises image that you want to upload.

  • 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 the OSS bucket to which the on-premises image is uploaded. If the specified OSS bucket does not exist, Packer automatically creates an OSS bucket with the specified name when Packer uploads the image.

image_os_type

string

The type of the on-premises image. Valid values:

  • linux

  • windows

image_platform

string

The distribution of the on-premises image. In this example, CentOS is used.

image_architecture

string

The architecture of the on-premises image. Valid values:

  • i386

  • x86_64

format

string

The format of the on-premises image. Valid values:

  • RAW

  • VHD