|
16 | 16 | # pylint: disable=wrong-import-position
|
17 | 17 | from framework.artifacts import disks, kernels
|
18 | 18 | from framework.microvm import MicroVMFactory
|
19 |
| -from framework.utils import generate_mmds_get_request, generate_mmds_session_token |
| 19 | +from framework.utils import ( |
| 20 | + configure_mmds, |
| 21 | + generate_mmds_get_request, |
| 22 | + generate_mmds_session_token, |
| 23 | +) |
20 | 24 | from framework.utils_cpuid import CpuVendor, get_cpu_vendor
|
21 | 25 | from host_tools.cargo_build import get_firecracker_binaries
|
22 | 26 |
|
| 27 | + |
23 | 28 | # pylint: enable=wrong-import-position
|
24 | 29 |
|
25 | 30 | # Default IPv4 address to route MMDS requests.
|
26 | 31 | IPV4_ADDRESS = "169.254.169.254"
|
27 | 32 | NET_IFACE_FOR_MMDS = "eth3"
|
28 |
| -# Path to the VM configuration file. |
29 |
| -VM_CONFIG_FILE = "tools/create_snapshot_artifact/complex_vm_config.json" |
30 | 33 | # Root directory for the snapshot artifacts.
|
31 | 34 | SNAPSHOT_ARTIFACTS_ROOT_DIR = "snapshot_artifacts"
|
32 | 35 |
|
@@ -95,77 +98,66 @@ def main():
|
95 | 98 | for kernel in kernels(glob="vmlinux-*"):
|
96 | 99 | for rootfs in disks(glob="ubuntu-*.squashfs"):
|
97 | 100 | print(kernel, rootfs, cpu_template)
|
98 |
| - vm = vm_factory.build() |
99 |
| - create_snapshots(vm, rootfs, kernel, cpu_template) |
100 |
| - |
101 |
| - |
102 |
| -def create_snapshots(vm, rootfs, kernel, cpu_template): |
103 |
| - """Snapshot microVM built from vm configuration file.""" |
104 |
| - # Get ssh key from read-only artifact. |
105 |
| - vm.ssh_key = rootfs.with_suffix(".id_rsa") |
106 |
| - vm.rootfs_file = rootfs |
107 |
| - vm.kernel_file = kernel |
108 |
| - |
109 |
| - # adapt the JSON file |
110 |
| - vm_config_file = Path(VM_CONFIG_FILE) |
111 |
| - obj = json.load(vm_config_file.open(encoding="UTF-8")) |
112 |
| - obj["boot-source"]["kernel_image_path"] = kernel.name |
113 |
| - obj["drives"][0]["path_on_host"] = rootfs.name |
114 |
| - obj["drives"][0]["is_read_only"] = True |
115 |
| - obj["machine-config"]["cpu_template"] = cpu_template |
116 |
| - vm.create_jailed_resource(vm_config_file) |
117 |
| - vm_config = Path(vm.chroot()) / vm_config_file.name |
118 |
| - vm_config.write_text(json.dumps(obj)) |
119 |
| - vm.jailer.extra_args = {"config-file": vm_config_file.name} |
120 |
| - |
121 |
| - # since we are using a JSON file, we need to do this manually |
122 |
| - vm.create_jailed_resource(rootfs) |
123 |
| - vm.create_jailed_resource(kernel) |
124 |
| - |
125 |
| - for i in range(4): |
126 |
| - vm.add_net_iface(api=False) |
127 |
| - |
128 |
| - vm.spawn(log_level="Info") |
129 |
| - |
130 |
| - # Ensure the microVM has started. |
131 |
| - assert vm.state == "Running" |
132 |
| - |
133 |
| - # Populate MMDS. |
134 |
| - data_store = { |
135 |
| - "latest": { |
136 |
| - "meta-data": { |
137 |
| - "ami-id": "ami-12345678", |
138 |
| - "reservation-id": "r-fea54097", |
139 |
| - "local-hostname": "ip-10-251-50-12.ec2.internal", |
140 |
| - "public-hostname": "ec2-203-0-113-25.compute-1.amazonaws.com", |
141 |
| - } |
142 |
| - } |
143 |
| - } |
144 |
| - populate_mmds(vm, data_store) |
145 |
| - |
146 |
| - # Iterate and validate connectivity on all ifaces after boot. |
147 |
| - for i in range(4): |
148 |
| - exit_code, _, _ = vm.ssh_iface(i).run("sync") |
149 |
| - assert exit_code == 0 |
150 |
| - |
151 |
| - # Validate MMDS. |
152 |
| - validate_mmds(vm.ssh, data_store) |
153 |
| - |
154 |
| - # Snapshot the microVM. |
155 |
| - snapshot = vm.snapshot_diff() |
156 |
| - |
157 |
| - # Create snapshot artifacts directory specific for the kernel version used. |
158 |
| - guest_kernel_version = re.search("vmlinux-(.*)", kernel.name) |
159 |
| - |
160 |
| - snapshot_artifacts_dir = ( |
161 |
| - Path(SNAPSHOT_ARTIFACTS_ROOT_DIR) |
162 |
| - / f"{guest_kernel_version.group(1)}_{cpu_template}_guest_snapshot" |
163 |
| - ) |
164 |
| - snapshot_artifacts_dir.mkdir(parents=True) |
165 |
| - snapshot.save_to(snapshot_artifacts_dir) |
166 |
| - print(f"Copied snapshot to: {snapshot_artifacts_dir}.") |
167 |
| - |
168 |
| - vm.kill() |
| 101 | + vm = vm_factory.build(kernel, rootfs) |
| 102 | + vm.spawn(log_level="Info") |
| 103 | + vm.basic_config( |
| 104 | + vcpu_count=2, |
| 105 | + mem_size_mib=1024, |
| 106 | + cpu_template=cpu_template, |
| 107 | + track_dirty_pages=True, |
| 108 | + ) |
| 109 | + # Add 4 network devices |
| 110 | + for i in range(4): |
| 111 | + vm.add_net_iface() |
| 112 | + # Add a vsock device |
| 113 | + vm.api.vsock.put(vsock_id="vsock0", guest_cid=3, uds_path="/v.sock") |
| 114 | + # Add MMDS |
| 115 | + configure_mmds(vm, ["eth3"], version="V2") |
| 116 | + # Add a memory balloon. |
| 117 | + vm.api.balloon.put( |
| 118 | + amount_mib=0, deflate_on_oom=True, stats_polling_interval_s=1 |
| 119 | + ) |
| 120 | + |
| 121 | + vm.start() |
| 122 | + # Ensure the microVM has started. |
| 123 | + assert vm.state == "Running" |
| 124 | + |
| 125 | + # Populate MMDS. |
| 126 | + data_store = { |
| 127 | + "latest": { |
| 128 | + "meta-data": { |
| 129 | + "ami-id": "ami-12345678", |
| 130 | + "reservation-id": "r-fea54097", |
| 131 | + "local-hostname": "ip-10-251-50-12.ec2.internal", |
| 132 | + "public-hostname": "ec2-203-0-113-25.compute-1.amazonaws.com", |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + populate_mmds(vm, data_store) |
| 137 | + |
| 138 | + # Iterate and validate connectivity on all ifaces after boot. |
| 139 | + for i in range(4): |
| 140 | + exit_code, _, _ = vm.ssh_iface(i).run("sync") |
| 141 | + assert exit_code == 0 |
| 142 | + |
| 143 | + # Validate MMDS. |
| 144 | + validate_mmds(vm.ssh, data_store) |
| 145 | + |
| 146 | + # Snapshot the microVM. |
| 147 | + snapshot = vm.snapshot_diff() |
| 148 | + |
| 149 | + # Create snapshot artifacts directory specific for the kernel version used. |
| 150 | + guest_kernel_version = re.search("vmlinux-(.*)", kernel.name) |
| 151 | + |
| 152 | + snapshot_artifacts_dir = ( |
| 153 | + Path(SNAPSHOT_ARTIFACTS_ROOT_DIR) |
| 154 | + / f"{guest_kernel_version.group(1)}_{cpu_template}_guest_snapshot" |
| 155 | + ) |
| 156 | + snapshot_artifacts_dir.mkdir(parents=True) |
| 157 | + snapshot.save_to(snapshot_artifacts_dir) |
| 158 | + print(f"Copied snapshot to: {snapshot_artifacts_dir}.") |
| 159 | + |
| 160 | + vm.kill() |
169 | 161 |
|
170 | 162 |
|
171 | 163 | if __name__ == "__main__":
|
|
0 commit comments