-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzfs-autoreservation.sh
executable file
·59 lines (46 loc) · 1.32 KB
/
zfs-autoreservation.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
#!/bin/bash
###
# This should protect important system datasets from a total lack of free space.
###
# Helpful to read output when debugging
# set -x
#https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux
RED='\033[0;31m'
GREEN='\033[0;32m'
ORANGE='\033[0;33m'
NC='\033[0m' # No Color
# Strict mode
# set -eEuo pipefail
set -eEu
trap 'printf "${RED}Failed on line: $LINENO at command:${NC}\n$BASH_COMMAND\nexit $?\n"' ERR
# IFS=$'\n\t'
# get real path to script
SCRIPT=`realpath $0`
SCRIPTPATH=`dirname $SCRIPT`
# add binary folders to local path
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
#-----------------------START-----------------------#
if [ "$#" -ne 2 ]; then
echo "Illegal number of parameters"
exit 1
fi
# Add to cron if terminal exist
if [[ -t 1 ]]
then
TASK="* * * * * ${SCRIPT} $*"
if crontab -l 2>/dev/null | grep -F -q "${TASK}"
then
echo "task already has been added to crontab"
else
(crontab -l 2>/dev/null || true; echo "$TASK") | crontab -
fi
fi
size=`/usr/sbin/zfs get used $1 -o value -H -p`
if [[ $2 =~ "%" ]]; then
reserv=`echo "scale=0; $size*(100+${2//%})/100" | bc`
else
reserv=`echo "scale=0; $size+$2" | bc`
fi
#echo "size: $size"
#echo "reserv: $reserv"
`/usr/sbin/zfs set reservation="$reserv" $1`