VMware Cloud Community
Lug77
Enthusiast
Enthusiast
Jump to solution

Move-VM and Set Storage Policy

I'm in the process of developing a script to migrate VMs from NFS storage to VSAN and I'm running into a problem.  In order for the VMs to be placed on the VSAN datastore, I need to modify their storage policy when it's migrated.  If I do this via the web client, it works fine.  The issue I'm having is trying to figure out how to do this with PowerCLI.  I was hoping there would be a parameter in Move-VM but since there isn't, I used Set-SpbmEntityConfiguration to change the storage policy of the VM and tried the migration again.  Now I get the following error:

Move-VM : 7/6/2017 2:33:23 PM   Move-VM         The operation for the entity "vmname" failed with the following message: "Failed waiting for data. Error 195887107. Not found. ". Failed waiting for data. Error 195887107. Not found.

A

fatal internal error occurred. See the virtual machine's log for more details. Failed to create one or more destination disks.

At line:2 char:12

+     $VMs = Move-VM -VM $VMs -Destination $vmhost -Confirm:$false -Dat ...

+            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Move-VM], GenericVmConfigFault

    + FullyQualifiedErrorId : Client20_TaskServiceImpl_CheckServerSideTaskUpdates_OperationFailed,VMware.VimAutomation.ViCore.Cmdlets.Commands.MoveVM

