draft posix compliance; install.sh: added uninstall routine

This commit is contained in:
Scott B 2020-01-11 22:07:43 -08:00
parent 69f77e7e91
commit bfec2c31de
2 changed files with 143 additions and 46 deletions

View file

@ -1,21 +1,115 @@
#!/usr/bin/env bash
#!/bin/sh
[[ "$EUID" == 0 ]] || { echo "This script requires root." && exit 1; }
set -ex
#[ "$(id -u)" -eq '0' ] || { echo "This script requires root." && exit 1; }
case "$(readlink /proc/$$/exe)" in */bash) set -euo pipefail ;; *) set -eu ;; esac
if systemctl -q is-active zram-swap.service; then
systemctl stop zram-swap.service
fi
# ensure a predictable environment
export PATH=/usr/sbin:/usr/bin:/sbin:/bin
\unalias -a
install -o root zram-swap.sh /usr/local/sbin/zram-swap.sh
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
fi
# TODO detect need to update default/zram-swap and notify user; don't just blind overwrite
install -o root -m 0644 -b service/zram-swap.config /etc/default/zram-swap
install -o root -m 0644 service/zram-swap.service /etc/systemd/system/zram-swap.service
# installer main body:
_main() {
# ensure $1 exists so 'set -u' doesn't error out
[ "$#" -eq "0" ] && { set -- ""; } > /dev/null 2>&1
systemctl daemon-reload
systemctl enable zram-swap.service
systemctl start zram-swap.service
case "$1" in
"--uninstall")
# uninstall, requires root
assert_root
_uninstall
;;
"--install" | "")
# install dpkg hooks, requires root
assert_root
_install "$@"
;;
*)
# unknown flags, print usage and exit
_usage
;;
esac
exit 0
}
_install() {
set -x
oldconfig=''
configdiff=''
if systemctl -q is-active zram-swap.service; then
echo "Stopping zram-swap service"
systemctl stop zram-swap.service
fi
echo "Installing script and service"
install -o root zram-swap.sh /usr/local/sbin/zram-swap.sh
install -o root -m 0644 service/zram-swap.service /etc/systemd/system/zram-swap.service
# rename & cleanup old version config file
if [ -f /etc/default/zram-swap-service ]; then
echo "Old config found, moving to new path"
mv -f /etc/default/zram-swap-service /etc/default/zram-swap
chown root:root /etc/default/zram-swap
chmod 0644 /etc/default/zram-swap
fi
# TODO this really needs work... {{{
if [ -f /etc/default/zram-swap ]; then
#{ set +e; } >/dev/null 2>&1
#diff /etc/default/zram-swap service/zram-swap.config > /dev/null 2>&1
#[ "$?" -gt 0 ] && oldconfig='y'
#{ set -e; } >/dev/null 2>&1
{
set +e
# TODO only run diff once
#configdiff=$(diff /etc/default/zram-swap service/zram-swap.config)
diff /etc/default/zram-swap service/zram-swap.config
[ "$?" -gt 0 ] && oldconfig='y'
set -e
} > /dev/null 2>&1
fi
install -o root -m 0644 -b service/zram-swap.config /etc/default/zram-swap
echo "Reloading systemd unit files and enabling boot-time service"
systemctl daemon-reload
systemctl enable zram-swap.service
if [ -n "$oldconfig" ]; then
cat <<- HEREDOC
Configuration file updated; old config saved as /etc/default/zram-swap~
diff follows:
$(diff /etc/default/zram-swap~ /etc/default/zram-swap || true)
Make any desired changes to the new config and then start the service with
systemctl start zram-swap.service
HEREDOC
else
systemctl start zram-swap.service
fi
#}}}
}
_uninstall() {
set -x
if systemctl -q is-active zram-swap.service; then
echo "Stopping zram-swap service"
systemctl stop zram-swap.service
fi
echo "Uninstalling script and systemd service."
if [ -f /etc/systemd/system/zram-swap.service ]; then
systemctl disable zram-swap.service || true
rm -f /etc/systemd/system/zram-swap.service
fi
if [ -f /usr/local/sbin/zram-swap.sh ]; then
rm -f /usr/local/sbin/zram-swap.sh
fi
echo "Reloading systemd unit files"
systemctl daemon-reload
echo "zram-swap service uninstalled; remove configuration /etc/default/zram-swap if desired"
}
assert_root() { [ "$(id -u)" -eq '0' ] || { echo "This action requires root." && exit 1; }; }
_usage() { echo "Usage: $(basename "$0") (--install|--uninstall)"; }
_main "$@"

View file

