-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZFS-Quota-Server.sh
71 lines (62 loc) · 1.52 KB
/
ZFS-Quota-Server.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
#!/bin/bash
# ZFS Quota Server - Version 3
# 2021 - Adam Boutcher
# IPPP, Durham University
# This pulls the quota info from ZFS and outputs to a file to parse remotely
ZFS="/usr/sbin/zfs"
# Function to check that a binary exists
function check_bin() {
which $1 1>/dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo "$1 cannot be found. Please install it or add it to the path. Exiting."
exit 1
fi
}
check_bin which
check_bin $ZFS
check_bin echo
check_bin awk
check_bin sed
check_bin grep
if [[ -z "$1" ]]; then
echo "Please include a quota pool i.e. homes/home";
exit 1;
fi
# Check the zfs pool/vdev exists
$ZFS list 2>&1 | sed -n '1!p' | awk '{print $1}' | grep $1 >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "$1 cannot be found. Please check your ZFS setup."
exit 1
fi
# Modifying the IFS
SAVEIFS=$IFS
IFS="
"
if [ -z $2 ] || [ $2 != "group" ]; then
ZTYPE="userspace"
else
ZTYPE="groupspace"
fi
if [ -z $3 ]; then
QLOC=$($ZFS get mountpoint -H $1 2>/dev/null | awk '{print $3}')
else
QLOC=$3
fi
zcmd=$($ZFS $ZTYPE -p -n $1 2>/dev/null | sed -n '1!p');
if [ -f $QLOC/quota.zfs ]; then
> $QLOC/quota.zfs
for zquota in `echo "$zcmd"`; do
zuser=$(echo $zquota| awk '{print $3}');
zused=$(echo $zquota| awk '{print $4}');
zquot=$(echo $zquota| awk '{print $5}');
if [[ zquot -ne "none" ]]; then
zperc=$(echo $zquota| awk '{printf "%.0f\n", ($4/$5) * 100}');
else
zperc=0;
fi
echo -e "$zuser::$zused::$zquot::$zperc%" >> $QLOC/quota.zfs
done;
fi
# Reset IFS
IFS=$SAVEIFS
exit 0;