How to increase the swap file size on Home Assistant OS

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

No there is no issue… have you looked under the swap space section of your screenshot? 2.45GB means it worked…

Ah indeed, good job! But how come it’s still so much full? I think it is coming from MotionEye but I have only one camera…

That’s a different issue altogether… turn off different add-ons and pay attention to the usage of swap.

@JZhass I just wanted to say thank you for creating this thread and for your solution to increase swap size in Hassos. I eventually got it right and it seemed to cure a problem that I was having when HA froze up and crashed repeatedly. Seems that it was caused by swap usage at 100%.

Really grateful for your method, keep up the good work! :slightly_smiling_face:

1 Like

Thanks @Fete, much appreciated! Very happy to hear the positive experience with my work.

1 Like

Unfortunately i’m getting this error:

/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 ; else swapon /backup/_swap.swap ; fi' [09:56:11] 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 ; else swapon /backup/_swap.swap ; fi cont-init: info: /etc/cont-init.d/user.sh exited 1 cont-init: warning: some scripts exited nonzero s6-rc: warning: unable to start service legacy-cont-init: command exited 1 /run/s6/basedir/scripts/rc.init: warning: s6-rc failed to properly bring all the services up! Check your logs (in /run/uncaught-logs/current if you have in-container logging) for more information. prog: fatal: stopping the container. s6-rc: info: service fix-attrs: stopping s6-rc: info: service fix-attrs successfully stopped s6-rc: info: service s6rc-oneshot-runner: stopping s6-rc: info: service s6rc-oneshot-runner successfully stopped

what should I do? :slight_smile:

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.