To content | To menu | To search

Tag - HashiCorp Packer

Entries feed - Comments feed

Tuesday, September 6 2022

Petalinux のための vagrant 環境のスクリプトを作った

検索しても参照されていないようなのでこちらから。

takagiwa/petalinuxenv

Continue reading...

Thursday, March 31 2022

HashiCorp Packer で作った仮想マシンを Vagrant 向けの box ファイルで出力してインポートして起動

ubuntu.pkr.hcl の build の範囲を書き換えた。

build {
    sources = [
      "sources.virtualbox-iso.ubuntu"
    ]
    provisioner "shell" {
      # execute_command = "echo {{.ssh_password}} | sudo bash {{.Path}}"
      execute_command = "bash {{.Path}}"
      script = "scripts/setkey.sh"
    }
    post-processors {  
      post-processor "artifice" {
        files = [
          "build/packer-virtualbox/ubuntu18041-disk001.vmdk",
          "build/packer-virtualbox/ubuntu18041.ovf"
        ]
      }
      post-processor "vagrant" {
        keep_input_artifact = true
        provider_override   = "virtualbox"
      }  
    }
}

シェルスクリプトから sudo が必要なやつはカットして、キーを登録するところだけに集中。

#!/bin/bash

# https://www.gun.io/blog/building-vagrant-machines-with-packer
# set -e

# apt update -y -qq > /dev/null
# apt install -y linux-headers-$(uname -r) build-essential dkms nfs-common
# apt install -y curl git

