bundles/systemd-swap: be reboot safe by initializing swap in another unit

This commit is contained in:
cronekorkn 2023-05-04 17:07:31 +02:00
parent c0ccd78517
commit b6cfc84e86
Signed by: cronekorkn
SSH key fingerprint: SHA256:v0410ZKfuO1QHdgKBsdQNF64xmTxOF8osF1LIqwTcVw
2 changed files with 44 additions and 54 deletions

View file

@ -1,58 +1,13 @@
size = node.metadata.get('systemd-swap')
actions = {
'stop_swap': {
'command': f'systemctl stop swapfile.swap',
'unless': f'! systemctl is-active swapfile.swap',
'triggered': True,
},
'remove_swapfile': {
'command': f'rm /swapfile',
'unless': f'! test -e /swapfile',
'triggered': True,
'needs': {
'action:stop_swap',
},
},
'create_swapfile': {
'command': f'fallocate -l {size} /swapfile',
'unless': f'stat -c %s /swapfile | grep ^{size}$',
'preceded_by': {
'action:stop_swap',
'action:remove_swapfile',
},
'triggers': {
'action:initialize_swapfile',
'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:swapfile_mode',
}
},
}
svc_systemd = {
'swapfile-prepare.service': {
'enabled': True,
'running': None,
},
'swapfile.swap': {
'preceded_by': {
'action:initialize_swapfile',
},
'needs': {
'action:initialize_swapfile',
'action:systemd-reload',
},
'enabled': True,
'running': True,
'needs': [
'svc_systemd:swapfile-prepare.service',
],
},
}

View file

@ -10,3 +10,38 @@ defaults = {
},
},
}
@metadata_reactor.provides(
'systemd/units/swapfile.swap',
)
def unit(metadata):
size = metadata.get('systemd-swap')
return {
'systemd': {
'units': {
'swapfile-prepare.service': {
'Unit': {
'Before': {
'swapfile.swap',
},
},
'Service': {
'Type': 'oneshot',
'ExecStartPre': [
'-/bin/rm /swapfile -f',
f'/bin/fallocate -l {size} /swapfile',
'/bin/chmod 600 /swapfile',
],
'ExecStart': '/usr/sbin/mkswap /swapfile',
},
'Install': {
'RequiredBy': {
'swapfile.swap',
},
},
},
},
},
}