How to increase the swap file size on Home Assistant OS

for me give this error

[11:00:02] NOTICE: Session sharing has been disabled!
swapon: /backup/_swap.swap: Resource busy
[11:00:03] 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 && swapoff /backup/_swap.swap && swapon /backup/_swap.swap ; elif [[ ! $(dmesg|grep _swap.swap) = swap\ on ]] ; then swapoff /backup/_swap.swap && swapon /backup/_swap.swap ; else 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…

Thanks, this really works:

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

But it is necessary to make an explicit clarification that for this it is necessary to use an UNOFFICIAL add-on. Having an official add-on with the exact same name makes the road to victory a long one.

Is it possible to make the SWAP section 1Gb in size. Replacing “2G” with “1G” does not change anything.

Of course it works :slight_smile: and I will make it clearer that the UNOFFICIAL addon is required in the OP.

My guess as to why 2G vs. 1G is not working is because the file already exists. So to do 1G, just remove the command from the addon config. Then take a reboot, open up SSH/terminal in any way you want. Go to the folder and DELETE the file. Then go back to the addon config and add the command back with 1G like you want followed by a reboot or a restart of the addon.

Take a reboot for this and make sure you’re using the unofficial addon not the official one.

Some considerations:

  • on my Raspberry Pi 4 with boot from microSD and data on external hard drive, for a few days the system restarts itself two or three times a day

-now implementing the recommended change, the database has increased me by 2 GB, but the home assistant system has not restarted by itself for a couple of days now !

I had this same issue after moving to an SSD drive. (Boot files on SD card).

I couldn’t get it to “auto apply” with the init[] commands in the SSH &Web terminal add-on but I SSHd to the Pi and ran them manually. Seems to have worked and now have a 2GB swap file/partition.

This may help somebody who has the same.

Before:

After:

Try the command again from the first post. I removed the first swapoff call… Very sad that swapoff was broken with updates.

If you still get the error I’ll remove the second swapoff command as well. Take a reboot after applying the command as well.

Thanks for the info - mine is set now after running the various commands manually.

It won’t be set after a reboot which is the most attractive aspect of this solution.

Ah! Bugger!!

OK - trying now.

I get this:

swapoff: /backup/_swap.swap: Invalid argument
[21:20:31] 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 swapoff /backup/_swap.swap && swapon /backup/_swap.swap ; else 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...`

Grab the command again from the first post. I removed every swapoff command since it seems to be broken with these current versions of busybox.

That did it … survived a reboot now… thanks a stack!

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

1 Like

Ok, this solved my problem. Thx dude.

2 Likes

Thanks to JZ-SmarThings and bkbartk for assistance. Swap file creation and swap memory allocation now appears to have been successful.

Terminal & SSH init_commands set 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
    packages:

Checked sensors for RPi memory allocation:
sensor.memory_free 207.8 MB
sensor.memory_use 702.8 MB
sensor.memory_use_percent 77.2 %
sensor.swap_free 1934.1 MB
sensor.swap_use 341.3 MB
sensor.swap_use_percent 15.0 %

(i.e. looks okay but only a snapshot)

Checked backup directory for 2GB _swap.swap file creation.
.
-rw-r–r-- 1 root root 317460480 Dec 29 20:07 9b635441.tar
-rw------- 1 root root 2147483648 Jan 5 17:12 _swap.swap
-rw-r–r-- 1 root root 390932480 Dec 31 04:12 db0d8ce8.tar
.

Checked Supervisor log:
[18:26:09] INFO: Starting the ttyd daemon…
[18:26:10] INFO: Starting the SSH daemon…
Server listening on 0.0.0.0 port 22222.
Server listening on :: port 22222.
[2022/01/05 18:26:11:4225] N: Using foreign event loop…
Connection from 10.1.1.41 port 51763 on 10.1.1.111 port 22222 rdomain “”
.
.

I think it’s worked well. I’ll monitor memory allocation, stability and performance for any kinks.

One last question - should Protection mode be left disabled for future system reboots?

Thanks.

Yes, it’s a requirement or expect it to fail otherwise. That swapon command requires root privileges.

Let us all know if it improved your stability…

Thanks JZhass,
I have a daily restart automation set up on my HA. The SSH & Web Terminal init_command logic ran without issue. Also HA system appears much more stable with memory allocation generally less than 80% and the 2GB swap file working as hoped with around 80% of headroom. UI performance across the board appears much better with history graphs and other monitors loading within a couple of seconds like the HA of old. Will continue to monitor for a week or so and report any issues. Hope the SD card survives till I migrate to a USB portable drive.
Cheers,
Phil.

1 Like

This worked great, until the 10.0 Update for the SSH addon. Now I get swapon: /backup/_swap.swap: Resource busy (which should be fine), followed by FATAL: Failed executing init command: (your command here) and that blocks the whole SSH addon from starting :confused:

Did you reboot?

EDIT — I’m on v10 and it still works for me. The busy resource piece means it’s still being used so it trips the entire command. I used to have swapoff then swapon there but that command is friggin broken in busybox. So reboot is likely your answer.

1 Like

I tried that, same issue after a reboot :confused:

edit: Unless I accidentally only rebooted HA and not the whole PI. Let me check :smiley:

edit2: Yeah, I wanted to try it with a real restart, but didn’t. After properly restarting the full device, it works. Thanks :smiley: