Пример #1
0
        public VStorageObject GetVDiskByName(ManagedObjectReference dsMoref, string vdiskName)
        {
            ManagedObjectReference     vStorageMgr = svcConn.ServiceContent.vStorageObjectManager;
            ListVStorageObjectRequest  req         = new ListVStorageObjectRequest(vStorageMgr, dsMoref);
            ListVStorageObjectResponse response    = svcConn._service.ListVStorageObject(req);
            VStorageObject             result      = null;

            foreach (ID id in response.returnval)
            {
                VStorageObject disk = svcConn._service.RetrieveVStorageObject(vStorageMgr, id, dsMoref);
                if (disk.config.name == vdiskName)
                {
                    if (result == null)
                    {
                        result = disk;
                    }
                    Console.WriteLine("found disk = " + disk.config.id.id);
                }
            }

            if (result == null)
            {
                throw new Exception("could not find vDisk with name=" + vdiskName);
            }

            return(result);
        }
Пример #2
0
        public VStorageObject CreateNewVDisk(ManagedObjectReference dsMoref, string name, bool keepAfterDelete, string provisioningType, long sizeInMB)
        {
            ManagedObjectReference vStorageMgr = svcConn.ServiceContent.vStorageObjectManager;
            VslmCreateSpec         spec        = new VslmCreateSpec();

            spec.name = name;
            spec.keepAfterDeleteVm = keepAfterDelete;
            var backing = new VslmCreateSpecDiskFileBackingSpec();

            backing.datastore        = dsMoref;
            backing.provisioningType = provisioningType;
            spec.backingSpec         = backing;
            spec.capacityInMB        = sizeInMB;
            var task = svcConn._service.CreateDisk_Task(vStorageMgr, spec);

            var taskResult = WaitForTask(task);

            if (taskResult.Item1.Equals(TaskInfoState.success))
            {
                VStorageObject newDisk = (VStorageObject)taskResult.Item2.result;
                return(newDisk);
            }
            else
            {
                throw new Exception(taskResult.Item2.error.localizedMessage);
            }
        }
Пример #3
0
        static void Main(string[] args)
        {
            ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

            CommandLine.Parser.Default.ParseArguments <Options>(args).WithParsed <Options>(o =>
            {
                Program p = new Program(o.Server, o.Username, o.Password);

                Console.WriteLine("Finding Datastore with name=" + o.Datastore);
                ManagedObjectReference dsMoref = p.svcUtil.getEntityByName("Datastore", o.Datastore);
                Console.WriteLine("Found Datastore with Moref=" + dsMoref.Value);

                Console.WriteLine("Finding VM with name=" + o.VMName);
                ManagedObjectReference vm = p.svcUtil.getEntityByName("VirtualMachine", o.VMName);
                Console.WriteLine("Found VM with Moref=" + vm.Value);

                Console.WriteLine("Finding a SCSI Placement for new disk");
                var diskPlacement = p.GetControllerPlacement(vm);
                Console.WriteLine("SCSI Controller=" + diskPlacement.Item1);
                Console.WriteLine("SCSI UnitNumber=" + diskPlacement.Item2);

                Console.WriteLine("Reconcile the datastore inventory info of virtual storage objects...");
                p.ReconcileDatastoreInventory(dsMoref); //in case files were deleted without vStorageManager is aware.

                Console.WriteLine("Finding VDisk with name=" + o.FcdName);
                VStorageObject vdisk = p.GetVDiskByName(dsMoref, o.FcdName);
                Console.WriteLine("Using vDisk id=" + vdisk.config.id.id);
                Console.WriteLine("Using vDisk Name=" + vdisk.config.name);

                string snapshotName = o.SnapshotNamePrefix + Guid.NewGuid();
                Console.WriteLine("going to create snapshot with name=" + snapshotName);
                ID snapshotId = p.CreateVDiskSnapshot(dsMoref, vdisk.config.id, snapshotName);
                Console.WriteLine("created snapshot with id=" + snapshotId.id);

                string newDiskName = o.FcdName + "_" + snapshotId.id;
                Console.WriteLine("Creating a new vDisk with name=" + newDiskName);
                VStorageObject newDisk = p.CreateDiskFromSnapshot(dsMoref, vdisk.config.id, snapshotId, newDiskName);
                Console.WriteLine("Created a new  vDisk with id=" + newDisk.config.id.id);

                string vmdkPath = ((BaseConfigInfoDiskFileBackingInfo)newDisk.config.backing).filePath;
                Console.WriteLine("Created a new vDisk in path=" + vmdkPath);

                Console.WriteLine("Attaching new vDisk to VM=" + vm.Value);
                p.AttachDiskToVm(vm, newDisk.config.id, dsMoref, diskPlacement.Item1, diskPlacement.Item2);
                Console.WriteLine("Attached disk to vm in SCSI Controller=" + diskPlacement.Item1);
                Console.WriteLine("Attached disk to vm in SCSI UnitNumber=" + diskPlacement.Item2);
            });

            Console.WriteLine("Press Enter to continue...");
            Console.Read();
        }
Пример #4
0
        public VStorageObject CreateDiskFromSnapshot(ManagedObjectReference dsMoref, ID vdiskId, ID snapshotId, string newDiskName)
        {
            VirtualMachineProfileSpec[] profile    = null;
            string                 path            = null;
            CryptoSpec             crypto          = null;
            ManagedObjectReference vStorageManager = svcConn._sic.vStorageObjectManager;

            CreateDiskFromSnapshot_TaskRequest req = new CreateDiskFromSnapshot_TaskRequest(vStorageManager, vdiskId, dsMoref, snapshotId, newDiskName, profile, crypto, path);
            var task       = svcConn._service.CreateDiskFromSnapshot_Task(req).returnval;
            var taskResult = WaitForTask(task);

            if (taskResult.Item1.Equals(TaskInfoState.error))
            {
                throw new Exception(taskResult.Item2.error.localizedMessage);
            }
            else
            {
                VStorageObject newVdisk = (VStorageObject)taskResult.Item2.result;
                return(newVdisk);
            }
        }