cloudssh-keyspacker

Packer how to connect to Debian genericcloud image


is there any way that packer can connect to Debian genericcloud image? We are not using cloud-init, rather we are using packer to create our own image template for our private cloud. Using the iso and build an image template works straight forward.

The nocloud image allows root login without a password. Unfortunately ssh port 22 is commented in sshd_config, so ssh fails until it is commented out manually, which makes the automation process impossible.

Is there any way that packer can inject the temporary pub key to the genericcloud image and connect to it for further modifications?

I tried to use https://github.com/ivoronin/packer-plugin-sshkey but I am not quite sure how should it work, the process stuck at Waiting for SSH to become available... and breaks after the ssh_wait_timeout is over.

Thanks


Solution

  • Yes there is a way, took couple of hours to figure out this.

    When using qemu plugin you can specify cd_files and cd_label to run cloud-init inside this machine, but there is important thing. cd_label should be exactly "CIDATA" with capital letters, also you should pass to cd_files exactly two files user-data and meta-data.

    Without this cloud-init wouldn't understand you.

    There is example directory for you with files content: $ ls -1

    config.pkr.hcl
    meta-data
    packer_key
    packer_key.pub
    user-data
    

    packer_key is newly generated rsa ssh key file via ssh-keygen without password and packer_key.pub it's public part

    $ cat meta-data

    #cloud-config
    

    $ cat user-data

    #cloud-config
    users:
    - name: root
      ssh_authorized_keys:
        - "here_is_content_of_packer_key.pub"
    

    $ cat config.pkr.hcl

    packer {
      required_plugins {
        qemu = {
          version = "~> 1"
          source  = "github.com/hashicorp/qemu"
        }
      }
    }
    
    source "qemu" "example" {
      accelerator               = "kvm"
      boot_command              = []
      disk_compression          = true
      disk_interface            = "virtio"
      disk_image                = true
      disk_size                 = "10000M"
      boot_wait                 = "2s"
      vm_name                   = "debian-genericcloud-amd64-packer.qcow2"
      format                    = "qcow2"
      headless                  = "false"
      iso_checksum              = "sha512:49cbcfdb3d5401e8c731d33211cff5e1ef884f179a936c7378eeab00c582ace45dd7154ac9e4c059f1bd6c7ae2ce805879cb381a12a1cc493e3a58c847e134c7"
      iso_url                   = "https://cloud.debian.org/images/cloud/bookworm/20240102-1614/debian-12-genericcloud-amd64-20240102-1614.qcow2"
      net_device                = "virtio-net"
      output_directory          = "artifacts_tests"
      cd_files                  = ["./user-data", "./meta-data"]
      cd_label                  = "CIDATA"
      qemuargs                  = [["-m", "16384M"], ["-smp", "8"]]
      communicator              = "ssh"
      host_port_min             = 2222
      host_port_max             = 2299
      shutdown_command          = "sudo shutdown -P now"
      shutdown_timeout          = "10s"
      ssh_private_key_file      = "./packer_key"
      ssh_clear_authorized_keys = true
      ssh_username              = "root"
      ssh_timeout               = "20s"
    }
    
    build {
      sources = ["source.qemu.example"]
      provisioner "shell" {
        inline = [
          "apt-get update",
          "apt-get install python3",
          "apt-get autoclean",
          "apt-get clean"
        ]
      }
    }
    

    With this you should be able to reproduce build

    $ packer version

    Packer v1.10.0