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
20 indentLines = str: concatLines (map (s: " " + s) (splitString "\n" str));
29 image_dir = os.path.join(
30 os.environ.get("TMPDIR", tempfile.gettempdir()), "tmp", "vm-state-machine"
32 os.makedirs(image_dir, mode=0o700, exist_ok=True)
33 disk_image = os.path.join(image_dir, "machine.qcow2")
34 subprocess.check_call(
43 "backing_file=${image}",
47 subprocess.check_call(["qemu-img", "resize", disk_image, "10G"])
49 # Note: we use net=169.0.0.0/8 rather than
50 # net=169.254.0.0/16 to prevent dhcpcd from getting horribly
51 # confused. (It would get a DHCP lease in the 169.254.*
52 # range, which it would then configure and promptly delete
53 # again when it deletes link-local addresses.) Ideally we'd
54 # turn off the DHCP server, but qemu does not have an option
58 + " -device virtio-net-pci,netdev=vlan0"
59 + " -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}'"
60 + f" -drive file={disk_image},if=virtio,werror=report"
64 machine = create_machine(start_command)
66 '' + indentLines script + ''