docs
uhost
How Tos
Image
Import Image

Use Packer to create and import local images

Overview

Packer is a lightweight open-source tool for automated image packaging launched by Hashicorp (for a detailed introduction, please refer to the previous document)。SurferCloud now supports one-click import of custom local images to the SurferCloud cloud platform through the integration of Packer.

Related Links

Official reference document address for image import (opens in a new tab)

Used to query various parameters of ucloud import Post-Processors

Packer Official Download Page (opens in a new tab)

Used for installing Packer

Open Source Repository Address (opens in a new tab)

Welcome to contribute code to ucloud Packer Builder

Image Import Example

Next, we will use Packer to create and import a CentOS image, as shown below:

Packer first utilizes QEMU Builder (opens in a new tab) to create a RAW image, which is stored in the locally configured directory. Then, it uses ucloud-import Post-Processors (opens in a new tab) to store the local image in the UFile configured by the user, and automatically imports it into the ucloud cloud platform.

Environment Configuration

Installation Packer

Configure Default User

Set the keys UCLOUD_PUBLIC_KEY, UCLOUD_PRIVATE_KEY and set the project ID UCLOUD_PROJECT_ID as global environment variables (recommended), or explicitly specify public_key, private_key, project_id in the json file.

Installation QEMU

Create a UFile bucket space

Write JSON file

Let's take the example of creating and importing a custom CentOS 6.10 image using QEMU based on the MacOS system, Example Link (opens in a new tab). First, create a clean empty folder as a workspace and switch to that directory, write a JSON specification file (eg: local.json), as follows :

{"variables": {
  "ucloud_public_key": "{{env `UCLOUD_PUBLIC_KEY`}}",
  "ucloud_private_key": "{{env `UCLOUD_PRIVATE_KEY`}}",
  "ucloud_project_id": "{{env `UCLOUD_PROJECT_ID`}}",
  "disk_size": "4096",
  "iso_checksum": "0da4a1206e7642906e33c0f155d2f835",
  "iso_checksum_type": "md5",
  "iso_name": "CentOS-6.10-x86_64-minimal.iso",
  "ks_path": "centos-6.10/ks.cfg",
  "mirror": "http://mirrors.ustc.edu.cn/centos",
  "mirror_directory": "6.10/isos/x86_64",
  "template": "centos-6.10-x86_64"
},
  "builders":[
    {
      "type": "qemu",
      "boot_command": [
        "<tab> text ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/{{user `ks_path`}}<enter><wait>"
      ],
      "boot_wait": "10s",
      "disk_size": "{{user `disk_size`}}",
      "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 'packer'|sudo -S shutdown -P now",
      "ssh_password": "ucloud_packer",
      "ssh_port": 22,
      "ssh_username": "root",
      "ssh_timeout": "10000s",
      "vm_name": "{{ user `template` }}.raw",
      "net_device": "virtio-net",
      "disk_interface": "virtio",
      "format": "raw",
      "use_default_display": "false",
      "qemuargs": [
        ["-display", "cocoa"]
      ]
    }
  ],
  "post-processors":[
    {
      "type":"ucloud-import",
      "public_key": "{{user `ucloud_public_key`}}",
      "private_key": "{{user `ucloud_private_key`}}",
      "project_id": "{{user `ucloud_project_id`}}",
      "region":"cn-bj2",
      "ufile_bucket_name": "packer-test",
      "image_name": "packer_import_test",
      "image_os_type": "CentOS",
      "image_os_name": "CentOS 6.10 64位",
      "format": "raw"
    }
  ]
}

As defined above, a qemu Builder and a ucloud-import Post-Processors (opens in a new tab) are set up, where information such as the UFile bucket name is configured.

Write Kickstart file

According to the http_directory and boot_command configured in QEMU in the above JSON file, a ./http/centos-6.10/ directory needs to be created under the JSON file directory to store the Kickstart file, i.e., ks.cfg, as follows: (Kickstart Reference Document (opens in a new tab))

install
cdrom
lang en_US.UTF-8
keyboard us
network --bootproto=dhcp
rootpw ucloud_packer
firewall --disabled
selinux --permissive
timezone UTC
unsupported_hardware
bootloader --location=mbr
text
skipx
zerombr
clearpart --all
autopart
auth --enableshadow --passalgo=sha512
firstboot --disabled
reboot

%packages --nobase --ignoremissing
sudo
gcc
make
%end

Execute Command Line

By executing the command packer build local.json, you can create and import custom images with one click.

  • Company
  • ContactUs
  • Blog
Copyright © 2024 SurferCloud All Rights Reserved
  • Contact our sales to help your business.
  • Contact our sales to help your business.