• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

virtuallyGhetto

  • About
  • Privacy
  • VMware Cloud on AWS
  • Home Lab
  • Nested Virtualization
  • Automation
    • VMware Kickstart
    • VMware API/SDK/CLI
    • VMware vMA/VIMA
    • VMware OVF / OVFTOOL
  • Apple Mac
  • VCSA
  • VSAN

svmotion

How to easily disable vMotion & Cross vCenter vMotion for a particular Virtual Machine?

07/20/2016 by William Lam 6 Comments

The question of disabling vMotion for a specific set of Virtual Machine(s) is not a new one. In fact, this topic comes up on some what of a frequent basis and usually driven by arcane change management processes or worse licensing restrictions. Do not get me wrong, there are definitely some valid use cases where you would not want a particular VM to be migrated off. The classic example is a 3rd Party VM solution that provides Anti-Malware, Intrusion Detection & Firewall capabilities for your workload VMs. For this particular use case, VMware provides our partners with an integration hook into the vSphere platform called ESX Agent Manager (EAM) that ensures these "Service VMs" are not allowed to be powered off or migrated to another ESXi host, even in the case of a Maintenance Mode operation. This solutuion even allows you to configure custom icons for your Service VMs!

For all other use cases outside of the "Service VMs", there really is not an easy way of disabling vMotion for a particular VM. There have been many solutions that have been suggested in the past ranging from disabling DRS for a specific VM, DRS Affinity Rules, VM miss-configurations to break vMotion compatibility to using vSphere Permissions to prevent vMotion operations. However, many of these solutions do not work very well or is very difficult to manage at scale. I actually like the idea of using vSphere Permissions to prevent a vMotion, however, I have seen some customers push back on this because the vSphere Administrator still has the ability to perform this operation. For these cases, customers just want to be able to completely disable vMotion for a given VM and prevent anyone from migrating the VM, including the vSphere Administrators.

Given that this topic had recently come up again, I was wondering if there was an easier way in which this could be achieved and made more manageable for our customers. After thinking about about how EAM handles "disabling" certain operations for a VM and recalling an article I wrote last year which leveraged this exact capability to resolve an NSX Controller issue, I thought why not apply it to this use case here?

UPDATE (09/27/18) - As of vSphere 6.5, the MigrateVM_Task() method has been deprecated in favor of the RelocateVM_Task() which is used to handle BOTH vMotion,  Storage vMotion and other variants. This means that as of 6.5, you no longer have the ability to disable a specific migration type and when you apply the settings, both migration types will be disabled for the specific VM.

Disclaimer: The use of internal APIs are not officially supported by VMware and can change at any time. Please use at your own risk.

Each VM has a property called DisableMethod which lists the specific vSphere API methods that are currently disabled. These are not governed by vSphere Permissions but rather the runtime state of the VM. For example, if you have a VM that is currently powered on, then the PowerOnVM_Task API would not be available and would show up in the disabled list.

Here is a quick PowerCLI snippet on how to retrieve the current set of disable methods for a VM:

$vm = Get-Vm -Name TestVM-1
$vm.ExtensionData.DisabledMethod

disable-vmotion-for-vm-1
As mentioned in this article, the ability to enable and disable these methods are only available as an internal vCenter Server API. However, it is possible to access these APIs using the vSphere MOB, but it is not very user friendly nor intuitive. Below is a screenshot of invoking the disableMethods API using the vSphere MOB.

disable-vmotion-for-vm-0
A couple of weeks back I started to investigate on how we might be able to automate against the vSphere MOB. The result of that investigation lead to the creation of a simple PowerCLI script that allows you to automate operations using the vSphere MOB which I had published here. That work became the foundation for the new PowerCLI script that I had created for disabling and enabling the vMotion capability for a particular VM.

You can download the PowerCLI script here called enable-disable-vsphere-api-method.ps1 which includes two functions Enable-vSphereMethod and Disable-vSphereMethod. You will need to edit the script to provide a couple of pieces of information.

  1. Credentials to your vCenter Server
  2. Name of the VM you wish to either disable or enable vMotion capability on
  3. Name of the vSphere API method you wish to disable (by default this is MigrateVM_Task which maps to the vMotion capability)

By default, I have commented out both functions usage, you will need to manually uncomment one of the lines based on the operation you wish to perform.

To Disable the vMotion capability, run the following:

