Migrating an OpenVZ Virtual Machine

One of the great features of OpenVZ is the ability to easily migrate a virtual machine(VM) to another server. While identifying the best methods to perform this task recently, I read about two tools to accomplish this task: vzdump, and vzmigrate.

My first attempts were using vzdump. It was actually pretty darn easy. I used a VM that was running a small personal website of mine. The active components running are mail, mysql and httpd. In a few simple steps, I successfully moved the VM from 1 server to another. Here is how:

  1. On the originating server, do vzdump –compress –suspend VEID. This snapshot the VM, compress the image and drop it into /vz/dump as two files: vzdump-VEID.log and vzdump-VEID.tgz. The compressed file is the actual image of the VM, while the log file describes what was done. Note that most of the actual copying of the files occurs while the original VM is live and running. It is then briefly suspended and checkpointed  added into the image. This is important as the all of the private system information must be included in order to properly restore a system. This includes address space, register set, opened files/pipes/sockets, etc.
  2. rsync the compressed image file to the new server and place it into /vz/dump by doing rsync -av /vz/dump/vzdump-VEID.tgz NEWSERVER:/vz/dump/ . This assumes you have permissions on the remote server to place files in /vz/dump.  If not, just copy to /tmp or other area and copy the file to /vz/dump on the remote server.
  3. To restore, once the vzdump-VEID.tgz file is in /vz/dump, just do vzdump –restore /vz/dump/vzdump-VEID.tgz VEID.
  4. Do a vzlist -a and you’ll see your machine listed. Typically it should be running, but if not- just do a vzctl start VEID and it should start right up.

A couple of caveats:

  • If the machine you are migrating FROM has different network settings, you’ll need to adjust these settings on the new server. On the VM, you’ll need to ensure that it’s IP address, /etc/hosts, /etc/resolv.conf are appropriate for your new environment. Fix these in the /etc/vz/conf/VEID.conf file and restart the VM.
  • You’ll need to ensure that no changes are made to the source VM while you are in the process of copying and restoring on the new server. You may need to diable public access, or even shut down some services in the VM in order to avoid changes while you migrate.

Since vzdump doesn’t come with the OpenVZ utilities, you’ll need to download it here. Additionally, you’ll need to download and install cstream, which vzdump depends on.

After using vzdump, I explored vzmigrate. I think you’ll soon see why vzmigrate is such a terrific tool. To use it, just do the following:

  • On the originating server, do: vzmigrate -r no –online REMOTESERVER VEID. So- if the remote server (the server you are migrating to) is, you would replace ‘REMOTESERVER’ with Using vzmigrate in this way will, in one step, perform all of the vzdump setps outlined above. In this case, I also don’t want to remove the current (source) VM, hence the “-r no” flag. The –online flage tells vzmigrate to do a snapshot as above, but other than the brief ‘blip’ in service while checkpointing, keep the source VM running.

As with the caveats with vzdump, you’ll have to fix up the network settings for the new environment. Additionally,. make sure that the VM does not already exist on the remote machine (perhaps from a previous attempt to migrate). Other than that, it really is THAT easy.

Leave a Reply

Your email address will not be published. Required fields are marked *