Scripting : SVM Root Mirroring for SOLARIS

Here I am posting the script that mirrors primary root disk to selected mirror disk ( of same geometry and size ) . And script will automatically perform the mirror attach operations that required  before and after the server reboot.  The script  will perform following operations, just for your understanding purpose I have made it clear by making comments in each section so that the script will be self explanatory.

Note: You should have atleast one slice empty in primary harddisk which doesn’t have any filesystem and not mounted. This is for the purpose of storing Meta database replicas

### usage : SVM-root-mirror -d    ####
## Verify the Script arguments. If any error display the script usage
## Find out what is current root disk
## Find out what is mirror disk
## Backup the current partition table for both disks
## copy the Partition table from first disk to mirror disk
## find out the Disk slices from the primary disk, that needs to be mirrored
## Make entries to /etc/lvm/md.tab and Create  oneway mirror for root slice from first disk
## Create one way mirrors for remaining slices(from first disk) and modify /etc/vfstab.
## Create a Self destructive RUN level script to attach the second mirror components after the manual reboot

bash-3.2# cat SVM-root-mirror
#!/bin/bash
### This script to setup SVM root mirror                               ####
### usage : SVM-root-mirror -d   ####

## Verify the Script arguments. If any error display the script usage

cmd=$0
usage=”Usage : $cmd -d disk n
Options:n
-d diskt disk used as 2md submirror ( e.g. c1t1d0) n”

auxfile=/tmp/data.$$
if [ $# -ne 2 ]
then
        echo -e $usage
        exit 1
fi

while getopts d:R x 2>/dev/null
do
case $x in
        ‘d’)    disk2=$OPTARG
                expr $disk2 : ‘^c[0-9][0-9]*t[0-9][0-9]*d[0-9][0-9]*$’ 1>/dev/null 2>/dev/null
                if [ $? -ne 0 ];then
                echo -e $usage
                exit 1
                fi;;
        ‘R’)    action=$x;;
        ‘?’)    echo -e “$usage”
                exit 1;;
        esac
done

## Find out what is current root disk

disk1=`mount -p | nawk ‘$3==”/” {print $1}’`
        if [ `echo $disk1|grep ‘/md/’ 1>/dev/null 2>/dev/null` ];then
        echo “root disk under SDS”
        exit 0
        else
        disk1=`expr $disk1 : ‘.*(c[0-9][0-9]*t[0-9][0-9]*d[0-9][0-9]*).*’`
        echo “$disk1 — as identified as primary root disk”
                if [ -z $disk1 ]; then
                echo “Error: root disk cannot be identified”
                exit 1
                fi
                if [ “$disk2” == “$disk1” ]; then
                echo “The Disk you entered is root disk”
                echo -e $usage
                exit 1
                fi

        fi

## Find out what is mirror disk

        if [ `mount -p |grep ${disk2}` ]; then
        echo “$disk2 is appearing in current mount table, please check mount -p command”
        exit 1
        elif [ ! `echo | format 2>/dev/null | nawk ‘$1 ~ /[0-9][0-9]*./ {print $2}’|grep ${disk2}` ];then
        echo “$disk2 was not found in system disks”
        exit 1
        fi

## Backup the current partition table for both disks

        prtvtoc -s -h /dev/rdsk/${disk1}s2 > /var/tmp/$disk1.prtvtoc.b4.mirror
        prtvtoc -s -h /dev/rdsk/${disk2}s2 > /var/tmp/$disk2.prtvtoc.b4.mirror

## copy the Partition table from first disk to mirror disk

        prtvtoc -s -h /dev/rdsk/${disk1}s2|fmthard -s – /dev/rdsk/${disk2}s2
        if [ $? -ne 0 ];then
        echo “Error: $disk1 and $disk2 : Geometry Mismatch”
        fi
## findout the Disk slices from the primary disk,needs to be mirrored

slclist=`prtvtoc -s -h /dev/rdsk/${disk1}s2|nawk  ‘$1!=”2″ && $1!=”8″ {print $1}’`

## Make entries to /etc/lvm/md.tab and Create oneway mirror for root slice from first disk
        echo -e “mddb01 -c 3 /dev/rdsk/${disk1}s7” > /etc/lvm/md.tab.SVM
        echo -e “mddb02 -c 3 /dev/rdsk/${disk2}s7” >> /etc/lvm/md.tab.SVM
        echo > /lib/svc/method/svc-sdsattach.sh
        for s in $slclist
        do
        echo -e “d1${s} 1 1 /dev/rdsk/${disk1}s$s” >> /etc/lvm/md.tab.SVM
        echo -e “d2${s} 1 1 /dev/rdsk/${disk2}s$s” >> /etc/lvm/md.tab.SVM
        echo -e “d${s} -m d1${s}” >> /etc/lvm/md.tab.SVM
        echo -e “/usr/sbin/metattach d${s} d2${s}” >> /tmp/svc-sdsattach.sh
        done
        #echo “Printing new md.tab”
        #echo “======================”
        #cat /etc/lvm/md.tab.SVM
        #echo “======================”
        cp /etc/lvm/md.tab.SVM /etc/lvm/md.tab

