Jur Hillebrink Jur Hillebrink - 2 months ago 8
Java Question

UUID in argument Java servlet Libvirt

I want to call the method for creating a new virtual machine. I have everything except the UUID. How can I insert the random generated UUID in my arguments for calling the method?

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub


response.getWriter().append("Served at: ").append(request.getContextPath());

createVM("test",,20000,2,"/home/jur/Downloads/debian-8.6.0-amd64-netinst.iso");
}


public boolean createVM(String vmName,
UUID vmUuid,
long vmMemory,
int vmCpu,
String vmImage) {
String template;

Connect conn;

try {
System.out.println("Connecting to local hypervisor");
conn = new Connect("qemu:///system");

System.out.println("Creating template");
vmUuid = UUID.randomUUID();
template = TEMPLATE;
template = template.replace("$vmName", vmName);
template = template.replace("$vmMemory", String.valueOf(vmMemory));
template = template.replace("$vmCpu", String.valueOf(vmCpu));
template = template.replace("$vmImage", vmImage);
template = template.replace("$vmUuid", vmUuid.toString());

System.out.println("Resulting template: \n" + template);
System.out.println("Creating VM");
Domain domain = conn.domainCreateXML(template, 0);

conn.close();
} catch (LibvirtException e) {
e.printStackTrace();
return false;
}

return true;
}

Answer

This will work:

createVM("test",UUID.randomUUID(),20000,2,"/home/jur/Downloa‌​ds/debian-8.6.0-amd6‌​4-netinst.iso");

But make sure to leave out the vmUuid = UUID.randomUUID(); line form inside your method.