| Current Path : /pkg/zhost/1.0/bin.old/ |
| Current File : //pkg/zhost/1.0/bin.old/zz-user-quota |
#!/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
conf='/etc/zz/web'
user=$1
action=$2 # get|set
format=$3 # kb|mb|gb
amount=$4
usage() {
script=$(basename $0)
echo "usage:$script <user> get|set kb|mb|gb <amount>\n"
exit 1
}
if [ ! -f $conf/mount_point ]; then
echo "error: $conf/mount_point not configured"
fi
mount_point=$(head -1 $conf/mount_point)
if ! id $user &>/dev/null; then
echo "error: user $user does not exist"
fi
if echo $action | grep -Pqv '^(get|set)'; then
echo "error: action must be get|set and not $action"
usage
fi
if echo $format | grep -Pqv '^(kb|mb|gb)'; then
echo "error: format must be kb|mb|gb and not $format"
usage
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
)
set_quota() {
local user=$1
local kb=$2
local mount_point=$3
echo "setquota -u $user $kb $kb 0 0 $mount_point"
#setquota -u $user $kb $kb 0 0 $mount_point
exit 0
}
if [ "$action" == 'get' ]; then
if quota $user | grep -q none$; then
echo "no quota set"
exit 1
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 0
elif [ "$action" == 'set' ]; then
if [ "$format" == 'kb' ]; then
kb=$amount
elif [ "$format" == 'mb' ]; then
kb=$(calc_mb_to_kb $amount)
elif [ "$format" == 'gb' ]; then
kb=$(calc_gb_to_kb $amount)
fi
set_quota $user $kb $mount_point
fi