What is the equivalent to a VirtualBox setting in Qemu?

878 views Asked by At

I'm trying to create a Packer template that has both a VirtualBox and a Qemu builder for the same thing. The VirtualBox one works fine, but with Qemu, I'm having some difficulty figuring out how to setup the networking properly.

More specifically, in the VirtualBox section, I have

  "vboxmanage": [
    ...
    ["modifyvm", "{{.Name}}", "--nic2", "nat"],
    ["modifyvm", "{{.Name}}", "--cableconnected2", "on"],
    ["modifyvm", "{{.Name}}", "--nic3", "null"],
    ["modifyvm", "{{.Name}}", "--cableconnected3", "off"]
  ],
  "vboxmanage_post": [
    ["modifyvm", "{{.Name}}", "--nic1", "hostonly"],
    ["modifyvm", "{{.Name}}", "--hostonlyadapter1", "VirtualBox Host-Only Ethernet Adapter"],
    ["modifyvm", "{{.Name}}", "--cableconnected1", "on"]
  ],

I tried to initially just configure at least the second NIC with

  "qemuargs": [
    [ "-netdev", "user,id=mynet0,net=192.168.56.101/24,host=192.168.56.2,dns=192.168.56.2,dhcpstart=192.168.56.2"],
    ["-m", "128M"]
  ]

But Packer says there's an error when invoking Qemu with that command. What am I doing wrong? And also, how would I create a host-only adapter when I later run the created image?

1

There are 1 answers

0
boen_robot On

OK, I kind of managed to solve my original problem.

The problem was that both -netdev and -device are required AND (the thing that really tripped me up) their order is important: "-netdev" first, and "-device" second.

Furthermore, Packer seems to overwrite its own first interface, which in turn requires its explicit redefinition. That explicit redefinition needs two hostfwd-ed ports. I'm guessing Packer uses one of them as its source port. So:

  "ssh_host_port_min": 3213,
  "ssh_host_port_max": 3214,
  "qemuargs": [
    ["-netdev", "user,id=user.0,hostfwd=tcp::3213-:22,hostfwd=tcp::3214-:22,net=10.0.2.0/24"],
    ["-device", "virtio-net,netdev=user.0"],
    ["-netdev", "user,id=user.1"],
    ["-device", "virtio-net,netdev=user.1"],
    ["-netdev", "user,id=user.2"],
    ["-device", "virtio-net,netdev=user.2"],
    ["-m", "128M"]
  ],

This is not an equivalent of the above VirtualBox setup, but at least it's enough to make Packer create the image successfully. Actually running the image properly afterwards is a separate problem.