Preface

The information from this blog is aiming to help you to successfully convert a “traditional” operating system, may be pre-installed on your notebook into a virtual machine running on XenClient.

Background an motivation

When ever I do workshops on XenClient this is one of the questions I get always: How I can run/install XenClient without loosing my current windows installation?

Our plan is to add a functionality into XenConvert, to make a P2V smooth and integrated into Synchronizer for XenClient. The idea is to convert a physical installation of Windows into a VHD (Virtual Harddisk) and upload this file to  the Synchronizer for XenClient, so this OS can be downloaded again that PC after the installation of XenClient.

Today XenConvert (and Synchronizer) can’t provide this type of functions, however you still can save your current OS, it’s just a manual process which needs some additional steps.

Requirements

Step by step guide

In this example I’ll start with a freshly restored system from the vendors media. In my case I’m using a HP Elitbook 6930p which was delivered with an OEM copy of Windows Vista Business.
 






The first thing we need to do, is to get a P2V tool, which converts our physical hard disk into a VHD file which we’ll store on our external USB harddrive. There are many disk imaging solutions on the market, but Citrix provides an excellent tool for that purpose for a long time as a part on the XenServer product family – XenConvert.

Download the latest version of XenConvert from the Citrix Website







Install XenConvert on your PC and attach the USB hard disk.

Once the installation is done, launch XenConvert and follow these steps:

  • Select the type of conversion -  From this Machine to XenServer Virtual Harddisk
  • Chose the partition (e.g. we don’t need/want the recovery partition)
  • Select where to store the VHD file – This should be on your USB hard drive 


 

 


If you’re really serious about keeping a 100% intact copy of your factory installation, assuming you haven’t got a physical restore media, I still would recommend to image the the whole hard disk with a imaging product you feel confident with. This would assure you always can revert back to to that stage.

Even more sophisticate would be to just buy a new hard drive and keep your physical installation in a safe place.

Since you got your backup by now and have a VHD file on your external USB hard drive, it’s a good time to install XenClient - Don’t forget to disconnect your USB drive for now.

The installation process is straight forward, insert the XenClient installer CD and boot from that media. You may have to press a function key (e.g. F9 on my HP) to start the BIOS boot menu, if the setup of your computer isn’t set to boot from optical media first.

If you want to learn more about installing XenClient and the backend system consider to consult the XenClient Proof of Concept Implementation Guide that’s a good document to start with.

Once your finished the XenClient installation, boot XenClient – You should see the Citrix Receiver for XenClient GUI.






We’ll create an VM without installing the OS, this will consist of a configuration and an “empty” VHD file.

  • Click on Create form Install Disk 
  • Select Microsoft Windows Vista SP2 32Bit (or the respective OS you’ve created the VHD from)
  • Enter a name, description and select an icon
  • Specify Memory and vCPU’s (keep Memory below 3GB)
  • Define the Virtual Disk Size and use bridged as network type
    (XenClient is using “Thin Provisioning”, so the actual size wont be used initially)
  • Select Create VM & Install OS later   

  •  







Now we want to copy out VHD file (created with XenConvert earlier) from the USB drive to the file system on the XenClient. XenClient uses a Linux type of system which hosts the hypervisor, we’ll leverage these tools form the so called Dom0 to conduct this steps. You’ll get access to a shell either pressing CTRL+SHIFT+t key on the XenClient screen or you may consider to use a Secure Shell to remotely get access to the Dom0 shell e.g. PUTTY on Windows. If you’re being asked for a password, that’s the one you specified during the XenClient installation (user is root).


 
 
 
 
 
 
 
In the next step we need to mount the USB hard drive in the file system of the XenClient, so we can copy the VHD file from the USB drive to the XenClient file system.
(XenClient stores the VHD files in the directory /storage/disks)

  • Plugin the USB drive to your XenClient
  • In the terminal enter: dmesg [enter]
  • You should see a device id such as sdc1
  • mount your hard drive with the following command: mount /dev/sdc1 /mnt
  • Verify the VHD (name you used on XenConvert destination) by: ls -la /mnt/name_you_gave
  • copy the VHD file to the storage directory of the XenClient

 



Before we can use this VHD file on XenClient we need to modify the VHD file, due to the encryption functionality we added with version 1.0 of XenClient. This step was NOT necessary with previose release candidates. There’s a article about this on  http://support.citrix.com/article/CTX127075

NOTE: the steps below are not necessary anymore with XenClient V1.0 SP1

You can either download a script (see CTX127075), or copy the code below into your clipboard and follow the steps described below (Using a SSH Terminal e.g. PUTTY):

Code:

#! /bin/bash

error() {
  echo &gt;&amp;2 $0: <span class="code-quote">"$@"</span>
  exit 1
}

usage() {
  cat &gt;&amp;2 &lt;&lt;EOF
Usage: $0 [OPTION]...

      --src=DISK
      --dest=DISK
EOF
  exit 1
}

