Pages

Fully automated AIX rootvg mirror script


AIX rootvg mirror script that:
  • Finds all disks and their sizes.
  • Identifies rootvg disks and picks the first as the reference.
  • Finds any free disk (None) with the same size.
  • Asks for user confirmation.
  • Adds that disk to rootvg.
  • Mirrors rootvg (mirrorvg -S).
  • Checks for STALE PPs = 0 (ensures full sync).
  • Updates bootlist with both rootvg disks.
  • Runs bosboot on both disks.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#!/bin/ksh
# ------------------------------------------------------------------------------
# Script Name : mirror_rootvg_auto.ksh
# Purpose     : Automatically find a free disk same size as rootvg disk,
#               add it to rootvg, mirror rootvg, and update bootlist.
# Platform    : AIX
# Author      : Tasleem A Khan
# ------------------------------------------------------------------------------
# Safety Notes:
#  - Must be run as root.
#  - Run only after verifying system health and disk availability.
#  - Logs can be redirected with: ./mirror_rootvg_auto.ksh | tee /tmp/mirror_rootvg.log
# ------------------------------------------------------------------------------

# Temporary files
disk_size1=/tmp/disk_size1
disk_size2=/tmp/disk_size2

# Clear temp files if they exist
> "$disk_size1"
> "$disk_size2"

echo "-------------------------------------------------------------------"
echo "Collecting all disk sizes..."

# Collect all disk sizes
for pv1 in $(lspv | awk '{print $1}')
do
  size=$(bootinfo -s "$pv1" 2>/dev/null)
  if [ -n "$size" ]; then
    echo "Disk Size: $pv1 $size" >> "$disk_size1"
  fi
done

echo "Collecting rootvg disk sizes......................................"
for pv2 in $(lspv | grep rootvg | awk '{print $1}')
do
  size=$(bootinfo -s "$pv2" 2>/dev/null)
  if [ -n "$size" ]; then
    echo "Disk Size: $pv2 $size" >> "$disk_size2"
  fi
done

# Get the first rootvg disk as the target
target_disk=$(awk '{print $3; exit}' "$disk_size2")

if [ -z "$target_disk" ]; then
  echo "No target disk found in $disk_size2"
  exit 1
fi

# Get size of that disk
target_size=$(awk -v disk="$target_disk" '$3 == disk {print $4}' "$disk_size1")

if [ -z "$target_size" ]; then
  echo "Disk $target_disk not found in $disk_size1"
  exit 1
fi

echo "-------------------------------------------------------------------"
echo "Target disk: $target_disk"
echo "Target disk size: ${target_size}MB"
echo "-------------------------------------------------------------------"

# Find all disks with the same size and save to /tmp/disk_size2 (overwrite) 
awk -v size="$target_size" '$4 == size {print "Disk Size: " $3 " " $4}' "$disk_size1" > "$disk_size2" 
echo "Root VG DISK are.................................................." 
cat "$disk_size2" 
echo "------------------------------------------------------------------" 
echo "Detailed lspv info for matching disks:............................" 

for i in $(awk '{print $3}' "$disk_size2")
do lspv | grep "$i" 
done 
echo "------------------------------------------------------------------" 
echo "Detailed lspv info for matching disks:" 

for i in $(awk '{print $3}' "$disk_size2") 
do 
rootadd=$(lspv | grep None | awk '{print $1}' | head -1) 
done
echo $rootadd

rootdisk=$(lspv | grep rootvg | awk '{print $1}')
echo $rootdisk

if [ -n "$rootadd" ]; then
  echo "Found free disk: $rootadd (size: ${target_size}MB)"
  echo "Do you want to add it to rootvg? (y/n)\c"
  read answer
  if [ "$answer" = "y" ]; then
    echo "Adding $rootadd to rootvg.........................................."
    extendvg rootvg "$rootadd"
    echo "Updated rootvg physical volumes:"
    lsvg -p rootvg
  else
    echo "Aborted by user...................................................."
    exit 0
  fi
else
  echo "No free disk with matching size found.............................."
  exit 1
fi

# Mirror the rootvg
echo "-------------------------------------------------------------------"
echo "Starting mirroring of rootvg to $rootadd..."
mirrorvg -S rootvg "$rootadd"

if [ $? -ne 0 ]; then
  echo "Mirror operation failed............................................"
  exit 1
fi

# Wait for mirroring to start before checking stale PPs
echo "Waiting for mirroring to stabilize..."
sleep 60

