参数Java servlet Libvirt中的UUID

时间:2016-10-02 18:10:55

标签: java servlets methods uuid 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;
}

1 个答案:

答案 0 :(得分:1)

这将有效:

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

但请务必在方法中省略vmUuid = UUID.randomUUID();行格式。