文書の過去の版を表示しています。
DietPi で ZRAM スワップを設定する方法
dietpi-set_swapfile 編
ZRAM (428MB) + Swap (3072MB) を設定する方法
ZRAM スワップを 428MB に設定する
free コマンドで RAM サイズを調べる🤔
$ free -m
total used free shared buff/cache available Mem: 429 109 20 233 299 37 Swap: 0 0 0
現在の dietpi-set_swapfile では ZRAM のサイズは RAM サイズ (429MB) -1 の 428MB まで設定できる😉
$ sudo /boot/dietpi/func/dietpi-set_swapfile 428 zram
DietPi-Set_swapfile ───────────────────────────────────────────────────── Mode: Applying 428 zram [ INFO ] DietPi-Set_swapfile | Disabling and deleting all existing swap files [ OK ] DietPi-Set_swapfile | swapoff -a [ OK ] DietPi-Set_swapfile | Generating new swap space [ INFO ] DietPi-Set_swapfile | Size = 428 MiB [ INFO ] DietPi-Set_swapfile | Path = /dev/zram0 [ OK ] DietPi-Set_swapfile | eval echo 1 > /sys/block/zram0/reset [ OK ] DietPi-Set_swapfile | eval echo '428M' > /sys/block/zram0/disksize [ OK ] DietPi-Set_swapfile | chmod 0600 /dev/zram0 [ OK ] DietPi-Set_swapfile | mkswap /dev/zram0 [ OK ] DietPi-Set_swapfile | swapon /dev/zram0 [ OK ] DietPi-Set_swapfile | eval echo 'zram' > /etc/modules-load.d/dietpi-zram-swap.conf [ OK ] DietPi-Set_swapfile | eval echo 'SUBSYSTEM=="block", KERNEL=="zram0", ACTION=="add", ATTR{disksize}="428M", RUN+="chmod 0600 /dev/zram0", RUN+="/sbin/mkswap /dev/zram0", RUN+="/sbin/swapon /dev/zram0"' > /etc/udev/rules.d/98-dietpi-zram-swap.rules [ OK ] DietPi-Set_swapfile | eval echo 'swappiness=50' > /etc/sysctl.d/98-dietpi-zram-swap.conf [ OK ] DietPi-Set_swapfile | Setting in /boot/dietpi.txt adjusted: AUTO_SETUP_SWAPFILE_SIZE=428 [ OK ] DietPi-Set_swapfile | Desired setting in /boot/dietpi.txt was already set: AUTO_SETUP_SWAPFILE_LOCATION=zram [ INFO ] DietPi-Set_swapfile | Setting /tmp tmpfs size: 428 MiB [ OK ] DietPi-Set_swapfile | mount -o remount /tmp
ZRAM の他にさらに Swap を 3072MB 追加する
$ sudo fallocate -l 3072M /var/swap $ sudo chmod 0600 /var/swap
Setting up swapspace version 1, size = 3 GiB (3221221376 bytes) no label, UUID=9239781e-559a-47a4-b17c-f9f359004181
$ sudo swapon /var/swap $ sudo swapon -s
Filename Type Size Used Priority /dev/zram0 partition 438268 56152 -2 /var/swap file 3145724 0 -3
$ free -m
total used free shared buff/cache available Mem: 429 141 43 192 244 46 Swap: 3499 55 3444
$ echo '/var/swap none swap sw' | sudo tee -a /etc/fstab
/var/swap none swap sw
dietpi-set_swapfile の動作...
通常のスワップファイルを設定する場合
$ sudo /boot/dietpi/func/dietpi-set_swapfile 1 /var/swap
DietPi-Set_swapfile ───────────────────────────────────────────────────── Mode: Applying 1 /var/swap [ INFO ] DietPi-Set_swapfile | Disabling and deleting all existing swap files [ OK ] DietPi-Set_swapfile | swapoff -a removed '/etc/modules-load.d/dietpi-zram-swap.conf' removed '/etc/udev/rules.d/98-dietpi-zram-swap.rules' [ OK ] DietPi-Set_swapfile | Free space check: path=/var/ | available=104886 MiB | required=1619 MiB [ OK ] DietPi-Set_swapfile | Generating new swap space [ INFO ] DietPi-Set_swapfile | Size = 1619 MiB [ INFO ] DietPi-Set_swapfile | Path = /var/swap [ OK ] DietPi-Set_swapfile | fallocate -l 1619M /var/swap [ OK ] DietPi-Set_swapfile | mkswap /var/swap [ OK ] DietPi-Set_swapfile | swapon /var/swap [ OK ] DietPi-Set_swapfile | chmod 0600 /var/swap [ OK ] DietPi-Set_swapfile | eval echo '/var/swap none swap sw' >> /etc/fstab [ OK ] DietPi-Set_swapfile | Setting in /boot/dietpi.txt adjusted: AUTO_SETUP_SWAPFILE_SIZE=1619 [ OK ] DietPi-Set_swapfile | Setting in /boot/dietpi.txt adjusted: AUTO_SETUP_SWAPFILE_LOCATION=/var/swap [ INFO ] DietPi-Set_swapfile | Setting /tmp tmpfs size: 1024 MiB [ OK ] DietPi-Set_swapfile | mount -o remount /tmp
通常のスワップファイルを 3GB に設定する場合
$ sudo /boot/dietpi/func/dietpi-set_swapfile 3072 /var/swap
DietPi-Set_swapfile ───────────────────────────────────────────────────── Mode: Applying 3072 /var/swap [ INFO ] DietPi-Set_swapfile | Disabling and deleting all existing swap files [ OK ] DietPi-Set_swapfile | swapoff -a removed '/var/swap' [ OK ] DietPi-Set_swapfile | Free space check: path=/var/ | available=104886 MiB | required=3072 MiB [ OK ] DietPi-Set_swapfile | Generating new swap space [ INFO ] DietPi-Set_swapfile | Size = 3072 MiB [ INFO ] DietPi-Set_swapfile | Path = /var/swap [ OK ] DietPi-Set_swapfile | fallocate -l 3072M /var/swap [ OK ] DietPi-Set_swapfile | mkswap /var/swap [ OK ] DietPi-Set_swapfile | swapon /var/swap [ OK ] DietPi-Set_swapfile | chmod 0600 /var/swap [ OK ] DietPi-Set_swapfile | eval echo '/var/swap none swap sw' >> /etc/fstab [ OK ] DietPi-Set_swapfile | Setting in /boot/dietpi.txt adjusted: AUTO_SETUP_SWAPFILE_SIZE=3072 [ OK ] DietPi-Set_swapfile | Desired setting in /boot/dietpi.txt was already set: AUTO_SETUP_SWAPFILE_LOCATION=/var/swap [ INFO ] DietPi-Set_swapfile | Setting /tmp tmpfs size: 1750 MiB [ OK ] DietPi-Set_swapfile | mount -o remount /tmp
ZRAM スワップを設定する場合
$ sudo /boot/dietpi/func/dietpi-set_swapfile 1 zram
DietPi-Set_swapfile ───────────────────────────────────────────────────── Mode: Applying 1 zram [ INFO ] DietPi-Set_swapfile | Disabling and deleting all existing swap files [ OK ] DietPi-Set_swapfile | swapoff -a removed '/var/swap' [ OK ] DietPi-Set_swapfile | Generating new swap space [ INFO ] DietPi-Set_swapfile | Size = 214 MiB [ INFO ] DietPi-Set_swapfile | Path = /dev/zram0 [ OK ] DietPi-Set_swapfile | eval echo 1 > /sys/block/zram0/reset [ OK ] DietPi-Set_swapfile | eval echo '214M' > /sys/block/zram0/disksize [ OK ] DietPi-Set_swapfile | chmod 0600 /dev/zram0 [ OK ] DietPi-Set_swapfile | mkswap /dev/zram0 [ OK ] DietPi-Set_swapfile | swapon /dev/zram0 [ OK ] DietPi-Set_swapfile | eval echo 'zram' > /etc/modules-load.d/dietpi-zram-swap.conf [ OK ] DietPi-Set_swapfile | eval echo 'SUBSYSTEM=="block", KERNEL=="zram0", ACTION=="add", ATTR{disksize}="214M", RUN+="chmod 0600 /dev/zram0", RUN+="/sbin/mkswap /dev/zram0", RUN+="/sbin/swapon /dev/zram0"' > /etc/udev/rules.d/98-dietpi-zram-swap.rules [ OK ] DietPi-Set_swapfile | eval echo 'swappiness=50' > /etc/sysctl.d/98-dietpi-zram-swap.conf [ OK ] DietPi-Set_swapfile | Setting in /boot/dietpi.txt adjusted: AUTO_SETUP_SWAPFILE_SIZE=214 [ OK ] DietPi-Set_swapfile | Setting in /boot/dietpi.txt adjusted: AUTO_SETUP_SWAPFILE_LOCATION=zram [ INFO ] DietPi-Set_swapfile | Setting /tmp tmpfs size: 321 MiB [ OK ] DietPi-Set_swapfile | mount -o remount /tmp
ZRAM スワップを 428MB に設定する場合
$ sudo /boot/dietpi/func/dietpi-set_swapfile 428 zram
DietPi-Set_swapfile ───────────────────────────────────────────────────── Mode: Applying 428 zram [ INFO ] DietPi-Set_swapfile | Disabling and deleting all existing swap files [ OK ] DietPi-Set_swapfile | swapoff -a [ OK ] DietPi-Set_swapfile | Generating new swap space [ INFO ] DietPi-Set_swapfile | Size = 428 MiB [ INFO ] DietPi-Set_swapfile | Path = /dev/zram0 [ OK ] DietPi-Set_swapfile | eval echo 1 > /sys/block/zram0/reset [ OK ] DietPi-Set_swapfile | eval echo '428M' > /sys/block/zram0/disksize [ OK ] DietPi-Set_swapfile | chmod 0600 /dev/zram0 [ OK ] DietPi-Set_swapfile | mkswap /dev/zram0 [ OK ] DietPi-Set_swapfile | swapon /dev/zram0 [ OK ] DietPi-Set_swapfile | eval echo 'zram' > /etc/modules-load.d/dietpi-zram-swap.conf [ OK ] DietPi-Set_swapfile | eval echo 'SUBSYSTEM=="block", KERNEL=="zram0", ACTION=="add", ATTR{disksize}="428M", RUN+="chmod 0600 /dev/zram0", RUN+="/sbin/mkswap /dev/zram0", RUN+="/sbin/swapon /dev/zram0"' > /etc/udev/rules.d/98-dietpi-zram-swap.rules [ OK ] DietPi-Set_swapfile | eval echo 'swappiness=50' > /etc/sysctl.d/98-dietpi-zram-swap.conf [ OK ] DietPi-Set_swapfile | Setting in /boot/dietpi.txt adjusted: AUTO_SETUP_SWAPFILE_SIZE=428 [ OK ] DietPi-Set_swapfile | Desired setting in /boot/dietpi.txt was already set: AUTO_SETUP_SWAPFILE_LOCATION=zram [ INFO ] DietPi-Set_swapfile | Setting /tmp tmpfs size: 428 MiB [ OK ] DietPi-Set_swapfile | mount -o remount /tmp
ZRAM スワップを 429MB に設定する場合
$ sudo /boot/dietpi/func/dietpi-set_swapfile 429 zram
DietPi-Set_swapfile ───────────────────────────────────────────────────── Mode: Applying 429 zram [ INFO ] DietPi-Set_swapfile | Disabling and deleting all existing swap files [ OK ] DietPi-Set_swapfile | swapoff -a removed '/etc/modules-load.d/dietpi-zram-swap.conf' removed '/etc/udev/rules.d/98-dietpi-zram-swap.rules' [FAILED] DietPi-Set_swapfile | Insufficient RAM size for desired zram-swap size [ INFO ] DietPi-Set_swapfile | Leaving swap space disabled [ OK ] DietPi-Set_swapfile | Setting in /boot/dietpi.txt adjusted: AUTO_SETUP_SWAPFILE_SIZE=0 [ OK ] DietPi-Set_swapfile | Desired setting in /boot/dietpi.txt was already set: AUTO_SETUP_SWAPFILE_LOCATION=zram [ INFO ] DietPi-Set_swapfile | Setting /tmp tmpfs size: 214 MiB [FAILED] DietPi-Set_swapfile | mount -o remount /tmp
free コマンドの RAM サイズ以上を設定出来ない🤔
「Insufficient RAM size for desired zram-swap size」
「必要な zram スワップサイズに対して RAM サイズが不十分です」
と言われる😥確かに RAM は無い😅
# Free spacey, checkey weckey: zram < RAM size elif [[ $SWAP_FS == 'zram' ]] && (( $(free -m | mawk '/^Mem:/{print $2;exit}') <= $SWAP_SIZE )); then Error_Reset 'Insufficient RAM size for desired zram-swap size'
$ free -m total used free shared buff/cache available Mem: 429 109 20 233 299 37 Swap: 0 0 0
$ free -m | mawk '/^Mem:/{print $2;exit}' 429
ZRAM にさらに Swap を 3072MB 追加する
$ sudo fallocate -l 3072M /var/swap $ sudo chmod 0600 /var/swap
Setting up swapspace version 1, size = 3 GiB (3221221376 bytes) no label, UUID=9239781e-559a-47a4-b17c-f9f359004181
$ sudo swapon /var/swap $ sudo swapon -s
Filename Type Size Used Priority /dev/zram0 partition 438268 56152 -2 /var/swap file 3145724 0 -3
$ free -m
total used free shared buff/cache available Mem: 429 141 43 192 244 46 Swap: 3499 55 3444
$ echo '/var/swap none swap sw' | sudo tee -a /etc/fstab
/var/swap none swap sw
再起動すると…😱 zram が優先されない😥
$ sudo reboot $ sudo swapon -s
Filename Type Size Used Priority /var/swap file 3145724 256 -2 /dev/zram0 partition 438268 0 -3
$ sudo nano /etc/udev/rules.d/98-dietpi-zram-swap.rules
修正前🤔
- /etc/udev/rules.d/98-dietpi-zram-swap.rules
SUBSYSTEM=="block", KERNEL=="zram0", ACTION=="add", ATTR{disksize}="428M", RUN+="chmod 0600 /dev/zram0", RUN+="/sbin/mkswap /dev/zram0", RUN+="/sbin/swapon /dev/zram0"
swapon に priority 100 を指定する🤤 (大きい priority が優先される🤔ここでは 100 のzram から優先して使われる😻)
- /etc/udev/rules.d/98-dietpi-zram-swap.rules
SUBSYSTEM=="block", KERNEL=="zram0", ACTION=="add", ATTR{disksize}="428M", RUN+="chmod 0600 /dev/zram0", RUN+="/sbin/mkswap /dev/zram0", RUN+="/sbin/swapon -p 100 /dev/zram0"
再起動すると…😱 zram が優先される🥰
$ sudo reboot $ sudo swapon -s Filename Type Size Used Priority /var/swap file 3145724 0 -2 /dev/zram0 partition 438268 256 100
dietpi-set_swapfile スクリプトの内容
$ cat /boot/dietpi/func/dietpi-set_swapfile
#!/bin/bash { #//////////////////////////////////// # DietPi Function: swap file # #//////////////////////////////////// # Created by Daniel Knight / daniel.knight@dietpi.com / dietpi.com # #//////////////////////////////////// # # Info: # - Location: /boot/dietpi/func/dietpi-set_swapfile # - Allows set the swap file size and path or disable it, based in input arguments, dietpi.txt entry, existing swap file or default, in that priority order. # # Usage: # - <empty> = this will apply the settings stored in dietpi.txt, refresh the current swap file or apply defaults as fallback # - $1 = swap file size [MiB], use "0" to disable or "1" for auto-choice assuring min 2048 MiB total memory, respectively 50% of RAM size when used with "zram" # - $2 = swap file path, or use "zram" to create a zram-swap on /dev/zram0 instead #//////////////////////////////////// # Import DietPi-Globals -------------------------------------------------------------- . /boot/dietpi/func/dietpi-globals readonly G_PROGRAM_NAME='DietPi-Set_swapfile' G_CHECK_ROOT_USER G_INIT # Import DietPi-Globals -------------------------------------------------------------- EXIT_CODE=0 # Check and apply valid input, else dietpi.txt entry, else current swap file, else revert to defaults # - Size if disable_error=1 G_CHECK_VALIDINT "$1"; then SWAP_SIZE=$1 elif ! SWAP_SIZE=$(sed -n '/^[[:blank:]]*AUTO_SETUP_SWAPFILE_SIZE=/{s/^[^=]*=//p;q}' /boot/dietpi.txt) || ! disable_error=1 G_CHECK_VALIDINT "$SWAP_SIZE"; then SWAP_SIZE=$(mawk '$2=="file" {printf "%.0f",$3/1024;exit}' /proc/swaps) && disable_error=1 G_CHECK_VALIDINT "$SWAP_SIZE" || SWAP_SIZE=1 fi # - Path: Store dietpi.txt entry (excluding zram-swap) and active swap files for disabling and removal before creating a new swap space SWAP_PATH=$(sed -n '/^[[:blank:]]*AUTO_SETUP_SWAPFILE_LOCATION=/{s/^[^=]*=//p;q}' /boot/dietpi.txt) read -ra SWAP_FILES_ACTIVE < <(mawk '$2=="file" {print $1}' /proc/swaps) [[ $SWAP_PATH == '/'* && $SWAP_PATH != '/dev/zram0' ]] && SWAP_FILES_ACTIVE+=("$SWAP_PATH") if [[ $2 == '/'* || $2 == 'zram' || $2 == 'zram0' ]]; then SWAP_PATH=$2 elif [[ $SWAP_PATH != '/'* && $SWAP_PATH != 'zram' && $SWAP_PATH != 'zram0' ]]; then SWAP_PATH=$(mawk '$2=="file" {print $1;exit}' /proc/swaps) && [[ $SWAP_PATH == '/'* || $SWAP_PATH == 'zram' || $SWAP_PATH == 'zram0' ]] || SWAP_PATH='/var/swap' fi Update_DietPi_Conf(){ G_CONFIG_INJECT 'AUTO_SETUP_SWAPFILE_SIZE=' "AUTO_SETUP_SWAPFILE_SIZE=$SWAP_SIZE" /boot/dietpi.txt [[ $SWAP_FS == 'zram' ]] && SWAP_PATH='zram' # Revert zram path to input value G_CONFIG_INJECT 'AUTO_SETUP_SWAPFILE_LOCATION=' "AUTO_SETUP_SWAPFILE_LOCATION=$SWAP_PATH" /boot/dietpi.txt } Update_Tmp(){ # Skip if /tmp is not dedicated mount grep -q '[[:blank:]]/tmp[[:blank:]]' /etc/fstab || return 0 # Set /tmp to 50% of RAM+SWAP: https://github.com/MichaIng/DietPi/issues/1027#issuecomment-369373082 local tmp_size=$(( $(free -tm | mawk '/^Total:/{print $2;exit}') / 2 )) G_DIETPI-NOTIFY 2 "Setting /tmp tmpfs size: $tmp_size MiB" # Apply to existing fstab entry sed -i "/[[:blank:]]\/tmp[[:blank:]]/ctmpfs \/tmp tmpfs size=${tmp_size}M,noatime,lazytime,nodev,nosuid,mode=1777" /etc/fstab systemctl daemon-reload # Apply now to existing mount via "remount" to preserve current content and avoid doubled mount [[ $(findmnt -Ufnro FSTYPE -M /tmp) == 'tmpfs' ]] && G_EXEC mount -o remount /tmp } Swap_Disable(){ G_DIETPI-NOTIFY 2 'Disabling and deleting all existing swap files' (( $G_HW_MODEL == 75 )) || grep -zaq '^container=' /proc/1/environ || G_EXEC swapoff -a rm -fv "${SWAP_FILES_ACTIVE[@]}" sed -i '/[[:blank:]]swap[[:blank:]]/d' /etc/fstab # zram-swap [[ -f '/etc/modules-load.d/dietpi-zram-swap.conf' ]] && rm -v /etc/modules-load.d/dietpi-zram-swap.conf [[ -f '/etc/udev/rules.d/98-dietpi-zram-swap.rules' ]] && rm -v /etc/udev/rules.d/98-dietpi-zram-swap.rules } # Create new swappey whappey Swap_Enable(){ G_DIETPI-NOTIFY 0 'Generating new swap space' G_DIETPI-NOTIFY 2 "Size = $SWAP_SIZE MiB" G_DIETPI-NOTIFY 2 "Path = $SWAP_PATH" # zram if [[ $SWAP_FS == 'zram' ]]; then [[ -b $SWAP_PATH ]] || G_EXEC modprobe zram G_EXEC eval 'echo 1 > /sys/block/zram0/reset' G_EXEC eval "echo '${SWAP_SIZE}M' > /sys/block/zram0/disksize" G_EXEC chmod 0600 "$SWAP_PATH" G_EXEC mkswap "$SWAP_PATH" G_EXEC swapon "$SWAP_PATH" # Make persistent, proceed with failure in case of R/O rootfs G_EXEC_NOHALT=1 G_EXEC eval "echo 'zram' > /etc/modules-load.d/dietpi-zram-swap.conf" || return 1 G_EXEC_NOHALT=1 G_EXEC eval "echo 'SUBSYSTEM==\"block\", KERNEL==\"zram0\", ACTION==\"add\", ATTR{disksize}=\"${SWAP_SIZE}M\", RUN+=\"chmod 0600 /dev/zram0\", RUN+=\"/sbin/mkswap /dev/zram0\", RUN+=\"/sbin/swapon /dev/zram0\"' > /etc/udev/rules.d/98-dietpi-zram-swap.rules" || return 1 G_EXEC_NOHALT=1 G_EXEC eval "echo 'swappiness=50' > /etc/sysctl.d/98-dietpi-zram-swap.conf" || return 1 return 0 # Swap file pre-allocation: Try faster and less write-intense fallocate first and dd as fallback elif ! G_EXEC_NOHALT=1 G_EXEC fallocate -l "${SWAP_SIZE}M" "$SWAP_PATH" || ! G_EXEC_NOHALT=1 G_EXEC mkswap "$SWAP_PATH" || ! G_EXEC_NOHALT=1 G_EXEC swapon "$SWAP_PATH"; then G_DIETPI-NOTIFY 2 'Swap file generation via "fallocate" failed, falling back to "dd"...' G_EXEC swapoff -a G_EXEC dd if=/dev/zero of="$SWAP_PATH" bs=1M "count=$SWAP_SIZE" G_EXEC mkswap "$SWAP_PATH" G_EXEC swapon "$SWAP_PATH" fi # Harden permissions, allow to fail on filesystems without UNIX permission support G_EXEC_NOHALT=1 G_EXEC chmod 0600 "$SWAP_PATH" # Make persistent, proceed with failure in case of R/O rootfs G_EXEC_NOHALT=1 G_EXEC eval "echo '$SWAP_PATH none swap sw' >> /etc/fstab" || return 1 } Error_Reset(){ G_DIETPI-NOTIFY 1 "$*" G_DIETPI-NOTIFY 2 'Leaving swap space disabled' SWAP_SIZE=0 EXIT_CODE=1 } #///////////////////////////////////////////////////////////////////////////////////// # Main Loop #///////////////////////////////////////////////////////////////////////////////////// G_DIETPI-NOTIFY 3 "$G_PROGRAM_NAME" "Applying $SWAP_SIZE $SWAP_PATH" # Always reset existing swap files Swap_Disable # zram-swap if [[ $SWAP_PATH == 'zram' || $SWAP_PATH == 'zram0' || $SWAP_PATH == '/dev/zram0' ]]; then (( $SWAP_SIZE == 1 )) && SWAP_SIZE=$(( $(free -m | mawk '/^Mem:/{print $2;exit}') / 2 )) # Auto size SWAP_PATH='/dev/zram0' SWAP_FS='zram' # swap file else # Auto size: Skip if size would be less than 100 MiB, since else on all 2 GiB SBCs there is a non-reasonable ~50 MiB swap file created if (( $SWAP_SIZE == 1 )); then SWAP_SIZE=$(( 2048 - $(free -m | mawk '/^Mem:/{print $2;exit}') )) if (( $SWAP_SIZE > 1 && $SWAP_SIZE < 100 )); then G_DIETPI-NOTIFY 0 "Skipping the auto-creation of a tiny $SWAP_SIZE MiB swap file" G_DIETPI-NOTIFY 0 'Set < 100 MiB values manually to create such small swap files' SWAP_SIZE=0 fi fi SWAP_DIR="${SWAP_PATH%/*}/" SWAP_FS=$(findmnt -Ufnro FSTYPE -T "$SWAP_DIR") fi # Disable if (( $SWAP_SIZE < 1 )); then SWAP_SIZE=0 # Unsupported in container elif (( $G_HW_MODEL == 75 )) || grep -zaq '^container=' /proc/1/environ; then Error_Reset 'Swap space cannot be controlled form within a container.' # Unsupported on Btrfs elif [[ $SWAP_FS == 'btrfs' ]]; then Error_Reset 'Swap files are not supported on Btrfs file system.' # Free spacey, checkey weckey: zram < RAM size elif [[ $SWAP_FS == 'zram' ]] && (( $(free -m | mawk '/^Mem:/{print $2;exit}') <= $SWAP_SIZE )); then Error_Reset 'Insufficient RAM size for desired zram-swap size' elif [[ $SWAP_FS != 'zram' ]] && ! G_CHECK_FREESPACE "$SWAP_DIR" "$SWAP_SIZE"; then Error_Reset 'Insufficient free space for desired swap files size' else Swap_Enable || EXIT_CODE=1 fi Update_DietPi_Conf Update_Tmp #----------------------------------------------------------------------------------- exit "$EXIT_CODE" #----------------------------------------------------------------------------------- }
Zram-swap-config 編
git のインストール
$ sudo apt install git
Reading package lists... Done Building dependency tree... Done Reading state information... Done Suggested packages: git-daemon-run | git-daemon-sysvinit git-doc git-el git-email git-gui gitk gitweb git-cvs git-mediawiki git-svn The following NEW packages will be installed: git 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 4,724 kB of archives. After this operation, 31.1 MB of additional disk space will be used. Get:1 http://ftp.udx.icscoe.jp/Linux/raspbian/raspbian bullseye/main armhf git armhf 1:2.30.2-1 [4,724 kB] Fetched 4,724 kB in 5s (999 kB/s) debconf: delaying package configuration, since apt-utils is not installed Selecting previously unselected package git. (Reading database ... 69783 files and directories currently installed.) Preparing to unpack .../git_1%3a2.30.2-1_armhf.deb ... Unpacking git (1:2.30.2-1) ... Setting up git (1:2.30.2-1) ...
Zram-swap-config のダウンロード
$ git clone --depth 1 https://github.com/StuartIanNaylor/zram-swap-config
Cloning into 'zram-swap-config'... remote: Enumerating objects: 9, done. remote: Counting objects: 100% (9/9), done. remote: Compressing objects: 100% (9/9), done. remote: Total 9 (delta 0), reused 2 (delta 0), pack-reused 0 Receiving objects: 100% (9/9), 17.69 KiB | 646.00 KiB/s, done.
Zram-swap-config のインストール
$ cd zram-swap-config $ sudo ./install.sh
Created symlink /etc/systemd/system/sysinit.target.wants/zram-swap-config.service → /etc/systemd/system/zram-swap-config.service. ##### Reboot to activate zram-swap-config ##### ##### edit /etc/zram-swap-config.conf for options #####
Zram 圧縮アルゴリズムの確認
$ sudo modprobe zram $ cat /sys/block/zram0/comp_algorithm
lzo [lzo-rle] lz4 zstd
Zram-swap-config の設定
$ cat /etc/zram-swap-config.conf
# Configuration file for zram-swap-config (https://github.com/StuartIanNaylor/zram-swap-config) under GNU General Public License v3.0 license. # This configuration file is read by the zram-swap-config service # MEM_FACTOR = Percentage of available ram to allocate to all zram swap devices which is divided equally by swap_devices number MEM_FACTOR=40 # DRIVE_FACTOR = Virtual uncompressed zram drive size = estimate approx alg compression ratio an results and is the % of the Memfactor DRIVE_FACTOR=200 # COMP_ALG lz4 is faster than lzo but some distro's show compile and operation difference and in use lzo depending on binary may be faster COMP_ALG=lzo-rle # SWAP_DEVICES = number of indivial drives sharing memeory provided by MEM_FACTOR each device support multiple streams 1 large drive is generally better SWAP_DEVICES=1 # SWAP_PRI = swap_priority for each drive 75 is a hig order preference and should be well above other swap drives SWAP_PRI=75 # PAGE_CLUSTER default page cluster is 3 which caches fetches in batches of 8 and helps with HDD paging, with zram mem 0 forces single page fetches # This can help reduce latency and increase performance PAGE_CLUSTER=0 # SWAPPINESS default swappiness is 60 but with increased performance of zram swap 80 garners overall performance gain without excessive load # Because zram uses compression load is created and even if minimal at intense load periods such as boot any extra load is unwanted # Unfortunately there is no dynamic load balancing of swappiness as with zram in general operation SWAPINESS=100 will garner performance benefit # If the overall load is reasonable at high load it will cause load to accumulate. SWAPPINESS=90 # If you check my repo there will also be a simple dynamic load branch based SWAPPINESS governor that will get of the hurdle of a static compromise on swappiness
パラメータ | 説明 |
---|---|
MEM_FACTOR | すべての zram スワップデバイスに割り当てるために使用可能な RAM のパーセンテージ。これは、SWAP_DEVICES 番号で均等に除算されます。 |
DRIVE_FACTOR | 仮想非圧縮 zram ドライブサイズ=結果のおおよその alg 圧縮率を見積もり、MEM_FACTOR の % です。 |
COMP_ALG | lz4 は lzo よりも高速ですが、一部のディストリビューションではコンパイルと操作の違いが示され、バイナリによっては lzo の方が高速な場合があります。 |
SWAP_DEVICES | MEM_FACTOR によって提供されるメモリを共有する個々のドライブの数各デバイスは複数のストリームをサポートします 1 つの大きなドライブが一般的に優れています。 |
SWAP_PRI | 各ドライブ 75 の swap_priority は、高位の優先順位であり、他のスワップドライブよりもはるかに高い必要があります。 |
PAGE_CLUSTER | デフォルトのページクラスターは 3 で、フェッチを 8 のバッチでキャッシュし、HDD ページングを支援します。zrammem0 はシングルページフェッチを強制します。 これにより、レイテンシーを削減し、パフォーマンスを向上させることができます。 |
SWAPPINESS | デフォルトの SWAPPINESS は 60 ですが、zram swap 80 のパフォーマンスが向上すると、過度の負荷をかけることなく全体的なパフォーマンスが向上します。 zram は圧縮負荷を使用するため、作成され、ブートなどの激しい負荷期間で最小であっても、余分な負荷は不要です。 残念ながら、一般的な操作で zram を使用する場合のように、SWAPINESS の動的な負荷分散はありません。SWAPINESS=100 はパフォーマンス上の利点をもたらします。 高負荷で全体の負荷が妥当な場合、負荷が蓄積します。 |
Raspberry Pi 3B (メモリー: 1 GB)
- zram-swap-config.conf
MEM_FACTOR=25 DRIVE_FACTOR=300 COMP_ALG=lz4 SWAP_DEVICES=1 SWAP_PRI=75 PAGE_CLUSTER=0 SWAPPINESS=80
Raspberry Pi 4B (メモリー: 1 GB)
- zram-swap-config.conf
MEM_FACTOR=10 DRIVE_FACTOR=300 COMP_ALG=lz4 SWAP_DEVICES=1 SWAP_PRI=75 PAGE_CLUSTER=0 SWAPPINESS=80
Raspberry Pi 4B (メモリー: 2 GB)
- zram-swap-config.conf
MEM_FACTOR=15 DRIVE_FACTOR=300 COMP_ALG=lz4 SWAP_DEVICES=1 SWAP_PRI=75 PAGE_CLUSTER=0 SWAPPINESS=80
zram-swap-config の開始
$ sudo systemctl start zram-swap-config
Zram の動作確認
$ sudo systemctl status zram-swap-config
● zram-swap-config.service - zram-swap-config Loaded: loaded (/etc/systemd/system/zram-swap-config.service; enabled; vendor preset: enabled) Active: active (exited) since Mon 2022-04-18 06:04:20 JST; 14s ago Process: 14634 ExecStart=/usr/local/bin/zram-swap-config start (code=exited, status=0/SUCCESS) Main PID: 14634 (code=exited, status=0/SUCCESS) CPU: 160ms Apr 18 06:04:19 dietpi-001 zram-swap-config[14636]: modprobe: ERROR: could not insert 'zram': Module already in kernel Apr 18 06:04:19 dietpi-001 zram-swap-config[14634]: zram1 1 Apr 18 06:04:20 dietpi-001 zram-swap-config[14645]: Setting up swapspace version 1, size = 129 MiB (135225344 bytes) Apr 18 06:04:20 dietpi-001 zram-swap-config[14645]: no label, UUID=5c52fd2d-6ae3-4e3f-b3fd-484bd1bb8042 Apr 18 06:04:20 dietpi-001 zram-swap-config[14646]: swapon: /dev/zram1: found signature [pagesize=4096, signature=swap] Apr 18 06:04:20 dietpi-001 zram-swap-config[14646]: swapon: /dev/zram1: pagesize=4096, swapsize=135229440, devsize=1352> Apr 18 06:04:20 dietpi-001 zram-swap-config[14646]: swapon /dev/zram1 Apr 18 06:04:20 dietpi-001 zram-swap-config[14647]: vm.page-cluster = 0 Apr 18 06:04:20 dietpi-001 zram-swap-config[14648]: vm.swappiness = 80 Apr 18 06:04:20 dietpi-001 systemd[1]: Finished zram-swap-config.
$ sudo zramctl
NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT /dev/zram1 lz4 129M 4K 63B 4K 1 [SWAP]
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT mmcblk0 179:0 0 116.4G 0 disk ├─mmcblk0p1 179:1 0 128M 0 part /boot └─mmcblk0p2 179:2 0 116.2G 0 part / zram1 254:1 0 129M 0 disk [SWAP]
$ sudo swapon --show
NAME TYPE SIZE USED PRIO /var/swap file 1.6G 1.3M -2 /dev/zram1 partition 129M 0B 75
参考文献
How to Squeeze 50% More Memory Out of Your Raspberry Pi with zram
StuartIanNaylor/zram-swap-config: Replacement for broken zram-config-0.5 package
teeコマンドの使い方 - Qiita (swappiness の設定)
【 tee 】コマンド――標準出力とファイルの両方に出力する
Ubuntu 20.04にスワップ領域を追加する方法 | DigitalOcean
1GBのダミーファイルを作成するコマンドは?
すぐ忘れてしまう、仕事で使う技 - Qiita
How to make zram swap persistent with higher priority? - Q&A - Clear Linux OS Forum
How permanently change Linux swap disk priority
linux スワップ(swap)領域の作成
swapon - システム管理コマンドの説明 - Linux コマンド集 一覧表