## Create oneway mirrors for remaining slices(from first disk) and modify /etc/vfstab.
## Create a Self destructive RUN level script to attach the second mirror components after the reboot

        /usr/sbin/metadb -f -a mddb01
        /usr/sbin/metadb -a mddb02
        /usr/sbin/metainit -f -a
        /usr/sbin/metaroot d0

        for i in `metastat -p | nawk ‘$2==”-m” {print $1}’`
        do
        grep $i /tmp/svc-sdsattach.sh|sort -u >> /tmp/svc-sdsattach.out
        done

        cp /etc/vfstab /etc/vfstab.orig
        sed -e “s?/dsk/${disk1}s?/md/dsk/d?”  -e “s?/rdsk/${disk1}s?/md/rdsk/d?” /etc/vfstab    > /etc/vfstab.SVM
        #sdiff -s /etc/vfstab  /etc/vfstab.SVM
        cp /etc/vfstab.SVM /etc/vfstab

cat > /etc/rc2.d/S999metattach <#!/sbin/sh
case “$1” in
        ‘start’)
                if [ -x /usr/sbin/metattach ]; then
                        echo “Attching mirrors…”
EOF
cat /tmp/svc-sdsattach.out >> /etc/rc2.d/S999metattach
cat >> /etc/rc2.d/S999metattach <                        /usr/sbin/dumpadm -d swap
                fi
                ;;
        *)      :
                ;;
esac
rm /etc/rc2.d/S999metattach
EOF
chmod 500 /etc/rc2.d/S999metattach

echo “SVM root mirror Configuration Completed. Please Reboot the Machine”
echo “Second mirror components will automatically attach after the reboot”

bash-3.2#

SERVER STATUS BEFORE THE SCRIPT EXECUTION:

 

bash-3.2# echo|format
Searching for disks…done

AVAILABLE DISK SELECTIONS:
       0. c1t0d0
          /pci@0,0/pci15ad,1976@10/sd@0,0
       1. c1t1d0
          /pci@0,0/pci15ad,1976@10/sd@1,0
Specify disk (enter its number): Specify disk (enter its number):
bash-3.2# metastat -p
bash-3.2# metadb -i

SAMPLE OUTPUT FOR WRONG USAGE:

 

bash-3.2# ./SVM-root-mirror
Usage : ./SVM-root-mirror -d disk
 Options:
 -d disk         disk used as 2md submirror ( e.g. c1t1d0)

 

OUTPUT of the SCRIPT EXECUTION:

 

bash-3.2# ./SVM-root-mirror  -d c1t1d0
c1t0d0 — as identified as primary root disk
fmthard: Partition 2 specifies the full disk and is not equal
full size of disk.  The full disk capacity is 31423140 sectors.
fmthard:  New volume table of contents now in place.
d10: Concat/Stripe is setup
d20: Concat/Stripe is setup
d0: Mirror is setup
d11: Concat/Stripe is setup
d21: Concat/Stripe is setup
d1: Mirror is setup
d16: Concat/Stripe is setup
d26: Concat/Stripe is setup
d6: Mirror is setup
d17: Concat/Stripe is setup
d27: Concat/Stripe is setup
d7: Mirror is setup
SVM root mirror Configuration Completed. Please Reboot the Machine
Second mirror components will automatically attach after the reboot

META DEVICE STATUS before the REBOOT:

bash-3.2# metastat -p
d7 -m d17 1
d17 1 1 c1t0d0s7
d6 -m d16 1
d16 1 1 c1t0d0s6
d1 -m d11 1
d11 1 1 c1t0d0s1
d0 -m d10 1
d10 1 1 c1t0d0s0
d27 1 1 c1t1d0s7
d26 1 1 c1t1d0s6
d21 1 1 c1t1d0s1
d20 1 1 c1t1d0s0

META DEVICE STATUS AFTER THE REBOOT :

 

bash-3.2# reboot
updating /platform/i86pc/boot_archive

————–Server rebooted ———–

login as: root
Using keyboard-interactive authentication.
Password:
Last login: Thu Oct  1 10:07:06 2009 from 192.168.1.201
Oracle Corporation      SunOS 5.10      Generic Patch   January 2005
# metastat -p
d7 -m d17 d27 1
d17 1 1 c1t0d0s7
d27 1 1 c1t1d0s7
d6 -m d16 d26 1
d16 1 1 c1t0d0s6
d26 1 1 c1t1d0s6
d1 -m d11 d21 1
d11 1 1 c1t0d0s1
d21 1 1 c1t1d0s1
d0 -m d10 d20 1
d10 1 1 c1t0d0s0
d20 1 1 c1t1d0s0

 

I will be adding more scripting examples ( along with demos) in further posts of this section. Just stay connected. You can subscribe the complete post in your  email  by subscribing here

Ramdev

Ramdev

I have started unixadminschool.com ( aka gurkulindia.com) in 2009 as my own personal reference blog, and later sometime i have realized that my leanings might be helpful for other unixadmins if I manage my knowledge-base in more user friendly format. And the result is today's' unixadminschool.com. You can connect me at - https://www.linkedin.com/in/unixadminschool/

4 Responses

  1. friv says:

    Nice share info

  2. jatin says:

    this is very good website for me…plz give me a script

    (( how to add a 100 ip adress in a server remotely..))and 100 user

  3. g9g says:

    good article, you always appreciate the good article, the information is always useful and people will always remember you

  4. g9g says:

    Your article has shared the information very useful, thanks for sharing

What is in your mind, about this post ? Leave a Reply

Close
  Our next learning article is ready, subscribe it in your email

What is your Learning Goal for Next Six Months ? Talk to us