参数中的 UUID Java servlet Libvirt

UUID in argument Java servlet Libvirt

我想调用创建新虚拟机的方法。除了UUID,我什么都有。如何在调用方法的参数中插入随机生成的 UUID?

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;
}

这将起作用:

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

但请确保在您的方法中省略 vmUuid = UUID.randomUUID(); 行形式。