echo "Checking for stale partitions......................................"
stale_pps=$(lsvg rootvg | grep "STALE PPs:" | awk '{print $3}')
echo "Current STALE PPs: $stale_pps"

# Give a few retries to let sync complete
retries=5
while [ "$stale_pps" != "0" -a $retries -gt 0 ]
do
  echo "Still syncing... Waiting 60 seconds (remaining retries: $retries)"
  sleep 60
  stale_pps=$(lsvg rootvg | grep "STALE PPs:" | awk '{print $3}')
  retries=$((retries-1))
done

if [ "$stale_pps" = "0" ]; then
  echo "------------------------------------------------------------------"
  echo "Mirror successful. No stale PPs detected."
  rootdisk=$(lspv | grep rootvg | awk '{print $1}' | head -1)
  echo "Setting boot order to: $rootdisk $rootadd"
  bootlist -m normal $rootdisk $rootadd
  echo "Verifying bootlist................................................"
  bootlist -m normal -o
  echo "------------------------------------------------------------------"
  lsvg -p rootvg
  echo "Creating boot images for both rootvg disks........................"
  for d in $rootdisk $rootadd
  do
    echo "Running bosboot for /dev/$d....................................."
    bosboot -ad /dev/$d
  done

  echo "------------------------------------------------------------------"
  echo "Mirroring and boot configuration completed successfully."
else
  echo "Mirror still has stale PPs ($stale_pps)."
  echo "Please run 'syncvg -v rootvg' to complete synchronization........."
  exit 1
fi

exit 0
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1. Add the above script contents
# vi mirror_rootvg_auto.ksh
2. Make it executable
# chmod +x mirror_rootvg_auto.ksh
3. Run script
./mirror_rootvg_auto.ksh | tee /tmp/mirror_rootvg.log

Script Output:
-------------------------------------------------------------------
Collecting all disk sizes...
Collecting rootvg disk sizes......................................
-------------------------------------------------------------------
Target disk: hdisk2
Target disk size: 46080MB
-------------------------------------------------------------------
Root VG DISK are..................................................
Disk Size: hdisk1 46080
Disk Size: hdisk2 46080
------------------------------------------------------------------
Detailed lspv info for matching disks:............................
hdisk1        00c2d8d8c55583fa                    None
hdisk2        00c2d8d823db38fb                    rootvg          active
------------------------------------------------------------------
Detailed lspv info for matching disks:
hdisk1
hdisk2
Found free disk: hdisk1 (size: 46080MB)
Do you want to add it to rootvg? (y/n)y
Adding hdisk1 to rootvg..........................................
Updated rootvg physical volumes:
rootvg:
PV_NAME           PV STATE          TOTAL PPs   FREE PPs    FREE DISTRIBUTION
hdisk2           active            179         45          00..00..00..09..36
hdisk1           active            179         179         36..36..35..36..36
-------------------------------------------------------------------
Starting mirroring of rootvg to hdisk1...
0516-1804 chvg: The quorum change takes effect immediately.
0516-1126 mirrorvg: rootvg successfully mirrored, user should perform
        bosboot of system to initialize boot records.  Then, user must modify
        bootlist to include:  hdisk1 hdisk2.
Waiting for mirroring to stabilize...
Checking for stale partitions......................................
Current STALE PPs: 1
Still syncing... Waiting 60 seconds (remaining retries: 5)
Still syncing... Waiting 60 seconds (remaining retries: 4)
Still syncing... Waiting 60 seconds (remaining retries: 3)
------------------------------------------------------------------
Mirror successful. No stale PPs detected.
Setting boot order to: hdisk1 hdisk2
Verifying bootlist................................................
hdisk1 pathid=2
hdisk1 pathid=3
hdisk2 pathid=2
hdisk2 pathid=3
------------------------------------------------------------------
rootvg:
PV_NAME           PV STATE          TOTAL PPs   FREE PPs    FREE DISTRIBUTION
hdisk1           active            179         45          00..00..00..09..36
hdisk2           active            179         45          02..00..00..07..36
Creating boot images for both rootvg disks........................
Running bosboot for /dev/hdisk1.....................................
trustchk: /etc/vfs: Verification of attributes failed: mode

bosboot: Boot image is 61468 512 byte blocks.
Running bosboot for /dev/hdisk2.....................................
trustchk: /etc/vfs: Verification of attributes failed: mode

bosboot: Boot image is 61468 512 byte blocks.
------------------------------------------------------------------
Mirroring and boot configuration completed successfully.

No comments:

Post a Comment