Sunday, 12 October 2014

Vserver/SVM Dismantling with PowerShell

Back in June I wrote the post: Vserver/Storage Virtual Machine (SVM) Dismantling.
To automate this process for lab/testing purposes only, the following script was written. It was tested on some old SVMs in a lab environment that needed getting rid of.

The 12 Steps

Its likely more steps will be required for certain SVM configurations - this was an adequate set of steps for the lab environment:

>>>>> STEP 1/12: Online All Volumes that have SnapMirrorDestinations
>>>>> STEP 2/12: Release SnapMirror Destinations
>>>>> STEP 3/12: Delete SnapMirrors
>>>>> STEP 4/12: Remove Vserver Peers
>>>>> STEP 5/12: Remove Vserver Transition Peers
>>>>> STEP 6/12: Unmount all volumes (but rootvol)
>>>>> STEP 7/12: Offline volumes (rootvol last)
>>>>> STEP 8/12: Destroy volumes (rootvol last)
>>>>> STEP 9/12: CIFS Server Delete
>>>>> STEP 10/12: Igroups Delete
>>>>> STEP 11/12: Remove SnapMirror Policies owned by vserver_admin
>>>>> STEP 12/12: SVM Delete

The Script

Save as say Dismantle-SVM.ps1 and run with PowerShell (e.g. PS C:\> .\Dismantle-SVM.ps1)

## TITLE: Dismantle-SVM.ps1 for CDOT 8.2.X
## CAVEAT UTILITOR: This program comes with no warranty and no support!
## DESCRIPTION: Dismantles and Deletes an SVM

"";"<<<<< Dismantle-SVM.ps1 for CDOT 8.2.X >>>>>";""
Import-Module DataONTAP
$cluster    = Read-Host "Enter Cluster Name"
$username   = Read-Host "Enter Admin User  "
$password   = Read-Host "Enter Password    " -AsSecureString
$credential = New-Object System.Management.Automation.PsCredential($username,$password)
[Void](Connect-NcController $cluster -Credential $credential -ErrorAction SilentlyContinue)
If(!$global:currentnccontroller){"";"Failed to connect - exiting program!";"";EXIT}

"";">>>>> Data SVMs in $cluster";""
$attributes        = Get-NcVserver -Template
$query             = Get-NcVserver -Template
$query.VserverType = "data"
$svms              = Get-NcVserver -Attributes $attributes -Query $query
If(!$svms){"$cluster has no Data SVMs, exiting!";"";EXIT}
$svms | foreach { $_.Vserver };""

$answered = $false
while (!$answered){
  $svm = Read-Host "Enter SVM Name"
  $svms | foreach {If($svm -eq $_){$answered = $true}}
}
$global:currentnccontroller.Vserver = $svm;""

$answered   = $false
while (!$answered){
  $yesNo = (Read-Host "Are you sure you want to dismantle and delete the SVM $vserver (Y/N)").ToUpper()
  If($yesNo -eq "Y"){$answered = $true}elseif($yesNo -eq "N"){EXIT}
}

"";">>>>> STEP 1/12: Online All Volumes that have SnapMirrorDestinations"
$SMDestinations = Get-NcSnapMirrorDestination
If($SMDestinations){
  ""
  $SMDestinations.SourceVolume | foreach {
    If ((Get-NcVol $_).state -ne "online"){
      Set-NcVol $_ -Online
    }
  }
}

"";">>>>> STEP 2/12: Release SnapMirror Destinations"
$SMDestinations | Invoke-NcSnapMirrorRelease -Confirm:$false

"";">>>>> STEP 3/12: Delete SnapMirrors"
<# Remove-NcSnapMirror does not work in Vserver Context for Pre 8.2 relationships (LS Mirrors) #>
$global:currentnccontroller.Vserver = $null
$SnapMirrors = Get-NcSnapMirror -DestinationVserver $SVM
$SnapMirrors | foreach {Remove-NcSnapmirror -SourceCluster $_.SourceCluster -SourceVserver $_.SourceVserver -SourceVolume $_.SourceVolume -DestinationCluster $_.DestinationCluster -DestinationVserver $_.DestinationVserver -DestinationVolume $_.DestinationVolume -Confirm:$false}
$global:currentnccontroller.Vserver = $svm

"";">>>>> STEP 4/12: Remove Vserver Peers"
[Void](get-ncvserverpeer | remove-ncvserverpeer -Confirm:$false -ErrorAction SilentlyContinue)
$VserverPeers = Get-NcVserverPeer

