6 makeEc2Test = { name, image, userData, script, hostname ? "ec2-instance", sshPublicKey ? null, meta ? {} }:
8 metaData = pkgs.stdenv.mkDerivation {
11 mkdir -p $out/1.0/meta-data
12 ln -s ${pkgs.writeText "userData" userData} $out/1.0/user-data
13 echo "${hostname}" > $out/1.0/meta-data/hostname
14 echo "(unknown)" > $out/1.0/meta-data/ami-manifest-path
15 '' + optionalString (sshPublicKey != null) ''
16 mkdir -p $out/1.0/meta-data/public-keys/0
17 ln -s ${pkgs.writeText "sshPublicKey" sshPublicKey} $out/1.0/meta-data/public-keys/0/openssh-key
28 image_dir = os.path.join(
29 os.environ.get("TMPDIR", tempfile.gettempdir()), "tmp", "vm-state-machine"
31 os.makedirs(image_dir, mode=0o700, exist_ok=True)
32 disk_image = os.path.join(image_dir, "machine.qcow2")
33 subprocess.check_call(
40 "backing_file=${image}",
44 subprocess.check_call(["qemu-img", "resize", disk_image, "10G"])
46 # Note: we use net=169.0.0.0/8 rather than
47 # net=169.254.0.0/16 to prevent dhcpcd from getting horribly
48 # confused. (It would get a DHCP lease in the 169.254.*
49 # range, which it would then configure and prompty delete
50 # again when it deletes link-local addresses.) Ideally we'd
51 # turn off the DHCP server, but qemu does not have an option
55 + " -device virtio-net-pci,netdev=vlan0"
56 + " -netdev 'user,id=vlan0,net=169.0.0.0/8,guestfwd=tcp:169.254.169.254:80-cmd:${pkgs.micro-httpd}/bin/micro_httpd ${metaData}'"
57 + f" -drive file={disk_image},if=virtio,werror=report"
61 machine = create_machine({"startCommand": start_command})