2016-10-02 27 views
0

我想调用创建新虚拟机的方法。除了UUID,我拥有一切。我怎样才能在我的参数中插入随机生成的UUID来调用该方法?参数Java servlet中的UUID Libvirt

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

为什么你需要在你的方法签名UUID如果你重新分配它的随机值('vmUuid = UUID.randomUUID();')里面的方法? –

+0

'createVM(“test”,UUID.randomUUID(),20000,2,“/ home/jur/Downloads/debian-8.6.0-amd64-netinst.iso”);'会工作,但在你的方法中你会覆盖反正UUID? –

回答

1

这将工作:

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

但请务必留下你的方法里面的vmUuid = UUID.randomUUID();线形态。