#1051761 zram-tools: I get error during boot process that zramswap failed to start.

#1051761#5
Date:
2023-09-12 09:04:20 UTC
From:
To:
Dear Maintainer,

*** Reporter, please consider answering these questions, where appropriate ***

   * What led up to the situation?
        It started after I installed the zramtools package. I get two errors
one is related to not being able to reserve MMIO and the other relates to zram
swap service not starting.
   * What exactly did you do (or not do) that was effective (or
     ineffective)?
        I googled the problem, found some blogs that showed me how to configure
zramswap but that made the situation worse.
   * What was the outcome of this action?
        The situation became worse with too many errors coming up as regards
zramswap.
   * What outcome did you expect instead?
        I expect the syatem to boot cleanly without any errors. Although
zramswap apparently works when I see the system monitor.

*** End of the template - remove these template lines ***

#1051761#10
Date:
2025-02-20 23:33:28 UTC
From:
To:
I found that the /usr/sbin/zramswap script is statically configure to always assume zram0, which conflicts with any other package which uses zram as well. I was able to solve this issue by modifying the script to follow the same dynamic assignment used by log2ram instead of statically assuming zram0 is always available:

#!/bin/bash

# This script does the following:
# zramswap start:
#  Space is assigned to the zram device, then swap is initialized and enabled.
# zramswap stop:
#  Somewhat potentially dangerous, removes zram module at the end

# https://github.com/torvalds/linux/blob/master/Documentation/blockdev/zram.txt

readonly CONFIG="/etc/default/zramswap"
#readonly SWAP_DEV="/dev/zram0"

if command -v logger >/dev/null; then
    function elog {
        logger -s "Error: $*"
        exit 1
    }

    function wlog {
        logger -s "$*"
    }
else
    function elog {
        echo "Error: $*"
        exit 1
    }

    function wlog {
        echo "$*"
    }
fi

function start {
    wlog "Starting Zram"

    # Load config
    test -r "${CONFIG}" || wlog "Cannot read config from ${CONFIG} continuing with defaults."
    source "${CONFIG}" 2>/dev/null

    # Set defaults if not specified
    : "${ALGO:=lz4}" "${SIZE:=256}" "${PRIORITY:=100}"

    SIZE=$((SIZE * 1024 * 1024)) # convert amount from MiB to bytes

    # Prefer percent if it is set
    if [ -n "${PERCENT}" ]; then
        readonly TOTAL_MEMORY=$(awk '/MemTotal/{print $2}' /proc/meminfo) # in KiB
        readonly SIZE="$((TOTAL_MEMORY * 1024 * PERCENT / 100))"
    fi

    # Check Zram Class created
    if [ ! -d "/sys/class/zram-control" ]; then
        modprobe zram || elog "inserting the zram kernel module"
        SWAP_DEV='zram0'
    elif [ -b "$(lsblk --noheadings -o name,mountpoints /dev/zram*|awk '$2 == "[SWAP]" {print $1}')" ]; then
        SWAP_DEV="$(lsblk --noheadings -o name,mountpoints /dev/zram*|awk '$2 == "[SWAP]" {print $1}')"
    else
        SWAP_DEV="zram$(cat /sys/class/zram-control/hot_add)"
    fi
    #modprobe zram || elog "inserting the zram kernel module"
    echo -n "${ALGO}" > /sys/block/${SWAP_DEV}/comp_algorithm || elog "setting compression algo to ${ALGO}"
    echo -n "${SIZE}" > /sys/block/${SWAP_DEV}/disksize || elog "setting zram device size to ${SIZE}"
    mkswap "/dev/${SWAP_DEV}" || elog "initialising swap device"
    swapon -p "${PRIORITY}" "/dev/${SWAP_DEV}" || elog "enabling swap device"
}

function status {
    test -x "$(which zramctl)" || elog "install zramctl for this feature"
    SWAP_DEV="/dev/$(lsblk -o name,mountpoints|awk '$2 == "[SWAP]" {print $1}')"
    test -b "${SWAP_DEV}" || elog "${SWAP_DEV} doesn't exist"
    # old zramctl doesn't have --output-all
    #zramctl --output-all
    zramctl "${SWAP_DEV}"
}

function stop {
    wlog "Stopping Zram"
    SWAP_DEV="/dev/$(lsblk -o name,mountpoints|awk '$2 == "[SWAP]" {print $1}')"
    test -b "${SWAP_DEV}" || wlog "${SWAP_DEV} doesn't exist"
    swapoff "${SWAP_DEV}" 2>/dev/null || wlog "disabling swap device: ${SWAP_DEV}"
}

function usage {
    cat << EOF

Usage:
    zramswap (start|stop|restart|status)

EOF
}

case "$1" in
    start)      start;;
    stop)       stop;;
    restart)    stop && start;;
    status)     status;;
    "")         usage;;
    *)          elog "Unknown option $1";;
esac