In this article I'd like to share a quick script to calculate the total storage RAW plus LOFS allocated to a Solaris zone.
The script would need to be run from the global zone and it's usage will be:
./zone_storage_cal.sh <zone name>
Here is the script:
[ssuri@localhost:~] $ cat zone_storage_cal.sh
#!/usr/bin/bash
ZONE=$1
DISK_LIST=$(cat /etc/zones/${ZONE}.xml | grep "device match" | awk -F/ '{print $4}')
FORMAT_OUT="/export/home/`whoami`/format_out"
>$FORMAT_OUT
echo | sudo format >> $FORMAT_OUT
DISK_OUT="/export/home/`whoami`/disk_out"
>$DISK_OUT
for DISK_NAME in ${DISK_LIST}
do
DISK=$(cat $FORMAT_OUT | grep -w ${DISK_NAME%s0\"} | awk '{print $2}')
SIZE=$(cat $FORMAT_OUT | grep -w ${DISK_NAME%s0\"} | awk '{print $3}' | tr -d "<>" | awk -F- '{print $4}' | tr -d "GB")
echo "${DISK} ${SIZE}" >> $DISK_OUT
done
RAW_DISK=$(cat $DISK_OUT | awk '{sum+=$2} END {print sum}')
echo "total RAW disk device size is ${RAW_DISK} GB"
##LOFS mount size calculation##
LOFS_LIST=$(cat /etc/zones/${ZONE}.xml | grep lofs | egrep -v "/var|/usr" | awk '{print $2}' | awk -F= '{print $2}' | tr -d \")
TOTAL_LOFS_SIZE=0
for LOFS in ${LOFS_LIST}
do
SIZE=$(df -k | cat | egrep "${LOFS}" | awk '{print $2}')
let "TOTAL_LOFS_SIZE+=$SIZE"
done
TOTAL_LOFS_SIZE_GB=$( echo "scale=2; ${TOTAL_LOFS_SIZE}/1024/1024" | bc -l)
if [ ! -z ${TOTAL_LOFS_SIZE_GB} ]
then
echo "Total size of LOFS mounts is ${TOTAL_LOFS_SIZE_GB} GB"
fi
##end LOFS calculation##
##print total size##
if [ ! -z ${TOTAL_LOFS_SIZE_GB} ] && [ ! -z ${RAW_DISK} ]
then
TOTAL_SIZE=$(echo "scale=2; ${TOTAL_LOFS_SIZE_GB} + ${RAW_DISK}" | bc -l)
echo "RAW disk and LOFS size combined is ${TOTAL_SIZE} GB"
fi
The script would need to be run from the global zone and it's usage will be:
./zone_storage_cal.sh <zone name>
Here is the script:
[ssuri@localhost:~] $ cat zone_storage_cal.sh
#!/usr/bin/bash
ZONE=$1
DISK_LIST=$(cat /etc/zones/${ZONE}.xml | grep "device match" | awk -F/ '{print $4}')
FORMAT_OUT="/export/home/`whoami`/format_out"
>$FORMAT_OUT
echo | sudo format >> $FORMAT_OUT
DISK_OUT="/export/home/`whoami`/disk_out"
>$DISK_OUT
for DISK_NAME in ${DISK_LIST}
do
DISK=$(cat $FORMAT_OUT | grep -w ${DISK_NAME%s0\"} | awk '{print $2}')
SIZE=$(cat $FORMAT_OUT | grep -w ${DISK_NAME%s0\"} | awk '{print $3}' | tr -d "<>" | awk -F- '{print $4}' | tr -d "GB")
echo "${DISK} ${SIZE}" >> $DISK_OUT
done
RAW_DISK=$(cat $DISK_OUT | awk '{sum+=$2} END {print sum}')
echo "total RAW disk device size is ${RAW_DISK} GB"
##LOFS mount size calculation##
LOFS_LIST=$(cat /etc/zones/${ZONE}.xml | grep lofs | egrep -v "/var|/usr" | awk '{print $2}' | awk -F= '{print $2}' | tr -d \")
TOTAL_LOFS_SIZE=0
for LOFS in ${LOFS_LIST}
do
SIZE=$(df -k | cat | egrep "${LOFS}" | awk '{print $2}')
let "TOTAL_LOFS_SIZE+=$SIZE"
done
TOTAL_LOFS_SIZE_GB=$( echo "scale=2; ${TOTAL_LOFS_SIZE}/1024/1024" | bc -l)
if [ ! -z ${TOTAL_LOFS_SIZE_GB} ]
then
echo "Total size of LOFS mounts is ${TOTAL_LOFS_SIZE_GB} GB"
fi
##end LOFS calculation##
##print total size##
if [ ! -z ${TOTAL_LOFS_SIZE_GB} ] && [ ! -z ${RAW_DISK} ]
then
TOTAL_SIZE=$(echo "scale=2; ${TOTAL_LOFS_SIZE_GB} + ${RAW_DISK}" | bc -l)
echo "RAW disk and LOFS size combined is ${TOTAL_SIZE} GB"
fi
The script uses the zones's XML file to get the details of any raw disk devices exported to the zone and also gather information about loop back file systems mounted on the zones.
The addition part is done using let. I didn't have to use bc because I opted not to be floating point data for initial calculations.
In the final calculations I've used bc to provide a more accurate final result.