# https://www.covermymeds.com/main/insights/articles/repeatable-vagrant-builds-with-packer/
# setup insecure vagrant user ssh key
/bin/mkdir /home/vagrant/.ssh
/bin/chmod 700 /home/vagrant/.ssh
/usr/bin/curl -L -o /home/vagrant/.ssh/id_rsa https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant
/usr/bin/curl -L -o /home/vagrant/.ssh/authorized_keys https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub
/bin/chown -R vagrant:vagrant /home/vagrant/.ssh
/bin/chmod 0600 /home/vagrant/.ssh/*

これで通った。

https://discuss.hashicorp.com/t/virtualbox-iso-no-tty-present-and-no-askpass-program-specified/19513

https://www.packer.io/docs/debugging

https://acloudguru.com/hands-on-labs/formatting-a-packer-template-in-hcl2

https://www.gun.io/blog/building-vagrant-machines-with-packer

https://www.covermymeds.com/main/insights/articles/repeatable-vagrant-builds-with-packer/

https://lab.unicast.ne.jp/2013/09/09/troubleshooting-create-vagrant-box-with-packer/

https://qiita.com/mitzi2funk/items/c963483a11a1912e3c44

https://www.packer.io/docs/communicators/ssh

Tuesday, March 29 2022

HashiCorp Packer で作った仮想マシンを Vagrant 向けの box ファイルで出力してインポートして起動(失敗)

pkr.hcl を書き換え。


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

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

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

variable "provider_name" {
    default = "virtualbox"
}

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

variable "preseed" {
    default = ""
}

variable "boot_command" {
    default = "autoinstall ds=nocloud-net;s=http://{{ .HTTPIP }}:{{ .HTTPPort }}/"
}

# refer to https://github.com/boxcutter/ubuntu/blob/master/ubuntu.json

variable "boot_command_prefix" {
    default = "<enter><enter><f6><esc><wait>"
}

variable "cleanup_pause" {
    default = ""
}

variable "cpus" {
    default = "1"
}

variable "custom_script" {
    default = "custom-script.sh"
}

variable "desktop" {
    default = "false"
}

variable "disk_size" {
    default = "65536"
}

variable "ftp_proxy" {
    # {{env `ftp_proxy`}}
    default = ""
}

variable "headless" {
    default = "false"
}

variable "http_proxy" {
    # {{env `http_proxy`}}
    default = ""
}

variable "https_proxy" {
    # {{env `https_proxy`}}
    default = ""
}

variable "install_vagrant_key" {
    default = "true"
}

variable "iso_checksum" {
    default = "sha256:d1f2bf834bbe9bb43faf16f9be992a6f3935e65be0edece1dee2aa6eb1767423"
}

variable "iso_name" {
    default = "ubuntu-20.04.1-live-server-amd64.iso"
}

variable "iso_path" {
    default = "/Volumes/Storage/software/ubuntu"
}

variable "iso_url" {
    default = "http://releases.ubuntu.com/20.04/ubuntu-20.04.2-live-server-amd64.iso"
}

variable "memory" {
    default = "512"
}

variable "no_proxy" {
    # {{env `no_proxy`}}
    default = ""
}

variable "parallels_guest_os_type" {
    default = "ubuntu"
}

variable "rsync_proxy" {
    # {{env `rsync_proxy`}}
    default = ""
}

variable "hostname" {
    default = "vagrant"
}

variable "ssh_fullname" {
    default = "vagrant"
}

variable "ssh_password" {
    default = "vagrant"
}

variable "ssh_username" {
    default = "vagrant"
}

variable "update" {
    default = "false"
}

variable "vagrantfile_template" {
    default = ""
}

variable "version" {
    default = "0.1.0"
}

variable "virtualbox_guest_os_type" {
    default = "Ubuntu_64"
}

variable "vm_name" {
    default = "ubuntu2004"
}

variable "vmware_guest_os_type" {
    default = "ubuntu-64"
}

source "virtualbox-iso" "ubuntu" {

    boot_command = [
        "${var.boot_command_prefix}",
        "${var.boot_command}",
        " ${var.preseed}",
        " --- <wait>",
        " <enter><wait>"
    ]

    #boot_command = [
    #    "<enter><wait>",
    #    "<esc><wait>",
    #    "<enter><wait>",
    #    "/install/vmlinuz<wait>",
    #    " file=/cdrom/preseed/ubuntu-server.seed vga=788 initrd=/install/initrd.gz",
    #    " auto-install/enable=true",
    #    " debconf/priority=critical",
    #    " preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/ubuntu18.04.1.cfg",
    #    " --- <wait>",
    #    "<enter><wait>"
    #]

    boot_wait              = var.boot_wait
    cpus                   = var.cpus
    disk_size              = var.disk_size
    guest_os_type          = var.virtualbox_guest_os_type
    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.ssh_username}' | sudo -S shutdown -P now"
    ssh_timeout            = var.ssh_timeout
    ssh_username           = var.ssh_username
    ssh_password           = var.ssh_password
    vm_name                = var.vm_name
    guest_additions_path   = "VBoxGuestAdditions_{{.Version}}.iso"
    post_shutdown_delay    = "1m"
    ssh_wait_timeout       = var.ssh_timeout
    ssh_handshake_attempts = "200"
}

build {
    sources = [
      "sources.virtualbox-iso.ubuntu"
    ]
    post-processors {  
      post-processor "artifice" {
        files = [
          "build/packer-virtualbox/ubuntu18041-disk001.vmdk",
          "build/packer-virtualbox/ubuntu18041.ovf"
        ]
      }
      post-processor "vagrant" {
        keep_input_artifact = true
        provider_override   = "virtualbox"
      }  
    }
}

これで実行したら、カレントディレクトリに、どこから名前を作ったか分からないものの、packer_ubuntu_virtualbox.box というファイルが生成された。

vagrant box add bionic1-test packer_ubuntu_virtualbox.box

として、bionic1-test という名前で box ファイルを登録。

>mkdir test
>cd test
\test>vagrant init bionic1-test
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

\test>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'bionic1-test'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: test_default_1648544752485_71620
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection reset. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default: Warning: Connection aborted. Retrying...
    default: Warning: Connection reset. Retrying...
    default: Warning: Connection aborted. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
^C
\test>==> default: Waiting for cleanup before exiting...
Vagrant exited after cleanup due to external interrupt.

というわけで Vagrant から SSH でアクセスできなかった模様。

でも TeraTerm で localhost:2222 に SSH してユーザー名&パスワードでアクセスする分にはログインできた。

https://www.vagrantup.com/docs/boxes/base

https://www.vagrantup.com/docs/providers/virtualbox/boxes

https://www.packer.io/plugins/post-processors/vagrant/vagrant


2022/Mar/30 追記

https://lab.unicast.ne.jp/2013/09/09/troubleshooting-create-vagrant-box-with-packer/

ここの /home/vagrant/.ssh/authorized_keys の問題だった。ただ curl だと取れなかったので、別のブラウザで https://github.com/mitchellh/vagrant/raw/master/keys/vagrant.pub を開いて /home/vagrant/.ssh/authorized_keys に保存したほうが良さそう。

あとはこれをどう Packer で自動化すればいいのか。

Monday, February 7 2022

メモ。Packer で起動した Ubuntu で Pressed が動かない

そもそも Ubuntu (というか Xubuntu) Desktop で Pressed が動くのかどうかよくわからない。検索すると、デスクトップ環境が欲しいなら Ubuntu Server インストール後に Desktop のパッケージを入れれば良い、という情報もある。

Pressed のファイルは、live OS 上のブラウザからもアクセスできるし、起動ログにもロードしたらしい情報があるのに動いてくれないので、次は Ubuntu Server で試してみるしかなさそう。

この辺でうごいてくれたら、Packer 関係無く Ubuntu の話だ。

試した Packer ファイルはこちら

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

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

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" "ubuntu20041" {

    boot_command = [
        "<esc><wait>",
        "<esc><wait>",
        "<esc><wait>",
        "<enter><wait>",
        "/casper/vmlinuz<wait>",
        " file=/cdrom/preseed/xubuntu.seed boot=casper initrd=/casper/initrd",
        " noninteractive",
        " preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/preseed_2.cfg",
        " -- <wait>",
        "<enter><wait>"
    ]

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

boot_command に対しては、いろいろ書いてみたけどわからない(全部一気に書いたわけではない)。

        " auto-install/enable=true",
        " debconf/priority=critical",
        " ksdevice=enp0s3 interface=enp0s3",
        " debug-ubiquity automatic-ubiquity only-ubiquity noninteractive",

そもそも起動するまでも紆余曲折だった。

boot_wait を 10s に設定していたら、boot_command が始まるのが間に合わなかったので 5s に短縮。これは実機合わせが必要だと思う。

boot_command の最初の

        "<esc><wait>",
        "<esc><wait>",
        "<esc><wait>",
        "<enter><wait>",

これも実際に動かして確認。

        "/casper/vmlinuz<wait>",
        " file=/cdrom/preseed/xubuntu.seed boot=casper initrd=/casper/initrd",

ここはしばらくかかった。ISO で起動したときの boot オプションを参照しなければいけない。けど Ubuntu18 のインストーラは vmlinuz 書いてないからわからなかった。古いやつなら /install/vmlinuz でいいはず。実際に ISO をマウントして確認したパスを設定。なので、ここも実際の ISO を確認しながら作っていかなければいけない。

Thursday, February 3 2022

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