@ -1,30 +1,31 @@
#!/bin/bash
#!/bin/sh
# source: https://github.com/foundObjects/zram-swap
[[ "$EUID" == "0" ]] || { echo "This script requires root." && exit 1; }
set -euo pipefail
[ "$(id -u)" -eq '0' ] || { echo "This script requires root." && exit 1; }
case "$(readlink /proc/$$/exe)" in */bash) set -euo pipefail ;; *) set -eu ;; esac
# 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
export PATH=/usr/sbin:/usr/bin:/sbin:/bin
\unalias -a
# set sane defaults, see /etc/default/zram-swap-service for explanations
# parse debug flag early so we can trace user configuration
[ "$#" -gt "0" ] && [ "$1" = "-x" ] && shift && set -x
# make sure $1 exists for 'set -u' so we can get through 'case "$1"' below
{ [ "$#" -eq "0" ] && set -- ""; } > /dev/null 2>&1
# set sane defaults, see /etc/default/zram-swap for explanations
_zram_fraction="1/2"
_zram_algorithm="lz4"
_comp_factor=''
_zram_fixedsize=''
# load user config
[[ -f /etc/default/zram-swap ]] &&
source /etc/default/zram-swap
[ -f /etc/default/zram-swap ] &&
. /etc/default/zram-swap
# set expected compression ratio based on algorithm; this is a rough estimate
# skip if already set in user config
if [[ -z "$_comp_factor" ]]; then
if [ -z "$_comp_factor" ]; then
case $_zram_algorithm in
lzo* | zstd) _comp_factor="3" ;;
lz4) _comp_factor="2.5" ;;
@ -35,21 +36,21 @@ fi
# main script:
_main() {
if ! modprobe zram; then
err "Failed to load zram module, exiting"
err "main: Failed to load zram module, exiting"
return 1
fi
case "$1" in
"init" | "start")
if grep -q zram /proc/swaps; then
err "zram swap already in use, exiting"
err "main: zram swap already in use, exiting"
return 1
fi
_init
;;
"end" | "stop")
if ! grep -q zram /proc/swaps; then
err "no zram swaps to cleanup, exiting"
err "main: no zram swaps to cleanup, exiting"
return 1
fi
_end
@ -63,9 +64,9 @@ _main() {
# initialize swap
_init() {
if [[ -n "$_zram_fixedsize" ]]; then
if ! [[ $_zram_fixedsize =~ ^[[:digit:]]+(\.[[:digit:]]+)?(G|M)$ ]]; then
err "Invalid size '$_zram_fixedsize'. Format sizes like: 100M 250M 1.5G 2G etc."
if [ -n "$_zram_fixedsize" ]; then
if ! _regex_match "$_zram_fixedsize" '^[[:digit:]]+(\.[[:digit:]]+)?(G|M)$'; then
err "init: Invalid size '$_zram_fixedsize'. Format sizes like: 100M 250M 1.5G 2G etc."
exit 1
fi
# Use user supplied zram size
@ -82,10 +83,10 @@ _init() {
for i in $(seq 3); do
sleep "$(calc "0.1 * $i")"
_device=$(zramctl -f -s "$mem" -a "$_zram_algorithm") || true
[[ -b "$_device" ]] && break
[ -b "$_device" ] && break
done
if [[ -b "$_device" ]]; then
if [ -b "$_device" ]; then
# cleanup the device if swap setup fails
trap "_rem_zdev $_device" EXIT
mkswap "$_device"
@ -93,7 +94,7 @@ _init() {
trap - EXIT
return 0
else
err "Failed to initialize zram device"
err "init: Failed to initialize zram device"
return 1
fi
}
@ -105,7 +106,7 @@ _end() {
for d in $DEVICES; do
swapoff "$d"
if ! _rem_zdev "$d"; then
err "Failed to remove zram device $d"
err "end: Failed to remove zram device $d"
ret=1
fi
done
@ -114,17 +115,17 @@ _end() {
# Remove zram device with retry
_rem_zdev() {
if [[ ! -b "$1" ]]; then
err "No zram device '$1' to remove"
if [ ! -b "$1" ]; then
err "rem_zdev: No zram device '$1' to remove"
return 1
fi
for i in $(seq 3); do
sleep "$(calc "0.1 * $i")"
zramctl -r "$1" || true
[[ -b "$1" ]] || break
[ -b "$1" ] || break
done
if [[ -b "$1" ]]; then
err "Couldn't remove zram device '$1' after 3 attempts"
if [ -b "$1" ]; then
err "rem_zdev: Couldn't remove zram device '$1' after 3 attempts"
return 1
fi
return 0
@ -132,7 +133,9 @@ _rem_zdev() {
calc() { awk "BEGIN{print $*}"; }
#crapout() { echo "$@" >&2 && exit 1; }
err() { echo "Err '${FUNCNAME[1]}': $*" >&2; }
err() { echo "Err $*" >&2; }
_usage() { echo "Usage: $(basename "$0") (init|end)"; }
# posix compliant replacement for [[ $foo =~ bar-pattern ]]
_regex_match() { echo "$1" | grep -Eq "$2" > /dev/null 2>&1; }
_main "$@"