From e1ccec74f4b250ba38847b7dbf438e4dd27f59a4 Mon Sep 17 00:00:00 2001 From: Ethan Dye Date: Mon, 29 Nov 2021 21:25:28 -0700 Subject: [PATCH] Minor tweaks to improve code readability Signed-off-by: Ethan Dye --- README.md | 2 +- install.bash | 1 - zram-config | 78 ++++++++++++++++++++++++---------------------------- 3 files changed, 37 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index b3dcb54..be09d4b 100644 --- a/README.md +++ b/README.md @@ -164,7 +164,7 @@ NAME TYPE SIZE USED PRIO ``` ``` pi@raspberrypi:/opt/zram $ ls -log.bind magicmirror.bind oldlog zram1 zram2 +log.bind oldlog zram1 zram2 ``` ``` pi@raspberrypi:/opt/zram $ top diff --git a/install.bash b/install.bash index 60d7efd..49431fd 100755 --- a/install.bash +++ b/install.bash @@ -17,7 +17,6 @@ if ! dpkg -s 'gcc' 'make' &> /dev/null; then echo "Installing needed packages (gcc, make)" apt-get install --yes gcc make || exit 1 fi - if [[ "$(grep -o '^ID=.*$' /etc/os-release | cut -d'=' -f2)" == "ubuntu" ]] && [[ $(bc -l <<< "$(grep -o '^VERSION_ID=.*$' /etc/os-release | cut -d'=' -f2 | tr -d '"') >= 21.10") -eq 1 ]]; then echo "Install zram module package for Ubuntu (linux-modules-extra-raspi)" if ! dpkg -s 'linux-modules-extra-raspi' &> /dev/null; then diff --git a/zram-config b/zram-config index db27a3d..40185f7 100755 --- a/zram-config +++ b/zram-config @@ -51,7 +51,7 @@ createZswap() { echo "zram${RAM_DEV} no swappiness" >> "$ZLOG" fi - echo "swap /zram${RAM_DEV} zram-config${RAM_DEV}" >> "${TMPDIR}"/zram-device-list + echo "swap /zram${RAM_DEV} zram-config${RAM_DEV}" >> "$TMPDIR"/zram-device-list } createZdir() { @@ -61,43 +61,41 @@ createZdir() { local dirMountOpt local dirFSType - if [[ -n $BIND_DIR ]]; then - mkdir -p "${ZDIR}${BIND_DIR}" >> "$ZLOG" 2>&1 || return 1 + if [[ -z $BIND_DIR ]]; then + echo "No bind directory provided in '/etc/ztab'. Exiting!" >> "$ZLOG" + return 1 + elif [[ -z $TARGET_DIR ]]; then + echo "No mount directory provided in '/etc/ztab'. Exiting!" >> "$ZLOG" + return 1 + fi - if [[ -n $TARGET_DIR ]]; then - dirPerm="$(stat -c "%a" "$TARGET_DIR")" - dirUser="$(stat -c "%u" "$TARGET_DIR")" - dirGroup="$(stat -c "%g" "$TARGET_DIR")" + mkdir -p "${ZDIR}${BIND_DIR}" >> "$ZLOG" 2>&1 || return 1 - echo "dirPerm ${TARGET_DIR} ${dirPerm} ${dirUser}:${dirGroup}" >> "$ZLOG" + dirPerm="$(stat -c "%a" "$TARGET_DIR")" + dirUser="$(stat -c "%u" "$TARGET_DIR")" + dirGroup="$(stat -c "%g" "$TARGET_DIR")" - mount --verbose --bind "${TARGET_DIR}/" "${ZDIR}${BIND_DIR}/" >> "$ZLOG" 2>&1 || return 1 - mount --verbose --make-private "${ZDIR}${BIND_DIR}/" >> "$ZLOG" 2>&1 || return 1 + echo "dirPerm ${TARGET_DIR} ${dirPerm} ${dirUser}:${dirGroup}" >> "$ZLOG" - dirMountOpt="$(awk -v a="${ZDIR}${BIND_DIR}" '$2 == a {print $4}' /proc/mounts | head -1)" - dirFSType="$(awk -v a="${ZDIR}${BIND_DIR}" '$2 == a {print $3}' /proc/mounts | head -1)" + mount --verbose --bind "${TARGET_DIR}/" "${ZDIR}${BIND_DIR}/" >> "$ZLOG" 2>&1 || return 1 + mount --verbose --make-private "${ZDIR}${BIND_DIR}/" >> "$ZLOG" 2>&1 || return 1 - echo "dirMountOpt: ${dirMountOpt}; dirFsType: ${dirFSType}" >> "$ZLOG" - createZdevice || return 1 + dirMountOpt="$(awk -v a="${ZDIR}${BIND_DIR}" '$2 == a {print $4}' /proc/mounts | head -1)" + dirFSType="$(awk -v a="${ZDIR}${BIND_DIR}" '$2 == a {print $3}' /proc/mounts | head -1)" - mke2fs -v -t "$dirFSType" "/dev/zram${RAM_DEV}" >> "$ZLOG" 2>&1 || return 1 - mkdir -p "${ZDIR}/zram${RAM_DEV}" >> "$ZLOG" 2>&1 || return 1 - mount --verbose --types "$dirFSType" -o "$dirMountOpt" "/dev/zram${RAM_DEV}" "${ZDIR}/zram${RAM_DEV}/" >> "$ZLOG" 2>&1 || return 1 - mkdir -p "${ZDIR}/zram${RAM_DEV}/upper" "${ZDIR}/zram${RAM_DEV}/workdir" "$TARGET_DIR" >> "$ZLOG" 2>&1 || return 1 - mount --verbose --types overlay -o redirect_dir=on,lowerdir="${ZDIR}${BIND_DIR}",upperdir="${ZDIR}/zram${RAM_DEV}/upper",workdir="${ZDIR}/zram${RAM_DEV}/workdir" "overlay${RAM_DEV}" "$TARGET_DIR" >> "$ZLOG" 2>&1 || return 1 + echo "dirMountOpt: ${dirMountOpt}; dirFsType: ${dirFSType}" >> "$ZLOG" + createZdevice || return 1 - chown "${dirUser}:${dirGroup}" "${ZDIR}/zram${RAM_DEV}/upper" "${ZDIR}/zram${RAM_DEV}/workdir" "$TARGET_DIR" >> "$ZLOG" 2>&1 || return 1 - chmod "$dirPerm" "${ZDIR}/zram${RAM_DEV}/upper" "${ZDIR}/zram${RAM_DEV}/workdir" "$TARGET_DIR" >> "$ZLOG" 2>&1 || return 1 + mke2fs -v -t "$dirFSType" "/dev/zram${RAM_DEV}" >> "$ZLOG" 2>&1 || return 1 + mkdir -p "${ZDIR}/zram${RAM_DEV}" >> "$ZLOG" 2>&1 || return 1 + mount --verbose --types "$dirFSType" -o "$dirMountOpt" "/dev/zram${RAM_DEV}" "${ZDIR}/zram${RAM_DEV}/" >> "$ZLOG" 2>&1 || return 1 + mkdir -p "${ZDIR}/zram${RAM_DEV}/upper" "${ZDIR}/zram${RAM_DEV}/workdir" "$TARGET_DIR" >> "$ZLOG" 2>&1 || return 1 + mount --verbose --types overlay -o redirect_dir=on,lowerdir="${ZDIR}${BIND_DIR}",upperdir="${ZDIR}/zram${RAM_DEV}/upper",workdir="${ZDIR}/zram${RAM_DEV}/workdir" "overlay${RAM_DEV}" "$TARGET_DIR" >> "$ZLOG" 2>&1 || return 1 - echo "${ZTYPE} /zram${RAM_DEV} ${TARGET_DIR} ${BIND_DIR}" >> "${TMPDIR}"/zram-device-list - else - echo "No mount directory provided in '/etc/ztab'. Exiting!" >> "$ZLOG" - return 1 - fi - else - echo "No bind directory provided in '/etc/ztab'. Exiting!" >> "$ZLOG" - return 1 - fi + chown "${dirUser}:${dirGroup}" "${ZDIR}/zram${RAM_DEV}/upper" "${ZDIR}/zram${RAM_DEV}/workdir" "$TARGET_DIR" >> "$ZLOG" 2>&1 || return 1 + chmod "$dirPerm" "${ZDIR}/zram${RAM_DEV}/upper" "${ZDIR}/zram${RAM_DEV}/workdir" "$TARGET_DIR" >> "$ZLOG" 2>&1 || return 1 + + echo "${ZTYPE} /zram${RAM_DEV} ${TARGET_DIR} ${BIND_DIR}" >> "$TMPDIR"/zram-device-list if [[ $ZTYPE == "log" ]] && [[ -n $OLDLOG_DIR ]]; then echo -e "olddir ${OLDLOG_DIR}\\ncreateolddir 755 root root\\nrenamecopy" > /etc/logrotate.d/00_oldlog @@ -224,11 +222,11 @@ case "$1" in ALG="$2" MEM_SIZE="$3" DISK_SIZE="$4" - if [[ -f "${TMPDIR}"/zram-device-list ]]; then + if [[ -f "$TMPDIR"/zram-device-list ]]; then if [[ $1 == "swap" ]]; then - entry="$(grep "^swap" "${TMPDIR}"/zram-device-list)" + entry="$(grep "^swap" "$TMPDIR"/zram-device-list)" else - entry="$(grep "${1}.*${5}" "${TMPDIR}"/zram-device-list)" + entry="$(grep "${1}.*${5}" "$TMPDIR"/zram-device-list)" fi if [[ -n $entry ]]; then echo "Entry ${entry} already exists as a zram device, skipping recreation of device." >> "$ZLOG" @@ -266,7 +264,7 @@ case "$1" in systemctl stop zram-config.service & systemctl kill zram-config.service >> "$ZLOG" 2>&1 || return 1 fi echo "zram-config stop $(date +%Y-%m-%d-%H:%M:%S)" | tee -a "$ZLOG" - tac "${TMPDIR}"/zram-device-list > "${TMPDIR}"/zram-device-list.rev + tac "$TMPDIR"/zram-device-list > "$TMPDIR"/zram-device-list.rev while read -r line; do case "$line" in "#"*) @@ -293,16 +291,14 @@ case "$1" in ZRAM_DEV="$2" TARGET_DIR="$3" BIND_DIR="$4" - if [[ -z $SERVICE ]]; then - serviceConfiguration "stop" - fi + [[ -z $SERVICE ]] && serviceConfiguration "stop" removeZdir ;; esac ;; esac - done < "${TMPDIR}"/zram-device-list.rev - rm -fv "${TMPDIR}"/zram-device-list.rev "${TMPDIR}"/zram-device-list >> "$ZLOG" + done < "$TMPDIR"/zram-device-list.rev + rm -fv "$TMPDIR"/zram-device-list.rev "$TMPDIR"/zram-device-list >> "$ZLOG" ;; *) @@ -310,6 +306,4 @@ case "$1" in exit 1 ;; esac -if [[ -z $SERVICE ]]; then - serviceConfiguration "start" -fi +[[ -z $SERVICE ]] && serviceConfiguration "start"