Saturday, 9 July 2016

Converting 7-Mode Quotas to Clustered Data ONTAP Quota Policy Rules

This is something I meant to share a while back. It is actually a very small part of a much larger 7 to C migration script I worked on. I’ve re-engineered the quota conversion element so it’s actually better than what is in the larger script. Admittedly, I’ve not had time to really test it out (the larger script did get a lot of use), so, if anyone happens to try it, please let me know how it goes. Cheers!

How to Run

Save the script below into a text editor as say Quotas7toC.ps1, then to run in PowerShell, simply specify the 7-Mode Quotas file to convert, the SVM/Vserver name, and the Quota Policy (which is usually just default), like:


.\Quotas7toC.ps1 -QuotasFile nas123_quotas -SVM SVM1 -QuotaPolicy default


The Script

Formatted for blogger - as per usual - with tabs replaced by two spaces


###################
## QUOTAS 7 TO C ##
###################

## Assumptions:
## 1) Volume names stay the same
## 2) Volume junction paths stay the same
## 3) All volumes in quotas are taken into account
## Read: http://www.wafl.co.uk/quotas/trackback/index.html

Param(
  [Parameter(Mandatory=$true)][String]$QuotasFile,
  [Parameter(Mandatory=$true)][String]$SVM,
  [Parameter(Mandatory=$true)][String]$QuotaPolicy
)
[String]$Title = "Quotas 7 to C v1"

## BASIC DISPLAY FUNCTIONS / GENERIC FUNCTIONS ##
FUNCTION Wr{
  Param([String]$Echo = "",[String]$Ink = "WHITE") #### ,[Switch]$N)
  If($Echo){ Write-Host $Echo -ForegroundColor $Ink }
  else { Write-Host }
};Wr
FUNCTION Is-Numeric ($Value) {return $Value -match "^[\d\.]+$"}

## LOADING QUOTAS FILE ##
Wr ">>> LOADING QUOTAS FILE <<<" Magenta;Wr
If( !(Test-Path $QuotasFile) ){ Wr "Test path $QuotasFile failed - exiting!" RED;Wr;EXIT }
$Quotas = @()
Get-Content $QuotasFile | Foreach {
  $_ = $_.Trim(" ","`t")
  If( ($_ -ne "") -and !($_.StartsWith("#")) ){ $Quotas += $_ }
}
If($Quotas.count -eq 0){ Wr "Loaded $QuotasFile, but no valid content - exiting!" RED;Wr;EXIT }
Wr "Loaded $QuotasFile" GREEN;Wr

