Linux-KVM Management: Offline Migration

When you compare Linux-KVM to Hyper-V or VMWare your initial results will indicate that Linux-KVM is lacking when it comes to management tools, and basic functionality.  You would be correct, however you would also be incorrect.  You see with Linux-KVM we can leverage the underlying power of the Linux userland, and with this frankly all things are possible.  Here is one of the basic bits of management functionality which can be attained with a little bit of bash scripting knowledge.  I started by writing a VM backup script, then a VM export script, and of course a VM import script.  Eventually I ended up with a full-blown end-to-end VM migration script.

My environment is based on Ubuntu 11.04 amd64 with the latest patches as of August 1, 2011.  This script should work elsewhere unless file locations are different (VG Name) or if certain utilities are not included by default.

Features:

  • Compression available via gzip, will reduce the amount of network traffic transferred, could be helpful for migrations over the WAN.
  • Uses temporary SSH keys to minimize password prompts, and save you the trouble of configuring SSH keys yourself.
  • User specified Volume Group on the remote machine, so that if you are using SAN attached storage you can place it on the appropriate storage.
  • Safety checks to ensure that your data remains intact.
  • Integrates a progress monitor if pv is installed.

Requirements:

  • VMs must use LVM Logical Volumes directly, raw images or any other disks that are encapsulated in a file are not supported (at this time).
  • On the receiving host the LV for the new VM cannot already exist.  This script will create the LV, and will not overwrite an LV that it did not create (this is of course to prevent it from overwriting an existing volume which could be in use elsewhere).
  • Obviously the better your network speed the better your mileage.  I am not using any sort of “magic” here if you have a 1TB LV then it will be transferred over the network in full (unless of course you use the compression option and your data lends itself well to compression).
  • As this is an Offline migration that means that the VM will be down during the entire duration of the migration.
  • Each disk is transferred separately, and will not display a progress, unless you have pv installed (Ubuntu 11.04 amd64 doesn’t have it by default but it is in the repos).

Known Issues:

  • When using compression the data transfer rate will vary widely, this is because since we are transferring as a data stream, and performing data compression and decompression within the stream.  So essentially the compression slows down how fast we are able to feed data through the pipe, sometimes this will result in the data being fed at a really low rate and sometimes this will result in a really fast rate.  There is no real way around this, that is the nature of compression.
  • When using compression the progress bar is inaccurate, in that we are measuring progress as the data enters into the compression.  So when the last byte of data is metered it still needs to be compressed, transferred, uncompressed, and written to disk.  So basically what you will see is that the progress bar will show 100% and stop counting up time, and not move on to the next step.  In my tests it has never taken longer than 20% of the time.  I think I will be able to fix this by moving the measurement to the distant end, after the compression and decompression have already taken place, however that will take a bit of work, because if I do that currently I lose the progress through the ssh.
  • Cannot copy ISO images, this is a design choice really.  There is too much complexity in this since it is quite possible that you are hosting your ISO images on NFS, or it could be shared locally.  Either way we strip the ISO image out of the configuration leaving you with a Virtual CD/DVD drive with no disk mounted.  Upon completion of migration you can remount your ISO images on the distant end.
  • Cannot handle disk image files.  Disk images are really quite trivial to handle and don’t fit well into the complexity of this script.  I will most likely add this functionality or separate it into a different script.

Data Flow Example

data mover diagram with compression offFigure 1 – This diagram illustrates the data movers without compression.

Figure 2 – This diagram illustrates the data movers with compression.

When I first started this script I assumed that compression was a must, however as I started doing some test scripts I noticed that compression really did not impact the speed of the migration, now of course using compression will drastically reduce the amount of data which is transferred over the network.  However when factoring in the processing time on either end for compression and decompression it just really didn’t matter.  That said that was on 1Gb networking, so as long as your moves are local then it probably won’t make sense to use compression.  It might make sense to use compression if your networking is less than 1Gb or if you are running it over a WAN connection.  Additionally if you have very large volumes with a large amount of compressible space then you could gain a benefit from the compression option.

Please keep in mind this script has been tested thoroughly in my environments, and other logical environments that I can replicate.  However I did not test this in your environment, that responsibility will lie with you.  So please do not use this in a production environment until you (1) have tested it and (2) understand what it is doing.

Now to get to the code…

Name       :  vmmigrate.sh
Version   :  0.9.1
MD5        :  73112729debb55add8f59df603ac8286
SHA256  :  35e70841b90d67194f6bc9c57aa658038dc1de7a5b0aedc68a1c25b53f2fed88
URL         :  http://blog.allanglesit.com/downloads/vmmigrate-current

 


Related posts:

  1. Linux-KVM Management: Live Migration Without Shared Storage
  2. Hyper-V Guests: Linux Integration Components (v2) on Oracle Enterprise Linux
  3. Linux-KVM: Ubuntu 12.04 with Openvswitch
  4. Linux-KVM: VLAN Tagging for Guest Connectivity
  5. Linux-KVM: Migrating Hyper-V VHD Images to KVM
  1. May 16th, 2012 at 11:26
    Reply | Quote | #1

    Hi there,

    I just tried your script and by default I got a few errors, if you dont use compressión you get the usage help, and if you dont use temporary keys you get this:

    # ./vmmigrate.sh -n ldap1 -r 192.168.11.22 -v poola2 -c 5
    ./vmmigrate.sh: line 77: [: : integer expression expected
    Usage: grep [OPTION]… PATTERN [FILE]…
    Try `grep –help’ for more information.
    expr: syntax error
    expr: syntax error
    expr: syntax error
    ./vmmigrate.sh: line 114: [: : integer expression expected
    Would you like to begin the migration of ldap1 to 192.168.11.22? (y/n) ^C

    So you need to use -t 1 to get rid this error.

    Another one is that you expect that the lvm name ends with _boot, if you have something different another error, I change mine from this:

    localvgname=`virsh dumpxml ${vmname} | grep “_boot” | cut -d “‘” -f 2 | cut -d “/” -f 3`

    To this:

    localvgname=`virsh dumpxml ${vmname} | grep “disk0″ | cut -d “‘” -f 2 | cut -d “/” -f 3`

    I guest we can use a general variable like lvm_bootprefix=”disk0″ or something like that.

    Thank you very much this script does the work :) .