How to increase the swap file size on Home Assistant OS

I think that a reboot of the host would have worked, because then, the swap didn’t existed yet.
restarting the addon was the problem. (restart after the upgrade of the SSH-addon)

Sadly after a certain version of the SSH addon the swapoff command stopped working. This means that after an upgrade of the addon, it’s best to reboot the host.

Hi,

with that code

init_commands:
  - >-
    if [ ! -f /backup/_swap.swap ]; then fallocate -l 8G /backup/_swap.swap &&
    mkswap /backup/_swap.swap && chmod 0600 /backup/_swap.swap && swapon
    /backup/_swap.swap ; elif [[ ! $(cat /proc/swaps|grep _swap.swap) =
    *_swap.swap* ]] ; then swapon /backup/_swap.swap ; fi

It returns below

SWAP 0%
total: 2.93G
used: 0
free: 2.93G

I would expect 8G. Am I wrong?

I’d expect to see that as well… however, 8GB is pretty large and likely unnecessary. Try messing with the value. Use 1GB then 2GB and see whether the number changes. We may have hit some hard limit.

You can also manually run a few of those commands using a new/different file name. Maybe each file has a limit of 2GB, something like that…

1 Like

Unfortunately my swap is back at 100% again, after working more than fine for several months thanks to @JZhass great help here!

Might be related to recent update to:

Home Assistant 2022.10.4
Supervisor 2022.10.0
Operating System 8.2
Frontend-version: 20221010.0 - latest

Anyone else experiencing the same problem?

Nope …

All good on my RPI4.

It could be one of your add-ons. Try disabling most/all heavy ones then enable one at a time while monitoring swap.

Thank you for your reply, it’s really appreciated!

I tried disabling heavy ones lika Influxdb and Grafana, and about everything except Deconz.

Didn’t help much, went down to ~93%

I then checked your initial post and compared the code with the code I have sed in SSH web terminal and noticed that a " - " was missing. For what reason, I don’t know. I have not changed anything in there since I used your method a couple of months back. I don’t know if the " - " matters but i just put in your code from the top of this page under the old entry and started SSH web terminal and just like that the swap was down to 10% :smiley:

Started the heavy addons again and I’m currently at 15%.

Since my knowledge of this equals null, I have no idea what happened, what fixed it and what the two different codes in the SSH web terminal does. But I’m leaving this post here for you skilled people and for someone that might stumble across the same problem one day, perhaps it can be of some use!

Thank you again, I’m cincerely grateful for your method in this thread!

did anyone succeed in using the swap command with the latest version 12.1.2 of the add-on?

yes, 12.1.2 works for me, here is my code. I’m using Hassos on a pi4.

#!/bin/sh
# put this script in /backup/swap.sh
# chmod 755 /backup/swap.sh
# add "/backup/swap.sh" as an init_commands: option in "ssh & web terminal"
# addon configuration
# disable protection mode in addon

if ! grep swapfile /proc/swaps
then
echo "adding swap space"
if [[ ! -f /backup/swapfile.swap ]]
then
echo "creating /backup/swapfile.swap"
dd if=/dev/zero of=/backup/swapfile.swap bs=1M count=$((8*1024))
mkswap /backup/swapfile.swap
fi
swapon /backup/swapfile.swap
else
echo "swap space found"
fi

Hi, unfortunately it didn’t work for me: after following the steps you mentioned, the result is that after a reboot of the host the swap memory remains at 100%.
Only after manually executing the commands:

fallocate -l 2G /backup/_swap.swap
mkswap /backup/_swap.swap
chmod 0600 /backup/_swap.swap
swapon /backup/_swap.swap

I successfully allocate the swap, and the swap memory instantly decreases at 15%.
So at this moment I can’t succeed in executing these commands automatically at every startup.

Any suggestions for troubleshooting your method? In your case you verified that the swap memory is correctly allocated automatically at startup?

Just upgraded everything to the latest in my environment and all still seems to work fine. Glances still showing 2.6GB total swap after a reboot of HA OS. I’ll do another reboot at the hypervisor level to make sure but I don’t see any issues. Would be interesting if others have issues… considering how many views this post has, there are MANY users :slight_smile:

Hello, could you please post a screenshot of your SSH & Web Terminal Config? Have setup a simple bash script that runs from the terminal and enables swap. However, when I place it in the config section, nothing happens.

You are doing something different then my technique. Not sure why you want to put a script and deal with permissions, etc. That concept does not survive upgrades as easily — but that’s your choice and your problem though I’d recommend sticking to my original method documented in the first post. As you see nobody else is having issues so why go outside of the box and create problems for yourself when the original method has proven functional.

See the original post at the top along with this one

Thanks for the response. I agree it is different than your technique above as it did not work. Like @Vinz87, I had to manually create the swap file then on HA system reboots I just have to type “swapon /backup/_swap.swap” to enable the swap. See screenshot. Have included the if statement in the config and rebooted. As you can see from HTOP that the swap is unchanged. Pelease advise on why it might not be running the command?

firstly, protection mode must be disabled in the add-on. I don’t think the script will work when put in the /config directory, put it in /backup as described in my post, /backup will survive any update also. then you have to set the script executable by doing this; chmod 755 /backup/swap.sh

The script is located in the /backup directory. Have ran chmod 755 on ~/backup/_swap.sh. Also, protection mode is disabled on the add-on. For your add-on config, did you use:

init_commands:
- /backup/swap.sh
or
init_commands:
- bash /backup/swap.sh

Thanks.

I think I see your problem, remove the ‘~’ from before /backup

Have tried multiple iterations of the command. “bash ~/backup/_swap.sh” and “bash /backup/_swap.sh” both work in the terminal. But in config, neither work.

what is your option yaml look like? also in the terminal do ‘ls -l /backup’

I don’t see anything in the log which indicates the swap.sh command is being executed.