-
Notifications
You must be signed in to change notification settings - Fork 80
/
btrfs-defrag-plugin.sh
executable file
·151 lines (127 loc) · 3.71 KB
/
btrfs-defrag-plugin.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/bash
#
# This plugin defragments rpm files after update.
#
# If the filesystem is btrfs, run defrag command in /var/lib/rpm, set the
# desired extent size to 32MiB, but this may change in the result depending
# on the fragmentation of the free space
#
## Why 32MiB:
# - the worst fragmentation has been observed on /var/lib/rpm/Packages
# - this can grow up to several hundred of megabytes
# - the file gets updated at random places
# - although the file will be composed of many extents, it's faster to
# merge only the extents that affect some portions of the file, instead
# of the whole file; the difference is negligible
# - due to the free space fragmentation over time, it's hard to find
# contiguous space, the bigger the extent is, the worse and the extent
# size hint is not reached anyway
DEBUG="false"
EXTENT_SIZE="32M"
RPMDIR=$(rpm --eval "%_dbpath")
SCRIPTNAME="$(basename "$0")"
cleanup() {
test -n "$tmpdir" -a -d "$tmpdir" && execute rm -rf "$tmpdir"
}
trap cleanup EXIT
tmpdir=$(mktemp -d /tmp/btrfs-defrag-plugin.XXXXXX)
log() {
logger -p info -t $SCRIPTNAME --id=$$ "$@"
}
debug() {
$DEBUG && log "$@"
}
respond() {
debug "<< [$1]"
echo -ne "$1\n\n\x00"
}
execute() {
debug -- "Executing: $@"
$@ 2> $tmpdir/cmd-output
ret=$?
if $DEBUG; then
if test $ret -ne 0; then
log -- "Command failed, output follows:"
log -f $tmpdir/cmd-output
log -- "End output"
else
log -- "Command succeeded"
fi
fi
return $ret
}
btrfs_defrag() {
# defrag options:
# - verbose
# - recursive
# - flush each file before going to the next one
# - set the extent target hint
execute btrfs filesystem defragment -v -f -r -t "$EXTENT_SIZE" "$RPMDIR"
}
debug_fragmentation() {
if $DEBUG; then
log -- "Fragmentation $1"
execute filefrag $RPMDIR/* > $tmpdir/filefrag-output
if test $? -eq 0; then
log -f $tmpdir/filefrag-output
log -- "End output"
else
log "Non-fatal error ignored."
fi
fi
}
ret=0
# The frames are terminated with NUL. Use that as the delimeter and get
# the whole frame in one go.
while IFS= read -r -d $'\0' FRAME; do
echo ">>" $FRAME | debug
# We only want the command, which is the first word
read COMMAND <<<$FRAME
# libzypp will only close the plugin on errors, which may also be logged.
# It will also log if the plugin exits unexpectedly. We don't want
# to create a noisy log when using another file system, so we just
# wait until COMMITEND to do anything. We also need to ACK _DISCONNECT
# or libzypp will kill the script, which means we can't clean up.
debug "COMMAND=[$COMMAND]"
case "$COMMAND" in
COMMITEND) ;;
_DISCONNECT)
respond "ACK"
break
;;
*)
respond "_ENOMETHOD"
continue
;;
esac
# We don't have anything to do if it's not btrfs.
FSTYPE=$(execute stat -f --format=%T $RPMDIR)
if test $? -ne 0; then
respond "ERROR"
ret=1
break
fi
debug "Output follows:"
debug "$FSTYPE"
debug -- "End output"
if test "$FSTYPE" != "btrfs"; then
debug "Nothing to do: RPM Database is on $FSTYPE file system."
respond "_ENOMETHOD"
continue
fi
debug_fragmentation "before defrag run"
btrfs_defrag > $tmpdir/defrag-output
if test $? -ne 0; then
respond "ERROR"
ret=1
break
fi
# Log the output if we're in debug mode
debug "Output follows:"
debug -f $tmpdir/defrag-output
debug -- "End output"
debug_fragmentation "after defrag run"
respond "ACK"
done
debug "Terminating with exit code $ret"
exit $ret