Small changes to 'set -u' handling, a bit of tidying and cleanup

This commit is contained in:
Scott B 2019-12-27 00:44:18 -08:00
parent 4241a70d94
commit b26c3ac79b
6 changed files with 40 additions and 36 deletions

View file

@ -22,7 +22,7 @@ Run `zramctl` during use to monitor swap compression and real memory usage.
### Debugging
Either start zram-swap.sh with `bash -x zram-swap.sh` or `./zram-swap.sh -x (start|stop)` to see what's going wrong.
Start zram-swap.sh with `bash -x zram-swap.sh (start|stop)` or `./zram-swap.sh -x (start|stop)` to see what's going wrong.
To dump the full execution trace during service start/stop edit /etc/systemd/systemd/zram-swap.service and add -x to the following two lines:

View file

@ -8,8 +8,13 @@ if systemctl -q is-active zram-swap.service; then
fi
install -o root zram-swap.sh /usr/local/sbin/zram-swap.sh
install -o root zram-swap-service /etc/default/zram-swap-service
install -o root zram-swap.service /etc/systemd/system/zram-swap.service
if [[ -f /etc/default/zram-swap-service ]]; then
mv -f /etc/default/zram-swap-service /etc/default/zram-swap
chmod 0644 /etc/default/zram-swap
else
install -o root -m 0644 service/zram-swap.config /etc/default/zram-swap
fi
install -o root -m 0644 service/zram-swap.service /etc/systemd/system/zram-swap.service
systemctl daemon-reload
systemctl enable zram-swap.service

18
service/zram-swap.config Normal file
View file

@ -0,0 +1,18 @@
# portion of real ram to use as zram swap (expression: "1/2", "0.5", etc)
_zram_fraction="1/2"
# compression algorithm to employ (lzo, lz4, zstd, lzo-rle)
_zram_algorithm="lz4"
# expected compression ratio; this is a rough estimate
#_comp_factor="2.5"
# Note:
# set _comp_factor by hand if you use an algorithm other than lzo/lz4/zstd or if your
# use case produces drastically different compression results than my estimates
#
# defaults if otherwise unset:
# lzo*|zstd) _comp_factor="3" ;;
# lz4) _comp_factor="2.5" ;;
# *) _comp_factor="2" ;;
#

View file

@ -1,22 +0,0 @@
# portion of real ram to use as zram swap (expression: "1/2", "0.5", etc)
_zram_fraction="1/2"
# compression algorithm to employ (lzo, lz4, zstd, lzo-rle)
_zram_algorithm="lz4"
# set expected compression ratio based on algorithm; this is a rough estimate
# set this by hand if you use an algorithm other than lzo/lz4/zstd
#_comp_factor="2.5"
#
# defaults:
#case $_zram_algorithm in
# lzo*|zstd)
# _comp_factor="3"
# ;;
# lz4)
# _comp_factor="2.5"
# ;;
# *)
# _comp_factor="2"
# ;;
#esac

View file

@ -2,10 +2,15 @@
# source: https://github.com/foundObjects/zram-swap
[[ "$EUID" == "0" ]] || { echo "This script requires root." && exit 1; }
set -eo pipefail
set -euo pipefail
# parse debug flag early so we can trace everything
[[ "$1" == "-x" ]] && { shift && set -x; } >&/dev/null
# make sure our environment is predictable
PATH=/usr/sbin:/usr/bin:/sbin:/bin
unalias -a
# parse debug flag early so we can trace user configuration
(("$#" > 0)) && [[ "$1" == "-x" ]] && { shift && set -x; } >&/dev/null
# make sure $1 exists for 'set -u' so we can get through 'case "$1"' below
(("$#" == 0)) && { set -- ""; } >&/dev/null
# set sane defaults, see /etc/default/zram-swap-service for explanations
_zram_fraction="1/2"
@ -13,8 +18,8 @@ _zram_algorithm="lz4"
_comp_factor=''
# load user config
[[ -f /etc/default/zram-swap-service ]] &&
source /etc/default/zram-swap-service
[[ -f /etc/default/zram-swap ]] &&
source /etc/default/zram-swap
# set expected compression ratio based on algorithm; this is a rough estimate
# skip if already set in user config
@ -34,9 +39,6 @@ _main() {
fi
case "$1" in
# a little magic to quietly set -u after testing our last expected empty variable
# matches every case, runs set -u without logging to the trace and then continues matching
*) { set -u; } 2>/dev/null ;;&
"init" | "start")
if grep -q zram /proc/swaps; then
err "zram swap already in use, exiting"
@ -52,8 +54,8 @@ _main() {
_end
;;
*)
echo "Usage: $0 (init|end)"
return 1
_usage
exit 1
;;
esac
}
@ -89,7 +91,7 @@ _init() {
# end swapping and cleanup
_end() {
local ret="0"
DEVICES=$(grep zram /proc/swaps | awk '{print $1}')
DEVICES=$(awk '/zram/ {print $1}' /proc/swaps)
for d in $DEVICES; do
swapoff "$d"
if ! _rem_zdev "$d"; then
@ -121,5 +123,6 @@ _rem_zdev() {
calc() { awk "BEGIN{print $*}"; }
#crapout() { echo "$@" >&2 && exit 1; }
err() { echo "Err '${FUNCNAME[1]}': $*" >&2; }
_usage() { echo "Usage: $(basename "$0") (init|end)"; }
_main "$@"