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?
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:
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.