## PROCESSING QUOTAS FILE ##
Wr ">>> PROCESSING QUOTAS FILE <<<" Magenta
$QuotasObj = @()
$Quotas | Foreach{
  $QUOTATARGET = $QUOTATYPE = $VolQtree = $VOLNAME = $QTREE = ""
  $LIMITDISK = $LIMITFILES = $THRESHOLD = $SOFTDISK = $SOFTFILES = ""
  $ValidLine = $TRUE
  If($_.StartsWith("*")){
    ## LINE STARTS WITH * ##
    $_ = $_.Substring(1).Trim(" ","`t")
    $QUOTATARGET = "ALL"
    If($_.Split("@").Count -eq 2){
      $QUOTATYPE = $_.Split("@")[0]
      $_ = $_.Split("@")[1]
      If($_.StartsWith('"')){ # Qtrees can have spaces in!
        $_ = $_.Substring(6) # Removes the "/vol/
        $VolQtree = $_.Split('"')[0]
        $VOLNAME  = $VolQtree.Split("/")[0]
        $QTREE    = $VolQtree.Split("/")[1].Trim('"')
        $_ = $_.Substring($VolQtree.length + 1).Trim(" ","`t")                                        
      }else{
        $_ = $_.Substring(5) # Removes the /vol/
        $VolQtree = $_.Split(" ")[0].Split("`t")[0]
        $VOLNAME  = $VolQtree.Split("/")[0]
        $QTREE    = $VolQtree.Split("/")[1]
        $_ = $_.Substring($VolQtree.length).Trim(" ","`t")
      }
      $LIMITDISK = $_.Split(" ")[0].Split("`t")[0]
      $_ = $_.Substring($LIMITDISK.length).Trim(" ","`t")
      $LIMITFILES = $_.Split(" ")[0].Split("`t")[0]
      $_ = $_.Substring($LIMITFILES.length).Trim(" ","`t")
      $THRESHOLD = $_.Split(" ")[0].Split("`t")[0]
      $_ = $_.Substring($THRESHOLD.length).Trim(" ","`t")
      $SOFTDISK = $_.Split(" ")[0].Split("`t")[0]
      $_ = $_.Substring($SOFTDISK.length).Trim(" ","`t")
      $SOFTFILES = $_.Split(" ")[0].Split("`t")[0]
    }else{
      Wr "Unable to process: $_" RED; $ValidLine = $FALSE
    }        
  }elseif($_.StartsWith("/vol/") -or $_.StartsWith('"/vol/')){
    ## LINE STARTS WITH / ##
    $QUOTATARGET = "PATH"
    If($_.StartsWith('"')){ # Qtrees can have spaces in!
      $_ = $_.Substring(6) # Removes the "/vol/
      $VolQtree = $_.Split('"')[0]
      $VOLNAME  = $VolQtree.Split("/")[0]
      $QTREE    = $VolQtree.Split("/")[1].Trim('"')
      $_ = $_.Substring($VolQtree.length + 1).Trim(" ","`t")                                   
    }else{
      $_ = $_.Substring(5) # Removes the /vol/
      $VolQtree = $_.Split(" ")[0].Split("`t")[0]
      $VOLNAME  = $VolQtree.Split("/")[0]
      $QTREE    = $VolQtree.Split("/")[1]
      $_ = $_.Substring($VolQtree.length).Trim(" ","`t")
    }        
    $QUOTATYPE = $_.Split(" ")[0].Split("`t")[0]
    $_ = $_.Substring($QUOTATYPE.length).Trim(" ","`t")
    $LIMITDISK = $_.Split(" ")[0].Split("`t")[0]
    $_ = $_.Substring($LIMITDISK.length).Trim(" ","`t")
    $LIMITFILES = $_.Split(" ")[0].Split("`t")[0]
    $_ = $_.Substring($LIMITFILES.length).Trim(" ","`t")
    $THRESHOLD = $_.Split(" ")[0].Split("`t")[0]
    $_ = $_.Substring($THRESHOLD.length).Trim(" ","`t")
    $SOFTDISK = $_.Split(" ")[0].Split("`t")[0]
    $_ = $_.Substring($SOFTDISK.length).Trim(" ","`t")
    $SOFTFILES = $_.Split(" ")[0].Split("`t")[0]
  }else{
    Wr "Unable to process: $_" RED; $ValidLine = $FALSE
  }
  If($ValidLine){
    $QuotaObj = New-Object PSObject
    Add-Member -InputObject $QuotaObj -MemberType NoteProperty -Name "QUOTATARGET" -Value $QUOTATARGET
    Add-Member -InputObject $QuotaObj -MemberType NoteProperty -Name "QUOTATYPE"   -Value $QUOTATYPE
    Add-Member -InputObject $QuotaObj -MemberType NoteProperty -Name "VOLNAME"     -Value $VOLNAME
    Add-Member -InputObject $QuotaObj -MemberType NoteProperty -Name "QTREE"       -Value $QTREE
    If($LIMITDISK  -eq ""){ $LIMITDISK  = "-"}; If(Is-Numeric $LIMITDISK){ $LIMITDISK += "K"}
    If($LIMITFILES -eq ""){ $LIMITFILES = "-"}
    If($THRESHOLD  -eq ""){ $THRESHOLD  = "-"}; If(Is-Numeric $THRESHOLD){ $THRESHOLD += "K"}
    If($SOFTDISK   -eq ""){ $SOFTDISK   = "-"}; If(Is-Numeric $SOFTDISK){ $SOFTDISK += "K"}
    If($SOFTFILES  -eq ""){ $SOFTFILES  = "-"}
    Add-Member -InputObject $QuotaObj -MemberType NoteProperty -Name "LIMITDISK"   -Value $LIMITDISK
    Add-Member -InputObject $QuotaObj -MemberType NoteProperty -Name "LIMITFILES"  -Value $LIMITFILES
    Add-Member -InputObject $QuotaObj -MemberType NoteProperty -Name "THRESHOLD"   -Value $THRESHOLD
    Add-Member -InputObject $QuotaObj -MemberType NoteProperty -Name "SOFTDISK"    -Value $SOFTDISK
    Add-Member -InputObject $QuotaObj -MemberType NoteProperty -Name "SOFTFILES"   -Value $SOFTFILES  
    $QuotasObj += $QuotaObj
  }
}
$QuotasObj | FT -Autosize

## CREATING CLUSTERSHELL COMMANDS ##
Wr ">>> CREATING CLUSTERSHELL COMMANDS <<<" Magenta;Wr
$CshellOutput = @()
$CshellOutput += ">>> CLUSTERSHELL COMMANDS FOR QUOTAS <<<",""
$VolsList = @()
$QuotasObj | Foreach{
  $Line = ""
  $Line += "quota policy rule create -vserver $SVM -policy-name $QuotaPolicy -volume " + $_.VOLNAME
  If($VolsList -notContains $_.VOLNAME){ $VolsList += $_.VOLNAME }
  $Line += " -type " + $_.QUOTATYPE
  If($_.QUOTATARGET -eq "ALL"){     $Line += (' -target ""') }
  elseif($_.QUOTATYPE -eq "TREE"){  $Line += (' -target "' + $_.QTREE + '"') }
  elseif($_.QUOTATYPE -eq "USER"){  $Line += (' -target "' + $_.QUOTATARGET + '"') }
  elseif($_.QUOTATYPE -eq "GROUP"){ $Line += (' -target "' + $_.QUOTATARGET + '"') }
  If($_.QUOTATYPE -ne "TREE"){ $Line += (' -qtree "' + $_.QTREE + '"')}
  $Line += " -disk-limit "      + $_.LIMITDISK
  $Line += " -file-limit "      + $_.LIMITFILES
  $Line += " -threshold "       + $_.THRESHOLD
  $Line += " -soft-disk-limit " + $_.SOFTDISK
  $Line += " -soft-file-limit " + $_.SOFTFILES
  $CshellOutput += $Line
}
$CshellOutput += "",">>> ENABLING QUOTAS <<<",""
$VolsList | Foreach{ $CshellOutput += "volume quota on -vserver $SVM -volume $_" }
$CshellOutput | Out-File ($QuotasFile + "_CSHELL.TXT")
Notepad ($QuotasFile + "_CSHELL.TXT")


No comments:

Post a Comment