Packer is a lightweight open source tool for creating images and runs on commonly used mainstream 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 Create an AccessKey.
Note To avoid 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 OSS service is activated. For more information, see Activate OSS.
Background information
- Operating system of the on-premises server: Ubuntu 16.04
- Operating system from which to create an image: CentOS 6.9
Example
CentOS 6.9 is used in this example. Perform the following operations to create an on-premises image:
Create a Packer template
{"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"
}
]
}
Parameter | Type | Description |
---|---|---|
iso_checksum | string | The checksum for the ISO file of the operating system. Packer verifies this parameter before a virtual machine with the ISO file attached is started. Make sure that you specify at least one of the iso_checksum and iso_checksum_url parameters. If you specified 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:
|
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 specified 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 installation image. This URL can
be an HTTP URL or file path:
|
headless | boolean | By default, Packer starts the GUI to build a QEMU virtual machine. If you set headless to True, a virtual machine without any console is started. |
Parameter | Type | Description |
---|---|---|
access_key | string | Your AccessKey ID. |
secret_key | string | Your AccessKey secret. |
region | string | The ID of the region where you want to upload your on-premises image. cn-beijing is used in this example. For more information about regions, see Regions and zones.
|
image_name | string | The name of your on-premises image.
|
oss_bucket_name | string | The name of your OSS bucket. If you specify a bucket name that does not exist, Packer automatically creates a bucket with the specified name when Packer uploads the image. |
image_os_type | string | The type of the image. Valid values:
|
image_platform | string | The distribution of the image. CentOS is used in this example. |
image_architecture | string | The architecture of the image. Valid values:
|
format | string | The format of the image. Valid values:
|
What to do next
You can use the custom image uploaded to Alibaba Cloud to create ECS instances. For more information, see Create an ECS instance by using a custom image.