How to increase the swap file size on Home Assistant OS

Take a screenshot of the SSH configuration page…

Can’t even make a start! Normally I’m further in before crying for help.

I’m getting “502: Bad Gateway” as soon as I try to open web UI

Home Assistant Core 2022.6.7
Home Assistant Supervisor 2022.07.0
Home Assistant OS 8.1

Not sure what step you are at… each of those add-ins has a LOG tab that could have good info. Also look at this post which should get you running:

This is the result if you haven’t populated the password field. It needs to be anything except empty

Looks like one of my steps in the OP was ignored… thanks for weighing in @justcop

I have been using the increase swapfile on my RPI3B+ for past year. Was talking with @in2deep over at https://community.home-assistant.io/t/home-assistant-keeps-crashing/443119/9 about the risks to the SSD. I’m happy to put a bit of wear on my SSD ( and continue to use my 3B+) but have you any thoughts on it.

@Spiro, it’s actually really simple… forget HassOS for a sec and ask yourself this: can I use an SSD for Linux? The obvious answer is yes you can… and guess what, you’ll want swap space on it. Same goes for Windows, the page file is used all the time because some OS components are programmed NOT to use RAM and use the page file instead (if available, of course because it can be disabled).

My point is that you should never be afraid to “use” your hardware for its designated purpose. Will it die? Yes eventually but that could be 5 years, 10 years or even 20 years. The key to your dilemma is to have superior backup strategy where losing a drive can be easily recovered. So that’s my take on it :slight_smile:

Use something like Acronis True Image or one I’ve been loving lately DiskGenius to grab a bare metal backup. That’s the way to solve your worry.

3 Likes

Hi!
I get this error when i try to start the terminal,

/etc/cont-init.d/user.sh: eval: line 93: syntax error near unexpected token `then'
/etc/cont-init.d/user.sh: eval: line 93: `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'
[16:47:23] FATAL: Failed executing init command: 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

I got it to work after i fixed some spaces.

Do you have the correct string as well? (I’m trying to do this as well and are getting the same error as you)
Never-mind: I just grabbed the yaml from here:

@thomasw and @the-programmer weird that two happened at the same time. Are you guys able to just copy and paste the full command from the 1st post as-is and save? After it saves it will re-format the single line into multiple but you shouldn’t care. I want to make sure the very first post has a well formatted, copy paste friendly command and you guys are scaring me :slight_smile:

1 Like

Sorry for scaring you. However it is just a coincidence. See also this one from vsauce:

Anyway, personally I would prefer to get a yaml line since that is somehow more accurate when copy-pasting.
However I am thinking of creating an issue/feature request on the home assistant bugtracker if someone can make an add-on for this task.
For me, this should be solved soon when I switch to a PI 4 (4Gb) in a home assistant yellow.

Since the last upgrade of the SSH en Web Terminal, i got an error when restarting the addon. (init script failed to start, because swap file was in use, no exit code 0?)
I then changed it to:

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 [[ ! $(cat /proc/swaps|grep _swap.swap) = *_swap.swap* ]] ; then swapon /backup/_swap.swap ; fi
FWIW…
regards,

1 Like

I’ll look at the diff in a bit… however, did you simply try a restart of the host not just a restart of the add on?

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.