| Current Path : /pkg/zhost/1.0/bin.old/ |
| Current File : //pkg/zhost/1.0/bin.old/zz-user-getquota |
#!/bin/bash
# TODO: this is for total quota. show per mount point
# calling executable sets $_zootzone_version
_zootzone_version='1.0'
source /pkg/zz/$_zootzone_version/lib/data.sh
user=$1
action='get'
format=$2 # kb|mb|gb
if ! id $user >/dev/null; then
echo "user $user does not exist"
exit
fi
quota_tidy=$(quota $user | tail -n +3 | tr -s ' ' | sed 's/^\s//g;s/*//g;')
used_kb=$(echo "$quota_tidy" | cut -d ' ' -f2 | tr '\n' ' ' | sed 's/\s$//g;s/ /+/g' | paste -s -d '+' | bc)
quota_kb=$(echo "$quota_tidy" | cut -d ' ' -f3 | tr '\n' ' ' | sed 's/\s$//g;s/ /+/g' | paste -s -d '+' | bc)
usage() {
script=$(basename $0) # TODO isn't there a bash var for this?
echo "$script <user> get|set kb|mb|gb <amount> <mount_point>"
}
set_quota() {
local user=$1
local kb=$2
local mount_point=$3
setquota -u $user $kb $kb 0 0 $mount_point
# TODO: return value?
# what about group quota?
}
if [ "$action" == 'get' ]; then
if quota $user | grep -q none$; then
echo "no quota set"
exit 0
fi
if [ "$format" == 'kb' ]; then
echo "$used_kb/$quota_kb KB"
elif [ "$format" == 'mb' ]; then
used_mb=$(calc_kb_to_mb $used_kb)
quota_mb=$(calc_kb_to_mb $quota_kb)
echo "$used_mb/$quota_mb MB"
elif [ "$format" == 'gb' ]; then
used_gb=$(calc_kb_to_gb $used_kb)
quota_gb=$(calc_kb_to_gb $quota_kb)
echo "$used_gb/$quota_gb GB"
elif [ "$format" == '' ]; then
used_gb=$(calc_kb_to_gb $used_kb)
quota_gb=$(calc_kb_to_gb $quota_kb)
echo "$used_gb/$quota_gb GB"
else
echo "error: invalid format"
usage
fi
exit
elif [ "$action" == 'set' ]; then
if [ "$format" == 'kb' ]; then
kb=$amount
set_quota $user $kb $mount_point
elif [ "$format" == 'mb' ]; then
kb=$(calc_mb_to_kb $amount)
set_quota $user $kb $mount_point
elif [ "$format" == 'gb' ]; then
kb=$(calc_gb_to_kb $amount)
set_quota $user $kb $mount_point
else
echo "error: invalid format"
usage
fi
else
echo "error: invalid action"
usage
fi