#!/bin/sh
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

VROOT=/var/lib/vservers
GUEST="$1"
SPACE="$2"
if [ -z $GUEST ]
then
    echo \
	$"Usage: $(basename $0) <vserver>
	<vserver> is the name of a vserver."
exit 0
fi

if [ -f /etc/vservers/$1/context ]
then
    XID=`cat /etc/vservers/$1/context`
else
    echo \
    $"Fixed Context-ID not found:
    To limit disk space of a vserver required a fixed Context-ID.
    Please set a fixed context ID to your vserver, it could be simply done
    by echo xid-number > /etc/vservers/$1/context" 
    exit 0
fi

if [ -z $SPACE ]
then

    echo \
    $"Usage: $(basename $0) $GUEST <space>
    <space> is the space you want to limit to a vserver.
    The current space usage of $GUEST vserver = `du -s "$VROOT"/"$GUEST" | awk '{print $1}'`
    Please give <space> greater than the current space usage of $GUEST."

else 
    chxid -c $XID -R "$VROOT"/"$GUEST"
    /usr/sbin/vdlimit --xid $XID --set space_total="$SPACE" --set space_used=`du -sx "$VROOT"/"$GUEST" | awk '{print $1}'` --set inodes_total="$SPACE" --set inodes_used=`ls -1aRi $VROOT/$GUEST | awk '/^[0-9]+ / { print $1 }' | sort -u | wc -l` --set reserved=5 $VROOT/$GUEST
fi
