Bash script to add multiple resources to NFS pacemaker cluster

All,

I'm looking for some guidance on how to accomplish automating the addition of exports to an HA Pacemaker NFS cluster. I would like to do it in bash for logistics reasons.

The resource creation command looks like this:

pcs resource create nfs-b2b-hg-media-10.1 exportfs clientspec=10.1.0.0/255.255.0.0 options=rw,sync,no_root_squash directory=/nfs/exports/media fsid=1 --group nfsgroup

However, I would like the script to be able to add multiple exports at one time and am trying to figure out the best way to do it. I have been playing around with arrays and can gather 2 of the necessary variables in the command, but there are 4 needed variables:

resource_name --> nfs-b2b-hg-media-10.1
clientspec --> 10.1.0.0/255.255.0.0
directory --> /nfs/exports/media
fsid --> 1

The rest of the command is pretty static.

Here is a small test script that gathers resource name and clientspec (and puts in order):

#!/usr/bin/bash

cs10168="10.16.8.0/255.255.255.0"
cs101="10.1.0.0/255.255.0.0"
cs1091="10.91.0.0/255.255.0.0"
dirs=(media hotdrive images)
nfsenv="nfs-b2b-hg"
nfsoptions="rw,sync,no_root_squash"
lv=exports
mt="/nfs"
nfsgrp=nfsgroup

declare -A exports
exports[${nfsenv}-${dirs[0]}-10.1]=${cs101}
exports[${nfsenv}-${dirs[1]}-10.91]=${cs1091}
exports[${nfsenv}-${dirs[2]}-10.1]=${cs101}
exports[${nfsenv}-${dirs[2]}-10.91]=${cs1091}

declare -a orders
orders+=( ${nfsenv}-${dirs[0]}-10.1 )
orders+=( ${nfsenv}-${dirs[1]}-10.91 )
orders+=( ${nfsenv}-${dirs[2]}-10.1 )
orders+=( ${nfsenv}-${dirs[2]}-10.91 )

for i in ${orders[@]}

do

       pcs resource create $i exportfs clientspec=${exports[$i]} options=$nfsoptions directory=${mt}/${lv}/${dirs[0]} fsid=1 --group $nfsgrp

done

As you can see there is a problem with how to define 'directory' and 'fsid'.

Does anyone have any suggestions on how to accomplish this in bash? Any guidance is greatly appreciated.

Thanks,

HB

Is this what your after, remove echo to implement if it matches what you want.

nfsenv="nfs-b2b-hg"
nfsoptions="rw,sync,no_root_squash"
lv=exports
mt="/nfs"
nfsgrp=nfsgroup
dirs_list="media hotdrive images"
clispec_list="10.16.8.0/255.255.255.0 10.1.0.0/255.255.0.0 10.91.0.0/255.255.0.0"

