Hi,
I would like to know how to correctly configure swap nowadays. In the past we just added swap to my LVM mirror and a line to /etc/fstab, but since then people decided for systemd which infects more and more of the system. Being open I tried to align and use systemd correctly, but I now spent more than two hours reading and trying but I'm too stupid to correctly configure swap and thus please need some hints, or an example.
Background: I installed Proxmox which did not add swap (which I though was intentional, but swap is recommended). I learned that I must not put swap on ZFS if I want a safe system, because ZFS may want swap to access ZFS vols which thus cannot be on swap. Fortunately, I left a bit of space on the SSDs, so I could add at least a bit of swap space. Google brings up a lot of articles about swap, but all using the deprecated /etc/fstab, where a single line was sufficient. Now I need to use the (said to be) modern, simple systemd approach.
Since sometimes drives re-order (for example, I had issues with ZFS where /dev/sda and /dev/sdb were renamed after a reboot), so I read we should use UUIDs. I tried to create the simple units replacing the complicated single line by just a few simple commands:
yes, "systemd-escape" produces "escaped" file names (!) that must be escaped for the shell (!),
no, the systemd bash completion does not complete the nice and simple unit names like
But it does not work:
although the manpage says "Swap unit files may include [Unit] and [Install] sections", "may", not "must" (apparently a bug).
Then I tried
(I'm so glad that I can use these simple commands instead of adding two lines to fstab).
I verified the result:
Obvioulsy there now is no redundancy anymore!
So I just downgraded my ECC-RAM, redundant-disk-with-ZFS-checksums down to "I must trust each individual medium for my most valuable active data" and on a single disk failure I can expect my system to fail. That's not the way.
How to add swap correctly?
Should I use mdadm or lvm to create a mirror? LVM was used to have safe autodetect, but I read that since introduction of systemd a lot of boot problems happen (especially after updates), for example because UUIDs change but are not updated in initrd and what not.
Any hints appreciated!
I would like to know how to correctly configure swap nowadays. In the past we just added swap to my LVM mirror and a line to /etc/fstab, but since then people decided for systemd which infects more and more of the system. Being open I tried to align and use systemd correctly, but I now spent more than two hours reading and trying but I'm too stupid to correctly configure swap and thus please need some hints, or an example.
Background: I installed Proxmox which did not add swap (which I though was intentional, but swap is recommended). I learned that I must not put swap on ZFS if I want a safe system, because ZFS may want swap to access ZFS vols which thus cannot be on swap. Fortunately, I left a bit of space on the SSDs, so I could add at least a bit of swap space. Google brings up a lot of articles about swap, but all using the deprecated /etc/fstab, where a single line was sufficient. Now I need to use the (said to be) modern, simple systemd approach.
Since sometimes drives re-order (for example, I had issues with ZFS where /dev/sda and /dev/sdb were renamed after a reboot), so I read we should use UUIDs. I tried to create the simple units replacing the complicated single line by just a few simple commands:
Code:
root@pve:~# fdisk -l /dev/sd[cd]|grep swap
Partition 1 does not start on physical sector boundary.
/dev/sdc4 1853884416 1875384974 21500559 10.3G Linux swap
Partition 1 does not start on physical sector boundary.
/dev/sdd4 1853884416 1875384974 21500559 10.3G Linux swap
root@pve:~#
root@pve:~# blkid /dev/sd[cd]4
/dev/sdc4: UUID="968b2b23-9e51-4ac2-99d1-610200f91091" TYPE="swap" PARTUUID="8ab263bc-34ee-0b4f-9534-d7b0f53788a7"
/dev/sdd4: UUID="d32e80c3-a944-493b-afd7-ca1a779b721f" TYPE="swap" PARTUUID="e403401e-9286-e54b-9f79-47f11ce4945f"
root@pve:~# systemd-escape --suffix=swap --path /dev/disk/by-uuid/968b2b23-9e51-4ac2-99d1-610200f91091
dev-disk-by\x2duuid-968b2b23\x2d9e51\x2d4ac2\x2d99d1\x2d610200f91091.swap
root@pve:~# systemd-escape --suffix=swap --path /dev/disk/by-uuid/d32e80c3-a944-493b-afd7-ca1a779b721f
dev-disk-by\x2duuid-d32e80c3\x2da944\x2d493b\x2dafd7\x2dca1a779b721f.swap
root@pve:~# systemd-escape --suffix=swap --path /dev/disk/by-uuid/968b2b23-9e51-4ac2-99d1-610200f91091
dev-disk-by\x2duuid-968b2b23\x2d9e51\x2d4ac2\x2d99d1\x2d610200f91091.swap
root@pve:~# echo -e "[Swap]\nOptions=defaults\nTimeoutSec=5" >> '/etc/systemd/system/dev-disk-by\x2duuid-d32e80c3\x2da944\x2d493b\x2dafd7\x2dca1a779b721f.swap'
root@pve:~# echo -e "[Swap]\nOptions=defaults\nTimeoutSec=5" >> '/etc/systemd/system/dev-disk-by\x2duuid-968b2b23\x2d9e51\x2d4ac2\x2d99d1\x2d610200f91091.swap'
root@pve:~# systemctl daemon-reload
yes, "systemd-escape" produces "escaped" file names (!) that must be escaped for the shell (!),
no, the systemd bash completion does not complete the nice and simple unit names like
dev-disk-by\x2duuid-968b2b23\x2d9e51\x2d4ac2\x2d99d1\x2d610200f91091.swap
.But it does not work:
Code:
root@pve:~# systemctl list-units --type=swap --all
UNIT LOAD ACTIVE SUB DESCRIPTION
0 loaded units listed.
To show all installed unit files use 'systemctl list-unit-files'.
root@pve:~#root@csdpve:~# systemctl enable 'dev-disk-by\x2duuid-968b2b23\x2d9e51\x2d4ac2\x2d99d1\x2d610200f91091.swap'
The unit files have no installation config (WantedBy=, RequiredBy=, Also=,
Alias= settings in the [Install] section, and DefaultInstance= for template
units). This means they are not meant to be enabled using systemctl.
Possible reasons for having this kind of units are:
• A unit may be statically enabled by being symlinked from another unit's
.wants/ or .requires/ directory.
• A unit's purpose may be to act as a helper for some other unit which has
a requirement dependency on it.
• A unit may be started when needed via activation (socket, path, timer,
D-Bus, udev, scripted systemctl call, ...).
• In case of template units, the unit is meant to be enabled with some
instance name specified.
although the manpage says "Swap unit files may include [Unit] and [Install] sections", "may", not "must" (apparently a bug).
Then I tried
Code:
root@pve:~# echo -e "[Install]\nWantedBy=multi-user.target\n[Swap]\nOptions=defaults\nTimeoutSec=5" >> '/etc/systemd/system/dev-disk-by\x2duuid-d32e80c3\x2da944\x2d493b\x2dafd7\x2dca1a779b721f.swap'
root@pve:~# echo -e "[Install]\nWantedBy=multi-user.target\n[Swap]\nOptions=defaults\nTimeoutSec=5" > '/etc/systemd/system/dev-disk-by\x2duuid-968b2b23\x2d9e51\x2d4ac2\x2d99d1\x2d610200f91091.swap'
root@pve:~# systemctl enable 'dev-disk-by\x2duuid-968b2b23\x2d9e51\x2d4ac2\x2d99d1\x2d610200f91091.swap'
root@pve:~# systemctl enable 'dev-disk-by\x2duuid-d32e80c3\x2da944\x2d493b\x2dafd7\x2dca1a779b721f.swap'
root@pve:~# systemctl start 'dev-disk-by\x2duuid-d32e80c3\x2da944\x2d493b\x2dafd7\x2dca1a779b721f.swap'
root@pve:~# systemctl start 'dev-disk-by\x2duuid-968b2b23\x2d9e51\x2d4ac2\x2d99d1\x2d610200f91091.swap'
(I'm so glad that I can use these simple commands instead of adding two lines to fstab).
I verified the result:
Code:
root@pve:~# cat /proc/swaps
Filename Type Size Used Priority
/dev/sdd4 partition 10750272 5120 -2
/dev/sdc4 partition 10750272 0 -3
Obvioulsy there now is no redundancy anymore!
So I just downgraded my ECC-RAM, redundant-disk-with-ZFS-checksums down to "I must trust each individual medium for my most valuable active data" and on a single disk failure I can expect my system to fail. That's not the way.
How to add swap correctly?
Should I use mdadm or lvm to create a mirror? LVM was used to have safe autodetect, but I read that since introduction of systemd a lot of boot problems happen (especially after updates), for example because UUIDs change but are not updated in initrd and what not.
Any hints appreciated!
Last edited: