// you are reading...

Citrix

使用XAPI根据模板创建XenServer虚拟机

下面的程序可以根据一个给定的模板克隆一个虚拟机,该代码也给出了一个方法来得到新创建的虚拟机的MAC地址。使用方法如下:

CreateClone XenServer的IP地址 <用户名> <密码> <模板名>

///


/// Clone a Virtual machine from a template and determine virtual machines mac address.
///

public class Program
{
public static void Main(string[] args)
{

// Host information necessary to get started
string hostname = args[0];
int port = 80; // default
string username = args[1]; ;
string password = args[2];
string template = args[3];

// Establish a session
Session session = new Session(hostname, port);

// Authenticate with username and password.
//The third parameter tells the server which API
//version we support.
session.login_with_password(username, password, API_Version.API_1_3);

List> vmRefs = VM.get_by_name_label(session, template);
if (vmRefs.Count == 0)
System.Console.WriteLine(“Template not found”);

foreach (XenRef vmRef in vmRefs)
{
if (vmRefs.Count == 1)
{
VM vm = VM.get_record(session, vmRef);
System.Console.WriteLine(“Cloning VM ‘{0}’…”, vm.name_label);
XenRef cloneVMref = VM.clone(session, vmRef,
string.Format(“Cloned VM (from ‘{0}’)”,vm.name_label));
System.Console.WriteLine(“Cloning VM ‘{0}’… Done”, cloneVMref.ToString());
VM.provision(session, cloneVMref);
VM CloneVM = VM.get_record(session, cloneVMref);

foreach (XenRef vifref in CloneVM.VIFs)
{
System.Console.WriteLine(VIF.get_MAC(session, vifref));
}
}
else { System.Console.WriteLine(“More then one VM Template found with same name”); }
}
}

}

Bookmark and Share

Discussion

No comments for “使用XAPI根据模板创建XenServer虚拟机”

Post a comment