Friday, May 15, 2009

Cloning without VirtualCenter

ESX and ESXi both have the capability to perform linked clones. In small environments it can be leveraged via scripting. William Law has written a script that automates cloning as well as linked clones. One of the problems is that the cloned VMs all have the same name and syspreping them with the same name of the VM is a problem.

To sysprep properly I made an edit to the script and created a second script to execute on the VM itself to sysprep the VM. below is a simple explanation.



I edited the Ghettoclone script so that it places the computername into the VM's vmx file during the cloning process. This is done by having the script enter a line setting machine.id equal to the VM's name. I added the following line to line 175 of the script

sed -i "s/^machine.id =.*$/machine.id = \"${CLONED_VM_NAME}\"/g" "${CLONED_VM_VMX}"

Though I haven't tested it yet you should be able to add the following to line 221 of the linked clones script to achieve the same result.

sed -i 's/machine.id = "'${GOLDEN_VM_NAME}'"/machine.id = "'${FINAL_VM_NAME}'"/' ${STORAGE_PATH}/$FINAL_VM_NAME/$FINAL_VM_NAME.vmx


A second script running from within the VM pulls the computername from the VMX file, inserts it into the Sysprep.inf file and then starts Sysprep. The VM is then renamed and added to the domain. below is the script

Dim getid
'#### Set sysprep pathsSource = "C:\sysprep"
'#### perform query for sysprep folder. Do not execute if folder does not existSet objFSO = CreateObject("Scripting.FileSystemObject")If objFSO.FolderExists(sSource) Then
set WshShell = createobject("wscript.shell")
'#### set executable statement variablesvmexec = "vmwareservice.exe -cmd machine.id.get"spexec = "c:\sysprep\sysprep.exe -mini -reseal -activated -reboot -quiet"
'### query for machine name from VM's VMX file and store it as a variable set getid = WshShell.exec("%ComSpec% /c """ & vmexec & """")compname = getid.StdOut.readall
'#### write variable value into the c:\sysprep\sysprep.infConst ForReading = 1Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")Set objFile = objFSO.OpenTextFile("C:\sysprep\sysprep.inf", ForReading)
strText = objFile.ReadAllobjFile.ClosestrNewText = Replace(strText, "ComputerName=", "ComputerName=" & compname & "")
Set objFile = objFSO.OpenTextFile("C:\sysprep\sysprep.inf", ForWriting)objFile.WriteLine strNewTextobjFile.Close
'#### launch Sysprep.exe using edited sysprep.infWshShell.exec("%ComSpec% /c """ & spexec & """")
End If


There are a few couple of caveats

1. I had to reset the local administrator's password to blank. Sysprep hangs during the process. I think there is another solution, but I haven't found it yet.

2. the second script needs to be run on the VM when it first boots. The only way that I can find to do this is to set the script as a computer startup script in AD. The script will only run if the Sysprep folder exists on the VM and the sysprep process removes the folder after it runs. So the script can be set at the domain level.

3. The template VM must be acessible by the ESX(i) host the script is being executed on. Thus it can be done via local storage, but with multiple ESX(i) hosts you need some sort of mechanism to distribute the template such as writing a bash script to copy the template. Maybe that's another post.

4. The VMs must be manually powered on after VM cloning for sysprep to run. The script can be editted to do this automatically, but I haven't determined the best place to set the command in the ghetto clones script yet.

5. and I can't recall if I said this already. but the a folder called sysprep needs to be placed on the c: drive of the template VM with the sysprep files and the sysprep vbs.

Tuesday, April 28, 2009

intro

Desktop Virtualization is building up steam and there are many options available as more and more companies are jumping into the fray. With the field growing each month, it is getting more and more cluttered. Everyone wants a piece of the Gartner projected 64 billion dollars to be spent on virtualizing desktops over the next five years. This blog will focus on some of the things I am doing in the space to solve problems for my customers. Couple few things I am working on now are regarding cost savings and reducing the cost per desktop on inital purchase, as that is a limiting factor in acceptance of the technology. The focus has been on ESX as the backend hypervisor but there is momentum toward changing that. ESXi (the free version) has been adopted at one of my customers (5400 desktop install and growing). Managing it has been a challenge, but there are now tools out there (commerical and free) that address some of the headaches. But ESX is not the end all, Microsoft will soon be comparable to ESX for most customers needs, especially on the desktop side. And Parallels Virtuozzo is making a push with Quest's vWorkspace (pretty slick technology there and Quest will broker all three hypervisors). Watch this space, it's about as dynamic as they come.