T=$(getopt -l src:,dest: <span class="code-quote">"" "</span>$@")
[ $? == 0 ] || usage
eval set -- <span class="code-quote">"$T"</span>

<span class="code-keyword">while</span> <span class="code-keyword">true</span>
<span class="code-keyword">do</span>
  <span class="code-keyword">case</span> $1 in
  --src)
    srcvhd=$(readlink -e <span class="code-quote">"$2"</span>)
    [ $? -eq 0 ] || error src $2 doesn\'t exist
    shift 2
    ;;
  --dest)
    destvhd=$(readlink -f <span class="code-quote">"$2"</span>)
    [ $? -eq 0 ] || error path to dest $2 doesn\'t exist
    ! [ -e $destvhd ] || error dest $destvhd already exists
    shift 2
    ;;
  --)
    shift
    <span class="code-keyword">break</span>
    ;;
  *)
    usage
    ;;
  esac
done

atexit() {
  [ -z <span class="code-quote">"$desttap"</span> ] || echo 1 &gt;/sys/class/blktap2/blktap$(echo $desttap | sed 's,.*tapdev,,')/remove
}
trap atexit EXIT

size=$(vhd-util query -n $srcvhd -v)
[ $? -eq 0 ] || error couldn\'t query size of src $srcvhd
vhd-util create -n $destvhd -s $size
[ $? -eq 0 ] || error couldn\'t create dest $destvhd
desttap=$(tap-ctl create -a <span class="code-quote">"vhd:$destvhd"</span>)
[ $? -eq 0 ] || error couldn\'t open dest $destvhd

vhd-util read -n $srcvhd -e 0 -c $(($size*1024*2)) | <span class="code-keyword">while</span> read s c
<span class="code-keyword">do</span>
  #echo $s $c
  <span class="code-keyword">while</span> [ $c -gt 0 ]
  <span class="code-keyword">do</span>
    _c=$c
    [ $_c -gt 65536 ] &amp;&amp; _c=65536
    printf <span class="code-quote">"\\r%d.%02d%% complete"</span> $((100*$s/($size*1024*2))) $(((10000*$s/($size*1024*2)) % 100))
    #echo $s $_c
    LD_PRELOAD=/usr/lib/libvhdio.so.1.0 dd-buffered -s $srcvhd -d $desttap -o $s -c $_c -b 2048
    s=$(($s + $_c))
    c=$(($c - $_c))
  done
done
printf <span class="code-quote">"\\r100.00%% complete\\n"</span>
  • Copy the code from the frame above
  • Enter the command: vi vhd-copy [enter]
  • Enter the character: i
  • Click the right mouse button (you should see the code scrolling)
  • Hit the ESC key and enter “:wq” (write & quit)
  • Enter: cat -v vhd-copy (to check for formatting errors)

Now we’ll need to run that script to make the necessary changes to the VHD file. To execute the script you have to enter the following syntax in the terminal:

sh vhd-copy --src /storage/disks/your_converted_vhd --dest /storage/disks/new_file_name

This step will take a long time, depending on your VHD file size – There’s a progress indicator which gives you some ideas how long that takes.

 


In the next step we need to find out the what file name the config of your newly created VM is referring to as hard drive. If this is your first and only VM on the XenClient, just list the file with the following command:

ls /storage/disks

In my example the following files were listed.

 


I got the two files, I’m aware of “6930p.vhd” and “6930pvhd-new.vhd” the third file “40f7e0e2-eb14-4d1a-ac9f-b00cf24552c6.vhd” is the virtual hard drive file used by XenClient for the new VM.

If you got multiple VM’s already on your XenClient, you can use some command line tools to see the mapping between VM’s and their associated VHD files. Enter the following commands in your terminal:

xec-vm (The output will list your vm names and UUID’s)

Copy the UUID referring to your new VM into the clipboard (your mark the number in PUTTY)

xec-vm -u the_uuid_from_the_clipboard get_disk_path 1

The output from the terminal will be the path and file name of that virtual hard drive.


 



Now we have to exchange the “empty” VHD file with the new converted on with the following commands in the terminal:

cd /storae/disks
mv 40f7e0e2-eb14-4d1a-ac9f-b00cf24552c6.vhd 40f7e0e2-eb14-4d1a-ac9f-b00cf24552c6.vhd.bak
mv 6930p-new.vhd 40f7e0e2-eb14-4d1a-ac9f-b00cf24552c6.vhd

(Obviously you’ll need to use the file name discovered with the xec-vm command)

 



Now you can start your VM and it should boot OK, however there’s a final step you need to do – Install the XenClient Tools. After your new VM has booted, open the explorer on your Windows VM and execute the “setup.exe” which is on CD Drive (xc-tools)




After the installation of the XenClient Tools you’ll have a full featured virtual machine based on your existing physical installation of Windows. If you check on the Citrix Receiver for XenClient GUI, the explanation mark which indicates that the XenClient Tools are missing or outdated is no more present.


 


Have fun with your XenClient!



Walter Hofstetter

walter.hofstetter[at]citrix.com