Simple Bash script to add flexgroup qtree and quota

As the title says, a simple Bash script to create a flexgroup qtree and a reporting quota rule. You’ll need to be running ONTAP 9.3GA:

 

#!/bin/bash
#
# This script takes cluster, vserver, flexgroup, qtree names
# create qtree and a corresponding quota
# March 2018

showhelp="

Usage: fg_qtree_quota_create [ -h | options ]
Options are:
[ -c ] [ -v ] [ -f ] [ -q ]

This script creates a qtree in the flexgroup specified, very little error checking.
If there are any errors the script will bomb out, you'll need to investigate why.

The script will then create a qtree based quota reporting rule within the quota policy.

You'll then be asked if you want to create another qtree before applying the new quota
rule(s). If you want to, the script will exit out, you'll need to re-run the script to
create the other qtree(s). If not the script will enable the new quota rule(s).

BE AWARE: 9.3GA has a bug that breaks quotas for the flexgroup if a snapshot operation
occurs during the quota initialisation process.

This script assumes the following that:
  - the flexgroup specified has quotas already turned on.
  - the quota policyname is statically set to "user_reporting"
  - the user running the script has passwordless ssh set up.
  - the user running the script has cluster admin rights.

  -c The name of the NetApp cluster hosting the vfiler/flexgroup
  -v The name of the vfiler (SVM) exporting the flexgroup
  -f The name of the Flexgroup
  -q The name of the Qtree to be created
  -h display this help and exit

"
cluster=""
c_flag=false
vfiler=""
v_flag=false
flexgroup=""
f_flag=false
qtree=""
q_flag=false


# : after each option apart from h because they need an arguement
#
while getopts :hc:v:f:q: option
do
  case "$option"
  in
  h ) echo "$showhelp"
  exit 0
  ;;
  c ) cluster=$OPTARG
  c_flag=true
  ;;
  v ) vfiler=$OPTARG
  v_flag=true
  ;;
  f ) flexgroup=$OPTARG
  f_flag=true
  ;;
  q ) qtree=$OPTARG
  q_flag=true
  ;;
  \? ) echo "Invalid option: -$OPTARG" >&2
  exit 1
  ;;
  : ) echo "Option -$OPTARG requires an argument." >&2
  exit 1
  ;;
  * ) echo "Unimplemented option: -$OPTARG" >&2
  exit 1
  ;;
  esac
done

if [[ "$c_flag" = false || "$v_flag" = false || "$f_flag" = false || "$q_flag" = false ]]
then
  printf "-c -v -f -q must be specified and have a value"
  printf "Please use the -h switch for more information." >&2
  exit 1
fi

printf "Checking if the qtree \"$qtree\" already exists....."
printf "\n"
ssh $cluster qtree show -vserver $vfiler -volume $flexgroup -qtree $qtree > /dev/null 2>&1
if [ $? -eq 0 ]
then
  echo "Qtree present exiting"
  exit 1
else
  echo "Qtree not present, continuing....."
  if [ $(ssh $cluster volume quota show -vserver $vfiler -volume $flexgroup -fields state | grep $flexgroup | awk '{ print $3 }') = off ]
  then
    printf "The Flexgroup doesn't have quotas enabled"
    printf "\n"
  elif [ $(ssh $cluster volume quota show -vserver $vfiler -volume $flexgroup -fields state | grep $flexgroup | awk '{ print $3 }') != on ]
  then
    printf "The Flexgroup's quota state is not in a standard state, please investigate before trying to apply new quota rule(s)....."
    printf "\n"
    exit 1
  fi
fi

ssh $cluster qtree create -vserver $vfiler -volume $flexgroup -qtree $qtree -security-style unix -oplock-mode enable -unix-permissions ---rwxrwxr-x > /dev/null 2>&1
if [ $? -eq 0 ]
then
  echo "Successfully created Qtree"
  ssh $cluster qtree show -vserver $vfiler -volume $flexgroup -qtree $qtree
else
  echo "Could not create Qtree, please investigate further......" >&2
  exit 1
fi

printf "Creating Qtree quota reporting entry....."
printf "\n"
ssh $cluster volume quota policy rule create -policy-name user_reporting -vserver $vfiler -volume $flexgroup -type tree -target "$qtree" > /dev/null 2>&1
if [ $? -eq 0 ]
then
  echo "Quota created successfully."
else
  echo "Quota not created, please investigate....."
  exit 1
fi

printf "If you wish to add another qtree it's recommended you do this before you apply the new quota(s)."
printf "\n"
while true; do
  read -p "Do you wish to create another qtree? (y/n)" yn
  case $yn in
    [Yy]* ) printf "Exiting script, please run again to create another qtree/quota....."
    printf "\n"
    exit 0
    ;;
    [Nn]* ) printf "OK, applying the new quota(s)....."; printf "\n"
    if [ $(ssh $cluster volume quota show -vserver $vfiler -volume $flexgroup -fields state | grep $flexgroup | awk '{ print $3 }') = off ]
    then
      printf "Quotas already off, continuing....."
      printf "\n"
    else
      printf "Turning off quotas....."
      printf "\n"
      ssh $cluster quota off -vserver $vfiler -volume $flexgroup > /dev/null 2>&1
      printf "Pausing for 10 seconds while the command completes....."
      printf "\n"
      sleep 10
    fi
    printf "Turning on quotas....."
    printf "\n"
    ssh $cluster quota on -vserver $vfiler -volume $flexgroup > /dev/null 2>&1
    printf "Pausing for 10 seconds while the command completes....."
    printf "\n"
    sleep 10
    ssh $cluster volume quota show -vserver $vfiler -volume $flexgroup
    break
    ;;
    * ) echo "Please answer with [yY|nN]."
    ;;
  esac
done

 

Leave a Reply

Your email address will not be published.