Based on the accepted answer at How to set limit on directory size in Linux? by Sergei Nikulov and the original tutorial, I've put together a script which can create a quota-limited directory on demand.
The script operates on the premise of creating a loop device with specific size and filesystem, and mounting the loop device at the user-defined directory.
Script
Also available on GitHub. Further development will be done there.
#!/usr/bin/env bash
# Author: Serg Kolo
# Date: June 1, 2018
# Written for: https://askubuntu.com/q/1043035/295286
# Based on: https://www.linuxquestions.org/questions/linux-server-73/directory-quota-601140/
set -e
print_usage(){
cat <<EOF
Usage: sudo mklimdir.sh -m <Mountpoint Directory> -f <Filesystem> -s <INT>
-m directory
-f filesystem type (one of supported by mke2fs)
-s size in bytes
-h this message
Exit statuses:
0:
1: Invalid option
2: Missing argument
3: No args
4: root privillege required
EOF
} > /dev/stderr
parse_args(){
#set -x
option_handler(){
case ${opt} in
m) mountpoint=$( realpath -e "${OPTARG}" );;
s) size=${OPTARG} ;;
h) print_usage; exit 0 ;;
f) mkfs_cmd=mkfs."${OPTARG}" ;;
\?) echo ">>>Invalid option: -$OPTARG" > /dev/stderr; exit 1;;
\:) echo ">>>Missing argument to -${OPTARG}" > /dev/stderr; exit 2;;
esac
}
local OPTIND opt
getopts "m:s:f:h" opt || { echo "No args passed">/dev/stderr;print_usage;exit 3;}
option_handler
while getopts "m:s:f:h" opt; do
option_handler
done
shift $((OPTIND-1))
}
main(){
if [ $EUID -ne 0 ]; then
echo ">>> Please run the script with sudo/as root" > /dev/stderr
exit 4
fi
local mountpoint=""
local size=0
local mkfs_cmd
parse_args "$@"
quota_fs=/"${mountpoint//\//_}"_"$(date +%s)".quota
dd if=/dev/zero of="$quota_fs" count=1 bs="$size"
"$mkfs_cmd" "$quota_fs"
mount -o loop,rw,usrquota,grpquota "$quota_fs" "$mountpoint"
chown $SUDO_USER:$SUDO_USER "$mountpoint"
}
main "$@"
Usage
There's 3 required flags:
-m
for mountpoint, aka the directory which you want to limit
-s
the size to which you want to limit in bytes
-f
filesystem. If you're not sure what this should be, just stick with ext4 or ext3
Here's an example of how I used this script to create ./quoted_dir
limited to 1 MiB (that is 10242 in Binary Prefix )
sudo ./mklimdir.sh -m ./quoted_dir/ -s $((1024*1024)) -f ext4
I've tested the script with while true; do cat /etc/passwd >> ./quoted_dir/passwd; sleep 1; done
type of loop, which would append contents of /etc/passwd
to the file inside the quote-limited directory. The cat
eventually errored out with:
cat: write error: No space left on device
and the file writing stopped at 909KiB which is just a little short of 1 MiB and would prevent exceeding the limits within the directory.
Further development
For permanent effect, the loop device created by the script should be added to /etc/fstab
. This may be added later on GitHub. The script operates on a single directory, however this script can be used within another script for creating multiple limited directories, so it is flexible enough.