If($VserverPeers){

  "";"Unable to remove Vserver Peers.";"Connecting to Vserver Peer(s) Cluster(s) in turn to delete SnapMirrors"

  foreach ($VserverPeer in $VserverPeers) {

    $PeerCluster = $VserverPeer.PeerCluster
    $PeerSVM     = $VserverPeer.PeerVserver
    $answered    = $false

    while(!$answered){
      "";"Enter Cluster credentials for the cluster $PeerCluster, so we can release/delete SnapMirrors on the Data SVM $PeerSVM";""
      $userTemp    = Read-Host "Enter Admin User for $PeerCluster"
      $passTemp    = Read-Host "Enter Password" -AsSecureString
      $credTemp    = New-Object System.Management.Automation.PsCredential($userTemp,$passTemp)
      [Void](Connect-NcController $PeerCluster -Credential $credTemp)
      If(!$global:currentnccontroller){"";"Failed to connect to $PeerCluster!";""}
      If($global:currentnccontroller){$answered = $true}
    }

    "";">>>>> $PeerSVM STEP 1/3: Online All Volumes that have SnapMirrorDestinations"
       $SMDestinations = Get-NcSnapMirrorDestination -SourceVserver $PeerSVM -DestinationVserver $SVM
       If($SMDestinations){
      ""
      $SMDestinations.SourceVolume | foreach {
        If ((Get-NcVol $_ -VserverContext $PeerSVM).state -ne "online"){
          Set-NcVol $_ -VserverContext $PeerSVM -Online
        }
      }
    }
      
    "";">>>>> $PeerSVM STEP 2/3: Release SnapMirror Destinations"
    $SMDestinations | Invoke-NcSnapMirrorRelease -Confirm:$false

    "";">>>>> $PeerSVM STEP 3/3: Delete SnapMirrors"  
    $SnapMirrors = Get-NcSnapMirror -SourceVserver $SVM -DestinationVserver $PeerSVM
    $SnapMirrors | foreach {Remove-NcSnapmirror -SourceCluster $_.SourceCluster -SourceVserver $_.SourceVserver -SourceVolume $_.SourceVolume -DestinationCluster $_.DestinationCluster -DestinationVserver $_.DestinationVserver -DestinationVolume $_.DestinationVolume -Confirm:$false}

  }

  [Void](Connect-NcController $cluster -Credential $credential)
  $global:currentnccontroller.Vserver = $svm
  [Void](get-ncvserverpeer | remove-ncvserverpeer -Confirm:$false -ErrorAction SilentlyContinue)

}

"";">>>>> STEP 5/12: Remove Vserver Transition Peers"
Get-NcVserverPeerTransition -Vserver $svm | Remove-NcVserverPeerTransition -Confirm:$false

"";">>>>> STEP 6/12: Unmount all volumes (but rootvol)"
$attributes = Get-NcVol -Template
$volumes    = Get-NcVol -Attributes $attributes
$rootvol    = (Get-NcVserver).rootvolume
$volumes | foreach { If ($_.Name -ne $rootvol) { Dismount-NcVol $_ } }

"";">>>>> STEP 7/12: Offline volumes (rootvol last)"
$query       = Get-NcVol -Template
$query.state = "!offline"
$onlineVols  = Get-NcVol -Attributes $attributes -Query $query
$onlineVols | foreach { If ($_.Name -ne $rootvol) { Set-NcVol $_ -Offline } }
If($rootvol -and ((Get-NcVol $rootvol).State -ne "offline") ){Set-NcVol $rootvol -Offline}

"";">>>>> STEP 8/12: Destroy volumes (rootvol last)"
$volumes | foreach { If ($_.Name -ne $rootvol) { Remove-NcVol $_ -Confirm:$false } }
If($rootvol){Remove-NcVol $rootvol -Confirm:$false}

"";">>>>> STEP 9/12: CIFS Server Delete"
Get-NcCifsServer | Remove-NcCifsServer -Confirm:$false

"";">>>>> STEP 10/12: Igroups Delete"
Get-NcIgroup | Remove-NcIgroup -Confirm:$false

"";">>>>> STEP 11/12: Remove SnapMirror Policies owned by vserver_admin"
$query = Get-NcSnapmirrorPolicy -Template
$query.owner = "vserver_admin"
Get-NcSnapmirrorPolicy -Query $query | Remove-NcSnapMirrorPolicy

"";">>>>> STEP 12/12: SVM Delete"
Remove-NcVserver $svm -Confirm:$false
$global:currentnccontroller.Vserver = $null

If(!(Get-NcVserver $svm)){"Successfully removed the data SVM $svm from cluster $cluster!"}

## THE END

3 comments:

  1. 2 things missing, on top of my mind
    - clones => destroy clones first, then volumes
    - portsets

    ReplyDelete
    Replies
    1. Thank you very much for the comment Mirko. Excellent points, I'll be sure to add them in for the next version. Cheers, vCosonok!

      Delete
    2. Updated last week with this post - http://www.cosonok.com/2014/12/svm-destroyer.html - and four more steps ...
      Remove all LUN Maps
      Remove Portsets
      Offline Clones
      Delete Clones

      Delete