for clispec in $clispec_list
do
   clrm=${clispec#*.}
   clrm=.${clrm#*.}
   clispec2=${clispec%$clrm}

   for dirs in $dirs_list
   do
      echo pcs resource create $nfsenv-$dirs-$clispec2 clientspec=$clispec options=$nfsoptions directory=$mt/$lv/$dirs fsid=1 --group $nfsgrp
   done
done

Thank you for the reply. Actually, I believe I described the desired command output poorly. In the snippet I posted, the 'exports' array would define the resources that I'd want to define:

declare -A exports
exports[${nfsenv}-${dirs[0]}-10.1]=${cs101}
exports[${nfsenv}-${dirs[1]}-10.91]=${cs1091}
exports[${nfsenv}-${dirs[2]}-10.1]=${cs101}
exports[${nfsenv}-${dirs[2]}-10.91]=${cs1091}

So the commands I'd want to run would be:

pcs resource create nfs-b2b-hg-media-10.1 exportfs clientspec=10.1.0.0/255.255.0.0 options=rw,sync,no_root_squash directory=/nfs/exports/media fsid=1 --group nfsgroup
pcs resource create nfs-b2b-hg-hotdrive-10.91 exportfs clientspec=10.91.0.0/255.255.0.0 options=rw,sync,no_root_squash directory=/nfs/exports/hotdrive fsid=2 --group nfsgroup
pcs resource create nfs-b2b-hg-images-10.1 exportfs clientspec=10.1.0.0/255.255.0.0 options=rw,sync,no_root_squash directory=/nfs/exports/images fsid=3 --group nfsgroup
pcs resource create nfs-b2b-hg-images-10.91 exportfs clientspec=10.91.0.0/255.255.0.0 options=rw,sync,no_root_squash directory=/nfs/exports/images fsid=3 --group nfsgroup

Notice the need to increment the 'fsid' based upon the exported directory (media=1, hotdrive=2, images=3). If a given directory is exported to multiple subnets, it will maintain the same 'fsid'.

I will test further and welcome any suggestions.

Thanks,

HB

Lets look at this solution:

nfsenv="nfs-b2b-hg"
nfsoptions="rw,sync,no_root_squash"
lv=exports
mt="/nfs"
nfsgrp=nfsgroup
dirs_list="media hotdrive images"

clispec=(10.16.8.0/255.255.255.0 10.1.0.0/255.255.0.0 10.91.0.0/255.255.0.0)
cli2=(10.16 10.1 10.91)

drive=(media hotdrive images)
fsid=(1 2 3)

for((c=0;c<${#clispec[@]};c++))
do
   for((i=0;i<${#drive[@]};i++))
   do
      echo pcs resource create $nfsenv-${drive}-${cli2[c]} exportfs clientspec=${clispec[c]} options=$nfsoptions directory=$mt/$lv/${drive} fsid=${fsid} --group $nfsgrp
   done
done

Here we are running two paired arrays clispec/cli2 and drive/fsid .

Although we could derive cli2 from clispec (first two octets) and fsid could be array index + 1 it's more useful as a general example to show how we can step thru the arrays with a counter and reference matching entries. For example drive[0]="media" and fsid[0]="1" .

This idea also ends up being more useful if, for example, you wanted to add another entry to clispec array with 10.16.12.0/255.255.255.0 and make its cli2 value 10.16(2)

1 Like

Thanks again for the reply. The problem with this solution in my case is it loops through all drives and adds each of them to all subnets.

I need to be able to define individual drives for specific subnets. Not all the exports will need to be presented to all subnets. I.E.:

 pcs resource create nfs-b2b-hg-media-10.1 exportfs clientspec=10.1.0.0/255.255.0.0 options=rw,sync,no_root_squash directory=/nfs/exports/media fsid=1 --group nfsgroup
pcs resource create nfs-b2b-hg-hotdrive-10.91 exportfs clientspec=10.91.0.0/255.255.0.0 options=rw,sync,no_root_squash directory=/nfs/exports/hotdrive fsid=2 --group nfsgroup
pcs resource create nfs-b2b-hg-images-10.1 exportfs clientspec=10.1.0.0/255.255.0.0 options=rw,sync,no_root_squash directory=/nfs/exports/images fsid=3 --group nfsgroup
pcs resource create nfs-b2b-hg-images-10.91 exportfs clientspec=10.91.0.0/255.255.0.0 options=rw,sync,no_root_squash directory=/nfs/exports/images fsid=3 --group nfsgroup

My use case is to add new exports (this is just one part of a larger script that also partitions drives, creates volume groups, logical volumes, etc) to the cluster for varying needs.

I'm still troubleshooting, the paired arrays look encouraging..

Thanks again,

HB

This idea comes to mind. Have a 3rd array with list of drives for subnet:

nfsenv="nfs-b2b-hg"
nfsoptions="rw,sync,no_root_squash"
lv=exports
mt="/nfs"
nfsgrp=nfsgroup

clispec=(10.16.8.0/255.255.255.0 10.1.0.0/255.255.0.0 10.91.0.0/255.255.0.0)
cli2=(10.16 10.1 10.91)
cl_drives=("" "media images" "hotdrive images")

drive=(media hotdrive images)
fsid=(1 2 3)

for((c=0;c<${#clispec[@]};c++))
do
   for drv in ${cl_drives[c]}
   do
       for((i=0;i<${#drive[@]};i++))
       do
           [ "${drive}" = "$drv" ] &&
               echo pcs resource create $nfsenv-${drive}-${cli2[c]} exportfs clientspec=${clispec[c]} options=$nfsoptions directory=$mt/$lv/${drive} fsid=${fsid} --group $nfsgrp
       done
   done
done
1 Like

@Chubler_XL - Thank you very much for the guidance, this appears to work as needed.

This will allow me to move forward with the rest of the needs, I will mark this as solved..

Thanks again,

HB