linux:fedora:extend_zram_swap

差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

両方とも前のリビジョン 前のリビジョン
次のリビジョン
前のリビジョン
linux:fedora:extend_zram_swap [2024/02/04 13:37] – 削除 - 外部編集 (Unknown date) 非ログインユーザーlinux:fedora:extend_zram_swap [2025/10/03 05:57] (現在) – [ZRAM スワップの設定変更] ともやん
行 1: 行 1:
 +====== Fedora 33 以降で標準になった ZRAM スワップの容量を拡張する ======
 +
 +[[wpjp>Zram|Zram - Wikipedia]]\\
 +
 +===== ZRAM スワップの構成を確認 ===== 
 +ブロックデバイス zram0 が SWAP としてマウントされている。\\
 +<WRAP color_term>
 +<WRAP color_command><html><pre>
 +<font color="#0087FF"><b>$</b></font> <font color="#26A269">lsblk</font>
 +</pre></html></WRAP>
 +<WRAP color_result><html><pre>
 +NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
 +sr0     11:   1 1024M  0 rom
 +zram0  251:0    0  970M  0 disk [SWAP]
 +vda    252:0    0  100G  0 disk
 +├─vda1 252:1    0    1M  0 part
 +├─vda2 252:2    0   99G  0 part /
 +└─vda3 252:3    0    1G  0 part /boot
 +</pre></html></WRAP>
 +</WRAP>
 +
 +複数のスワップ領域が存在する場合は、swapon で優先順位なども分かります。\\
 +<WRAP color_term>
 +<WRAP color_command><html><pre>
 +<font color="#0087FF"><b>$</b></font> <font color="#26A269">swapon</font> <font color="#A347BA">--show</font>
 +</pre></html></WRAP>
 +<WRAP color_result><html><pre>
 +NAME       TYPE      SIZE USED PRIO
 +/dev/zram0 partition 970M 174M  100
 +</pre></html></WRAP>
 +</WRAP>
 +
 +ZRAM スワップの利用状況を表示する。\\
 +<WRAP color_term>
 +<WRAP color_command><html><pre>
 +<font color="#0087FF"><b>$</b></font> <font color="#26A269">zramctl</font>
 +</pre></html></WRAP>
 +<WRAP color_result><html><pre>
 +NAME       ALGORITHM DISKSIZE   DATA COMPR TOTAL STREAMS MOUNTPOINT
 +/dev/zram0 lzo-rle       970M 173.6M 55.4M 59.1M       2 [SWAP]
 +</pre></html></WRAP>
 +</WRAP>
 +
 +===== ZRAM スワップの設定変更 =====
 +<WRAP color_term>
 +<WRAP color_command><html><pre>
 +<font color="#0087FF"><b>$</b></font> <font color="#26A269"><u style="text-decoration-style:solid">sudo</u></font> <font color="#26A269">nano</font> <u style="text-decoration-style:solid">/usr/lib/systemd/zram-generator.conf</u>
 +</pre></html></WRAP>
 +</WRAP>
 +
 +標準の設定では使用可能な物理メモリー容量または最大 8GB の設定になっている。\\
 +そして、使用可能メモリー容量の 1 倍の設定です。\\
 +<WRAP color_term>
 +<WRAP color_mincode><html><pre>
 +<font color="#839496">   1</font> <font color="#75715E"># This config file enables a /dev/zram0 device with the default settings:</font>
 +<font color="#839496">   2</font> <font color="#75715E"># — size — same as available RAM or 8GB, whichever is less</font>
 +<font color="#839496">   3</font> <font color="#75715E"># — compression — most likely lzo-rle</font>
 +<font color="#839496">   4</font> <font color="#75715E">#</font>
 +<font color="#839496">   5</font> <font color="#75715E"># To disable, uninstall zram-generator-defaults or create empty</font>
 +<font color="#839496">   6</font> <font color="#75715E"># /etc/systemd/zram-generator.conf file.</font>
 +<font color="#839496">   7</font> <font color="#F8F8F2">[</font><font color="#A6E22E">zram0</font><font color="#F8F8F2">]</font>
 +<font color="#839496">   8</font> <font color="#F92672">zram-size</font><font color="#F8F8F2"> = </font><font color="#E6DB74">min(ram, 8192)</font>
 +</pre></html></WRAP>
 +</WRAP>
 +
 +ZRAM スワップの設定を、使用可能な物理メモリー容量の 3 倍に変更する事も可能だが...🤔(<wrap em>推奨しない!</wrap>)\\
 +物理メモリー容量を超えてスワップした場合に、メモリー圧縮しながら更にディスクスワップするので、最悪の場合マシンにログイン不能なほどパフォーマンスが劇的に悪化する😱\\
 +<WRAP color_term>
 +<WRAP color_mincode><html><pre>
 +<font color="#839496">   1</font> <font color="#75715E"># This config file enables a /dev/zram0 device with the default settings:</font>
 +<font color="#839496">   2</font> <font color="#75715E"># — size — same as available RAM or 8GB, whichever is less</font>
 +<font color="#839496">   3</font> <font color="#75715E"># — compression — most likely lzo-rle</font>
 +<font color="#839496">   4</font> <font color="#75715E">#</font>
 +<font color="#839496">   5</font> <font color="#75715E"># To disable, uninstall zram-generator-defaults or create empty</font>
 +<font color="#839496">   6</font> <font color="#75715E"># /etc/systemd/zram-generator.conf file.</font>
 +<font color="#839496">   7</font> <font color="#F8F8F2">[</font><font color="#A6E22E">zram0</font><font color="#F8F8F2">]</font>
 +<font color="#839496">   8</font> <font color="#75715E">#zram-size = min(ram, 8192)</font>
 +<font color="#839496">   9</font> <font color="#F92672">zram-fraction</font><font color="#F8F8F2"> = </font><font color="#E6DB74">3.0</font>
 +<font color="#839496">  10</font> <font color="#F92672">max-zram-size</font><font color="#F8F8F2"> = </font><font color="#E6DB74">8192</font>
 +</pre></html></WRAP>
 +</WRAP>
 +
 +ZRAM スワップの設定は標準のままにする。\\
 +<WRAP color_term>
 +<WRAP color_mincode><html><pre>
 +<font color="#839496">   1</font> <font color="#75715E"># This config file enables a /dev/zram0 device with the default settings:</font>
 +<font color="#839496">   2</font> <font color="#75715E"># — size — same as available RAM or 8GB, whichever is less</font>
 +<font color="#839496">   3</font> <font color="#75715E"># — compression — most likely lzo-rle</font>
 +<font color="#839496">   4</font> <font color="#75715E">#</font>
 +<font color="#839496">   5</font> <font color="#75715E"># To disable, uninstall zram-generator-defaults or create empty</font>
 +<font color="#839496">   6</font> <font color="#75715E"># /etc/systemd/zram-generator.conf file.</font>
 +<font color="#839496">   7</font> <font color="#F8F8F2">[</font><font color="#A6E22E">zram0</font><font color="#F8F8F2">]</font>
 +<font color="#839496">   8</font> <font color="#F92672">zram-size</font><font color="#F8F8F2"> = </font><font color="#E6DB74">min(ram, 8192)</font>
 +</pre></html></WRAP>
 +</WRAP>
 +
 +その代わりに従来のスワップファイルを追加する。\\
 +btrfs 上の swap ファイルは nocow(copy on write禁止) フラグを設定して生成する必要がある。\\
 +<WRAP color_term>
 +<WRAP color_command><html><pre>
 +<font color="#0087FF"><b>$</b></font> <font color="#26A269"><u style="text-decoration-style:solid">sudo</u></font> <font color="#26A269">truncate</font> <font color="#A347BA">-s</font> 0 /var/swap
 +<font color="#0087FF"><b>$</b></font> <font color="#26A269"><u style="text-decoration-style:solid">sudo</u></font> <font color="#26A269">chattr</font> +C <u style="text-decoration-style:solid">/var/swap</u>
 +<font color="#0087FF"><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> 3072M <u style="text-decoration-style:solid">/var/swap</u>
 +<font color="#0087FF"><b>$</b></font> <font color="#26A269"><u style="text-decoration-style:solid">sudo</u></font> <font color="#26A269">lsattr</font> <u style="text-decoration-style:solid">/var/swap</u>
 +</pre></html></WRAP>
 +<WRAP color_result><html><pre>
 +---------------C------ /var/swap
 +</pre></html></WRAP>
 +<WRAP color_command><html><pre>
 +<font color="#0087FF"><b>$</b></font> <font color="#26A269"><u style="text-decoration-style:solid">sudo</u></font> <font color="#26A269">chmod</font> 600 <u style="text-decoration-style:solid">/var/swap</u>
 +</pre></html></WRAP>
 +
 +<WRAP color_command><html><pre>
 +<font color="#0087FF"><b>$</b></font> <font color="#26A269"><u style="text-decoration-style:solid">sudo</u></font> <font color="#26A269">mkswap</font> <u style="text-decoration-style:solid">/var/swap</u>
 +</pre></html></WRAP>
 +<WRAP color_result><html><pre>
 +Setting up swapspace version 1, size = 3 GiB (3221221376 bytes)
 +no label, UUID=d79c6784-7da2-480d-b0bf-4985d2d8aea1
 +</pre></html></WRAP>
 +
 +<WRAP color_command><html><pre>
 +<font color="#0087FF"><b>$</b></font> <font color="#26A269"><u style="text-decoration-style:solid">sudo</u></font> <font color="#26A269">swapon</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=3221225472, devsize=3221225472
 +swapon /var/swap
 +</pre></html></WRAP>
 +
 +<WRAP color_command><html><pre>
 +<font color="#0087FF"><b>$</b></font> <font color="#26A269">echo</font> <font color="#A2734C">&apos;/var/swap                                 none                    swap    sw              0 0&apos;</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>
 +
 +サービス名を調べる。\\
 +<WRAP color_term>
 +<WRAP color_command><html><pre>
 +<font color="#0087FF"><b>$</b></font> <font color="#26A269">systemctl</font> <font color="#12488B"><b>|</b></font> <font color="#26A269">grep</font> systemd-zram
 +</pre></html></WRAP>
 +<WRAP color_result><html><pre>
 +  <font color="#C01C28"><b>systemd-zram</b></font>-setup@zram0.service                                                                                      loaded active exited    Create swap on /dev/zram0
 +  system-systemd\x2dzram\x2dsetup.slice                                                                                 loaded active active    Slice /system/<font color="#C01C28"><b>systemd-zram</b></font>-setup
 +</pre></html></WRAP>
 +</WRAP>
 +
 +サービスを再起動する。\\
 +<WRAP color_term>
 +<WRAP color_command><html><pre>
 +<font color="#0087FF"><b>$</b></font> <font color="#26A269"><u style="text-decoration-style:solid">sudo</u></font> <font color="#26A269">systemctl</font> restart systemd-zram-setup@zram0
 +</pre></html></WRAP>
 +</WRAP>
 +
 +ZRAM スワップ+スワップファイルの容量が増えていることを確認する。(大きい priority が優先される🤔ここでは 1 の zram0 から優先して使われる😻)\\
 +<WRAP color_term>
 +<WRAP color_command><html><pre>
 +<b class=GRN>$</b> <b class=HIY>swapon</b> <b class=HIK>--show</b>
 +</pre></html></WRAP>
 +<WRAP color_result><html><pre>
 +NAME       TYPE      SIZE   USED PRIO
 +/dev/zram0 partition 964M 295.3M    1
 +/var/swap  file        3G     0B   -2
 +</pre></html></WRAP>
 +
 +<WRAP color_command><html><pre>
 +<b class=GRN>$</b> <b class=HIY>zramctl</b>
 +</pre></html></WRAP>
 +<WRAP color_result><html><pre>
 +NAME       ALGORITHM DISKSIZE   DATA  COMPR  TOTAL STREAMS MOUNTPOINT
 +/dev/zram0 lzo-rle       964M 295.2M 96.7M 101.6M       2 [SWAP]
 +</pre></html></WRAP>
 +
 +<WRAP color_command><html><pre>
 +<b class=GRN>$</b> <b class=HIY>free</b>
 +</pre></html></WRAP>
 +<WRAP color_result><html><pre>
 +               total        used        free      shared  buff/cache   available
 +Mem:          987588      599640       77528       14828      310420      221952
 +Swap:        4132856      302364     3830492
 +</pre></html></WRAP>
 +</WRAP>
 +
 +===== Linux スワップの優先度について =====
 +<WRAP round tip 80%>
 +</WRAP>
 +
 +===== 参考文献 =====
 +[[https://dev.to/ptuladhar3/dynamically-increase-swap-zram-size-in-fedora-linux-2iog|Dynamically Increase SWAP (ZRAM) Size in Linux]]\\
 +[[https://bitwalk.blogspot.com/2020/09/fedora-33.html|Fedora 33 のプレリリース版を試す]]\\
 +[[https://www.soudegesu.com/post/raspberry/expand-memory-with-zram/|Raspberry Piでメモリを拡張したい時にZramを使う]]\\
 +[[qita>shimauma_Zzzzz/items/e3d686f0239a2878c965|ZRAM について軽く調べてみた - Qiita]]\\
 +[[qita>tmsn/items/41bf294728a4d2c0d4b8|btrfsでswapfile + hibernation - Qiita]]\\
 +[[https://www.reddit.com/r/btrfs/comments/qv2ezk/nocow_torrents_and_snapshots/|r/btrfs - nocow, torrents, and snapshots]]\\
 +
 +==== 付録 ====
 +[[tw>tomoyan596sp/status/1417704170333753346|fedora 33からデフォルトで有効になっているSwapのZRAMってなんじゃろう❓🤤]]\\
 +[[tw>tomoyan596sp/status/1536232579371659264|コンパイル後にリンクでtmp溢れて終了😢]]\\
 +[[tw>tomoyan596sp/status/1634521572697579520|Linux で swap priority を指定しないと、Kernelが自動で割り当てるswap priorityは負数 -1, -2 ...になりますが、その文献はどこにあるのか?🤔ここみたい😅]]\\