25 Sep 2008 09:54 PM EDT

Here is a code sample which will clone a virtual machine from a given template. I have also shown how to get the "MAC Address" of the newly created VM. Here is sample usage of the code

CreateClone <IP Address of XenServer> <UserName> <Password> <Template>
/// <summary>
/// Clone a Virtual machine from a template and determine virtual machines mac address.
/// </summary>
    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<XenRef<VM>> vmRefs = VM.get_by_name_label(session, template);
            if (vmRefs.Count == 0)
                System.Console.WriteLine("Template not found");

            foreach (XenRef<VM> vmRef in vmRefs)
            {
                if (vmRefs.Count == 1)
                {
                    VM vm = VM.get_record(session, vmRef);
                    System.Console.WriteLine("Cloning VM '{0}'...", vm.name_label);
                    XenRef<VM> 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<VIF> 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"); }
            }
        }

    }

On the other note XenServer 5.0 SDK has been released on the CDN and you can download the sample codes here

Please let me know what other samples would you like to see on the CDN regarding XenServer. If we had to do a webinar on XenServer API what would you like to hear about?

Which is your preferred language while writing XenServer API samples? Choose
Microsoft C#
PowerShell
Python
Java
Other
Permalink | Comments (5) |

what a luck...I was exactly looking for something like this.

Thanks

Posted by Anonymous at Sep 26, 2008 17:10 | Reply To This

Vishal thanks for the api sample. I need a similar script in PowerShell. How do I get it?

Posted by Anonymous at Sep 26, 2008 23:33 | Reply To This

We need more samples like this. Keep up the good work.

Posted by Anonymous at Sep 29, 2008 09:24 | Reply To This

Yeah, I agree.  This is really nice work, Vishal. 

Posted by Anonymous at Oct 09, 2008 14:36 | Reply To This

Thanks. We will be releasing  PowerShell commandlets very soon. Stay tuned.