How to increase the swap file size on Home Assistant OS

Think I figured it out. Did not realize you had to edit YAML to get the init_commands to work. Just did this now the original post works. Sorry for the confusion and appreciate the help.

Hi zhutson24,

Could You advise what specifically has to be changed in YAML config, please?

Fighting with the swap setup but with no luck so far. Seems like Current version: 12.1.3 ignores the init_command

Thanks
Marek

@machiina anything I can add or change in the original post to make it clearer?

Hi JZhass,

Your instruction is crystal clear to me and I followed it but still it does not work for me.

When I paste the syntax in the add-on config, save it, start the add-on and then reboot the raspi the syntax changes and it looks as per below:

init_commands:
    - >-
    if [ ! -f /backup/_swap.swap ]; then fallocate -l 2G /backup/_swap.swap &&
    mkswap /backup/_swap.swap && chmod 0600 /backup/_swap.swap && swapon
    /backup/_swap.swap ; elif [[ ! $(dmesg|grep _swap.swap) = *swap\ on* ]] ;
    then swapon /backup/_swap.swap ; else swapon /backup/_swap.swap ; fi


Not sure if it’s relevant but apart from it I can’t see what I might be doing wrong.
Also, there is nothing in the add-on logs pointing to a problem with the config:

s6-rc: info: service s6rc-oneshot-runner: starting
s6-rc: info: service s6rc-oneshot-runner successfully started
s6-rc: info: service fix-attrs: starting
s6-rc: info: service fix-attrs successfully started
s6-rc: info: service legacy-cont-init: starting
cont-init: info: running /etc/cont-init.d/00-banner.sh
-----------------------------------------------------------
 Add-on: SSH & Web Terminal
 SSH & Web Terminal access to your Home Assistant instance
-----------------------------------------------------------
 Add-on version: 12.1.3
 You are running the latest version of this add-on.
 System: Home Assistant OS 9.3  (aarch64 / raspberrypi3-64)
 Home Assistant Core: 2022.11.3
 Home Assistant Supervisor: 2022.10.2
-----------------------------------------------------------
 Please, share the above information when looking for help
 or support in, e.g., GitHub, forums or the Discord chat.
-----------------------------------------------------------
cont-init: info: /etc/cont-init.d/00-banner.sh exited 0
cont-init: info: running /etc/cont-init.d/01-log-level.sh
cont-init: info: /etc/cont-init.d/01-log-level.sh exited 0
cont-init: info: running /etc/cont-init.d/docker.sh
[17:16:05] INFO: Docker support has been enabled.
cont-init: info: /etc/cont-init.d/docker.sh exited 0
cont-init: info: running /etc/cont-init.d/mosquitto.sh
cont-init: info: /etc/cont-init.d/mosquitto.sh exited 0
cont-init: info: running /etc/cont-init.d/mysql.sh
cont-init: info: /etc/cont-init.d/mysql.sh exited 0
cont-init: info: running /etc/cont-init.d/ssh.sh
[17:16:18] WARNING: 
[17:16:18] WARNING: Logging in with a SSH password is security wise, a bad idea!
[17:16:18] WARNING: Please, consider using a public/private key pair.
[17:16:18] WARNING: What is this? https://kb.iu.edu/d/aews
[17:16:18] WARNING: 
cont-init: info: /etc/cont-init.d/ssh.sh exited 0
cont-init: info: running /etc/cont-init.d/user.sh
cont-init: info: /etc/cont-init.d/user.sh exited 0
s6-rc: info: service legacy-cont-init successfully started
s6-rc: info: service legacy-services: starting
services-up: info: copying legacy longrun sshd (no readiness notification)
services-up: info: copying legacy longrun ttyd (no readiness notification)
s6-rc: info: service legacy-services successfully started
[17:16:32] INFO: Starting the ttyd daemon...
[17:16:32] INFO: Starting the SSH daemon...
Server listening on 0.0.0.0 port 22.
Server listening on :: port 22.

regards

