|
38 | 38 | from framework.microvm_helpers import MicrovmHelpers |
39 | 39 | from framework.properties import global_props |
40 | 40 | from framework.utils_drive import VhostUserBlkBackend, VhostUserBlkBackendType |
| 41 | +from framework.utils_uffd import spawn_pf_handler, uffd_handler |
41 | 42 | from host_tools.fcmetrics import FCMetricsMonitor |
42 | 43 | from host_tools.memory import MemoryMonitor |
43 | 44 |
|
@@ -1128,6 +1129,52 @@ def build_from_snapshot(self, snapshot: Snapshot): |
1128 | 1129 | vm.restore_from_snapshot(snapshot, resume=True) |
1129 | 1130 | return vm |
1130 | 1131 |
|
| 1132 | + def build_n_from_snapshot( |
| 1133 | + self, |
| 1134 | + snapshot, |
| 1135 | + nr_vms, |
| 1136 | + *, |
| 1137 | + uffd_handler_name=None, |
| 1138 | + incremental=False, |
| 1139 | + use_snapshot_editor=True, |
| 1140 | + ): |
| 1141 | + """A generator of `n` microvms restored, either all restored from the same given snapshot |
| 1142 | + (incremental=False), or created by taking successive snapshots of restored VMs |
| 1143 | + """ |
| 1144 | + for _ in range(nr_vms): |
| 1145 | + microvm = self.build() |
| 1146 | + microvm.spawn(emit_metrics=True) |
| 1147 | + |
| 1148 | + uffd_path = None |
| 1149 | + if uffd_handler_name is not None: |
| 1150 | + pf_handler = spawn_pf_handler( |
| 1151 | + microvm, |
| 1152 | + uffd_handler(uffd_handler_name, binary_dir=self.binary_path), |
| 1153 | + snapshot.mem, |
| 1154 | + ) |
| 1155 | + uffd_path = pf_handler.socket_path |
| 1156 | + |
| 1157 | + snapshot_copy = microvm.restore_from_snapshot( |
| 1158 | + snapshot, resume=True, uffd_path=uffd_path |
| 1159 | + ) |
| 1160 | + |
| 1161 | + yield microvm |
| 1162 | + |
| 1163 | + if incremental: |
| 1164 | + new_snapshot = microvm.make_snapshot(snapshot.snapshot_type) |
| 1165 | + |
| 1166 | + if snapshot.is_diff: |
| 1167 | + new_snapshot = new_snapshot.rebase_snapshot( |
| 1168 | + snapshot, use_snapshot_editor |
| 1169 | + ) |
| 1170 | + |
| 1171 | + snapshot = new_snapshot |
| 1172 | + |
| 1173 | + microvm.kill() |
| 1174 | + snapshot_copy.delete() |
| 1175 | + |
| 1176 | + snapshot.delete() |
| 1177 | + |
1131 | 1178 | def kill(self): |
1132 | 1179 | """Clean up all built VMs""" |
1133 | 1180 | for vm in self.vms: |
|
0 commit comments