両方とも前のリビジョン 前のリビジョン 次のリビジョン 前のリビジョン hardware:raspberry_pi:dietpi:zram_swap [2022/06/16 00:19] – [参考文献] ともやん hardware:raspberry_pi:dietpi:zram_swap [2025/06/02 04:54] (現在) – [ZRAM の他にさらに Swap を 3072MB 追加する] ともやん
====== DietPi で ZRAM スワップを設定する方法 ====== ====== DietPi で ZRAM スワップを設定する方法 ======
===== dietpi-set_swapfile ===== ===== dietpi-set_swapfile 編 =====
==== 通常のスワップファイルを設定する場合 = === ==== ZRAM (476MB) + Swap (3072MB) を設定する方法 ====
=== ZRAM スワップを 476MB に設定する ===
**free** コマンドで RAM サイズを調べる🤔\\
<WRAP color_term>
<WRAP color_command><html><pre>
$ free -m
</pre></html></WRAP>
<WRAP color_result><html><pre>
total used free shared buff/cache available
<b class=GRN>Mem: 477</b> 30 411 1 34 404
Swap: 0 0 0
</pre></html></WRAP>
</WRAP>
現在の **dietpi-set_swapfile** では ZRAM のサイズは RAM サイズ (477MB) -1 の 476MB まで設定できる😉\\
<WRAP color_term>
<WRAP color_command><html><pre>
$ sudo /boot/dietpi/func/dietpi-set_swapfile 476 zram
</pre></html></WRAP>
<WRAP color_result><html><pre>
<b class=DiYE>DietPi-Set_swapfile</b>
<b class=BoHIK>─────────────────────────────────────────────────────</b>
<b class=HIK>Mode:</b> Applying 428 zram
<b class=HIK>[</b> INFO <b class=HIK>] DietPi-Set_swapfile | Disabling and deleting all existing swap files</b>
<b class=HIK>[</b> <b class=GRN>OK</b> <b class=HIK>] DietPi-Set_swapfile |</b> swapoff -a
<b class=HIK>[</b> <b class=GRN>OK</b> <b class=HIK>] DietPi-Set_swapfile |</b> Generating new swap space
<b class=HIK>[</b> INFO <b class=HIK>] DietPi-Set_swapfile | Size = 476 MiB</b>
<b class=HIK>[</b> INFO <b class=HIK>] DietPi-Set_swapfile | Path = /dev/zram0</b>
<b class=HIK>[</b> <b class=GRN>OK</b> <b class=HIK>] DietPi-Set_swapfile |</b> eval echo 1 > /sys/block/zram0/reset
<b class=HIK>[</b> <b class=GRN>OK</b> <b class=HIK>] DietPi-Set_swapfile |</b> eval echo '476M' > /sys/block/zram0/disksize
<b class=HIK>[</b> <b class=GRN>OK</b> <b class=HIK>] DietPi-Set_swapfile |</b> chmod 0600 /dev/zram0
<b class=HIK>[</b> <b class=GRN>OK</b> <b class=HIK>] DietPi-Set_swapfile |</b> mkswap /dev/zram0
<b class=HIK>[</b> <b class=GRN>OK</b> <b class=HIK>] DietPi-Set_swapfile |</b> swapon /dev/zram0
<b class=HIK>[</b> <b class=GRN>OK</b> <b class=HIK>] DietPi-Set_swapfile |</b> eval echo 'zram' > /etc/modules-load.d/dietpi-zram-swap.conf
<b class=HIK>[</b> <b class=GRN>OK</b> <b class=HIK>] DietPi-Set_swapfile |</b> eval echo 'SUBSYSTEM=="block", KERNEL=="zram0", ACTION=="add", ATTR{disksize}="476M", RUN+="chmod 0600 /dev/zram0", RUN+="/sbin/mkswap /dev/zram0", RUN+="/sbin/swapon /dev/zram0"' > /etc/udev/rules.d/98-dietpi-zram-swap.rules
<b class=HIK>[</b> <b class=GRN>OK</b> <b class=HIK>] DietPi-Set_swapfile |</b> eval echo 'swappiness=50' > /etc/sysctl.d/98-dietpi-zram-swap.conf
<b class=HIK>[</b> <b class=GRN>OK</b> <b class=HIK>] DietPi-Set_swapfile |</b> Setting in <b class=YEL>/boot/dietpi.txt</b> adjusted: <b class=YEL>AUTO_SETUP_SWAPFILE_SIZE=476</b>
<b class=HIK>[</b> <b class=GRN>OK</b> <b class=HIK>] DietPi-Set_swapfile |</b> Desired setting in <b class=YEL>/boot/dietpi.txt</b> was already set: <b class=YEL>AUTO_SETUP_SWAPFILE_LOCATION=zram</b>
<b class=HIK>[</b> INFO <b class=HIK>] DietPi-Set_swapfile | Setting /tmp tmpfs size: 476 MiB</b>
<b class=HIK>[</b> <b class=GRN>OK</b> <b class=HIK>] DietPi-Set_swapfile |</b> mount -o remount /tmp
</pre></html></WRAP>
</WRAP>
=== ZRAM の他にさらに Swap を 2048MB 追加する ===
<WRAP color_term>
<WRAP color_command><html><pre>
<font color="#AFFF00"><b>$</b></font> <font color="#26A269"><u style="text-decoration-style:solid">sudo</u></font> <font color="#26A269">fallocate</font> <font color="#A347BA">-l</font> 2048M /var/swap
<font color="#AFFF00"><b>$</b></font> <font color="#26A269"><u style="text-decoration-style:solid">sudo</u></font> <font color="#26A269">chmod</font> 0600 <u style="text-decoration-style:solid">/var/swap</u>
<font color="#AFFF00"><b>$</b></font> <font color="#26A269"><u style="text-decoration-style:solid">sudo</u></font> <font color="#26A269"><b>mkswap</b></font> <u style="text-decoration-style:solid">/var/swap</u>
</pre></html></WRAP>
<WRAP color_result><html><pre>
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=1a4e054d-d8b8-4b3f-8ca3-5143af119936
</pre></html></WRAP>
<WRAP color_command><html><pre>
<font color="#AFFF00"><b>$</b></font> <font color="#26A269"><u style="text-decoration-style:solid">sudo</u></font> <font color="#26A269"><b>swapon</b></font> <font color="#A347BA">-v</font> <u style="text-decoration-style:solid">/var/swap</u>
</pre></html></WRAP>
<WRAP color_result><html><pre>
swapon: /var/swap: found signature [pagesize=4096, signature=swap]
swapon: /var/swap: pagesize=4096, swapsize=2147483648, devsize=2147483648
swapon /var/swap
</pre></html></WRAP>
<WRAP color_command><html><pre>
<font color="#AFFF00"><b>$</b></font> <font color="#26A269"><u style="text-decoration-style:solid">sudo</u></font> <font color="#C01C28"><b>swapon</b></font> <font color="#A347BA">-s</font>
</pre></html></WRAP>
<WRAP color_result><html><pre>
Filename Type Size Used Priority
/dev/zram0 partition 485372 0 -2
/var/swap file 2097148 0 -3
</pre></html></WRAP>
<WRAP color_command><html><pre>
<font color="#AFFF00"><b>$</b></font> <font color="#26A269">free</font> <font color="#A347BA">-m</font>
</pre></html></WRAP>
<WRAP color_result><html><pre>
total used free shared buff/cache available
Mem: 477 29 405 1 42 402
Swap: 2521 0 2521
</pre></html></WRAP>
</WRAP>
<WRAP color_term>
<WRAP color_command><html><pre>
<font color="#AFFF00"><b>$</b></font> <font color="#26A269">echo</font> <font color="#A2734C">'/var/swap none swap sw 0 0'</font> <font color="#12488B"><b>|</b></font> <font color="#26A269"><u style="text-decoration-style:solid">sudo</u></font> <font color="#26A269">tee</font> <font color="#A347BA">-a</font> <u style="text-decoration-style:solid">/etc/fstab</u>
</pre></html></WRAP>
<WRAP color_result><html><pre>
/var/swap none swap sw 0 0
</pre></html></WRAP>
</WRAP>
※[[https://unix.stackexchange.com/questions/365953/what-sw-means-in-the-fstab-swap-entry-for-mount-options-column|What 'sw' means in the fstab swap entry for 'mount options' column]]\\
[[https://forums.ubuntulinux.jp/viewtopic.php?id=20079|Ubuntu日本語フォーラム / swap fileの設定をfstabへの記述するときの設定値の違いについて]]\\
==== dietpi-set_swapfile の動作... ====
=== 通常のスワップファイルを設定する場合 ===
<WRAP color_term> <WRAP color_term>
<WRAP color_command><html><pre> <WRAP color_command><html><pre>
<b class=DiYE>DietPi-Set_swapfile</b> <b class=DiYE>DietPi-Set_swapfile</b>
───────────────────────────────────────────────────── <b class=BoHIK> ─────────────────────────────────────────────────────</b>
Mode: Applying 1 /var/swap <b class=HIK> Mode:</b> Applying 1 /var/swap
<b class=HIK>[</b> INFO <b class=HIK>] DietPi-Set_swapfile | Disabling and deleting all existing swap files</b> <b class=HIK>[</b> INFO <b class=HIK>] DietPi-Set_swapfile | Disabling and deleting all existing swap files</b>
</WRAP> </WRAP>
= === 通常のスワップファイルを 3GB に設定する場合 = ====== 通常のスワップファイルを 3GB に設定する場合 ===
<WRAP color_term> <WRAP color_term>
<WRAP color_command><html><pre> <WRAP color_command><html><pre>
<b class=DiYE>DietPi-Set_swapfile</b> <b class=DiYE>DietPi-Set_swapfile</b>
───────────────────────────────────────────────────── <b class=BoHIK> ─────────────────────────────────────────────────────</b>
Mode: Applying 3072 /var/swap <b class=HIK> Mode:</b> Applying 3072 /var/swap
<b class=HIK>[</b> INFO <b class=HIK>] DietPi-Set_swapfile | Disabling and deleting all existing swap files</b> <b class=HIK>[</b> INFO <b class=HIK>] DietPi-Set_swapfile | Disabling and deleting all existing swap files</b>
</WRAP> </WRAP>
= === ZRAM スワップを設定する場合 = ====== ZRAM スワップを設定する場合 ===
<WRAP color_term> <WRAP color_term>
<WRAP color_command><html><pre> <WRAP color_command><html><pre>
</WRAP> </WRAP>
= === ZRAM スワップを 428MB に設定する場合 = ====== ZRAM スワップを 428MB に設定する場合 ===
<WRAP color_term> <WRAP color_term>
<WRAP color_command><html><pre> <WRAP color_command><html><pre>
</WRAP> </WRAP>
= === ZRAM スワップを 429MB に設定する場合 = ====== ZRAM スワップを 429MB に設定する場合 ===
<WRAP color_term> <WRAP color_term>
<WRAP color_command><html><pre> <WRAP color_command><html><pre>
</WRAP> </WRAP>
==== dietpi-set_swapfile スクリプトの内容 = === === ZRAM にさらに Swap を 3072MB 追加する ===
<WRAP color_term>
<WRAP color_command><html><pre>
$ sudo fallocate -l 3072M /var/swap
$ sudo chmod 0600 /var/swap
$ sudo mkswap /var/swap
</pre></html></WRAP>
<WRAP color_result><html><pre>
Setting up swapspace version 1, size = 3 GiB (3221221376 bytes)
no label, UUID=9239781e-559a-47a4-b17c-f9f359004181
</pre></html></WRAP>
<WRAP color_command><html><pre>
$ sudo swapon /var/swap
$ sudo swapon -s
</pre></html></WRAP>
<WRAP color_result><html><pre>
Filename Type Size Used Priority
/dev/zram0 partition 438268 56152 -2
/var/swap file 3145724 0 -3
</pre></html></WRAP>
<WRAP color_command><html><pre>
$ free -m
</pre></html></WRAP>
<WRAP color_result><html><pre>
total used free shared buff/cache available
Mem: 429 141 43 192 244 46
Swap: 3499 55 3444
</pre></html></WRAP>
</WRAP>
<WRAP color_term>
<WRAP color_command><html><pre>
$ echo '/var/swap none swap sw' | sudo tee -a /etc/fstab
</pre></html></WRAP>
<WRAP color_result><html><pre>
/var/swap none swap sw
</pre></html></WRAP>
</WRAP>
再起動すると...😱 zram が優先されない😥\\
<WRAP color_term>
<WRAP color_command><html><pre>
$ sudo reboot
$ sudo swapon -s
</pre></html></WRAP>
<WRAP color_result><html><pre>
Filename Type Size Used Priority
/var/swap file 3145724 256 -2
/dev/zram0 partition 438268 0 -3
</pre></html></WRAP>
</WRAP>
<WRAP color_term>
<WRAP color_command><html><pre>
$ sudo nano /etc/udev/rules.d/98-dietpi-zram-swap.rules
</pre></html></WRAP>
<WRAP color_result><html><pre>
</pre></html></WRAP>
</WRAP>
<WRAP mincode>
修正前🤔\\
<code autoconf /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"
</code>
**swapon** に priority **100** を指定する🤤 (大きい priority が優先される🤔ここでは 100 の zram0 から優先して使われる😻)\\
<code autoconf /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"
</code>
</WRAP>
再起動すると...😱 zram が優先される🥰\\
<WRAP color_term>
<WRAP color_command><html><pre>
$ sudo reboot
$ sudo swapon -s
Filename Type Size Used Priority
/var/swap file 3145724 0 -2
/dev/zram0 partition 438268 256 100
</pre></html></WRAP>
</WRAP>
=== dietpi-set_swapfile スクリプトの内容 ===
<WRAP color_term> <WRAP color_term>
<WRAP color_command><html><pre> <WRAP color_command><html><pre>
</WRAP> </WRAP>
===== Zram-swap-config ===== ===== Zram-swap-config 編 =====
==== git のインストール ==== ==== git のインストール ====
[[qita>wnoguchi/items/2fc3ec11043d139dc6bb|teeコマンドの使い方 - Qiita (swappiness の設定)]]\\ [[qita>wnoguchi/items/2fc3ec11043d139dc6bb|teeコマンドの使い方 - Qiita (swappiness の設定)]]\\
[[https://atmarkit.itmedia.co.jp/ait/articles/1611/16/news022.html|【 tee 】コマンド――標準出力とファイルの両方に出力する]]\\ [[https://atmarkit.itmedia.co.jp/ait/articles/1611/16/news022.html|【 tee 】コマンド――標準出力とファイルの両方に出力する]]\\
[[https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-20-04-ja|Ubuntu 20.04にスワップ領域を追加する方法 | DigitalOcean]]\\
[[https://ja.stackoverflow.com/questions/21468/1gb%E3%81%AE%E3%83%80%E3%83%9F%E3%83%BC%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%82%92%E4%BD%9C%E6%88%90%E3%81%99%E3%82%8B%E3%82%B3%E3%83%9E%E3%83%B3%E3%83%89%E3%81%AF|1GBのダミーファイルを作成するコマンドは?]]\\
[[https://qiita.com/hana_shin/items/53c3c78525c9c758ae7c|すぐ忘れてしまう、仕事で使う技 - Qiita]]\\
[[https://community.clearlinux.org/t/how-to-make-zram-swap-persistent-with-higher-priority/3641/2|How to make zram swap persistent with higher priority? - Q&A - Clear Linux OS Forum]]\\
[[https://superuser.com/questions/173353/how-permanently-change-linux-swap-disk-priority|How permanently change Linux swap disk priority]]\\
[[https://kazmax.zpp.jp/linux_beginner/mkswap.html|linux スワップ(swap)領域の作成]]\\
[[https://kazmax.zpp.jp/cmd/s/swapon.8.html|swapon - システム管理コマンドの説明 - Linux コマンド集 一覧表]]\\