From e44fc3dc049a0de59a80d6363871b470fa54366a Mon Sep 17 00:00:00 2001 From: mwiegand Date: Thu, 7 Jul 2022 16:16:18 +0200 Subject: [PATCH] further improve swap --- bundles/systemd-swap/items.py | 30 ++++++++++++++---------------- nodes/home.server.py | 2 +- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/bundles/systemd-swap/items.py b/bundles/systemd-swap/items.py index fae6b76..8df973e 100644 --- a/bundles/systemd-swap/items.py +++ b/bundles/systemd-swap/items.py @@ -1,5 +1,4 @@ -size = node.metadata.get('systemd-swap') -assert isinstance(size, int) +size_mb = node.metadata.get('systemd-swap')//1_000_000 actions = { 'stop_swap': { @@ -16,8 +15,8 @@ actions = { }, }, 'create_swapfile': { - 'command': f'dd if=/dev/zero of=/swapfile bs={size} count=1', - 'unless': f'stat -c %s /swapfile | grep ^{size}$', + 'command': f'dd if=/dev/zero of=/swapfile bs=1000000 count={size_mb}', + 'unless': f'stat -c %s /swapfile | grep ^{size_mb*1_000_000}$', 'preceded_by': { 'action:stop_swap', 'action:remove_swapfile', @@ -27,32 +26,31 @@ actions = { 'svc_systemd:swapfile.swap:restart', }, }, + 'swapfile_mode': { + 'command': f'chmod 600 /swapfile', + 'unless': f'stat -c "%a" /swapfile | grep -q "^600$"', + 'needs': { + 'action:create_swapfile', + }, + 'triggers': { + 'svc_systemd:swapfile.swap:restart', + }, + }, 'initialize_swapfile': { 'command': f'mkswap /swapfile', 'triggered': True, 'needs': { - 'action:create_swapfile', + 'action:swapfile_mode', } }, } -files = { - '/swapfile': { - 'content_type': 'any', - 'mode': '600', - 'triggers': { - 'svc_systemd:swapfile.swap:restart', - }, - } -} - svc_systemd = { 'swapfile.swap': { 'preceded_by': { 'action:initialize_swapfile', }, 'needs': { - 'file:/swapfile', 'action:initialize_swapfile', 'action:systemd-reload', }, diff --git a/nodes/home.server.py b/nodes/home.server.py index 00b7621..ef3d3f9 100644 --- a/nodes/home.server.py +++ b/nodes/home.server.py @@ -107,7 +107,7 @@ 'steam-chat-viewer': { 'hostname': 'steam-chats.ckn.li', }, - 'systemd-swap': 4*10**9, + 'systemd-swap': 4_000_000_000, 'tasmota-charge': { 'phone': { 'ip': '10.0.0.175',