To content | To menu | To search

Windows 上の HashiCorp Packer で VirtualBox 向けの Ubuntu イメージを作成

Packer は今回は 1.7.10 を使用。

あらかじめ VirtualBox をインストールしておく。今回は 6.1.32r149290 (Qt5.6.2) を使用。

カレントディレクトリには

  • xubuntu.pkr.hcl
  • xubuntu.pkrvars.hcl

そしてカレントディレクトリには http というディレクトリを作っておく。

ISO ファイルは F:\iso フォルダに格納。MD5 は

> certutil -hashfile <ISO ファイル名> MD5

で生成。

xubuntu.pkr.hcl の内容は

variable "build_directory" {
    default = "./build"
}

variable "boot_wait" {
    default = "10s"
}

variable "cpus" {
    default = 1
}

variable "disk_size" {
    default = 50000
}

variable "headless" {
    default = false
}

variable "http_directory" {
    default = "./http"
}

variable "iso_url" {
    type = string
}

variable "iso_checksum" {
    type = string
}

variable "memory" {
    default = 1024
}

variable "username" {
    default = "vagrant"
}

variable "vm_name" {
    type = string
}

variable "provider_name" {
    default = "virtualbox"
}

variable "ssh_timeout" {
    default = "45m"
}

source "virtualbox-iso" "ubuntu" {

    boot_command = [
        "<up><tab><wait>",
        " <enter>",
    ]

    boot_wait            = var.boot_wait
    cpus                 = var.cpus
    disk_size            = var.disk_size
    guest_os_type        = "Ubuntu_64"
    hard_drive_interface = "sata"
    headless             = var.headless
    http_directory       = var.http_directory
    iso_url              = var.iso_url
    iso_checksum         = var.iso_checksum
    memory               = var.memory
    output_directory     = "${var.build_directory}/packer-${var.provider_name}"
    shutdown_command     = "echo '${var.username}' | sudo -S shutdown -P now"
    ssh_timeout          = var.ssh_timeout
    ssh_username         = var.username
    ssh_password         = var.username
    vm_name              = var.vm_name
}

build {
    sources = ["sources.virtualbox-iso.ubuntu20041"]
}

xubuntu.pkrvars.hcl の内容は

iso_url="file://f:/iso/xubuntu-18.04.1-desktop-amd64.iso"
iso_checksum="md5:bb0636b2bfdf43e3aefbb8dc073c3396"
vm_name="test2"

これでコマンドプロンプトで

>packer.exe build --force -var-file=xubuntu.pkrvars.hcl xubuntu.pkr.hcl

とすることでひとまず VirtualBox の仮想環境の構築と起動ができた。ただこのままだと Ubuntu の自動セットアップもされず Packer が SSH アクセスを待つだけになるので、次は preseed file を使って Ubuntu が自動的にセットアップされるようにしなければいけない。

それがうまくいったら、次は Vagrant 向けの box ファイルに仕立てる予定。

いろんなサイトを参考にした。

  • https://help.ubuntu.com/lts/installation-guide/amd64/apbs02.html
  • https://www.packer.io/guides/automatic-operating-system-installs/preseed_ubuntu
  • https://corymollet.com/posts/packer-virtualbox-iso-builder/
  • https://ts0818.hatenablog.com/entry/2020/03/22/232329
  • https://www.packer.io/docs/templates/hcl_templates/blocks
  • https://help.ubuntu.com/lts/installation-guide/amd64/apbs01.html
  • https://help.ubuntu.com/lts/installation-guide/amd64/ch05s01.html
  • https://wiki.ubuntulinux.jp/UbuntuTips/Others/BootOptions
  • https://www.infracloud.io/blogs/deploy-ubuntu-packer-image-bare-metal/
  • https://devopscube.com/packer-tutorial-for-beginners/
  • https://eric.mink.li/publications/preseeding.html
  • https://wiki.ubuntu.com/DesktopCDOptions
  • https://wiki.ubuntu.com/UbiquityAutomation

Add a comment

HTML code is displayed as text and web addresses are automatically converted.

They posted on the same topic

Trackback URL : https://www.pseudomoon.jp/dotclear/index.php?trackback/146

This post's comments feed