cloneVm function
Implementation
Future<void> cloneVm({
required String baseVmName,
required String vmName,
required String buildJobId,
required String runId,
required String workerId,
}) async {
await logInfo(buildJobId, runId, 'Cloning VM $baseVmName to $vmName...');
await VirtualMachine.clone(sourceName: baseVmName, targetName: vmName);
// Give each worker's VM a distinct, stable MAC address so that multiple
// workers on the same physical host (one VM each) don't collide on the
// shared vmnet bridge (same MAC -> same DHCP IP/ARP entry). The base image
// tolerates a changed MAC (it re-runs DHCP on boot); we keep the validated
// da:d7:a6:2d:e9 prefix and vary only the last octet by the worker number.
final mac = macForWorker(workerId);
final configFile = File(
'${VirtualMachine.defaultVmsDir}/$vmName/config.json',
);
if (configFile.existsSync()) {
try {
final config =
jsonDecode(configFile.readAsStringSync()) as Map<String, dynamic>;
if (config['macAddress'] != mac) {
config['macAddress'] = mac;
configFile.writeAsStringSync(
const JsonEncoder.withIndent(' ').convert(config),
);
await logInfo(buildJobId, runId, 'Assigned VM MAC $mac for $workerId');
}
} catch (e) {
await logWarning(buildJobId, runId, 'Failed to set VM MAC: $e');
}
}
}