I reviewed VMware KB 2149585 (https://kb.vmware.com/kb/2149585) as it documents the same error but in my case I'm nut using virtual RDMs or snapshots.

Reply
0 Kudos
1 Solution

Accepted Solutions
Lug77
Enthusiast
Enthusiast
Jump to solution

Since I couldn't find a way to set the default storage policy of the VSAN datastore I went back to my initial question of how to set the storage policy while performing the move action and leaving the default storage policy as is.  After much research of the APIs I came up with this and it appears to be working.

$FTT0 = Get-SpbmStoragePolicy -Name "FTT=0 VSAN Storage Policy"

$VM = Get-VM "vmname"

$DS = Get-Datastore "vsanDatastore"

$VMMoref = Get-View $VM

$DSMoref = Get-View $DS

$DefProfileSpec = New-Object VMware.Vim.VirtualMachineDefinedProfileSpec

$DefProfileSpec.ProfileId = $FTT0.Id

$DiskID = $VMMoref.LayoutEx.Disk.Key


$spec = New-Object VMware.Vim.VirtualMachineRelocateSpec

$spec.datastore = $DSMoref.MoRef

$spec.Profile = $DefProfileSpec

$spec.disk = New-Object VMware.Vim.VirtualMachineRelocateSpecDiskLocator

$spec.disk[0].diskId = $DiskID

$spec.disk[0].datastore = $DSMoref.MoRef

$spec.disk[0].Profile = $DefProfileSpec


$VMMoref.RelocateVM_Task($spec, "defaultPriority")

View solution in original post

Reply
0 Kudos
11 Replies
LucD
Leadership
Leadership
Jump to solution

Shouldn't the order be first storage vMotion and then change the Storage Profile?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Lug77
Enthusiast
Enthusiast
Jump to solution

If I attempt to storage vMotion before changing the storage policy I get this error since the default storage policy is FTT=1:

Move-VM : 7/6/2017 2:13:35 PM   Move-VM         The operation for the entity "vmname" failed with the following message: "Cannot complete file creation operation.". Failed to create object. There are currently 1 usable fault domains. The operation requires 2 more usable fault domains.

In this scenario, I only have a single fault domain because the other node for the 2 Node VSAN cluster has yet to be upgraded.  It's currently hosting the NFS mount the VMs are running on.  The VMs need to be migrated off of the NFS mount so the host can be upgraded and joined to the VSAN cluster.

Reply
0 Kudos
Lug77
Enthusiast
Enthusiast
Jump to solution

I just found that if I change the default storage policy for the target VSAN Datastore I'm able to move the VMs with Move-VM.  The problem with this is I couldn't find how to change the default storage policy with PowerCLI.  Set-SpbmEntityConfiguration would seem to be the correct cmdlet to do this with but it doesn't take a datastore as an entity type.  Initially, I tried using Set-VSANClusterConfiguration -StoragePolicy to apply the FTT=0 policy but apparently this isn't the same as the default storage policy for the VSAN Datastore within the cluster.

Reply
0 Kudos
adamjg
Hot Shot
Hot Shot
Jump to solution

This works for tag-based SAN storage policies.  I'm not sure if it will work for vSAN, but it's worth a shot...

Get-VM -Name $myVM | Set-SpbmEntityConfiguration -StoragePolicy $myStoragePolicy

$MyHDs = Get-VM -Name $myVM | Get-HardDisk

Set-SpbmEntityConfiguration $MyHDs -StoragePolicy $myStoragePolicy

For some reason this also creates an SDRS override for the VM.  I added this code to delete it:

$vmEntry = @()

$storMgr = Get-View StorageResourceManager

$spec = New-Object VMware.Vim.StorageDrsConfigSpec

$dsc = Get-DatastoreCluster $myDatastoreCluster

Get-VM -Datastore $dsc | where {$_.Name -eq $MyVM} | %{

    $vmEntry = New-Object VMware.Vim.StorageDrsVmConfigSpec

    $vmEntry.Operation = "add"

    $vmEntry.Info = New-Object VMware.Vim.StorageDrsVmConfigInfo

    $vmEntry.Info.Vm = $_.ExtensionData.MoRef

    $spec.vmConfigSpec += $vmEntry

}

$storMgr.ConfigureStorageDrsForPod($dsc.ExtensionData.MoRef,$spec,$true)

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is a Storage Policy for a specific VM, is there also a way to set a default Storage Policy for a datastore?

Which SP does the VM get when it is created on that specific datastore.

I was looking for something like that, but haven't been able to find anything


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
adamjg
Hot Shot
Hot Shot
Jump to solution

Oops sorry, I misread the original post. I searched for the same thing a while back as I had a number (50+) of datastores to create.  I ended up creating them by hand, then running a script through to set a tag, then creating the tag based storage policy for a new datastore cluster which pulled in each datastore.  Not pretty but fortunately it's not a very regular thing.

Reply
0 Kudos
Lug77
Enthusiast
Enthusiast
Jump to solution

Thing is I have a few more than 50 datastores to do this on so I'd really prefer not to have to do this manually. 

Reply
0 Kudos
Lug77
Enthusiast
Enthusiast
Jump to solution

Since I couldn't find a way to set the default storage policy of the VSAN datastore I went back to my initial question of how to set the storage policy while performing the move action and leaving the default storage policy as is.  After much research of the APIs I came up with this and it appears to be working.

$FTT0 = Get-SpbmStoragePolicy -Name "FTT=0 VSAN Storage Policy"

$VM = Get-VM "vmname"

$DS = Get-Datastore "vsanDatastore"

$VMMoref = Get-View $VM

$DSMoref = Get-View $DS

$DefProfileSpec = New-Object VMware.Vim.VirtualMachineDefinedProfileSpec

$DefProfileSpec.ProfileId = $FTT0.Id

$DiskID = $VMMoref.LayoutEx.Disk.Key


$spec = New-Object VMware.Vim.VirtualMachineRelocateSpec

$spec.datastore = $DSMoref.MoRef

$spec.Profile = $DefProfileSpec

$spec.disk = New-Object VMware.Vim.VirtualMachineRelocateSpecDiskLocator

$spec.disk[0].diskId = $DiskID

$spec.disk[0].datastore = $DSMoref.MoRef

$spec.disk[0].Profile = $DefProfileSpec


$VMMoref.RelocateVM_Task($spec, "defaultPriority")

Reply
0 Kudos
Lug77
Enthusiast
Enthusiast
Jump to solution

Once I was ready to change the storage policy of the VMs back to the FTT=1 storage policy I tried using Set-SpbmEntityConfiguration.  The task completed successfully just as before but when I when to check the status of the virtual objects I found that only the VM Home objects were changed to the FTT=1 policy.  To change the policy of the disks, I ran Set-SpbmEntityConfiguration again but this time passing in the disks.  Once I did this, all the objects were changed to the FTT=1 policy.  In reflection, it's possible that this may have been why the move of the VMs failed after setting the FTT=0.  Next time I run through this scenario I will test to see if Set-SpbmEntityConfiguration can be used rather than the original solution I posted.

Changing the storage policy for the VM Home objects:

$VMs | Set-SpbmEntityConfiguration -StoragePolicy "Virtual SAN Default Storage Policy"

Changing the storage policy for the VM Disk objects:

$VMs | Get-HardDisk | Set-SpbmEntityConfiguration -StoragePolicy "Virtual SAN Default Storage Policy"

nicholas1982
Hot Shot
Hot Shot
Jump to solution

Hi Luc,

Just wondering if you know the latest PowerCLI might have a way to query or Set the Default Storage Policy of a VSAN datastore, or is there an API that can do this?

Nicholas
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

There is the Set-SpbmEntityConfiguration, but that apparently only "sets" profiles for specific entities.

The PbmAssignDefaultRequirementProfile method might be a solution, but I haven't seen any examples with that to change the default VSAN storage profile.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos