Showing posts with label Hyper-V. Show all posts
Showing posts with label Hyper-V. Show all posts

Saturday, December 17, 2011

How-To: Rapid Deploy VM Using Powershell

I have created a Powershell script to rapidly deploy VMs on Microsoft Hyper-V host and this was demo during the Hyper-V Workshop.


The demo Clustered Hyper-V infrastructure are as below diagram:

Clustered Hyper-V infrastructure with MS Hyper-V 2008 R2 SP1

On of my interesting demo is to rapid deploy VM on Microsoft Hyper-V host without using SCVMM. In achieving this I have created a Powershell script by referring to existing script found on http://social.technet.microsoft.com/wiki/contents/articles/176.aspx.

The script which I created is not perfect to suit everyone yet, but its enough for most of us out there who in need to create n numbers of VM given a short period. During this event, I have conducted a demo in creating 6 VMs with just a hit on 'enter' and all 6 VMs booted. And right now, I will share with you in details how to achieve this.

What the Powershell script does (In general speaking)
1. Read VMs configuration from CSV line-by-line
2. Create VM into 'C:\clusterstorage\volume1\vname'
3. Set the processor count of each VMs
4. Set the memory for each VMs
3. Create and attach a 'Differencing Disk' to each VMs

Future development of this script
1. To be able to set 'Dynamic Memory' for each VMs
2. To be able to set 'Fixed/Dynamic/Differencing Disk' for each VMs
3. To be able to set desire sysprep/preconfigured or mix OS platform base VMs
4. To be able to add each VMs to CSV cluster if the Hyper-V host is clustered
5. To add and improve comments into the script

CREATEVM.ps1

Import-Module "C:\Program Files\modules\HyperV"
$vmdefaultpath = "C:\ClusterStorage\volume1"
$ParentVHD = "win2k8r2sp1"
$path = "c:\createvm\VM.csv"
 import-csv -path $path|ForEach-Object {
$erroractionpreference = 0    
$vmName = $_.Name
$vmmemory = $_.Memory
$vmcpucount = $_.Cpucount
$vmSwitch = $_.Network
$vmpath = $vmdefaultpath
New-VM -Name $vmname
Set-VMMemory -VM $vmName -Memory $vmmemory
Set-VMCPUCount -VM $vmname -CPUCount $vmcpucount
Add-VMNIC -VM $vmName -VirtualSwitch $vmSwitch
New-VHD -VHDPaths $vmpath\$vmname\$vmname.vhd -ParentVHDPath $vmpath\vmbase\$ParentVHD.vhd
Add-VMDisk -VM $vmname -ControllerID 0 -Path $vmpath\$vmname\$vmname.vhd
Write-Host -BackgroundColor Green -ForegroundColor Black "Virtual Machine $vmname has been successfully created"     
}

VM.csv

CSV file which predefined VMs settings












How to use this script.
1. Create a CSV and list all necessary configuration of each VM as shown sample above.
2. Save the CSV as 'vm.csv'.
3. Copy 'vm.csv' to the Hyper-V host in 'c:\createvm\'
4. Copy the script above and save it as 'createvm.ps1' in 'c:\createvm\' (you may have to edit the variable to suit your requirement and configuration of your Hyper-V host.
5. Execute the PowerShell management Library for Hyper-V by codeplex.
6. Type 'powershell -file createvm.ps1'.

Requirement and caution
1. A syspreped OS image is required
2. Windows Server 2008 R2 and Windows 2008 R2 SP1 with Hyper-V role installed
3. Basic knowledge of Powershell script
4. Changing of variables in the script to suit your Hyper-V environment
5. Use at you own risk as you have been warned this may affects you Hyper-V server. KNOWING WHAT YOU ARE ABOUT TO EXECUTE IS IMPORTANT. 


A video worth thousand words





Wednesday, December 7, 2011

How-To: Compact Hyper-V Dynamic Expanding VHD

When you do housekeeping on your target VM which using 'Dynamic Disk', the free space that you free up in your VM will not added back to your Hyper-V host. If space is the concern for you Hyper-V host, then you may have to manually compact the VM's VHD.

