vaeth / zram-init

A wrapper script for the zram linux kernel module with zsh and openrc support
http://www.mathematik.uni-wuerzburg.de/~vaeth/download/index.html#zram-init
79 stars 26 forks source link

mlim option uses bytes instead of MB, results in service startup failure #32

Closed zpuskas closed 3 years ago

zpuskas commented 3 years ago

Problem

Help text in example configuration /etc/conf.d/zram-init says:

# mlim0, mlim1, ... are the variables for the maximal uncompressed memory (MB)

Using this option as per the help text results in service startup failure, as the filesystem creation fails on the ZRAM device. Any value written to this parameter will be interpreted in bytes, with a 8kB memory limit (minimum amount apparently) for any value less than 8192 (e.g. 6144 is supposed to be 6GB, but it's understood as 6kB, which is rounded up to the minimum of 8kB), and the actual kB amount above that.

File system creation fails, because we immediately fill up the tiny memory limit, regardless of the disk size specified for the ZRAM device.

Example failure when starting the zram-init service with ext2 filesystem:

# rc-service zram-init start
 * Caching service dependencies ...                                                                                                                          [ ok ]
 * Loading zram module...                                                                                                                                    [ ok ]
 * Mount zram0 /var/tmp
mke2fs 1.46.1 (9-Feb-2021)
mkfs.ext2: Input/output error while writing out and closing file system
zram-init:
mkfs.ext2 /dev/zram0 failed

Example failure when starting the zram-init service with btrfs filesystem:

# rc-service zram-init start
 * Caching service dependencies ...                                                                                                                          [ ok ]
 * Loading zram module...                                                                                                                                    [ ok ]
 * Mount zram0 /var/tmp
No valid Btrfs found on /dev/zram0
ERROR: open ctree failed
zram-init:
mkfs.btrfs /dev/zram0 failed

Tried filesystem creation manually for more verbose error messages:

# mkfs.ext2 /dev/zram0
mke2fs 1.46.1 (9-Feb-2021)
Discarding device blocks: done
Creating filesystem with 2097152 4k blocks and 524288 inodes
Filesystem UUID: dec334c6-b337-4b60-9187-5a1acff26bda
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Allocating group tables: done
Writing inode tables: done
Writing superblocks and filesystem accounting information: mkfs.ext2: Input/output error while writing out and closing file system

# mkfs.btrfs -f /dev/zram0
btrfs-progs v5.10.1
See http://btrfs.wiki.kernel.org for more information.

Detected a SSD, turning off metadata duplication.  Mkfs with -m dup if you want to force metadata duplication.
No valid Btrfs found on /dev/zram0
ERROR: open ctree failed

Rootcause

This setting expects only numbers as per zram-init.in:L262, and will not take sizes with unit suffixes, e.g. 1024M, or 4G. The number set for memory limit is actually in bytes not in megabytes because the raw number is passed to SysCtl function zram-init.in:L388, which simply writes it into the /sys file system on zram-init.in:L68.

Proposed solution

Since this option is most likely used as a safety feature (e.g. preventing ZRAM to consume all memory for larger filesystems), the solution should be backward compatible with existing configurations, where people set the value in bytes.

I propose to allow passing in numbers with unit suffixes in mlim option, e.g. 512M or 4G, since the ZRAM /sys filesystem supports it. E.g.:

echo 4G > /sys/block/zram0/mem_limit

vaeth commented 3 years ago

Thank you for the report. However, for consistency with the size (discsize) option, I decided to not use units but instead to fix the bug and make it work with megabytes as it was documented. I am aware that this might break existing configs without a warning, but I prefer to have interface consistency. This is fixed in zram-10.10.