Xenserver 5.5 Api Cheat Sheet Page 2

ADVERTISEMENT

Replace the MAC address from a configurable list.
// Recycling MAC addresses solves the DHCP IP address pool problem
// macAddresses is a comma delimited configuration string of
// reusable MAC addresses.
// find first virtual network interface
VIF vif = vm.getVIFs(connection).iterator().next();
String macAddress = getVacantMacAddress(connection,macAddresses);
replaceMacAddress(vif,macAddress);
// returns the first configured MAC address that is not in use
private String getVacantMacAddress(Connection connection, String macAddresses) {
Set<String> usedMacAddresses =
new
HashSet<String>();
Map<VIF, Record> allVifs = VIF.getAllRecords(connection);
for
(VIF vif : allVifs.keySet()) {
VIF.Record vifrecord = allVifs.get(vif);
usedMacAddresses.add(vifrecord.MAC);
}
String vacantMacAddress = null;
for
(String macAddress : macAddresses.split(",")) {
if
(!usedMacAddresses.contains(macAddress)) {
vacantMacAddress = macAddress;
break;
}
}
return
vacantMacAddress;
}
// updates the specified vif with the new MAC address
private void replaceMacAddress(VIF vif, String newMac) {
// get virtual network interface details
VIF.Record vifrecord = vif.getRecord(connection);
// destroy old virtual network interface
vif.destroy(connection);
// create a new virtual network with the modified MAC address
vifrecord.MAC = newMac;
vifrecord.MACAutogenerated = false;
VIF.create(connection, vifrecord);
}

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 4