From bca632d4f61331d4a5c27d78ea8751542be88cd0 Mon Sep 17 00:00:00 2001 From: Clayton Craft Date: Sun, 18 Dec 2022 11:26:09 -0800 Subject: [PATCH] postmarketos-base: enable using zram swap by default (MR 3752) This installs zram-init and sets it to start on boot for all devices/UIs. The included conf.d/zram-init also allows diabling using zram swap or overriding the size by using a deviceinfo var. I did an analysis of the pmos base install size with the 'none' UI selected, after depending on zram-init, and the following new package is installed: zram-init-11.1-r1 installed size: 40 KiB These packages are dependencies of zram-init, however they are already installed in the base image (with 'none' UI) so they are not counted above: util-linux-misc-2.38.1-r0 installed size: 6816 KiB e2fsprogs-extra-1.46.5-r4 installed size: 1324 KiB So this seems like a very small price to pay for the benefit of not making the logic/implementation more complicated than this. * Starting busybox syslog ... [ ok ] ssh-keygen: generating new host keys: RSA ECDSA ED25519 * Starting sshd ... [ ok ] zram swap: activating with size: 243 MB * Loading zram module... [ ok ] * Swap->zram0 [ ok ] * Starting local ... [ ok ] This change introduces one new deviceinfo variable: deviceinfo_zram_swap_pct: percentage of RAM to use for zram swap Default percentages if the second var is unset are explained in the zram-init file this commit adds. A value of 0 disables zram swap. fixes #1133 --- main/postmarketos-base/APKBUILD | 6 +- .../postmarketos-base.post-install | 2 +- .../rootfs-etc-conf.d-zram-init | 55 +++++++++++++++++++ 3 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 main/postmarketos-base/rootfs-etc-conf.d-zram-init diff --git a/main/postmarketos-base/APKBUILD b/main/postmarketos-base/APKBUILD index 8636bb066..3435ecd86 100644 --- a/main/postmarketos-base/APKBUILD +++ b/main/postmarketos-base/APKBUILD @@ -1,5 +1,5 @@ pkgname=postmarketos-base -pkgver=24.1 +pkgver=25 pkgrel=0 pkgdesc="Meta package for minimal postmarketOS base" url="https://postmarketos.org" @@ -13,6 +13,7 @@ depends=" postmarketos-mvcfg postmarketos-keys cmd:sudo + zram-init " install="$pkgname.post-install $pkgname.pre-upgrade $pkgname.post-upgrade" triggers="$pkgname.trigger=/etc" @@ -33,6 +34,7 @@ replaces=" busybox-openrc openrc sudo + zram-init-openrc " replaces_priority=100 # leave plenty for alpine @@ -43,6 +45,7 @@ _source440=" _source644=" etc/conf.d/bootmisc etc/conf.d/swapfile + etc/conf.d/zram-init etc/conf.d/syslog etc/fstab etc/issue @@ -122,6 +125,7 @@ e529f5cef1f31481b577f99b8917704f2cfefb963d98bf40a14b017938e55a00134d2033f81d2cb0 9591c9cb2194fa1092505159c9eb3d899bec95b6dbd9a0222be62098db012da113a125e40d70373d60af9880dfa489bdbe12b6b897bdef5613e023800fb6ee1b rootfs-etc-doas.d-10-postmarketos.conf a5c6bfaf6ee41f9623f463b64dab95924453d7b70bdd114cc441be9fc68fb6b0e042ae3163ec357d0dd6db93767232224b8c03df29f2cadf3ac2c6e2c998784f rootfs-etc-conf.d-bootmisc e0d2d48b82a03239a4c0a00acaf83e00d397c23a8d7c71053d4e2a383357c22dcedef9e81b0e12a1d7514e1fdbe0bb3eb82613d18b29034a7ce5447f13c84a53 rootfs-etc-conf.d-swapfile +488440c14ff7a662a6a0e5cad9caed1a972f82d2361b4985a68c3f05fab0a3fe9c58887907e66f683c8f8b6c12ba7e17cafdec1aa72cd9055190bd9f7a14c888 rootfs-etc-conf.d-zram-init e4576c58c35f80bedddb1e89e186f37d31a186d3e9eb046581b8c5d7b7d435e18924539e851d3e67dc0ede80f9d44d16bd9ef52e73350d3f13224edc31d73a34 rootfs-etc-conf.d-syslog 9b8d0493bb64457fe176fea801e0771d3c5279302c61559824bf81b3d2b66d2c1e076f4aaac65f55389005acb18c27e44bed858c2bdbad37d74199f07c86c354 rootfs-etc-fstab 45bd0742a64a9d3c4a88e152b97edcf3fa1edca28884f9ea69e7c4c365f1e41ef9056dbe204545de7d4b2ba92e1e5872b2a929c2dcc1dd468e627cc3f090b8e6 rootfs-etc-issue diff --git a/main/postmarketos-base/postmarketos-base.post-install b/main/postmarketos-base/postmarketos-base.post-install index ae448f3a3..40fe21a28 100644 --- a/main/postmarketos-base/postmarketos-base.post-install +++ b/main/postmarketos-base/postmarketos-base.post-install @@ -13,7 +13,7 @@ done for service in hwclock modules sysctl hostname bootmisc syslog; do rc-update -q add $service boot done -for service in sshd swapfile local; do +for service in sshd swapfile local zram-init; do rc-update -q add $service default done for service in mount-ro killprocs savecache; do diff --git a/main/postmarketos-base/rootfs-etc-conf.d-zram-init b/main/postmarketos-base/rootfs-etc-conf.d-zram-init new file mode 100644 index 000000000..4fbd187c2 --- /dev/null +++ b/main/postmarketos-base/rootfs-etc-conf.d-zram-init @@ -0,0 +1,55 @@ +. /etc/deviceinfo + +# Size defaults to: +# - 75% of RAM for devices with 2GB of RAM or less +# - 50% of RAM for devices with between 2GB and 6GB of RAM +# - 25% of RAM for devices with greater 6GB of RAM +# This is loosely based on some data here: +# https://gitlab.com/postmarketOS/pmaports/-/merge_requests/3752#note_1229631449 + +_mem_size_mb="$(LC_ALL=C free -m | awk '/^Mem:/{print $2}')" + +if [ $_mem_size_mb -le 2048 ]; then + _size_pct=75 +elif [ $_mem_size_mb -gt 6144 ]; then + _size_pct=25 +else + _size_pct=50 +fi + +# Allow overriding the size as a percentage of RAM using a deviceinfo var, or +# disabling it completely +if [ -n "$deviceinfo_zram_swap_pct" ]; then + + # 0% --> disable zram swap + if [ "$deviceinfo_zram_swap_pct" == "0" ]; then + echo "zram swap: disabled in deviceinfo, exiting" + exit 0 + fi + + _size_pct="$deviceinfo_zram_swap_pct" +fi + +size0=$(($_mem_size_mb * $_size_pct / 100)) + +echo "zram swap: activating with size: $size0 MB" +# zstd (since linux-4.18), lz4 (since linux-3.15), or lzo. +# Size: zstd (best) > lzo > lz4. Speed: lz4 (best) > zstd > lzo +algo0=zstd + +# The following are defaults from the zram-init package: +load_on_start=yes +unload_on_stop=yes +num_devices=1 +type0=swap +flag0= # The default "16383" is fine for us +mlim0= # no hard memory limit +back0= # no backup device +icmp0= # no incompressible page writing to backup device +idle0= # no idle page writing to backup device +wlim0= # no writeback_limit for idle page writing for backup device +notr0= # keep the default on linux-3.15 or newer +maxs0=1 # maximum number of parallel processes for this device +labl0=zram_swap # the label name +uuid0= # Do not force UUID +args0= # we could e.g. have set args0="-L 'zram_swap'" instead of using labl0