Friday 16 December 2011

Converting a Debian VM from Xenserver to vSphere

You can transfer the data for the machine from one environment to another using clonezilla. Once it is transferred I found that the machine does not boot. On further investigation I realised that there are many differences between a Debian install on Xenserver and on vSphere.

If you get the message "Operating system not found" you will need to install grub to the virtual hard disk. To do this boot a liveCD (such as clonezilla).

As root run grub, at the grub> prompt type:

grub> find /boot/grub/stage1

This will return the location of grubs stage one, make a note of this location for the next command. Type

grub> root (hd0,0) < this is the location returned by the last command

Now install grub to the master boot record (MBR) with:

grub> setup (hd0)

then quit grub with:

grub> quit

Reboot to get the grub menu but if you let it try and continue using the default settings it will very likely hang because it will try to use a block device name that begins with xvd, these are xenserver specific and will not exist on the VMware VM. To get it to continue booting you need to hit 'e' on the grub selection screen, then edit the boot line to replace the device that looks like /dev/xvda1 and made it look like /dev/sda1, also remove the "console=hvc0" otherwise you will not be able to see interact with your OS via the console (hvc0 is another xenserver specific device name).

Once we have booted the system we need to go about preparing the system to boot correctly when left to it's own devices (geddit?), firstly ensure the keyboard keymap is set up correctly with:

# dpkg-reconfigure console-data

Edit /etc/fstab to change all references to xvd devices to sda in /etc/fstab

On many of the machines I transferred using this method I found that they had no swap partitions after the transfer. So I simply set the disk size slightly bigger than the source disk when initially creating the VM in vSphere, then added a partition with fdisk. I usually set the swap partition as /dev/sda2.
You then need to make that partition a swap partition with (I have found often that a reboot is required before the command works):

# mkswap /dev/sda2

Then you can mount it with:

# swapon /dev/sda2

And see if the swap space is available with:

# free

Don't forget to update /etc/fstab so it is pointing to the correct device for the swap partition so it is automounted at boot.

The CDRom is set to /dev/hdc unless you have changed the VM's hardware settings, update this in /etc/fstab as well.

We need to change grubs configuration so that it automatically boots a with appropriate parameters, this information is stored in /boot/grub/menu.list. You can achieve this by either running the following commands:

sed -i 's/xvd/sd/g' /boot/grub/menu.lst
sed -i 's/console=hvc0//g' /boot/grub/menu.lst

Or edit  /boot/grub/menu.lst, find the line that begins # kopt, remove the console=hvc0 from the end and change the "xvd" device to the correct boot device. move down to the botton where the menu choices are configured and change the device and remove the console=hvc0 from each of the one you will likely use (the top one only in my case).

Edit /etc/inittab and comment out the line that begins "co:"

Remove the xenserver specific packages from your apt sources list with:

rm /etc/apt/sources.list.d/citrix.list

Update your packages list:

apt-get update

Remove all the kernels that are installed with:

apt-get remove linux-image*

You will get a warning asking about the removal of the running linux kernel, say "No" to this as we will install another one that we can get headers for in the next step.

Install a more appropriate kernel with:

apt-get install linux-image-2.6-686

Reboot to run the newly installed kernel.

Install VMware tools, details of how in my post here, then reboot to test.

To get the most out of the VM you should switch hardware to paravirutal drivers for SCSI controllers and NICs.


Thursday 15 December 2011

Convert a Windows machine to a VMware VM with Clonezilla

I have had need to do manual conversions of physical and virtual machines for several reasons previously. Some of these reasons have included:
  • There is not enough free space on the machine's disk(s), (also know as "I haven't got time to wait for the machine's administrator to tidy up")
  • VMware Convert no longer supports the conversion of that operating system.
  • We can't afford PlateSpin
  • I wonder if I could do a conversion the hard way.
This takes me back to my first ESX Project. The company I worked for had about 8 test machines sitting along one office wall. The situation was already out of hand and I was being asked for more test machines. I decided we needed a to think big and decided on and got approved ESX on a couple of big (at least for us) servers to virtualise the test environment. I managed to simply move all the test machines by installing a fresh OS and installing and configuring all the software my self, with the help of the relevant departments. I then came to our Windows NT 4.0 software build machine.... No-one, not even the developer that put it together knew what was on there. We were building releases every couple of weeks at the time. It goes without saying this is not a good place to be, and baring a complete hardware failure on the physical machine there was no way anyone was going to be rebuilding the machine. I scratched my head and had previously used dd and tar to move installs from one machine to another on Linux, so I had a crack at it, and to my surprise a few hours later I had run a P2V by hand with a Linux live CD. I had even managed convince Windows NT to let me switch the drives from IDE to SCSI.

Having used Clonezilla to store a few images from machines that were shipped without restore media in the past, I wondered how easy it would be to back up an image of a machine and restore it into a new virtual environment. As it turns out with Clonezilla I don't even need to create an image, the tools are there to transfer the data from one machine's disk and write it directly to the disk of another.

The conversion here is from a Xenserver VM to a VMware VM but the technique used here will probably work else where. 
  • Put a Clonezilla live CD in both your (virtual) machines and boot them.
  • The default boot option for Clonezilla worked fine for me on both machines, your mileage may vary.
  • Select your language options as necessary
