-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmklimdir.sh
executable file
·75 lines (57 loc) · 1.62 KB
/
mklimdir.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/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 "$@"