The fact that the yaml changes a bit (to look better with line wrapping, etc. is not inherently a problem, just a feature of HA. Strange that it doesn’t work and you’re not sure what changes actually made it work.

See screenshot. If you don’t edit YAML, the init_commands was being inserted under and inline with username. If you edit in YAML, the init_commands at the top before packages is what made it work for me. Make sure you didn’t manually enable swapon. If you did, restart before making these edits.
SSH-YAML

I can confirm that with the latest update of the add-on 12.1.3 solves the problem

“Edit in YAML”
I searched 2 days how I can do this.
Before, I added the command directly in the UI of the configuration but it would not worked. You could see in the log that always init_commands[] was used.
You can switch to YAML if you press the three dots at the right of ‘Options’. And than you see the init_commands line to edit.

Thank you for the command. Now my RPi 3 works fine again :grinning:

1 Like

I fixed the instructions to hopefully be clearer, thanks for the feedback @janutz

Hello
I configured as explained in the first post, seems that ti worked and I have now a 2Gb Swap, but since then the SSH terminal addon is keeping giving this error on boot (and keeps rebooting since I have activated the option to reboot on errors)

[00:42:29] WARNING: 
[00:42:29] WARNING: Logging in with a SSH password is security wise, a bad idea!
[00:42:29] WARNING: Please, consider using a public/private key pair.
[00:42:29] WARNING: What is this? https://kb.iu.edu/d/aews
[00:42:29] WARNING: 
cont-init: info: /etc/cont-init.d/ssh.sh exited 0
cont-init: info: running /etc/cont-init.d/user.sh
[00:42:35] NOTICE: Session sharing has been disabled!
swapon: /backup/_swap.swap: Resource busy
[00:42:36] FATAL: Failed executing init command: if [ ! -f /backup/_swap.swap ]; then fallocate -l 2G /backup/_swap.swap && mkswap /backup/_swap.swap && chmod 0600 /backup/_swap.swap && swapon /backup/_swap.swap ; elif [[ ! $(dmesg|grep _swap.swap) = *swap\ on* ]] ; then swapon /backup/_swap.swap ; else swapon /backup/_swap.swap ; fi
cont-init: info: /etc/cont-init.d/user.sh exited 1
cont-init: warning: some scripts exited nonzero
s6-rc: warning: unable to start service legacy-cont-init: command exited 1
/run/s6/basedir/scripts/rc.init: warning: s6-rc failed to properly bring all the services up! Check your logs (in /run/uncaught-logs/current if you have in-container logging) for more information.
prog: fatal: stopping the container.
s6-rc: info: service fix-attrs: stopping
s6-rc: info: service fix-attrs successfully stopped
s6-rc: info: service s6rc-oneshot-runner: stopping
s6-rc: info: service s6rc-oneshot-runner successfully stopped

I tried to reboot the whole HASSIO and also to power-cycle the Rasberry (Pi3) but nothing changed, I still have the error.

Anyway the SWAP file is still there, also after the reboots and also after deleting the addon setting that “creates” the swap and is giving the error.

My concer is that the swap maybe will not be used anymore after an update if I dont keep the setting saved in the addon.

Any idea why the error appears and how to fix?

Thanls

Seems I fixed.
I added

swapoff /backup/_swap.swap

as first command in the addon init config string.

Seems that I had to add it TWO times, because using just one entry was likely “skypped”, dont know why! Maybe is not the best solution, but seems to works quite fine, for now.

If anyone has a better solution or fix I’m open to suggestions

You’re telling me that if you remove the init_command line, then reboot, then open the shell, you are not able to manually remove the file? I highly doubt it because it should not be “busy”

They broke the swapoff command like a year ago. Easiest thing to do is reboot. Also share a screenshot of you YAML configuration for the add on but make sure to use what I have in the first post without the swapoff command which I removed a long time ago from my logic.

You are right, I supposed to have rebooted the Pi, but I only rebooted HAOS. Rebooting the Pi, deletes the swap without the init line. But again, if recreate the init line, reboot the addon, the swap creates. Then, if I reboot again the addon (or HAOS) the problem shows again…

This is my config

init_commands:
  - >-
    if [ ! -f /backup/_swap.swap ]; then fallocate -l 2G /backup/_swap.swap &&
    mkswap /backup/_swap.swap && chmod 0600 /backup/_swap.swap && swapon
    /backup/_swap.swap ; elif [[ ! $(dmesg|grep _swap.swap) = *swap\ on* ]] ;
    then swapon /backup/_swap.swap ; else swapon /backup/_swap.swap ; fi
packages: []
share_sessions: false
ssh:
  allow_agent_forwarding: false
  allow_remote_port_forwarding: false
  allow_tcp_forwarding: false
  authorized_keys: []
  compatibility_mode: false
  password: *******************
  sftp: false
  username: *******************
zsh: true
packages: []
share_sessions: false
ssh:
  allow_agent_forwarding: false
  allow_remote_port_forwarding: false
  allow_tcp_forwarding: false
  authorized_keys: []
  compatibility_mode: false
  password: *******************
  sftp: false
  username: ******************
zsh: true

Update the add on and make sure to provide the exact details of the problem. Your configuration looks fine

The addon is updated to the last available version.
I really dont know why I have the issue of the “busy” swap.
Thanks anyway.

I updated the command in the OP because the swapoff command is finally fixed. The newer version has a bit more feedback (echo statements) for the log which helps troubleshooting. The new version also makes it easy not to have to reboot and it survives reboots with ease, etc. Also resource busy errors should go away since I can use the swapoff command. Feel free to give it a shot… I just did :slight_smile:

1 Like

I’m not skilled at all, still waiting for the day that HA becomes user friendly to those without basic understanding of coding. I still don’t understand how this work, but I don’t have to thanks to the brilliance of @JZhass and this code. Thank you very much again!

Tried the fix before and it worked for a while, but something happened and then swap size flooded again, leading to crashes. Saw this thread and the updated code, I added it without removing the two other entries that I had in the SSH & Web Terminal (for some reason, it worked with two similar codes the last time I had problems, so I didn’t remove them).

Two uneducated questions:

  • Should I remove the top two lines of code in my config settings? Thinking that one should be enough and that the “extra” ones might cause some problem down the road.
  • Does the SSH & Web terminal need to be running constantly, or is it enough to enter the code line and start it once? EG, should I have the terminal set to start each time HA is rebooted for whatever reason?

My code:

init_commands:
  - >-
    if [ ! -f /backup/_swap.swap ]; then fallocate -l 2G /backup/_swap.swap &&
    mkswap /backup/_swap.swap && chmod 0600 /backup/_swap.swap && swapon
    /backup/_swap.swap ; elif [[ ! $(dmesg|grep _swap.swap) = *swap\ on* ]] ;
    then swapon /backup/_swap.swap ; else swapon /backup/_swap.swap ; fi
  - >-
    - if [ ! -f /backup/_swap.swap ]; then fallocate -l 2G /backup/_swap.swap &&
    mkswap /backup/_swap.swap && chmod 0600 /backup/_swap.swap && swapon
    /backup/_swap.swap ; elif [[ ! $(dmesg|grep _swap.swap) = *swap\ on* ]] ;
    then swapon /backup/_swap.swap ; else swapon /backup/_swap.swap ; fi
  - >-
    if [ ! -f /backup/_swap.swap ]; then fallocate -l 2G /backup/_swap.swap &&
    mkswap /backup/_swap.swap && chmod 0600 /backup/_swap.swap && swapon
    /backup/_swap.swap && echo SWAP_NEW_FILE_CREATED_USED ; elif [[ !
    $(dmesg|grep _swap.swap) = *swap\ on* ]] ; then swapon /backup/_swap.swap &&
    echo SWAP_USING_OLD_FILE ; else swapoff /backup/_swap.swap && swapon
    /backup/_swap.swap && echo SWAP_FINAL_ELSE_STATEMENT ; fi
packages: []
share_sessions: false
ssh:
  allow_agent_forwarding: false
  allow_remote_port_forwarding: false
  allow_tcp_forwarding: false
  authorized_keys: []
  compatibility_mode: false
  password: XXX
  sftp: false
  username: hassio
zsh: true

does anyone know what happens to the original ram swap file with this fix? Is it released, or still used as part of the swap process?

@Fete you should definitely only have one of those lines, not three. You may be causing an issue by having 3 lines but not a huge issue that will break your system so not to worry. I guess you can kill the addon as the command already ran. Try it out and see if the swap size (use Glances addon or htop command) remains large after killing the addon. I believe it does stay…