Skip to content

Commit

Permalink
Minor tweaks to improve code readability
Browse files Browse the repository at this point in the history
Signed-off-by: Ethan Dye <mrtops03@gmail.com>
  • Loading branch information
ecdye committed Nov 30, 2021
1 parent 793276c commit e1ccec7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 44 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion install.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
78 changes: 36 additions & 42 deletions zram-config
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
"#"*)
Expand All @@ -293,23 +291,19 @@ 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"
;;

*)
echo "Usage: zram-config {start|stop}" > 2
exit 1
;;
esac
if [[ -z $SERVICE ]]; then
serviceConfiguration "start"
fi
[[ -z $SERVICE ]] && serviceConfiguration "start"

0 comments on commit e1ccec7

Please sign in to comment.