On the source machine:
  • Select "Start_Clonezilla"
  • Select "device-device"
  • Select "Beginner"
  • Select "disk_to_remote_disk"
  • Select "dhcp" or assign an IP address with "static"
  • Now it should show you a list of disks in the system, select the one you wish to transfer.
  • Select "Skip checking/repairing source file system" 
  • Clonezilla will now show you the command it is about to run, press "Enter."
  • You will be asked to confirm at a few important stages that you wish to proceed.
  • When you see "Waiting for the target machine to connect..." you have finished with the source machine for now, except take a note of the commands it is telling you run on the destination machine (See below image.)
On the destination machine:
  • Select "Enter_shell"
  • Select (2)
  • Type: sudo su - (to change to the root user)
  • Type: ocs-live-netcfg (and setup the networking)
  • Type: ocs-onthefly -s 192.168.1.1 -t sda  (replacing 192.168.1.1 and sda with the relevant IP address and device name)
  • You will again be asked to confirm at poignant stages.
You should see a progress bar which includes an estimate for the finish time.


When this finishes you should now be able to boot the destination VM and install the VMWare tools to make everything pretty and efficient. Don't forget to install the paravirtual SCSI and network card drivers if your OS is supported.


If you have success (or failure) with this migration method please leave a comment below, it certainly helped me out of a hole.

Monday 5 December 2011

Virtual Center Template for Debian

When using a windows template, VMware's Virtual Center can help you customise the resulting virtual machine with a wizard, unfortunately it doesn't do this for Debian which happens to be my Linux distribution of choice.

To create the template I just installed a nice minimal installation onto a new VM, added the vmware tools, changed the VM to use paravirtual hardware then converted it to a template.

After each "Deploy from template" I simply run through the following:

It would be potential security risk to have all the VMs using the same SSH keys so I regenerate SSH keys for the VM with:

# rm /etc/ssh/ssh_host*


# dpkg-reconfigure openssh-server


Update packages

# apt-get update


# apt-get upgrade


Set the IP address (well it is likely to be a server) with:

# vi /etc/network/interfaces

Change host name as it isn't helpful to have duplicate host names due to the confusion it can cause me:

# vi /etc/hostname

Amend hosts file (to match the host name)

# vi /etc/hosts


Do a quick confidence reboot then go about installing the software onto the VM as usual to make it fit for the task you need it for.

Thursday 1 December 2011

Paravirtualizing vSphere guests

Although just setting up a virtual machine on a ESX server works well there are ways to improve the VM's performance and reduce some of the CPU overhead required when the VM interacts with the virtual hardware. The two ways I have used are installing paravirtualized drivers for network interface and for the storage controller.

Windows
If you are installing Windows from media or an ISO image:
  • Edit the VM settings, replace any networks adapters with ones that are of the type "VMXNET 3."
  • Change the SCSI controller type to "Paravirtual" 
  • On the floppy drive choose "use existing floppy image in datastore:", click browse, then find the relevant image in vmimages\floppies for your version of windows. Dont forget to "select connect at startup"
  • Hit F6 during the initial installation to add the paravirtual SCSI drivers from the floppy image. 
  • The OS wont recognise the network card until the VMware tools are installed.
If the VM is already set up and running Windows with VMware tools the drivers for the paravirtualised SCSI adapter are also installed with VMware tools but you will need to add a second SCSI adapter of the type "Paravirtual" and boot before Windows will be happy to with the first (boot) SCSI adapter being set to "Paravirtial."
  • Edit the VM settings, replace any networks adapters with ones that are of the type "VMXNET 3"
  • Using the Vshpere client Edit the settings of your Windows VM, Click the "Add..." button, choose "Hard Disk", Choose "Create a new Virtual disk" and set a small size like 8 MB.
  • If your VM already has a SCSI controller you will need to add an additional one. To get another controller you will need to choose a virtual device node which is from the second SCSI controller. I usually just select SCSI(1:0).
  • Check device manager to ensure the the PVSCSI controller has been detected.
  • Shutdown the VM, remove the hard drive you have just added and change the remaining SCSI adapter to type "Paravirtual"
  • Start the machine, if all is well (you will see a blue screen if it isn't) you will have successfully changed windows to use a more efficient way to talk to it's boot drive.
Converting IDE drives to SCSI
I struggled with Windows XP box that I had imported using VMware Converter that only had an IDE drive. I tried deleting the hard drive (I was careful not to delete the underlying files), then I went to add the hard drive and set it to SCSI it would only allow IDE. I overcame this by shutting down the virtual machine, editing the relevant .vmdk file and changing the ddb.adapterType parameter to lsilogic like so:
ddb.adapterType = "lsilogic"
You should now be able to add the hard drive back in specifying the existing .vmdk file and set the SCSI adapter to "Paravirtual"

Debian 6
On a system that is already running and the VMware tools are installed. I ran all updates and it was simply a case of removing the existing network adapter and adding a new one of the type "VMXNET 3" and changing the SCSI adapter to "Paravirtual" and everything worked.

Update: Also check out this post on changing the IO scheduler on linux VMs squeezing more from your Linux VMs