Skip to content

Instantly share code, notes, and snippets.

@summerwind
Last active April 10, 2024 19:32
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save summerwind/b73d0f72df98fdb6c76e0f2e9ce3971e to your computer and use it in GitHub Desktop.
Save summerwind/b73d0f72df98fdb6c76e0f2e9ce3971e to your computer and use it in GitHub Desktop.
Build a OS image with Packer and QEMU

Build a OS image with QEMU and Ubuntu Cloud Image

Prerequirements

  • Packer
  • QEMU
  • Docker

Build an image of cloud-init settings

$ docker run -it -v `pwd`:/tmp/host --rm ubuntu:16.04
# apt update
# apt install -y vim cloud-utils
# cloud-localds /tmp/host/cloud.img /tmp/host/cloud.cfg

Build a OS image with Packer

$ packer build template.json
#cloud-config
password: ubuntu
ssh_pwauth: true
chpasswd:
expire: false
{
"builders":
[
{
"type": "qemu",
"iso_url": "ubuntu-16.04-server-cloudimg-amd64-disk1.img",
"iso_checksum": "3a4b7f6115ca074c4728e4628ca73160e6b7ef6995db3c382015545940568939",
"iso_checksum_type": "sha256",
"disk_image": true,
"output_directory": "image",
"disk_size": 5000,
"format": "qcow2",
"disk_compression": true,
"headless": true,
"accelerator": "none",
"ssh_username": "ubuntu",
"ssh_password": "ubuntu",
"ssh_port": 22,
"ssh_wait_timeout": "300s",
"vm_name": "ubuntu",
"use_default_display": true,
"qemuargs": [
["-m", "2048M"],
["-smp", "2"],
["-fda", "cloud.img"],
["-serial", "mon:stdio"]
]
}
]
}
@splashx
Copy link

splashx commented Aug 5, 2017

Have you by chance been successful with this setup? The boot runs but packer is unable to ssh into the box.

2017/08/05 14:49:02 ui error: ==> qemu: Error waiting for SSH: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none], no supported methods remain
==> qemu: Error waiting for SSH: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none], no supported methods remain

@ydp
Copy link

ydp commented Feb 3, 2023

This is working for me, in case someone like me hit this

{
  "builders": [
    {
      "type": "qemu",
      "iso_url": "ubuntu-22.04-server-cloudimg-amd64.img",
      "iso_checksum": "md5:9688fc9011dcd4d960620d2ebb98a639",
      "ssh_username": "ubuntu",
      "ssh_password": "a",
      "ssh_timeout": "1m",
      "ssh_port": 2222,
      "skip_nat_mapping": true,
      "disk_size": "40G",
      "disk_image": true,
      "disk_compression": true,
      "format": "qcow2",
      "headless": true,
      "accelerator": "kvm",
      "output_directory": "images",
      "qemuargs": [
        [ "-machine", "accel=kvm,type=q35" ],
        [ "-cpu", "host" ],
        [ "-m", "2G" ],
        [ "-smp", "2" ],
        [ "-nographic" ],
        [ "-device", "virtio-net-pci,netdev=net0" ],
        [ "-netdev", "user,id=net0,", "hostfwd=tcp::2222-:22" ],
        [ "-cdrom", "seed.img" ]
      ],
      "shutdown_command": "echo a |sudo -S shutdown -P now",
      "boot_command": [],
      "vm_name": "jammy"
    }
  ]
}

keypoint is

  • set port forward
  • use -cdrom

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment