This post is related to AHV only!!! Make sure a recent backup of the VM exist!
In the last weeks customers asked me how to move a VM from one AHV container to another AHV container on the same cluster. The answer is: “There is no PRISM/GUI option for this and the manual task is pretty difficult”. So i wrote a script called move_vm which I show in this post to simplify this.
But why should you move a VM?
There are several reasons for this.
- container settings don’t fit
- different containers for different organization units
- DR/backup concepts based on containers
- Automation based on containers
- … and more
Example: Let’s say the customer started with two containers:
- ISO – just for templates and CD/DVD ISO images
- prod – productions environment
Now he realizes that some of the server VMs would be great for compression but some are not. He used the best practices to figure out which server VMs would fit.
For this case he created a new container called:
3 . prod_comp with active inline compression for SQL Server 2012 R2 VMs
Now he tried to move the VM from container prod to prod_comp and found that this does not work and asked me.
USE CASE: Move a VM from one AHV container to another AHV container to make use off inline compression for a several VMs!
I created a new container called prod_comp with inline compression in my Intel NUC environment.
Then I created a VM called “Move_VM_Test1” with different kind of disk devices and CD/DVD drives with all reside on the prod container.
Now I am using the tool move_vm which is installed on the NTNX-AVM to move the VM. The tool has several options which I list here.
[nutanix@NTNX-AVM move_vm]$ ./move_vm --help
Usage: move_vm [OPTIONS]
move_vm [ --help | --version ]
FOR NUTANIX AHV ONLY- clones a VM from one container to another
vNic MAC Addresses will change unless --delete is used
--host Specify CVM host or Cluster IP
--username Specify username for connect to host
--password Specify password for user
--vm-name Specify Virtual Machine name which will be moved
--new-vm-name New Virtual Machine name if specified (clone)
--vdisk-mapping Specify the container mapping for each vdisk - ORDER IS IMPORTANT
--list-mapping Shows the actual vdisk-mapping
--container Specify the container where vm will be moved to
--debug Enables debug mode
--keep-images If enabled clones to image service will not be deleted
--delete Deletes source VM - ARE YOU REALLY SURE?
--overwrite Overwrites target VM/Images (delete and creates new one)
--help List this help
--version Show the move_vm version
move_vm --host=NTNX-CVM --username=admin --password=nutanix/4u --vm-name=MyVM --container=ISO
move_vm --host=NTNX-CVM --username=admin --password=nutanix/4u --vm-name=MyVM --vdisk-mapping=scsi.0/ISO,scsi.1/Prod2
Move a whole VM from container prod to prod_comp
In the first case the syntax is pretty simple to move the whole VM from prod to prod_comp. REMEMBER the source VM needs to be powered off before you are able to move it.
move_vm --host=192.168.178.130 --username=admin --password=nutanix/4u --vm-name=Move_VM_Test1 --container=prod_comp
You may noticed that there are two VMs now with the same name. I believe it makes sense to keep the old VM unless you are sure the new copy works.
You can use the option “–delete” to delete the source VM. The advantage is that the new network adapters will have the same MAC address then the source VM!
Move just one vDisk/disk of the VM from container prod_comp back to prod
I renamed the new Move_VM_Test1 to Move_VM_Test2 for the next part.
Let’s say you would like to move just one vDisk from a container prod_comp back to prod because you found that inline compression makes no sense. An example maybe the “transaction log” vdisk of a MS SQL Server.
First we need to find out which vdisks exist and how the mapping looks like. This can be done with the “–list_mapping” option.
./move_vm --host=192.168.178.130 --vm-name=Move_VM_Test2 --list-mapping
the output is:
which means that there are four vdisks and two CD/DVD drives. Let’s say we identified that the the second vdisk: “scsi.1” is the one we would like to move back to container prod. In this case we need to specify the whole mapping when calling the move_vm tool to only move the second vdisk. Copy and paste is the way to go!
The syntax is:
./move_vm --host=192.168.178.130 --vm-name=Move_VM_Test2 --vdisk-mapping=ide.0/EMPTY,scsi.0/prod_comp,scsi.1/prod,pci.0/prod_comp,ide.1/prod_comp,ide.2/prod_comp
I only changed the prod_comp to prod for the scsi.1 vdisk!
You may ask: “Why is the whole VM cloned and not only the vdisk?” Yep you are right. This would be the better way. This is how this tool works atm. This is more a copy then a move but it works and its pretty fast because only the vdisk “scsi.1” needs to be copied.
You could specify the option “–delete” the delete the source VM and to make sure the new network adapters get the same MAC address.
There we go.
For all the people who wants to know more, this is an overview how this tool works:
- Upload vDisk from source VM to image service. This is needed while a direct copy is not possible
- Create new VM
- Clone Images to Disks
- Add network
- Delete images