Disable-vSphereMethod -vc_server $vc_server -vc_username $vc_username -vc_password $vc_password -vmmoref $vm_moref -disable_method $method_name

To Enable the vMotion capability if you had disabled it, run the following:

Enable-vSphereMethod -vc_server $vc_server -vc_username $vc_username -vc_password $vc_password -vmmoref $vm_moref -enable_method $method_name

After the script has completed, you can now re-run the command that we ran earlier to see which methods have been disabled and you should see that the MigrateVM_Task is now part of the disable methods.

disable-vmotion-for-vm-5
If we now login to either the vSphere Web/C# Client and right click on the VM that we had disabled vMotion on, you should also see that the Migrate option is now grayed out and unavailable. This behavior will be true for ALL users including those in the vSphere Administrators group.

disable-vmotion-for-vm-3
It is important to note that vMotion is not only disabled from the UI, but it is also disabled from the vSphere API standpoint which the UI is built on top of. Here is an example of trying to perform a vMotion using the PowerCLI Move-VM cmdlet and you can see that an error is thrown immediately stating that the method has been disabled.

disable-vmotion-for-vm-4
Note: The "self" text output from the PowerCLI command is actually something that you can specify as part of disabling the vMotion capability. This might be useful to specify a change control ID or some string to signal to the user who might be trying to perform the operation. Please refer to the script and search for the "self" keyword if you wish to change it.

The really nice thing about this solution is not only is it really easy to enable or disable, but it can also be managed at scale which many of the other solutions mentioned earlier start to break down. The last thing anyone would want is additional operational overhead to manage manage complex DRS rules (which can still be overridden through manual migrations) or additional vSphere Permissions which also runs into the same problem where a vSphere Administrator can still override by performing a manual migration. This solution does prevent both standard vMotion as well as the new Cross vCenter vMotion capability (both between same/different SSO Domain) that was introduced in vSphere 6.0. You do not need to be running vSphere 6.0 to be able to leverage this solution, this should actually work for almost all versions of vSphere. Lastly, enabling or disabling the functionality does not require any type of system restart or impact to your VM other than the ability to vMotion.

Limitations

Beyond artificially limiting what vSphere DRS and HA can do, I did observe an interesting behavior when a Maintenance Mode operation is performed. If you leave the "Move powered-off and suspended virtual machines to other hosts in the cluster" uncheck, then all VMs will be migrated off and the VMs that have vMotion disabled will reside on the ESXi host while it goes into Maintenance Mode. However, if you do check the box, I did find that the system would override setting and actually move the VM to another ESXi host. This is something to be aware of and may not be a bad thing depending on your requirements.

disable-vmotion-vm-6

Disabling Storage vMotion

You may have noticed that if the VM is in a powered off state, that the Migrate option is still available in the UI. The reason for this is that we only disabled vMotion but you are still allowed to perform a Storage vMotion. If you wish to also disable the Storage vMotion capability, then you will need to disable RelocateVM_Task vSphere API method as well.

Auditing vMotion and Storage vMotion Operations

With or without this solution, you may still want another level of confidence that a VM has either not migrated or migrated to authorized set of ESXi hosts. We can easily do so by auditing the VM's Event system and looking for migration events. The name of the vMotion event is called VmMigratedEvent and the name of the Storage vMotion event is called VmRelocatedEvent. Here is a sample script using the vSphere SDK for Perl that exercises this specific vSphere API and provides you with all the ESXi hosts a given VM might have migrated to. For those that rather consume the vSphere API using something like PowerCLI, here is a quick one-liner to extract vMotion events:

$vm = Get-VM TestVM-2
Get-VIEvent -Entity $vm | Where { $_.Gettype().Name -eq "VmMigratedEvent"} | Select CreatedTime, UserName, FullFormattedMessage | ft -wrap -AutoSize

disable-vmotion-for-vm-5

Share this...
  • Twitter
  • Facebook
  • Linkedin
  • Reddit
  • Pinterest

Filed Under: Automation, PowerCLI, vSphere, vSphere Web Client Tagged With: Cross vMotion, disableMethods, enableMethods, ExVC-vMotion, Managed Object Browser, MigrateVM_Task, PowerCLI, RelocateVM_Task, Storage vMotion, svmotion, vSphere, vSphere MOB, xVC-vMotion

