=========================================================================
#!/bin/ksh
# =========================================================================
# mountvgfs.ksh - Varyon VG if not active, then mount all filesystems
# for an AIX LVM volume group.
# Usage : ./mountvgfs.ksh <vgname>
# ./mountvgfs.ksh -f <vgname>
# Author : Tasleem A Khan
# =========================================================================
usage() {
echo
echo "Usage: $0 [-f] <vgname>"
echo "Mounts all filesystems for the given AIX LVM volume group."
echo
exit 1
}
# --- Parse command-line options ---
opt_force=0
if [[ "$1" = "-f" ]]; then
opt_force=1
shift
fi
vg="$1"
[[ -z "$vg" ]] && usage
logfile="/mountvgfs.log"
fsck="/usr/sbin/fsck"
# --- Initialize log ---
: > "$logfile"
exec > >(tee -a "$logfile") 2>&1
echo "============================================================"
echo "Starting filesystem mount for VG: $vg"
echo "Logfile: $logfile"
echo "Start time: $(date '+%Y-%m-%d %H:%M:%S')"
echo "============================================================"
# --- Verify VG existence ---
if ! lsvg "$vg" >/dev/null 2>&1; then
echo "Error: Volume group '$vg' does not exist."
exit 1
fi
# --- Check if VG is active ---
vg_state=$(lsvg "$vg" | grep "VG STATE" | awk '{print $NF}')
if [[ "$vg_state" != "active" ]]; then
echo "Volume group '$vg' is not active. Attempting to varyon..."
varyonvg "$vg" >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo "Error: Failed to varyon volume group '$vg'."
exit 1
else
echo "Successfully varied on volume group '$vg'."
fi
else
echo "Volume group '$vg' is already active."
fi
# --- Build filesystem list ---
typeset -A fs_to_dev
fslist=""
cmd="lsvg -l $vg | sed 1,2d | grep -v 'N/A'"
while read -r line; do
set -A arr -- $line
dev=${arr[0]}
type=${arr[1]}
mp=${arr[6]}
[[ -z "$mp" || "$mp" = "N/A" ]] && continue
fs_to_dev["$mp"]="/dev/$dev"
fslist="$fslist $mp"
done <<EOF
$($cmd)
EOF
# --- Sort filesystem list ---
fslist=$(echo "$fslist" | tr ' ' '\n' | sort)
if [[ -z "$fslist" ]]; then
echo "No filesystems found in VG '$vg'."
exit 0
fi
echo
echo "Filesystem list to mount:"
for fs in $fslist; do
echo " $fs"
done
echo
# --- Function: Check if filesystem is mounted ---
is_fs_mounted() {
fs="$1"
df -k "$fs" 2>/dev/null | sed 1d | awk '{print $NF}' | grep -qx "$fs"
}
# --- Mount filesystems ---
for fs in $fslist; do
echo "------------------------------------------------------------"
echo "Processing filesystem: $fs"
if is_fs_mounted "$fs"; then
echo "Already mounted."
continue
fi
if [[ ! -d "$fs" ]]; then
echo "Creating mount point: $fs"
mkdir -p "$fs"
fi
echo "Mounting: $fs"
mount "$fs" >/dev/null 2>&1
if ! is_fs_mounted "$fs"; then
echo "Mount failed. Running fsck..."
cmd="$fsck -y $fs"
echo "Executing: $cmd"
eval "$cmd"
echo "Retrying mount for $fs..."
mount "$fs" >/dev/null 2>&1
if is_fs_mounted "$fs"; then
echo "Successfully mounted after fsck."
else
echo "Error: Failed to mount $fs after fsck."
fi
else
echo "Mounted successfully."
fi
done
echo "------------------------------------------------------------"
echo "Finished mounting all filesystems."
echo "End time: $(date '+%Y-%m-%d %H:%M:%S')"
echo "============================================================"
=========================================================================
Example Outputs:
Case 1 — VG already active
Command:
# ./mountvgfs.ksh datavg
Example Output:
============================================================
Starting filesystem mount for VG: datavg
Logfile: /mountvgfs.log
Start time: 2025-11-08 12:42:01
============================================================
Volume group 'datavg' is already active.
Filesystem list to mount:
/data
/logs
/backup
------------------------------------------------------------
Processing filesystem: /backup
Already mounted.
------------------------------------------------------------
Processing filesystem: /data
Mounting: /data
Mounted successfully.
------------------------------------------------------------
Processing filesystem: /logs
Mounting: /logs
Mounted successfully.
------------------------------------------------------------
Finished mounting all filesystems.
End time: 2025-11-08 12:42:05
============================================================
Case 2 — VG not active (auto varyon)
Command:
# ./mountvgfs.ksh testvg
Example Output:
============================================================
Starting filesystem mount for VG: testvg
Logfile: /mountvgfs.log
Start time: 2025-11-08 12:50:18
============================================================
Volume group 'testvg' is not active. Attempting to varyon...
Successfully varied on volume group 'testvg'.
Filesystem list to mount:
/test1
/test2
------------------------------------------------------------
Processing filesystem: /test1
Mounting: /test1
Mounted successfully.
------------------------------------------------------------
Processing filesystem: /test2
Mounting: /test2
Mounted successfully.
------------------------------------------------------------
Finished mounting all filesystems.
End time: 2025-11-08 12:50:22
============================================================
Case 3 — VG doesn’t exist
Command:
# ./mountvgfs.ksh invalidvg
Example Output:
============================================================
Starting filesystem mount for VG: invalidvg
Logfile: /mountvgfs.log
Start time: 2025-11-08 12:55:11
============================================================
Error: Volume group 'invalidvg' does not exist.
No comments:
Post a Comment