-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtweak-fstab
executable file
·108 lines (98 loc) · 3.24 KB
/
tweak-fstab
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
#!/bin/sh
myname=${0##*/}
# default compression 10
comp=10
# werether other filesystems than btrfs should get the noatime option
other_fs=0
show_usage () {
printf '%s\n' "Usage:"
printf '\t%s\n' "${myname} [-o] [-c compression] [tweak] | [preview] | [help]"
}
show_help () {
printf '%s\n' "${myname}: tweak the mount options of btrfs file systems"
show_usage
printf '\n%s\n' "Options:"
printf '%s\n' "-o, --other-filesystem, other"
printf '\t%s\n' "tweak the mount options for other filesystems than btrfs, defaults to noatime."
printf '%s\n' "-c N, --compression N, compression N, comp N"
printf '\t%s\n' "where 'N' is the compression rate for btrfs."
printf '%s\n' "tweak"
printf '\t%s\n' "run the fstab tweak, you need the permissions tho, so run as root or with sudo"
printf '%s\n' "preview"
printf '\t%s\n' "show what the result changes to your fstab will be without writing anything"
printf '%s\n' "help"
printf '\t%s\n' "show this message, '-h' '-help' '--help' are also supported"
printf '\n%s\n' "Rationale:"
printf '\t%s\n' "i am very opinionated with my fstab, i like to use btrfs for '/', '/home' and"
printf '\t%s\n' "other storage drives mounted directly to my computer."
printf '\t%s\n' "'noatime' improves performance by not registering access time for inodes and"
printf '\t%s\n' "perhaps even reduces disc usage, maybe..."
printf '\t%s\n' "'compress=zstd:10' btrfs is one of the filesystems that support compression"
printf '\t%s\n' "it helps save space, which extra space or well more efficiently used space"
printf '\t%s\n' "is never a bad idea, zstd may not be the best at speed or compression ratio"
printf '\t%s\n' "but it hits a sweet spot where at compression level 10 it will try to save"
printf '\t%s\n' "a lot of space without loosing much speed, is not the 'realtime' of levels"
printf '\t%s\n' "1 to 5 buuuut considerably faster than 11 to 15."
}
fstab_awk () {
awk -v cm="noatime,compress=zstd:${comp}" -v of="$other_fs" \
'
/btrfs/ { sub(/defaults/,cm)}
{ if (of==1) sub(/defaults/,"noatime") }
{ print $0 }
' "$1"
}
# usage: is_num "value"
is_num() {
printf %d "$1" >/dev/null 2>&1
}
# input parsing
while [ "$#" -gt 0 ]; do
case "$1" in
-c|--compression|compression|comp)
if is_num "$2"; then
case "$2" in
[1-9]|1[0-5])
comp="$2"
;;
*)
printf '%s: argument for %s "%s" is not in range, using default value %s.\n' \
"${myname}" "$1" "$2" "$comp" >&2
;;
esac
else
printf '%s: argument for %s "%s" is not a number\n' "${myname}" "$1" "$2" >&2
exit 1
fi
shift
;;
-o|--other-filesystem|other) other_fs=1 ;;
*) choice="$1" ;;
esac
shift
done
case "$choice" in
preview)
echo "fstab:"
if [ -f /etc/old-fstab ]; then
fstabfile=/etc/old-fstab
else
fstabfile=/etc/fstab
fi
fstab_awk "$fstabfile"
;;
tweak)
if [ ! -f /etc/old-fstab ]; then
mv /etc/fstab /etc/old-fstab
fi
fstab_awk /etc/old-fstab > /etc/fstab
;;
help|-h|-help|--help)
show_help
;;
*)
printf '%s\n' "${myname}: does not recognize the argument ${choice}"
show_usage
exit 1
;;
esac