Swap: Difference between revisions
Artoria2e5 (talk | contribs) |
Artoria2e5 (talk | contribs) |
||
(8 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
[[Category:Configuration]] | [[Category:Configuration]] | ||
Swap | Swap allows "cold" pages of virtual memory to be stored in places other than directly in the physical RAM, effectively allowing more pages to be stored. This can be accomplished by using space on disk, such as [[#Swap file|swap file]] or [[#Swap partition|swap partition]], or through compression based methods like [[#Zram swap|zram]]. Additionally, [[#Zswap swap cache|zswap]] can act as a RAM-based compressed cache sitting in front of a traditional disk-based swap device. | ||
= Configuration = | = Configuration = | ||
Line 110: | Line 110: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
If you are using GPT partitioning tables, <code>systemd-gpt-auto-generator(8)</code> will still mount your swap partition automatically. You must therefore turn on attribute 63 ("no | If you are using GPT partitioning tables, <code>systemd-gpt-auto-generator(8)</code> will still mount your swap partition automatically. You must therefore turn on attribute 63 ("no-auto") on ''each'' swap partition partition in the partition table. This can be done with gptfdisk or similar: | ||
<syntaxhighlight lang="console"> | <syntaxhighlight lang="console"> | ||
Line 134: | Line 134: | ||
=== discard === | === discard === | ||
Solid state drives have fast random access times, which make them great for swap if you ignore the limited lifespan. Enabling TRIM (discard) on the swap files can help avoid unnecessary copy actions on the SSD, helping | Solid state drives have fast random access times, which make them great for swap if you ignore the limited lifespan. Enabling TRIM (discard) on the swap files can help avoid unnecessary copy actions on the SSD, reducing wear and potentially helping increase performance. | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
swapDevices = [{ | swapDevices = [{ | ||
Line 141: | Line 141: | ||
}]; | }]; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
A lower-impact option is <code>"discard=once"</code>, which runs discard exactly once when the swap is enabled, but does not continually issue discard commands as pages are being overwritten. This could make more sense depending on your hardware. | |||
<code>systemd-gpt-auto-generator(8)</code> does not automatically enable <code>discard</code>. Also, never enable <code>discard</code> on mdadm RAID setups, as ArchWiki reports that it causes lockup. | <code>systemd-gpt-auto-generator(8)</code> does not automatically enable <code>discard</code>. Also, never enable <code>discard</code> on mdadm RAID setups, as ArchWiki reports that it causes lockup. | ||
Line 152: | Line 154: | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
swapDevices = [{ | swapDevices = [{ | ||
device = "/dev/ | device = "/dev/disk/by-partuuid/aaaaaaaaa-bbbb-cccc-dddd-0123456789ab"; | ||
randomEncryption.enable = true; | randomEncryption.enable = true; | ||
}]; | }]; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
The selected device will have all its content made unusuable at every boot. Using a partuuid or partlabel is recommended because it is less subject to change when the overall partition scheme changes. | |||
If you want to use TRIM, set <code>randomEncryption.allowDiscards</code> in addition to the <code>options</code>. This has the security implication of: | If you want to use TRIM, set <code>randomEncryption.allowDiscards</code> in addition to the <code>options</code>. This has the security implication of: | ||
Line 162: | Line 166: | ||
You will need to weigh between the two. | You will need to weigh between the two. | ||
Using a random key makes hibernation impossible. If you want to use hibernation, use a regular [[Full Disk Encryption]] (or partial disk encryption with LUKS) scheme with an unchanging key. | |||
== Adjusting swap usage behaviour == | == Adjusting swap usage behaviour == |