Qemu GTK initialization error (Ubuntu 22.04)

I tried really hard to find an answer to this before asking, and knowing my luck I probably missed something but here goes…

I’ve been searching around for examples of using the qemu plugin to build ubuntu images (this is also my first time using packer).

My current HCL is

packer {
    required_plugins {
        qemu = {
            version = ">= 1.0.3"
            source = "github.com/hashicorp/qemu"
        }
        ansible = {
            version = ">= 1.0.1"
            source = "github.com/hashicorp/ansible"
        }
    }
}

source "qemu" "test" {
    vm_name           = "test"
    iso_url           = "http://www.releases.ubuntu.com/22.04/ubuntu-22.04-live-server-amd64.iso"
    iso_checksum      = "sha256:84aeaf7823c8c61baa0ae862d0a06b03409394800000b3235854a6b38eb4856f"
    shutdown_command  = "echo 'packer' | sudo -S shutdown -P now"
    disk_size         = "5000M"
    format            = "qcow2"
    accelerator       = "kvm"
    http_directory    = "http-server"
    net_device        = "virtio-net"
    disk_interface    = "virtio"
    boot_wait         = "10s"
    ssh_username      = "ing"
#    boot_command      = [
#        # Make the language selector appear...
#        " <up><wait>",
#        # ...then get rid of it
#        " <up><wait><esc><wait>",
#
#        # Go to the other installation options menu and leave it
#        "<f6><wait><esc><wait>",
#
#        # Remove the kernel command-line that already exists
#        "<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
#        "<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
#        "<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
#
#        # Add kernel command-line and start install
#        "/casper/vmlinuz ",
#        "initrd=/casper/initrd ",
#        "autoinstall ",
#        "ds=nocloud-net;s=http://{{.HTTPIP}}:{{.HTTPPort}}/test/ ",
#        "<enter>"
#    ]
}

build {
    sources = ["source.qemu.test"]

#    provisioner "ansible" {
#        playbook_file = "./playbook.yml"
#        roles_path    = "../roles"
#    }
}

I commented out the boot command (which I’m not sure is right as it’s for bionic, I’m sure it’s wrong lol) and the ansible provisioner as part of my troubleshooting (didn’t really expect a difference but felt like trying it). With or without those segments commented out I get the following (with PACKER_LOG=1)

qemu.test: output will be in this color.
2022/05/11 23:24:23 Build debug mode: false

