Remotely checks free space in the AIX volume group datavg on multiple servers, and if sufficient space is available, creates a 2GB JFS2 logical volume and filesystem mounted at /data/testfs. The script ensures the filesystem does not already exist before creation and logs all operations.
Platform: AIX
Usage: # ./create_fs_remote.ksh <server1> <server2> <server3> ...
Features:
- Validates volume group existence and available free physical partitions (PPs).
- Calculates required PPs based on volume group’s PP size to create a 2GB logical volume.
- Creates logical volume and corresponding JFS2 filesystem remotely via SSH.
- Creates mount directory if missing and mounts the new filesystem.
- Provides detailed logging of success or failure for each server.
#!/bin/ksh
#-----------------------------------------------------------------------
# Purpose : Check datavg free space and create a 2GB filesystem remotely
# Platform : AIX
# Author : Tasleem A. Khan
#-----------------------------------------------------------------------
# Usage : ./create_fs_remote.ksh <server1> <server2> <server3> ...
# Example: ./create_fs_remote.ksh aixsrv1 aixsrv2 aixsrv3
if [ $# -lt 1 ]; then
echo "Usage: $0 <server1> <server2> <server3> ..."
exit 1
fi
SERVERS="$@"
VG_NAME="datavg"
FS_SIZE=2G
FS_MOUNT="/data/testfs"
SSH_USER="root"
LOG_FILE="/tmp/create_fs_remote.log"
> "$LOG_FILE"
echo "===== Starting Filesystem Creation ($FS_MOUNT, $FS_SIZE) =====" | tee -a "$LOG_FILE"
echo "Volume Group: $VG_NAME" | tee -a "$LOG_FILE"
echo "Servers: $SERVERS" | tee -a "$LOG_FILE"
echo "Logs: $LOG_FILE" | tee -a "$LOG_FILE"
echo "---------------------------------------------------------------" | tee -a "$LOG_FILE"
for SERVER in $SERVERS; do
echo "\n=== Checking server: $SERVER ===" | tee -a "$LOG_FILE"
ssh -o BatchMode=yes -o ConnectTimeout=10 ${SSH_USER}@${SERVER} "
# Check if VG exists
if ! lsvg '$VG_NAME' >/dev/null 2>&1; then
echo \"ERROR: Volume group $VG_NAME not found on $SERVER.\"
exit 1
fi
# Get free PPs and PP size (MB)
VG_FREE=\$(lsvg '$VG_NAME' | awk '/FREE PPs:/ {print \$3}')
PP_SIZE=\$(lsvg '$VG_NAME' | awk '/PP SIZE:/ {print \$6}')
FREE_MB=\$((VG_FREE * PP_SIZE))
echo \"Free space in $VG_NAME: \$FREE_MB MB\"
# Check if enough space for 2GB
if [ \$FREE_MB -lt 2048 ]; then
echo \"Not enough space in $VG_NAME to create 2GB filesystem.\"
exit 0
fi
# Check if filesystem already exists
if df -g | grep -q \"$FS_MOUNT\"; then
echo \"Filesystem $FS_MOUNT already exists.\"
exit 0
fi
# Calculate required PPs
REQ_PPS=\$((2048 / PP_SIZE))
if [ \$((2048 % PP_SIZE)) -ne 0 ]; then
REQ_PPS=\$((REQ_PPS + 1))
fi
echo \"Creating logical volume with \$REQ_PPS PPs...................................................\"
# Create logical volume and capture its name
LV_NAME=\$(mklv -t jfs2 $VG_NAME \$REQ_PPS 2>/dev/null | awk '{print \$1}')
if [ -z \"\$LV_NAME\" ]; then
echo \"Failed to create logical volume on $VG_NAME.\"
exit 1
fi
echo \"Created LV: \$LV_NAME\"
# Create filesystem and mount
crfs -v jfs2 -d \$LV_NAME -m \"$FS_MOUNT\" -A yes -p rw -a size=$FS_SIZE >/dev/null 2>&1
if [ \$? -eq 0 ]; then
if [ ! -d \"$FS_MOUNT\" ]; then
mkdir -p \"$FS_MOUNT\"
echo \"Created mount directory $FS_MOUNT.\"
fi
mount \"$FS_MOUNT\"
echo \"Filesystem $FS_MOUNT created and mounted successfully.\"
else
echo \"Failed to create filesystem $FS_MOUNT.\"
fi
" | tee -a "$LOG_FILE"
# Check SSH exit status
if [ $? -ne 0 ]; then
echo "Error occurred while processing server $SERVER" | tee -a "$LOG_FILE"
fi
done
echo "\n===== Operation Complete =====" | tee -a "$LOG_FILE"
echo "Logs saved to: $LOG_FILE"
===== Starting Filesystem Creation (/data/testfs, 2G) =====
Volume Group: datavg
Servers: aixsrv1 aixsrv2 aixsrv3
Logs: /tmp/create_fs_remote.log
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Script Output:
=== Checking server: aixsrv1 ===
Free space in datavg: 3072 MB
Creating logical volume with 4 PPs...
Created LV: datavg_lv0001
Filesystem /data/testfs created and mounted successfully.
=== Checking server: aixsrv2 ===
Free space in datavg: 1024 MB
Not enough space in datavg to create 2GB filesystem.
=== Checking server: aixsrv3 ===
Free space in datavg: 4096 MB
Creating logical volume with 4 PPs...
Created LV: datavg_lv0002
Filesystem /data/testfs created and mounted successfully.
===== Operation Complete =====
Logs saved to: /tmp/create_fs_remote.log
VG_FREE=\$(lsvg '$VG_NAME' | awk '/FREE PPs:/ {print \$3}')
PP_SIZE=\$(lsvg '$VG_NAME' | awk '/PP SIZE:/ {print \$6}')
FREE_MB=\$((VG_FREE * PP_SIZE))
echo \"Free space in $VG_NAME: \$FREE_MB MB\"
# Check if enough space for 2GB
if [ \$FREE_MB -lt 2048 ]; then
echo \"Not enough space in $VG_NAME to create 2GB filesystem.\"
exit 0
fi
# Check if filesystem already exists
if df -g | grep -q \"$FS_MOUNT\"; then
echo \"Filesystem $FS_MOUNT already exists.\"
exit 0
fi
# Calculate required PPs
REQ_PPS=\$((2048 / PP_SIZE))
if [ \$((2048 % PP_SIZE)) -ne 0 ]; then
REQ_PPS=\$((REQ_PPS + 1))
fi
echo \"Creating logical volume with \$REQ_PPS PPs...................................................\"
# Create logical volume and capture its name
LV_NAME=\$(mklv -t jfs2 $VG_NAME \$REQ_PPS 2>/dev/null | awk '{print \$1}')
if [ -z \"\$LV_NAME\" ]; then
echo \"Failed to create logical volume on $VG_NAME.\"
exit 1
fi
echo \"Created LV: \$LV_NAME\"
# Create filesystem and mount
crfs -v jfs2 -d \$LV_NAME -m \"$FS_MOUNT\" -A yes -p rw -a size=$FS_SIZE >/dev/null 2>&1
if [ \$? -eq 0 ]; then
if [ ! -d \"$FS_MOUNT\" ]; then
mkdir -p \"$FS_MOUNT\"
echo \"Created mount directory $FS_MOUNT.\"
fi
mount \"$FS_MOUNT\"
echo \"Filesystem $FS_MOUNT created and mounted successfully.\"
else
echo \"Failed to create filesystem $FS_MOUNT.\"
fi
" | tee -a "$LOG_FILE"
# Check SSH exit status
if [ $? -ne 0 ]; then
echo "Error occurred while processing server $SERVER" | tee -a "$LOG_FILE"
fi
done
echo "\n===== Operation Complete =====" | tee -a "$LOG_FILE"
echo "Logs saved to: $LOG_FILE"
===== Starting Filesystem Creation (/data/testfs, 2G) =====
Volume Group: datavg
Servers: aixsrv1 aixsrv2 aixsrv3
Logs: /tmp/create_fs_remote.log
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Script Output:
=== Checking server: aixsrv1 ===
Free space in datavg: 3072 MB
Creating logical volume with 4 PPs...
Created LV: datavg_lv0001
Filesystem /data/testfs created and mounted successfully.
=== Checking server: aixsrv2 ===
Free space in datavg: 1024 MB
Not enough space in datavg to create 2GB filesystem.
=== Checking server: aixsrv3 ===
Free space in datavg: 4096 MB
Creating logical volume with 4 PPs...
Created LV: datavg_lv0002
Filesystem /data/testfs created and mounted successfully.
===== Operation Complete =====
Logs saved to: /tmp/create_fs_remote.log
No comments:
Post a Comment