To compact a Dynamic Expanding VHD:

Step 1: Right-click the target VM. Select 'Settings...'. Highlight the target Hadr Drive. Click 'Edit'
 Step 2: Select 'Compact'.
Compact - Applies to dynamically expanding virtual hard disks and differencing virtual hard disks. Reduces the size of the .vhd file by removing blank space that is left behind when data is deleted from the virtual hard disk. If the virtual hard disk is not NTFS formatted, the blank space must be overwritten with zeroes so that the compact action can reduce the file size by removing sectors that contain only zeroes.
If the virtual hard disk is not NTFS formatted, you must prepare the virtual hard disk for compacting by using a non-Microsoft disk utility program to replace the blank space with zeroes.
Step 3: Click 'Finish'

Wait for the compacting process to finish.

Below show the size of before and after VHD compacting process.



Thursday, May 19, 2011

How-To: Installing Hyper-V Intergration Tool for Centos 5.6

1.   Download Linux Integration Services 2.1 for Windows Server 2008 Hyper-V R2 at http://www.microsoft.com/downloads/en/details.aspx?FamilyID=eee39325-898b-4522-9b4c-f4b5b9b64551
2.       Extract the download file.
3.       Mount the "LinuxIC v21.iso" to the target Centos machine
4.       Start the Centos VM
5.       At the console copy the components into the Centos VM by executing the below commands as root:
#mkdir /mnt/cdrom
#mount /dev/cdrom /mnt/cdrom
#mkdir /opt/linux_ic_v21_rtm
#cp -R /mnt/cdrom/* /opt/linux_ic_v21_rtm
6.       Navigate to the directory where the components copied
           #cd /opt/linux_ic_v21_rtm
7.       Install the components using "make" command
           #make
           #make install
Below screenshot shows when the command executed successfully

8.      Shutdown the VM
9.      Change the network emulate to synthetic network adapter if you have not use synthetic network adapter
10.  Check all integration services


11.  Start the VM. This time the network interface name will change to SETH0 instead of ETH0

If you have issue similar to error "unable to find /usr/kernels/2.6.18-238.9.1.e15_x86_64", you have to check the symlink for the build located at "/lib/modules/kernel/build". make sure it is not broken.

Saturday, April 30, 2011

Removing "Update Failed" VMs from the VMM Administrator Console

Recently getting annoyed by the increasing number of VMs with status "Update
Failed". The last thing I remember that this happened after the one of my
Hyper-V host crashed, and ended up 2 identical VMs appear after the quick
migration took place.

Found this blog
http://blogs.technet.com/m2/archive/2010/04/16/removing-missing-vms-from-the-vmm-administrator-console.aspx
and below are my summary of the steps :

1. Close VMM Administrator Console
2. Stop "Virtual Machine Manager" service.
3. Backup the VMM database (optional)
4. Download and install "Microsoft SQL Server Management Studio Express"
from
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=c243a5ae-4bd1-4e3d-94b8-5a0f62bf7796.
5. Launch "Microsoft SQL Server Management Studio Express", select VMM
database and execute script below.
6. Start the "Virtual Machine Manager" service.
7. The VMs with "Update Failed" should have gone by now.

===========Start Script========
BEGIN TRANSACTION T1

DECLARE custom_cursor CURSOR FOR
SELECT ObjectId from
dbo.tbl_WLC_VObject WHERE [ObjectState] = 220

DECLARE @ObjectId uniqueidentifier
OPEN custom_cursor
FETCH NEXT FROM custom_cursor INTO @ObjectId

WHILE(@@fetch_status = 0)
BEGIN

DECLARE vdrive_cursor CURSOR FOR
SELECT VDriveId, VHDId, ISOId from
dbo.tbl_WLC_VDrive WHERE ParentId = @ObjectId

DECLARE @VDriveId uniqueidentifier
DECLARE @VHDId uniqueidentifier
DECLARE @ISOId uniqueidentifier


OPEN vdrive_cursor
FETCH NEXT FROM vdrive_cursor INTO @VDriveId, @VHDId, @ISOId
WHILE(@@fetch_status = 0)
BEGIN
DELETE FROM dbo.tbl_WLC_VDrive
WHERE VDriveId = @VDriveId
if(@VHDId is NOT NULL)
BEGIN

DELETE FROM dbo.tbl_WLC_VHD
WHERE VHDId = @VHDId
DELETE FROM dbo.tbl_WLC_PhysicalObject
WHERE PhysicalObjectId = @VHDId
END
if(@ISOId is NOT NULL)
BEGIN

DELETE FROM dbo.tbl_WLC_ISO
WHERE ISOId = @ISOId
DELETE FROM dbo.tbl_WLC_PhysicalObject
WHERE PhysicalObjectId = @ISOId
END

FETCH NEXT FROM vdrive_cursor INTO @VDriveId, @VHDId, @ISOId
END
CLOSE vdrive_cursor
DEALLOCATE vdrive_cursor

-----------------
DECLARE floppy_cursor CURSOR FOR
SELECT VFDId, vFloppyId from
dbo.tbl_WLC_VFloppy WHERE HWProfileId = @ObjectId

DECLARE @vFloppyId uniqueidentifier
DECLARE @vfdId uniqueidentifier

OPEN floppy_cursor
FETCH NEXT FROM floppy_cursor INTO @vfdId, @vFloppyId
WHILE(@@fetch_status = 0)
BEGIN
DELETE FROM dbo.tbl_WLC_VFloppy
WHERE VFloppyId = @vFloppyId

if(@vfdid is NOT NULL)
BEGIN
DELETE FROM dbo.tbl_WLC_VFD
WHERE VFDId = @vfdId
DELETE FROM dbo.tbl_WLC_PhysicalObject
WHERE PhysicalObjectId = @vfdId

END
FETCH NEXT FROM floppy_cursor INTO @vfdId, @vFloppyId
END
CLOSE floppy_cursor
DEALLOCATE floppy_cursor

----------------
DECLARE checkpoint_cursor CURSOR FOR
SELECT VMCheckpointId from
dbo.tbl_WLC_VMCheckpoint WHERE VMId = @ObjectId

DECLARE @vmCheckpointId uniqueidentifier
OPEN checkpoint_cursor
FETCH NEXT FROM checkpoint_cursor INTO @vmCheckpointId
WHILE(@@fetch_status = 0)
BEGIN
DELETE FROM dbo.tbl_WLC_VMCheckpointRelation
WHERE VMCheckpointId = @vmCheckpointId


FETCH NEXT FROM checkpoint_cursor INTO @vmCheckpointId
END
CLOSE checkpoint_cursor
DEALLOCATE checkpoint_cursor

-------------------------
---------Clean checkpoint

DELETE FROM dbo.tbl_WLC_VMCheckpoint
WHERE VMId = @ObjectID


exec [dbo].[prc_VMMigration_Delete_VMInfoAndLUNMappings] @ObjectId

DECLARE @RefreshId uniqueidentifier
exec [dbo].[prc_RR_Refresher_Delete] @ObjectId, @RefreshId


DELETE FROM dbo.tbl_WLC_VAdapter
WHERE HWProfileId = @ObjectId


DELETE FROM dbo.tbl_WLC_VNetworkAdapter
WHERE HWProfileId = @ObjectId


DELETE FROM dbo.tbl_WLC_VCOMPort
WHERE HWProfileId = @ObjectId

DELETE FROM dbo.tbl_WLC_HWProfile
WHERE HWProfileId = @ObjectId

DELETE FROM dbo.tbl_WLC_VMInstance
WHERE VMInstanceId = @ObjectId

DELETE FROM dbo.tbl_WLC_VObject
WHERE ObjectId = @ObjectId

FETCH NEXT FROM custom_cursor INTO @ObjectId
END
CLOSE custom_cursor
DEALLOCATE custom_cursor

COMMIT TRANSACTION T1
===========End Script===========

The above method does not work for me all time. In case you have the same
issue after the steps above are performed, you may try to remove host
cluster from the VMM.


This is my last resource and it always works for me ☺