差分

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

この比較画面へのリンク

両方とも前のリビジョン 前のリビジョン
linux:fedora:extend_zram_swap [2024/02/04 13:37] – 削除 - 外部編集 (Unknown date) 非ログインユーザーlinux:fedora:extend_zram_swap [2024/02/04 13:37] (現在) – ↷ linux:fedora_extend_zram_swap から linux:fedora:extend_zram_swap へページの移動と名称変更しました。 ともやん
行 1: 行 1:
 +====== Fedora 33 以降で標準になった ZRAM スワップの容量を拡張する ======
 +
 +[[wpjp>Zram|Zram - Wikipedia]]\\
 +
 +===== ZRAM スワップの構成を確認 ===== 
 +ブロックデバイス zram0 が SWAP としてマウントされている。\\
 +<WRAP color_term>
 +<WRAP color_command><html><pre>
 +<b class=GRN>$</b> <b class=HIY>lsblk</b>
 +</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>
 +<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 970M 174M  100
 +</pre></html></WRAP>
 +</WRAP>
 +
 +ZRAM スワップの利用状況を表示する。\\
 +<WRAP color_term>
 +<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       970M 173.6M 55.4M 59.1M       2 [SWAP]
 +</pre></html></WRAP>
 +</WRAP>
 +
 +===== ZRAM スワップの設定変更 =====
 +<WRAP color_term>
 +<WRAP color_command><html><pre>
 +<b class=GRN>$</b> <b class=HIY>sudo</b> nano /usr/lib/systemd/zram-generator.conf
 +</pre></html></WRAP>
 +<WRAP color_result><html><pre>
 +</pre></html></WRAP>
 +</WRAP>
 +
 +標準の設定では使用可能な物理メモリー容量または最大 8GB の設定になっている。\\
 +そして、使用可能メモリー容量の 1 倍の設定です。\\
 +<WRAP color_term>
 +<WRAP color_mincode><code autoconf /usr/lib/systemd/zram-generator.conf>
 +# This config file enables a /dev/zram0 device with the default settings:
 +# — size — same as available RAM or 8GB, whichever is less
 +# — compression — most likely lzo-rle
 +#
 +# To disable, uninstall zram-generator-defaults or create empty
 +# /etc/systemd/zram-generator.conf file.
 +[zram0]
 +zram-size = min(ram, 8192)
 +</code></WRAP>
 +</WRAP>
 +
 +ZRAM スワップの設定を、使用可能な物理メモリー容量の 3 倍に変更する事も可能だが...🤔(<wrap em>推奨しない!</wrap>)\\
 +物理メモリー容量を超えてスワップした場合に、メモリー圧縮しながら更にディスクスワップするので、最悪の場合マシンにログイン不能なほどパフォーマンスが劇的に悪化する😱\\
 +<WRAP color_term>
 +<WRAP color_mincode><code autoconf /usr/lib/systemd/zram-generator.conf>
 +[zram0]
 +#zram-size = min(ram, 8192)
 +zram-fraction = 3.0
 +max-zram-size = 8192
 +</code></WRAP>
 +</WRAP>
 +
 +ZRAM スワップの設定は標準のままにする。\\
 +<WRAP color_term>
 +<WRAP color_mincode><code autoconf /usr/lib/systemd/zram-generator.conf>
 +[zram0]
 +zram-size = min(ram, 8192)
 +</code></WRAP>
 +</WRAP>
 +
 +その代わりに従来のスワップファイルを追加する。\\
 +btrfs 上の swap ファイルは nocow(copy on write禁止) フラグを設定して生成する必要がある。\\
 +<WRAP color_term>
 +<WRAP color_command><html><pre>
 +<b class=GRN>$</b> <b class=HIY>sudo</b> truncate -s 0 /var/swap
 +<b class=GRN>$</b> <b class=HIY>sudo</b> chattr +C /var/swap
 +<b class=GRN>$</b> <b class=HIY>sudo</b> fallocate -l 3072M /var/swap
 +<b class=GRN>$</b> <b class=HIY>sudo</b> lsattr /var/swap
 +</pre></html></WRAP>
 +<WRAP color_result><html><pre>
 +---------------C------ /var/swap
 +</pre></html></WRAP>
 +
 +<WRAP color_command><html><pre>
 +<b class=GRN>$</b> <b class=HIY>sudo</b> mkswap /var/swap
 +</pre></html></WRAP>
 +<WRAP color_result><html><pre>
 +スワップ空間バージョン 1 を設定します。サイズ = 3 GiB (3221221376 バイト)
 +ラベルはありません, UUID=c15d187b-3a3b-4b6c-8eb3-fce516ba5121
 +</pre></html></WRAP>
 +
 +<WRAP color_command><html><pre>
 +<b class=GRN>$</b> <b class=HIY>sudo</b> swapon -v /var/swap
 +</pre></html></WRAP>
 +<WRAP color_result><html><pre>
 +swapon: /var/swap: 署名が見つかりました: [ページサイズ=4096, 署名=swap]
 +swapon: /var/swap: ページサイズ=4096, スワップサイズ=3221225472, デバイスサイズ=3221225472
 +スワップ /var/swap を有効化しています
 +</pre></html></WRAP>
 +
 +<WRAP color_command><html><pre>
 +<b class=GRN>$</b> <b class=HIY>echo</b> '/var/swap                                 none                    swap    sw              0 0' | sudo tee -a /etc/fstab
 +</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>
 +<b class=GRN>$</b> <b class=HIY>systemctl</b> | grep systemd-zram
 +</pre></html></WRAP>
 +<WRAP color_result><html><pre>
 +  systemd-zram-setup@zram0.service                    loaded active exited    Create swap on /dev/zram0
 +  system-systemd\x2dzram\x2dsetup.slice               loaded active active    Slice /system/systemd-zram-setup
 +</pre></html></WRAP>
 +</WRAP>
 +
 +サービスを再起動する。\\
 +<WRAP color_term>
 +<WRAP color_command><html><pre>
 +<b class=GRN>$</b> <b class=HIY>sudo</b> systemctl restart systemd-zram-setup@zram0
 +</pre></html></WRAP>
 +<WRAP color_result><html><pre>
 +</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 left 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 ...になりますが、その文献はどこにあるのか?🤔ここみたい😅]]\\