How to increase the swap file size on Home Assistant OS

Hi JZHass, bkbartk and others,
Just a quick update on performance of my HA system since implementing memory swap & swap file allocation via Terminal and SSH init_commands. The graphs below showing critical memory levels and swap file levels are typical since implementation. System stability has been very good with only one unexplained system failure over the last month.


Cheers,
Phil.

1 Like

This approach certainly works and does magic on my system.

Is there any attempt to make something official to change this under advanced settings, instead of relying on this cludge?

When I updated my core recently, the change did not survive, and it took me a bit to realize that the update had somehow wiped out the init commands. Of course, troubleshooting is annoying as well, because you have to get into the settings before the system overloads the swap file and dies.

It seems as if the standard swap file is probably not a good setting for a large chunk of the installed base, and yet we keep having to do this. Were I knowledgeable enough to figure this out myself, it would be my first priority. Is there something that I am missing that lets this issue linger on? It surely must discourage a lot of people who start out with an unstable system very early on.

2 Likes

@ncfoster in the OP click on the github issue link and feel free to make your comments there. That’s the official bug intake and it didn’t get the attention we all had hope and hence this solution.

1 Like

Did you all create template sensors to monitor the sensor.swap free, use, and use % or did I miss a step that creates those entities?

If you used a template, do you mind posting it?

There was no step to create those entities just enable the systemmonitor integration by adding YAML to your configuration.yaml file as described here System Monitor - Home Assistant

Ah, thanks. That was one of the first things I configured when I got started, so I forgot it was dependent on the parameters I specified…doh!

This

  - 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

…gets formatted like follows in my SSH & Web Termional Configuration.

init_commands:
  - >-
    if [ ! -f /backup/_swap.swap ]; then fallocate -l 1G /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; fi

Anyway, according to the log the result is this:

...
[cont-init.d] user.sh: executing... 
[22:05:58] NOTICE: Session sharing has been disabled!
swapon: /backup/_swap.swap: Resource busy
[22:05:58] FATAL: Failed executing init command: if [ ! -f /backup/_swap.swap ]; then fallocate -l 1G /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; fi
[cont-init.d] user.sh: exited 1.
[cont-finish.d] executing container finish scripts...
[cont-finish.d] 99-message.sh: executing... 
-----------------------------------------------------------
                Oops! Something went wrong.

 We are so sorry, but something went terribly wrong when
 starting or running this add-on.
 
 Be sure to check the log above, line by line, for hints.
-----------------------------------------------------------
[cont-finish.d] 99-message.sh: exited 0.
[cont-finish.d] done.
[s6-finish] waiting for services.
[s6-finish] sending all processes the TERM signal

Running Raspberry Pi 4 4GB on SSD boot, HAOS 7.6, core 2022.5.4 + latest version of the addon.

What might be the problem?

Notice how it says “resource busy” in the error. That means reboot which will release that currently busy file. Another thing to try is to remove that file after a clean reboot without my command being ran… clean boot for the same reason, busy resource.

Hmmm… There seems to bew 2 different versions of the script that I have grapped from these threads. I have 2 different systems, hw-wise identical and both using HAOS etc…

The first one is

init_commands:
  - >-
    if [ ! -f /backup/_swap.swap ]; then fallocate -l 1G /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; fi

…and that seems to work, at least it does not generate error messages.

The second one, with that else in the end, is as follows:

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

…and this one generates the error

  • What is that fi in the end of both of these by the way?

Some observations on the file created:

  • There does not seem to be any chnage in file “Date modified” and “Date accessed” dates when monitored via Samba.
  • How do I check the system is using the swap file? System monitor "swap_free and “swap_use” generate rather strange values with use/ free combinations like 5MiB/1MiB, , then 7/9, 4/9, 10/7, 10/10 etc… etc…
  • deleting the file makes system to regenerate it again immediately, with exactly same date accessed and date modified parameters than the original
  • If I change that ...fallocate -l 2G to something else than 2G, it has no effect, the file stays at the original size

What happens when u use the working script on the other box? Make sure to take reboots… I’ll explain why later.

FI is the end of IF.

Read and look at screenshots in this thread for usage and how to tell. Glances addin is the easiest IMO. See screenshots…

Can i enable protection mode back after applying this or do i have to keep it off?

It’s a required step above so must stay if you want it to work.

Hi! I am taking this thread from today. Is the initial post up to date with the latest instructions? I tried it and I got the ressource busy issue too. I tried to reboot but I still have the same issue. Not sure what is the exact process to follow xD

Open the terminal window and run htop command… or install adding called Glances. Provide a screenshot from one or both. We will see if the swap file is indeed large.

Here it is

Grab a screenshot of your addin like so and might as well take the Configuration page too:

It seems that the add-on is now running properly however no effect on the swap file it seems.

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: 11.0.1
 You are running the latest version of this add-on.
 System: Home Assistant OS 8.2  (aarch64 / raspberrypi4-64)
 Home Assistant Core: 2022.7.2
 Home Assistant Supervisor: 2022.07.0
-----------------------------------------------------------
 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
[10:59:30] 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
cont-init: info: /etc/cont-init.d/ssh.sh exited 0
cont-init: info: running /etc/cont-init.d/user.sh
[10:59:43] NOTICE: Session sharing has been disabled!
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)
[10:59:44] INFO: Starting the ttyd daemon...
s6-rc: info: service legacy-services successfully started
[10:59:44] INFO: Starting the SSH daemon...
Server listening on 0.0.0.0 port 22.

Server listening on :: port 22.

[2022/07/09 10:59:45:2978] N:  Using foreign event loop...

Check again that you truly don’t have swap enabled. I don’t see errors in your log paste so not sure where you were catching that error.

Lastly try using a simpler config… meaning just take the stock config, change the password to something other than empty and just copy in my init_command line. Meaning just test with that first w/o the ssh key, etc. Here’s mine:

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: sumthngdifficult321@
  sftp: false
  username: admin
zsh: true

Still the same issue