forked from hestiacp/hestiacp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathv-add-sys-quota
More file actions
executable file
·129 lines (108 loc) · 4.61 KB
/
v-add-sys-quota
File metadata and controls
executable file
·129 lines (108 loc) · 4.61 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/bash
# info: add system quota
# options: NONE
#
# example: v-add-sys-quota
#
# This function enables filesystem quota on /home partition
# Some kernels do require additional packages to be installed first
#----------------------------------------------------------#
# Variable & Function #
#----------------------------------------------------------#
# Includes
# shellcheck source=/usr/local/hestia/func/main.sh
source $HESTIA/func/main.sh
# load config file
source_conf "$HESTIA/conf/hestia.conf"
#----------------------------------------------------------#
# Verifications #
#----------------------------------------------------------#
# Ensure that quota kernel modules are installed
if ! find "/lib/modules/$(uname -r)" -type f -name '*quota_v*.ko*' | grep -q '.*'; then
# Install kernel modules for quota support.
# Requires reboot to activate updated kernel.
echo "Installing required kernel modules for quota support..."
reboot_req="Y"
apt-get -qq install linux-image-extra-virtual -y
check_result $? "kernel module installation failed" "$E_UPDATE"
fi
# Checking quota package
if ! type -P quota &> /dev/null; then
export DEBIAN_FRONTEND=noninteractive
apt-get -y install quota > /dev/null 2>&1
check_result $? "quota package installation failed" "$E_UPDATE"
fi
# Perform verification if read-only mode is enabled
check_hestia_demo_mode
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Adding group and user quota on /home partition
mnt=$(df -P /home | awk '{print $6}' | tail -n1)
fs_type=$(df -T "$mnt" | awk '{print $2}' | tail -n1)
lnr=$(cat -n /etc/fstab | grep -v "#" | awk '{print $1,$3}' | grep "$mnt$" | cut -f 1 -d ' ')
opt=$(sed -n ${lnr}p /etc/fstab | awk '{print $4}')
if [[ "$fs_type" == "xfs" ]]; then
log_history "XFS filesystem detected on $mnt. Modifying GRUB for XFS quota."
grub_conf="/etc/default/grub"
if ! grep -q "rootflags=.*uquota" "$grub_conf" || ! grep -q "systemd.unified_cgroup_hierarchy=1" "$grub_conf"; then
params="rootflags=uquota,pquota,gquota systemd.unified_cgroup_hierarchy=1"
sed -i 's/^\(GRUB_CMDLINE_LINUX="[^"]*\)/\1 '"$params"'/' "$grub_conf"
check_result $? "Failed to add kernel parameters to $grub_conf"
update-grub > /dev/null 2>&1
log_history "GRUB updated. A system reboot is required to apply changes."
reboot_req="Y"
else
log_history "XFS quota flags already present in GRUB configuration."
fi
else
# >> Non XFS Filesystem
log_history "$fs_type filesystem on $mnt. Using standard quota parameters."
fnd='usrquota\|grpquota\|usrjquota=aquota.user\|grpjquota=aquota.group\|jqfmt=vfsv0'
new='usrquota,grpquota,usrjquota=aquota.user,grpjquota=aquota.group,jqfmt=vfsv0'
# >> Modify /etc/fstab
if [ "$(echo "$opt" | tr ',' '\n' | grep -c -xE "$fnd")" -ne "$(echo "$fnd" | tr '\|' '\n' | wc -l)" ]; then
old=$(echo "$opt" | tr ',' '\n' | grep -vE 'usrquota|grpquota|usrjquota=|grpjquota=|jqfmt=|uquota|gquota' | tr '\n' ',' | sed 's/,$//')
if [ -n "$old" ]; then
sed -i "$lnr s/$opt/$old,$new/" /etc/fstab
fi
systemctl daemon-reload
mount -o remount "$mnt"
fi
fi
# >> Adding v2 group and user quota index
# >> For XFS, aquota files are not typically used, quota is managed directly within filesystem.
if [ "$fs_type" != "xfs" ]; then
if [ ! -e "$mnt/aquota.user" ] || [ ! -e "$mnt/aquota.group" ]; then
quotacheck -avcugm > /dev/null 2>&1
fi
fi
# Adding quotacheck on reboot
touch /forcequotacheck
# Adding cron job
echo '#!/bin/bash' > /etc/cron.daily/quotacheck
echo 'touch /forcequotacheck' >> /etc/cron.daily/quotacheck
chmod a+x /etc/cron.daily/quotacheck
# Enabling group and user quota
if [ -n "$(quotaon -pa | grep " $mnt " | grep 'user\|group' | grep 'is off')" ]; then
quotaon -v $mnt
check_result $? "quota can't be enabled in $mnt" "$E_DISK"
fi
# Updating hestia.conf value
$BIN/v-change-sys-config-value "DISK_QUOTA" "yes"
# Rebuilding user quota
for user in $("$BIN/v-list-users" list); do
"$BIN/v-update-user-quota" "$user"
done
#----------------------------------------------------------#
# Hestia #
#----------------------------------------------------------#
# Logging
$BIN/v-log-action "system" "Info" "Plugins" "System Quota enforcement enabled."
log_history "system quota enforcement enabled"
log_event "$OK" "$ARGUMENTS"
if [ "$reboot_req" = "Y" ]; then
log_history "A system reboot is required to complete enable quota."
echo "Warning: A system reboot is required to complete enable quota."
fi
exit