2022/05/11 23:24:23 Force build: false
2022/05/11 23:24:23 On error: 
2022/05/11 23:24:23 Waiting on builds to complete...
2022/05/11 23:24:23 Starting build run: qemu.test
2022/05/11 23:24:23 Running builder: 
2022/05/11 23:24:23 [INFO] (telemetry) Starting builder qemu.test
2022/05/11 23:24:23 packer-plugin-qemu_v1.0.3_x5.0_linux_amd64 plugin: 2022/05/11 23:24:23 Qemu path: /usr/bin/qemu-system-x86_64, Qemu Image path: /usr/bin/qemu-img
==> qemu.test: Retrieving ISO
2022/05/11 23:24:23 packer-plugin-qemu_v1.0.3_x5.0_linux_amd64 plugin: 2022/05/11 23:24:23 Acquiring lock for: http://www.releases.ubuntu.com/22.04/ubuntu-22.04-live-server-amd64.iso?checksum=sha256%3A84aeaf7823c8c61baa0ae862d0a06b03409394800000b3235854a6b38eb4856f (/home/ing/.cache/packer/b9441068de828d36573e1274dfe77f69aebda15a.iso.lock)
==> qemu.test: Trying http://www.releases.ubuntu.com/22.04/ubuntu-22.04-live-server-amd64.iso
==> qemu.test: Trying http://www.releases.ubuntu.com/22.04/ubuntu-22.04-live-server-amd64.iso?checksum=sha256%3A84aeaf7823c8c61baa0ae862d0a06b03409394800000b3235854a6b38eb4856f
==> qemu.test: http://www.releases.ubuntu.com/22.04/ubuntu-22.04-live-server-amd64.iso?checksum=sha256%3A84aeaf7823c8c61baa0ae862d0a06b03409394800000b3235854a6b38eb4856f => /home/ing/.cache/packer/b9441068de828d36573e1274dfe77f69aebda15a.iso
2022/05/11 23:24:33 packer-plugin-qemu_v1.0.3_x5.0_linux_amd64 plugin: 2022/05/11 23:24:33 Leaving retrieve loop for ISO
2022/05/11 23:24:34 packer-plugin-qemu_v1.0.3_x5.0_linux_amd64 plugin: 2022/05/11 23:24:34 No floppy files specified. Floppy disk will not be made.
2022/05/11 23:24:34 packer-plugin-qemu_v1.0.3_x5.0_linux_amd64 plugin: 2022/05/11 23:24:34 No CD files specified. CD disk will not be made.
2022/05/11 23:24:34 packer-plugin-qemu_v1.0.3_x5.0_linux_amd64 plugin: 2022/05/11 23:24:34 [INFO] Creating disk with Path: output-test/test and Size: 5000M
2022/05/11 23:24:34 packer-plugin-qemu_v1.0.3_x5.0_linux_amd64 plugin: 2022/05/11 23:24:34 Executing qemu-img: []string{"create", "-f", "qcow2", "output-test/test", "5000M"}
2022/05/11 23:24:34 packer-plugin-qemu_v1.0.3_x5.0_linux_amd64 plugin: 2022/05/11 23:24:34 stdout: Formatting 'output-test/test', fmt=qcow2 cluster_size=65536 extended_l2=off compression_type=zlib size=5242880000 lazy_refcounts=off refcount_bits=16
2022/05/11 23:24:34 packer-plugin-qemu_v1.0.3_x5.0_linux_amd64 plugin: 2022/05/11 23:24:34 stderr:
2022/05/11 23:24:34 packer-plugin-qemu_v1.0.3_x5.0_linux_amd64 plugin: 2022/05/11 23:24:34 Found available port: 8271 on IP: 0.0.0.0
==> qemu.test: Starting HTTP server on port 8271
2022/05/11 23:24:34 packer-plugin-qemu_v1.0.3_x5.0_linux_amd64 plugin: 2022/05/11 23:24:34 Looking for available communicator (SSH, WinRM, etc) port between 2222 and 4444
2022/05/11 23:24:34 packer-plugin-qemu_v1.0.3_x5.0_linux_amd64 plugin: 2022/05/11 23:24:34 Found available port: 3465 on IP: 127.0.0.1
==> qemu.test: Found port for communicator (SSH, WinRM, etc): 3465.
2022/05/11 23:24:34 packer-plugin-qemu_v1.0.3_x5.0_linux_amd64 plugin: 2022/05/11 23:24:34 Looking for available port between 5900 and 6000 on 127.0.0.1
==> qemu.test: Looking for available port between 5900 and 6000 on 127.0.0.1
2022/05/11 23:24:34 packer-plugin-qemu_v1.0.3_x5.0_linux_amd64 plugin: 2022/05/11 23:24:34 Found available port: 5910 on IP: 127.0.0.1
2022/05/11 23:24:34 packer-plugin-qemu_v1.0.3_x5.0_linux_amd64 plugin: 2022/05/11 23:24:34 Found available VNC port: 5910 on IP: 127.0.0.1
2022/05/11 23:24:34 packer-plugin-qemu_v1.0.3_x5.0_linux_amd64 plugin: 2022/05/11 23:24:34 Qemu --version output: QEMU emulator version 6.2.0 (Debian 1:6.2+dfsg-2ubuntu6)
2022/05/11 23:24:34 packer-plugin-qemu_v1.0.3_x5.0_linux_amd64 plugin: Copyright (c) 2003-2021 Fabrice Bellard and the QEMU Project developers
2022/05/11 23:24:34 packer-plugin-qemu_v1.0.3_x5.0_linux_amd64 plugin: 2022/05/11 23:24:34 Qemu version: 6.2.0
==> qemu.test: Starting VM, booting from CD-ROM
2022/05/11 23:24:34 packer-plugin-qemu_v1.0.3_x5.0_linux_amd64 plugin: 2022/05/11 23:24:34 Qemu Builder has no floppy files, not attaching a floppy.
2022/05/11 23:24:34 packer-plugin-qemu_v1.0.3_x5.0_linux_amd64 plugin: 2022/05/11 23:24:34 Executing /usr/bin/qemu-system-x86_64: []string{"-vnc", "127.0.0.1:10", "-machine", "type=pc,accel=kvm", "-drive", "file=output-test/test,if=virtio,cache=writeback,discard=ignore,format=qcow2", "-drive", "file=/home/ing/.cache/packer/b9441068de828d36573e1274dfe77f69aebda15a.iso,media=cdrom", "-boot", "once=d", "-netdev", "user,id=user.0,hostfwd=tcp::3465-:22", "-m", "512M", "-device", "virtio-net,netdev=user.0", "-name", "test", "-display", "gtk"}
2022/05/11 23:24:34 packer-plugin-qemu_v1.0.3_x5.0_linux_amd64 plugin: 2022/05/11 23:24:34 Started Qemu. Pid: 253120
2022/05/11 23:24:34 packer-plugin-qemu_v1.0.3_x5.0_linux_amd64 plugin: 2022/05/11 23:24:34 Qemu stderr: qemu-system-x86_64: warning: host doesn't support requested feature: CPUID.80000001H:ECX.svm [bit 2]
2022/05/11 23:24:34 packer-plugin-qemu_v1.0.3_x5.0_linux_amd64 plugin: 2022/05/11 23:24:34 Qemu stderr: gtk initialization failed
==> qemu.test: Error launching VM: Qemu failed to start. Please run with PACKER_LOG=1 to get more info.
2022/05/11 23:24:34 packer-plugin-qemu_v1.0.3_x5.0_linux_amd64 plugin: 2022/05/11 23:24:34 failed to unlock port lockfile: close tcp 127.0.0.1:5910: use of closed network connection
2022/05/11 23:24:34 packer-plugin-qemu_v1.0.3_x5.0_linux_amd64 plugin: 2022/05/11 23:24:34 failed to unlock port lockfile: close tcp 127.0.0.1:3465: use of closed network connection
==> qemu.test: Deleting output directory...
2022/05/11 23:24:34 [INFO] (telemetry) ending qemu.test
==> Wait completed after 10 seconds 601 milliseconds
2022/05/11 23:24:34 machine readable: error-count []string{"1"}
==> Some builds didn't complete successfully and had errors:
2022/05/11 23:24:34 machine readable: qemu.test,error []string{"Build was halted."}
==> Builds finished but no artifacts were created.
2022/05/11 23:24:34 [INFO] (telemetry) Finalizing.
Build 'qemu.test' errored after 10 seconds 601 milliseconds: Build was halted.

==> Wait completed after 10 seconds 601 milliseconds

==> Some builds didn't complete successfully and had errors:
--> qemu.test: Build was halted.

==> Builds finished but no artifacts were created.
2022/05/11 23:24:34 waiting for all plugin processes to complete...
2022/05/11 23:24:34 /home/ing/.config/packer/plugins/github.com/hashicorp/qemu/packer-plugin-qemu_v1.0.3_x5.0_linux_amd64: plugin process exited

Can anyone shed some light on what I’m doing wrong or perhaps just point me towards a working example (with ubuntu, not cent-os lol)?

Thanks!

After thinking about this it occurred to me that packer using qemu in gtk mode was the main issue (as the machine is headless) turns out I missed an option in the qemu builder docs:

(I have other issues I have to solve now but happy to be past this at least)

edit: I can’t figure out how to mark this as solved :confused: