RaspberryPiでZramを使う
History
#Raspberrypi #Linux #Zram

ラスパイでZramを有効にするメモ

Raspberry Pi OS Lite(bullseye)で確認

/usr/bin/zram.shで以下の内容を書き込む

#!/bin/bash

export LANG=C

cores=$(nproc --all)

# disable zram
core=0
while [ $core -lt $cores ]; do
    if [[ -b /dev/zram$core ]]; then
        swapoff /dev/zram$core
    fi
    let core=core+1
done
if [[ -n $(lsmod | grep zram) ]]; then
    rmmod zram
fi
if [[ $1 == stop ]]; then
    exit 0
fi

# disable all
swapoff -a

# enable zram
modprobe zram num_devices=$cores

echo lz4 > /sys/block/zram0/comp_algorithm

totalmem=$(free | grep -e "^Mem:" | awk '{print $2}')
mem=$(( $totalmem * 128 ))

core=0
while [ $core -lt $cores ]; do
    echo $mem > /sys/block/zram$core/disksize
    mkswap /dev/zram$core
    swapon -p 5 /dev/zram$core
    let core=core+1
done

/etc/systemd/system/zram.serviceに以下の内容を書き込む

[Unit]
Description=zram Service

[Service]
Type=simple
ExecStart=/usr/bin/zram.sh

[Install]
WantedBy=multi-user.target

sudo systemctl daemon-reload sudo systemctl enable zram sudo systemctl start zramで有効にする

再起動後も有効になっていればOK

« iptablesのセットアップメモ
Cloudflare Pagesに移行しました »