Automatically Remediating SvMotion / VDS Issue Using vCenter Alarms

04/20/2012 by William Lam 8 Comments

UPDATE 07/13/2012 - vSphere 5.0 Update 1a has just been released which resolves this issue, please take a look here for more details for the patch as this script is no longer required.

In my previous article Identifying & Fixing Virtual Machines Affected By SvMotion / VDS Issue, I provided a script for users to easily identify the impacted VMs as well as a way to remediate them. Though the the issue was only temporarily fixed, as any of the remediated VMs can be re-impacted if they are Storage vMotion again (manually or automatically) by Storage DRS. This meant that users would to re-run these scripts every so often to ensure their environment is not affected by this problem.

I decided to look into a more automated and hands-off approach in which a Storage vMotion of a VM will automatically trigger the execution of the remedation script. I was able to successfully accomplish this by leveraging vCenter Alarms and running a script on the vCenter Server (Here's a cool thing I did with alarms awhile back) .

Disclaimer: This script is not officially supported by VMware, please test this in a development environment before using on production systems.

You can create the alarm at any level of the inventory hierarchy, but I would recommend placing it at least at the datacenter or cluster level. The alarm type will be for a VirtualMachine and it we use "monitor for specific events". For the trigger, we will need to use "VM migrated" and set the status to "Unset" which will not create an alarm icon when it is triggered.

You might wonder why we selected "VM migrated" versus "VM relocated" and this is actually due to the fact that a Storage vMotion starts out just like a vMotion and if you manually perform a vMotion or Storage vMotion, only this event type will be triggered. Due to this single event being triggered by two completely different operations, it has an interesting impact which we will discuss in a bit.

Next we need to create an action for this alarm which will be running a command, you will need to specify the full path to perl.exe (assuming you're using my script which is based on vSphere SDK for Perl and you will need to have vCLI installed on the vCenter Server) as well as the path to the alarm script which in this example is called alarm.pl. Also ensure you set the green->yellow action to execute once.

You will need to create the alarm.pl script on your vCenter Server and here is what it looks like:

Perl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/perl -w
# William Lam
# http://www.virtuallyghetto.com/
 
use strict;
use warnings;
 
my $scriptlocation = "C:\\querySvMotionVDSIssue.pl";
my $server = "localhost"
my $username = "VC-USERNAME";
my $password = "VC-PASSWORD";
my $debug = 0;
 
###########################
# DO NOT MODIFY PAST HERE #
###########################
 
my $start1 = "from";
my $start2 = "to";
my $end = ",";
 
# extract VMware env variables from alarm
my $eventstring = $ENV{'VMWARE_ALARM_EVENTDESCRIPTION'};
my $vmname = $ENV{'VMWARE_ALARM_EVENT_VM'};
 
my @sourcehost = $eventstring =~ /$start1 (.*?)$end/;
my @destinationhost = $eventstring =~ /$start2 (.*?)$end/;
 
 
# Output environmental variables to see what's up
if($debug) {
open(FILE,">C:\\output.txt");
foreach my $key (keys %ENV) {
  print FILE $key . "=" . $ENV{$key} . "\n";
}
close(FILE);
}
 
# if the source/destination host is the same, means we had a Storage vMotion instead of vMotion
# and we execute the remediation script on the VM
if($sourcehost[0] eq $destinationhost[0]) {
`"$scriptlocation --server $server --username $username --password $password --vmname $vmname --fix true"`;
}

You will need to fill in the script location, in this example I have all scripts stored in C:\ and you will also need to populated the credentials which will be used to execute the script.

Earlier we mentioned that both a Storage vMotion and vMotion trigger the same event and because of that, we need to be able to identify when a Storage vMotion actually happens to run the script. The alarm.pl script above will be executed when the alarm is triggered and using the VMware specific environmental variables that is populated from the vCenter Alarm, we can extract from the event description to figure out whether it was a vMotion or Storage vMotion. Once we confirm it is a Storage vMotion, we then execute our remediation script which is from my previous article.

Note: Ensure you download the latest version of of the querySvMotionVDSIssue.pl from the previous article, as it has been updated to handle single remediation and targeted for this use case.

Now to verify that our alarm is functioning as expected, we can perform a manual Storage vMotion of a VM and we should see our alarm.pl execute and then after the Storage vMotion has completed, we should see some VM reconfiguration tasks which is from our remediation script.

So there you have it, you no longer have to worry about running the script every so often to ensure your VMs are not being impacted by the SvMotion / VDS problem. Again, I would like to stress though we are able to automate this remediation, this is not a real solution and VMware is actively working on a fix for this problem.

If you have any questions, feel free to leave a comment.

Share this...
  • Twitter
  • Facebook
  • Linkedin
  • Reddit
  • Pinterest

Filed Under: Uncategorized Tagged With: alarm, distributed virtual switch, dvportgroup, dvs, storage drs, svmotion, vds

Identifying & Fixing Virtual Machines Affected By SvMotion / VDS Issue

04/16/2012 by William Lam 24 Comments

UPDATE 07/13/2012 - vSphere 5.0 Update 1a has just been released which resolves this issue, please take a look here for more details for the patch as this script is no longer required.

Duncan Epping recently wrote an article about Clarifying the SvMotion / VDS problem in which he describes the scenario that would impact your VMs as well as a way to remediate those impacted VMs. I would recommend you go through Duncan's article before moving any further.

The challenge now, is how to easily identify all VMs that are currently impacted by this problem in your environment? The answer is of course Automation and leveraging the vSphere API! I created the following vSphere SDK for Perl script called querySvMotionVDSIssue.pl which searches for all VMs that are connected to a VDS and checks whether or not it's expected dvPortgroup file exists in the appropriate datastore. To use the script, you just need a system with the vCLI installed or you can just use the vMA appliance.

UPDATE: The script has now been updated to support remediation for VMs connected to both a VMware VDS as well as Cisco N1KV. The solution, thanks to one of our internal engineers was to "move" the VM's dvport from one to another, all while staying within the existing dvPortgroup which will also force the creation of the .dvsdb port file. Once the dvport move has successfully completed, we will move it back to it's original dvport that it initially resided on. We no longer have to rely on creating a temporally dvPortgroup and best of all, we can now remediate both VDS and N1KV. The script now combines both the "query" and "remediation" into single script. Please take a look at the examples below on usage.

Disclaimer: This script is not officially supported by VMware, please test this in a development environment before using on production systems.

Here is a sample output of the script running in "query" mode:

Only impacted VMs will be listed in the output. To remediate, I have combined the remediation script into the query script, if you wish to remediate ALL VMs that were listed as being impacted, you can specify the --fix flag and providing the option "true". This will go ahead and remediate all impacted VMs that were listed as before.

Here is a sample output of the script running in "remediation" mode:

In the screenshot above, you may noticed a few interesting details with VM3 and VM4. If you run out of dvports in a dvPortgroup, the script will automatically increase the number of ports to satisfy the swap (max of 10 due to number of ethernet interfaces a VM can have). Once the VM has been remediated, the dvportgroup will be reconfigured to it's original configured number of ports as shown with VM3.

If you have an impacted VM that is connected to an ephemeral dvportgroup, we will not be able to remediate due to the nature of how an ephemeral binding works. You will get a message on the specific interface and you will need to manually remediate using the steps outlined by Duncan or using the "old" remediation script which will create a temporally dvPortgroup (again, this will only work for VMware VDS' only).

If you run into any issues or have questions, feel free to leave a comment.

Share this...
  • Twitter
  • Facebook
  • Linkedin
  • Reddit
  • Pinterest

Filed Under: Uncategorized Tagged With: distributed virtual switch, dvportgroup, dvs, storage drs, svmotion, vds

Performing A Storage vMotion in vCloud Director Using vCO

02/20/2012 by William Lam 10 Comments

I wrote an article yesterday called Performing A Storage vMotion in vCloud Director Using vCloud REST API and at the beginning I mentioned a few alternative solution to using the vCloud REST API such as the vCloud SDKs: Java, .NET, PHP or the new vCloud Director cmdlets. Afterwards, I realized I left out one very important solution, which is using vCenter Orchestrator (vCO) with the vCloud Director plugin.

I was able to quickly put together a vCO workflow using the Query Service in the vCD plugin and with the help of this VMTN thread from Christophe Decanni. I have exported my workflow so you can easily import it into your vCO Server without any hassle.

Disclaimer: Please thoroughly test this in a non-critical environment before using it on production systems.

Download: StoragevMotionForvCloud.workflow

To start using the workflow, you will need to ensure you have the vCloud Director 1.5 vCO plugin installed and import the workflow after you have downloaded it to your local system:

Here is a quick view of what the Storage vMotion for vCloud Director workflow looks like:

Here is an example execution of the workflow where we will Storage vMotion (using vCloud "relocate" API operation) to move "vESXi-01" VM from "iSCSI-3" to "iSCSI-4" datastore.

Step 1 - Select vCloud vDC and vCloud VM that you wish to performed the Storage vMotion. As part of the initial query, user will be provided with the current datastore the vCloud VM is residing on:

Step 2 - Once the query has completed to identify the current datastore the vCloud VM is residing on, a user interaction request will be needed to select the destination datastore to perform the migration to. You will notice the workflow icon will change to a yellow color and requiring an "answer" from the user. Right click on the workflow and click on "Answer" to continue the workflow:

Step 3 - You will see a "result" field which is the query of the vCloud VM that was selected and the datastore it's currently residing on. You will now have the option to select the vCloud datastore you wish to Storage vMotion the vCloud VM to:

Step 4 - Now you are ready to kick off the Storage vMotion operation. This may take sometime to finish depending on the size of the VM and the speed of your underlying storage. You should see a message under "Log" specifying the operation has been started. If everything is successful, you should see the vCloud VM now running on the new datastore.

Now you can enjoy performing Storage vMotions in vCoud Director with just a click of a button with the help of vCenter Orchestrator! Happy workflowing 🙂

Share this...
  • Twitter
  • Facebook
  • Linkedin
  • Reddit
  • Pinterest

Filed Under: Uncategorized Tagged With: orchestrator, svmotion, vcd, vCO

Performing A Storage vMotion in vCloud Director Using vCloud REST API

02/19/2012 by William Lam 2 Comments

Currently there is not a way to perform vSphere Storage vMotion using the vCloud Director UI, but it is possible using the vCloud REST API. Since this feature is exposed in the vCloud API, it is also available using one of the three vCloud SDKs (Java, .NET, PHP), vCO or the new vCloud Director cmdlets but it does require some lines of code.

Since the vCloud API is exposed as a REST API, you can also interact with directly using a command line tool such as cURL or a REST client like the RESTClient Firefox plugin. In the example below, I will show you how to perform a Storage vMotion using cURL with the vCloud Director API 1.5.

Here we have a vApp in vCloud Director and it has two VMs, and as you can see both are located on the same datastore called "iSCSI-3".

Let's say we wanted to migrate the VM called "vESXi-01" to some other available datastore.

Step 1 - Login

You will need a system that has curl installed and you will need an account that has the "System Administrator" role to perform this operation. There are a few parameters you need to specify to login to vCD and obtain an authorization token. You will need to specify the following parameters and the URL to your vCloud Director instance which should be in the form of https://vcd-fqdn/api/sessions:

  • -i = Include headers
  • -k = Performs an "insecure" SSL connection
  • -H = Setting the header for the version of vCloud Director (1.5 in this example)
  • -u = User credentials in the format of [[email protected]:password]
  • -X = Request type

Note: For more details on the cURL flags, please refer to the cURL documentation.

curl -i -k -H "Accept:application/*+xml;version=1.5" -u [email protected]:vmware -X POST https://vcd.primp-industries.com/api/sessions

1
HTTP/1.1 200 OK Date: Sun, 19 Feb 2012 01:09:53 GMT Content-Type: application/*+xml;version=1.5 Date: Sun, 19 Feb 2012 01:09:53 GMT Content-Length: 1275 <?xml version="1.0" encoding="UTF-8"?> <QueryResultRecords xmlns="http://www.vmware.com/vcloud/v1.5" total="2" pageSize="25" page="1" name="adminVM" type="application/vnd.vmware.vcloud.query.records+xml" href="https://vcd.primp-industries.com/api/query?type=adminVM&page=1&pageSize=25&format=records&fields=name,datastoreName" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.vmware.com/vcloud/v1.5 http://172.30.0.139/api/v1.5/schema/master.xsd"> <Link rel="alternate" type="application/vnd.vmware.vcloud.query.references+xml" href="https://vcd.primp-industries.com/api/query?type=adminVM&page=1&pageSize=25&format=references&fields=name,datastoreName"/> <Link rel="alternate" type="application/vnd.vmware.vcloud.query.idrecords+xml" href="https://vcd.primp-industries.com/api/query?type=adminVM&page=1&pageSize=25&format=idrecords&fields=name,datastoreName"/> <AdminVMRecord name="vESXi-01" datastoreName="iSCSI-3" href="https://vcd.primp-industries.com/api/vApp/vm-0332975f-394f-49d2-baf9-b222d126b942"/> <AdminVMRecord name="vCenter" datastoreName="iSCSI-3" href="https://vcd.primp-industries.com/api/vApp/vm-c0e4ead6-6e7d-4914-979b-0da49a8103a8"/> </QueryResultRecords>

If you have successfully logged in, you should get an HTTP 200 response and get similar output as above. You will need to make note of your authorization token which is located on the third line that starts with "x-vcloud-authorization". This will be needed throughout the remainder of the session

Step 2 - Locate VM

Using the new Query Service API in vCloud 1.5, we will locate all VMs within the vCD instance with the adminVM type since we are in the System organization. As you can see the command is very similar to the first one but instead of specifying the credentials, we using the authorization token from step 1. We are also including specific fields in the XML output for readability, if you remove '&fields=name,datastoreName', you will see the other fields in the XML response.

curl -i -k -H "Accept:application/*+xml;version=1.5" -H "x-vcloud-authorization: aMcbkioTMvgVDDDJtdlwjrh6iutLPZ7fjL09hPa89mU=" -X GET 'https://vcd.primp-industries.com/api/query?type=adminVM&fields=name,datastoreName'

1
2
3
4
5
6
7
8
9
10
11
12
HTTP/1.1 200 OK
Date: Sun, 19 Feb 2012 01:10:15 GMT
Content-Type: application/*+xml;version=1.5
Date: Sun, 19 Feb 2012 01:10:15 GMT
Content-Length: 1232
 
<?xml version="1.0" encoding="UTF-8"?>
<QueryResultRecords xmlns="http://www.vmware.com/vcloud/v1.5" total="2" pageSize="25" page="1" name="datastore" type="application/vnd.vmware.vcloud.query.records+xml" href="https://vcd.primp-industries.com/api/query?type=datastore&page=1&pageSize=25&format=records&fields=name" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.vmware.com/vcloud/v1.5 http://172.30.0.139/api/v1.5/schema/master.xsd">
    <Link rel="alternate" type="application/vnd.vmware.vcloud.query.references+xml" href="https://vcd.primp-industries.com/api/query?type=datastore&page=1&pageSize=25&format=references&fields=name"/>
    <Link rel="alternate" type="application/vnd.vmware.vcloud.query.idrecords+xml" href="https://vcd.primp-industries.com/api/query?type=datastore&page=1&pageSize=25&format=idrecords&fields=name"/>
    <DatastoreRecord name="iSCSI-4" href="https://vcd.primp-industries.com/api/admin/extension/datastore/a5d6bf25-7916-45cb-a65a-5dfa14d2ba38"/>
    <DatastoreRecord name="iSCSI-3" href="https://vcd.primp-industries.com/api/admin/extension/datastore/c9bff014-deaf-4ad1-8871-b2f2479f454f"/>

If the operation was succesful, you should see a HTTP 200 response and list of VMs in your vCD instance. You will need to make a note of the VM's href property that you wish to perform the Storage vMotion on.

Step 3 - Locate Datastore

Next we will use the Query Service API again to locate all available datastores within the vCD instance. We are also limiting it to the name field (href property is included by default). If you want to further filter the output to a particular vCenter Server, you can specify a filter for the particular vCenter href. For more details, you can take a look at the Query Service API documentation.

curl -i -k -H "Accept:application/*+xml;version=1.5" -H "x-vcloud-authorization: aMcbkioTMvgVDDDJtdlwjrh6iutLPZ7fjL09hPa89mU=" -X GET 'https://vcd.primp-industries.com/api/query?type=datastore&fields=name'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
HTTP/1.1 202 Accepted
Date: Sun, 19 Feb 2012 01:11:46 GMT
Location: https://vcd.primp-industries.com/api/task/2078bfe6-0a47-4615-a377-01ed8067c637
Content-Type: application/vnd.vmware.vcloud.task+xml;version=1.5
Date: Sun, 19 Feb 2012 01:11:46 GMT
Content-Length: 1283
 
<?xml version="1.0" encoding="UTF-8"?>
<Task xmlns="http://www.vmware.com/vcloud/v1.5" status="running" startTime="2012-02-18T17:11:46.883-08:00" operationName="vappRelocateVm" operation="Relocating Virtual Machine (0332975f-394f-49d2-baf9-b222d126b942)" expiryTime="2012-05-18T17:11:46.883-07:00" name="task" id="urn:vcloud:task:2078bfe6-0a47-4615-a377-01ed8067c637" type="application/vnd.vmware.vcloud.task+xml" href="https://vcd.primp-industries.com/api/task/2078bfe6-0a47-4615-a377-01ed8067c637" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.vmware.com/vcloud/v1.5 http://172.30.0.139/api/v1.5/schema/master.xsd">
    <Link rel="task:cancel" href="https://vcd.primp-industries.com/api/task/2078bfe6-0a47-4615-a377-01ed8067c637/action/cancel"/>
    <Owner type="application/vnd.vmware.vcloud.vm+xml" name="" href="https://vcd.primp-industries.com/api/vApp/vm-0332975f-394f-49d2-baf9-b222d126b942"/>
    <User type="application/vnd.vmware.admin.user+xml" name="administrator" href="https://vcd.primp-industries.com/api/admin/user/b99d6670-5884-4345-a70d-f417d2e7556b"/>
    <Organization type="application/vnd.vmware.vcloud.org+xml" name="System" href="https://vcd.primp-industries.com/api/org/a93c9db9-7471-3192-8d09-a8f7eeda85f9"/>
</Task>

Again, if the operation was successful, you should see an HTTP 200 response and a list of datastores in your vCD instance. From the output, we can see we have two available datastores "iSCSI-3" and "iSCSI-4". Let's go ahead and select "iSCSI-4" to migrate "vESXi-01" to. Also make a note of the datastore href property you wish to migrate the VM to.

Step 4 - Perform Storage vMotion

Lastly, we just now just need to invoke the "relocate" API operation for a VM. We need to first create the XML response for relocate operation as specified by the API. You will need to create a file that contains href of the datastore you wish to Storage vMotion the VM to:

1
2
3
<RelocateParams xmlns="http://www.vmware.com/vcloud/v1.5">
    <Datastore href="https://vcd.primp-industries.com/api/admin/extension/datastore/a5d6bf25-7916-45cb-a65a-5dfa14d2ba38"/>
</RelocateParams>

Now we are ready to craft our final command to perform the Storage vMotion operation. Taking the information we recorded earlier for the VM, we need to append to the URL "/action/relocate" which will perform the relocate operation on this particular VM. You will also notice in this curl request, it is a POST request and we including an extra header specifying the "Content-Type" for the operation as defined in the vCloud API for "relocate" operation. The last parameter is specifying some data which is our relocate-response file that is needed as part of the request body.

curl -i -k -H "Accept:application/*+xml;version=1.5" -H "x-vcloud-authorization: aMcbkioTMvgVDDDJtdlwjrh6iutLPZ7fjL09hPa89mU=" -H "Content-Type:application/vnd.vmware.vcloud.relocateVmParams+xml" -X POST https://vcd.primp-industries.com/api/vApp/vm-0332975f-394f-49d2-baf9-b222d126b942/action/relocate -d @relocate-response

If everything went well, you should see an HTTP response code of 202 which means the operation was accepted by the server. Within the response of the "relocate" operation, you will get back a task handle for the Storage vMotion operation. If you watch your vCenter Server after you performed the command, you should see a Storage vMotion operation kick off for the VM that you have selected.

Once the operation has been completed, we can open up our vCloud Director UI and we should see that our VM has now been Storage vMotion to the datastore we specified.

Though you can interact with vCD directly with the REST API, you still may want to use one of the higher level abstraction languages to perform this type of operation in bulk.

Share this...
  • Twitter
  • Facebook
  • Linkedin
  • Reddit
  • Pinterest

Filed Under: Uncategorized Tagged With: REST API, svmotion, vcd, vcloud director

Primary Sidebar

Author

William Lam is a Staff Solution Architect working in the VMware Cloud on AWS team within the Cloud Platform Business Unit (CPBU) at VMware. He focuses on Automation, Integration and Operation of the VMware Software Defined Datacenter (SDDC).

  • Email
  • GitHub
  • LinkedIn
  • RSS
  • Twitter
  • Vimeo

Sponsors

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy