Microsoft Virtualization Discussions

Hyper-V Script for Recovering VMs from Snapmirror Replication Relationship

jcampaz516
5,581 Views

Hi guys.  Happy New Year.  Has anyone tried running the powershell script that Netapp has graciously shared in the Netapp Best Practices for MS Virtualization TR3702 doc?  They've developed a pretty cool scipt that pretty much automates the whole process of mounting a VM after you have replicated over to your DR SAN.  I have modified the pwershell script so that it fits out environment but I am having some issues where it will not map the LUN.  Below is the script.  Also, has anyone run into the NIC IP address dissapearing after replicating over from production to DR SAN & mounting VM on a different Hyper-V Parent server?  The issue we are seeing is that the VM's IP address dissapears as the GUID for the virtual adapter is different in the DR Hyper-V Parent server.  We're trying to script this and making it seamless but we're having difficulties.  Is anyone else out there working on a Hyper-V environment, using Snapmirror replication to mirror VMs across to DR SAN?  Please let me know your experiences.  Thanks.

Script Below:


# Purpose – To failover storage to Secondary NetApp storage system and Recover all VMs automatically
# Tools - PowerShell, plink (SSH Communication)
# Posted: January 2010

$DRFiler = Read-Host "SAN"
$VolName = Read-Host "VOL"
$LUNName = Read-Host "VM"
$VMVHD = Read-Host "VM.VHD"

$iqn = "iqn.1991-05.com.microsoft:server.domain.com"

$LUNPath = $DRFiler + ":" + "/vol/" + $VolName + "/" + $LUNName

function Create_VM ( $VM_Service, $VMName )
{
$wmiClassString = "\\" + "LocalHost" + "\root\virtualization:Msvm_VirtualSystemGlobalSettingData"
$wmiClass = [WMIClass]$wmiClassString
$newVSGlobalSettingData = $wmiClass.CreateInstance()
while ($newVSGlobalSettingData.psbase.Properties -eq $null) {}
# Set the VM name
$newVSGlobalSettingData.psbase.Properties.Item("ElementName").value = $VMName
$status = $VM_Service.DefineVirtualSystem($newVSGlobalSettingData.psbase.GetText(1))
if ($status.ReturnValue -eq 0)
{
$NewVM_PATH = $status.DefinedSystem
   return $NewVM_PATH
}
}
function Attach_VHD ( $VM_Service, $NewVM_PATH, $VHD )
{
$ListOfControllers = get-wmiobject -namespace root\virtualization Msvm_ResourceAllocationSettingData |
where-object {$_.ResourceSubType -like "*Emulated*IDE*"}
foreach ($Controller in $ListOfControllers)
{
if ($Controller.Address -eq 0)
      {
        $IDEController0 = $Controller
}
  }
$DiskDefault = get-wmiobject -namespace root\virtualization Msvm_ResourceAllocationSettingData |
where-object {($_.ResourceSubType -like "Microsoft Synthetic Disk Drive") –and ($_.InstanceID -like '*Default*')}
$DiskDrive = $DiskDefault.psbase.Clone()
$DiskDrive.Parent = $IDEController0.__PATH
$DiskDrive.Address = 0
$Status = $VM_Service.AddVirtualSystemResources($NewVM_PATH, $DiskDrive.psbase.GetText(1))
if ($Status.ReturnValue –eq 0)
{
$NewDiskDrive_PATH = $Status.NewResources
}

$VHDDefault = get-wmiobject -namespace root\virtualization Msvm_ResourceAllocationSettingData |
where-object {($_.ResourceSubType -like "Microsoft Virtual Hard Disk") –and ($_.InstanceID -like '*Default*' )}
$NewDiskDrive = [WMI]"$NewDiskDrive_PATH"
$VHDisk = $VHDDefault.psbase.Clone()
$VHDisk.Parent = $NewDiskDrive.__PATH
$VHFile = $VHD
$VHDisk.Connection = $VHFile
$VM_Service.AddVirtualSystemResources($NewVM_PATH, $VHDisk.psbase.GetText(1))
}

Write-Host " Select:
1 - Perform Recovery on the same Hyper-V Host
2 - Perform Recovery to a different Hyper-V Host
* - Exit the script"

$Choice = Read-Host "Enter the selection"
$fqdn = hostname

if ($Choice -eq 1)
{
   Write-Host "Performing recovery on the same Hyper-V Host can imply that
          it is not required to recreate the VM Configuration, provided
          the recovered LUN is attached with the same Drive Letter
          as it was before the Disaster"
    sdcli sysconfig list | select-string -pattern "Available drive letters" -CaseSensitive
   
    Write-Host "Ensure that the Drive Letter is in the available list in case the prior configuration is retained"
    Write-Host ""
    $Decide = Read-Host "Would you like to specify the same Drive as per configuration prior to disaster (y / n)"
    Write-Host ""

if ($Decide -eq 'y')
{
    $DriveLetter = Read-Host "Select an available Drive Letter from the above list""
    sdcli disk connect -p $LUNPath -d $DriveLetter -I $fqdn $iqn -dtype dedicated
    sleep -Seconds 4
    Write-Host "Now start the VM and check its status"
}
elseif ($Decide -eq 'n')
{
    $DriveLetter = Read-Host "Select an available Drive Letter from the above list"
    sdcli disk connect -p $LUNPath -d $DriveLetter -I $fqdn $iqn -dtype dedicated
    $VMName = $DRFiler + "-" + $VMVHD.trimend('.vhd')
    $VHD = $DriveLetter + ":\" + $VMVHD
    $VM_Service = Get-WmiObject –namespace root\virtualization
  Msvm_VirtualSystemManagementService
    $NewVM_PATH = Create_VM $VM_Service $VMName
    Attach_VHD $VM_Service $NewVM_PATH $VHD
  }
  else
  {
  Write-Host "Invalid Input. Rerun the script and Enter Y or N"
  exit
 
  }
}
elseif ($Choice -eq 2)
{
   $DriveLetter = Read-Host "Select an available Drive Letter from the above list"
   sdcli disk connect -p $LUNPath -d $DriveLetter -I $fqdn $iqn -dtype dedicated
   $VMName
   $VMVHD
   $VM_Service = Get-WmiObject –namespace root\virtualization Msvm_VirtualSystemManagementService
   $NewVM_PATH = Create_VM $VM_Service $VMName
   Attach_VHD $VM_Service $NewVM_PATH $VHD
}
else
{
   exit
}

3 REPLIES 3

rgraves2572
5,581 Views

This script was not available when we implemented our Hyper-V DR solution on Windows 2008 R1. Instead we us symbolic links allowing two clusters to access the same VM configuration.

It is a manual process and requires you to take production down so you can update the VM configuration. Currently we are going through great pain trying to maintain DR and I'm looking into alteratives.

Using Import/Export and maintainig two Configurations

Citrix Essentials for Hyper-V

I have tried the script you are showing yet so would be interested if it works with R1.

-Robert

jcampaz516
5,582 Views

Hi Rgraves.  We are in the process of actually writing a script that expedites the process of failing over.  The script actually pre configures the VMs so that it creates the export files (e.g. config & GUID.exp files), deletes the copy of the VHD & places the newly created export files in a folder on the VM's LUN.  Once you failover to DR Hyper-V Parent server, another script takes over that imports the VM (this is the only we could find to keep the IP configuration settings without hacking the XML config file & modifying the ChannelInstanceGuid string).  I'll post more details once we are done testing.

- Jeff

jim_ctr_merrell
4,639 Views

Did this script ever get finished?   We have been a HV shop since server 2008, Netapp the entire time.   This script would greatly streamline our DR process.

 

Public