How to increase the swap file size on Home Assistant OS

Increasing the swapfile size (especially on a USB SSD attached to my test RPi 3B) is pretty typical when trying to get a Linux box to perform and generally be more stable…

After hanging around on this GitHub issue and watching the progress, I believe there’s a simple and repeatable way to increase the swapfile size.

This solution will survive reboots and upgrades. You also never have to actually open the shell itself.

Please keep in mind that this is strictly for HA OS not Raspberry Pi OS aka Raspbian which has plenty of instructions of how to create and manage swap space.

The simple steps:

  • Have a functional HA OS box
  • Enable “Advanced Mode” for the user profile in HA (click on the user on the bottom left navigation bar in HA and scroll down). If you don’t do this, you will not be able to see this SSH add-on. See the warning at the very bottom of add-ons which states to enable this when not seeing a full list.
  • Install an add-on called “SSH & Web Terminal” — this is the UNOFFICIAL add-on. The official one will not work.
  • Disable Protection Mode for the add-on
  • Under the Configuration for the add-on, make sure to press the ellipsis (3 dots) on the right of the word “Options” and select “Edit in YAML”
  • Change the username and password to something NOT simple because it does validate your entry against the “have I been pwned” database.
  • Also under the Configuration for the add-on in YAML edit mode, replace the init_commands: [] line at the very bottom with the following and start the add-on:
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 && 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

46 Likes

That’s pretty neat.

I use the same addon and can call this command manually:

docker exec addon_a0d7b954_influxdb du -s /data/influxdb/data/homeassistant

Is there a way to run the command regularly (every 180 sec or so) and get the result into home assistant using init_commands?

Won’t a command line sensor or even glances return that for you/

Glances has too much overhead (if it is possible).

For the command line see:

OK so I use HA dockermon with curl that returns memory used for addons… Maybe look at that? Returns a bunch or other stats as well.

1 Like

Will do. Thanks.

Nice toy @DavidFW1960 — exposed API to docker is pretty cool.

@tom_l the thing I posted is really for “start-up” logic of the add-on and HA. It’s not really made to be a sensor because there’s no real output here (well except an actual file). I guess what you could do is indeed spit out a file to /backup and have HA parse it. If you want to execute something every X then take a look at the Linux “watch” command.

1 Like

Making some progress. Any further comments should be made over here to prevent derailing this topic.

I modified the command in the 1st post to have a bit more logic where it checks if swap is already on with dmesg and if so NOT to repeat the command. Otherwise the SSH add-on fails a restart… So this new update should make bullet-proof. Famous last words :slight_smile:

Thanks for putting this up here in the forums as well!

Hi I have tried to put the string in the Addon
This is my configuration:
authorized_keys:XXXXXXXXXX
password: ‘’
server:
tcp_forwarding: false
init_commands: >-
if [ ! -f /backup/_swap.swap ]; then fallocate -l 4G /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

But when it starts says this:
21-02-25 17:06:01 WARNING (MainThread) [supervisor.addons.options] Unknown options init_commands

How can i fix it.
Thanks
Sorry for my english.

Take a screenshot of the entire thing… either formatting is involved or you’re using the wrong addon.


Please see bullet #2 in the first post… you installed the wrong add-on

I’m just trying to go through this. My config for SSH & Web Terminal looks like this:

Is there anything else I have to do? Like creating the /backup folder or _swap.swap file?

I’m just a bit confused. I have a graph on my Overview showing free Swap space and it looks like this:

Screen Shot 2021-03-11 at 12.22.46

Have I got everything right?

Cheers
Andy

Looks like you got it all right. I would open up the shell and run the command htop… On the top left you will swap space and it should be bigger than the 200MB’s that I believe is done by default. Here’s a screenshot of mine:

1 Like

Thank you. Yes, mine looks fine. It’s just that it never seems to change so it doesn’t look like it’s being used.

Wow, great. Made my day after the second host reboot within 3 days due to swap above 80%
Thanks a lot!
Now I can wait until the home assistant blue! is available.

@jzhass Hi, today there was an update for the terminal ssh.
After the restart, I got this protokoll:

[cont-init.d] ssh.sh: exited 0.
[cont-init.d] user.sh: executing... 
[08:16:32] NOTICE: Session sharing has been disabled!
swapon: /backup/_swap.swap: Resource busy
[08:16:33] 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; 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.
s6-nuke: fatal: unable to kill: No such process

I deleted the init command, Started successful and added it again.

packages: []
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; fi

My question: will the addon be restarted during a normal HA restart or only when the host is restarted?
In the last case, the init command is only needed for a host restart. Can I let the command in the configuration so that it is used in case of a complete restart?

Have you tried restarting the host and seeing if htop command shows the swap space you expect to see?