How to increase the swap file size on Home Assistant OS

@richardcalgary403 it remains there and it’s not messed with because it’s done at a different level of Docker and inheritance, etc. It shouldn’t be of much concern as you can size my logic up or down as needed to offset what’s already there.

Thank you, it works perfectly

1 Like

Just wanted to let you know that during a reboot, if the addon isn’t checked to start when HA starts, the command doesn’t work. You probably already know that and I’m missing something, but anyway.

And the strange thing, when I saw that my swap was full again after a reboot, I removed the first two (old versions of your brilliant command) entries in my .yaml and left only the latest one, found at the very first post in this thread. Then I started SSH & Web terminal. To my surprise, it did nothing to my swap size.

I then stopped the terminal, added the first two versions of the command again, saved and started the terminal. And my swap size went from 100% to 10%.

I really don’t know why, but I thought I would let you know. So if I have three entries in the .yaml, with three different versions of your command, then it works. If I only have the latest version, it does nothing, atleast not on my setup.

This is way beyond my skill level so I can’t even guess why, but your the man with the skill, so there you have it :slight_smile:

Thank you once again, your coding is probably what makes me able to run Home Assistant at all. Would probably give up if the crashing/lagging/hangups continued, so I am very grateful! :smiley:

Very strange… I know there are many users of my method and I’d imagine lots would be experiencing it. I’ll try my setup which is simply a virtual machine done with an OVA image. I’ll take reboots just to see the results… though I’ve done that plenty before updating my command to include swapoff that broke at some point then became functional again a few months ago.

What I am trying to confirm is;; is the original swap file in memory and is it still used after adding the disk swap area?

Yes, there is no easy way of disabling it unless you follow the way more complex instructions on the GitHub post. It will use the original swap file first and then will spill over into the new one created with my instructions.

Hi fellow HAOS Swapfile fans,

My dev HA system (HAOS installed version 9.5) runs on a RPI 3B+ and a 32GB SD card for system and data storage. I am wanting to try a more permanent fix for the Swap file issue per solution by [@Biont]'s github posting on 14 Oct 2021 but I’m delayed by my lack of familiarity with Linux udev.

In the meantime I’ve been trying to re-configure my year ago HA instance with an enlarged swap area file on startup per JZhass posts over the last couple of years. I’m using the community SSH & Web Terminal addon with an ‘init_commands:’ script to allocate a 2GB swap file in the Backup folder. But I couldn’t get the addon to SAVE the script (UI mode or YAML mode) and got an error message:

‘Failed to save add-on configuration, Invalid list for option ‘init_commands’ in SSH & Web Terminal (a0d7b954_ssh).’

The YAML file looked like:

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
packages: []
share_sessions: false
ssh:
allow_agent_forwarding: false
allow_remote_port_forwarding: false
allow_tcp_forwarding: false
authorized_keys: []
compatibility_mode: false
password: …redacted…!
sftp: false
username: root
zsh: true

I tried running the individual commands via ssh checking the proper file creation occurred. The following were the command lines and return message:
‘fallocate -l 2G /backup/_swap.swap’
‘mkswap /backup/_swap.swap’
Return message was:
Setting up swapspace version 1, size = 2147479552 bytes
UUID=7a48b755-4066-471f-9d01-43e7d643ba1b"

Then ‘chmod 0600 /backup/_swap.swap’
‘ls -al’ confirmed the file’s existence and permission:
total 2097328
drwxr-xr-x 2 root root 4096 Jan 28 06:22 .
drwxr-xr-x 1 root root 4096 Jan 29 09:36 …
-rw-r–r-- 1 root root 143360 Jan 25 16:39 269e861c.tar
-rw-r–r-- 1 root root 10240 Jan 28 06:22 6a6485d4.tar
-rw------- 1 root root 2147483648 Jan 27 09:06 _swap.swap
-rw-r–r-- 1 root root 10240 Jan 27 07:58 b684cf86.tar

return message was:
Tried to add the init_command ‘swapon /backup/_swap.swap’ to the community addon but got the error:
Failed to save add-on configuration, Invalid list for option ‘init_commands’ in SSH & Web Terminal (a0d7b954_ssh). Got {‘init_commands’: ‘swapon /backup/_swap.swap’, ‘packages’: [], ‘share_sessions’: True, ‘ssh’: {‘allow_agent_forwarding’: False, ‘allow_remote_port_forwarding’: False, ‘allow_tcp_forwarding’: False, ‘authorized_keys’: [], ‘compatibility_mode’: False, ‘password’: ‘…redacted…’, ‘sftp’: False, ‘username’: ‘…redacted…!’}, ‘zsh’: True, ‘log_level’: ‘debug’}

The init_commands script worked without hitch in Jan 22 but not recently. I wondered if I may have done something wrong or maybe later versions of the Home Assistant Community Add-on: SSH & Web Terminal (version is 13.0.2) don’t allow the swap script to be inserted?

Then I looked back at previous postings I noticed a couple of different versions of the script in the ‘init_commands:’ headings:
a) some started the next line with a few text symbols ’ - >-’ prior to the script starting on the following line ’ if …’
b) others started the script line after the heading with a dash ’ - if …’ (e.g. emeyedeejay MJ in Nov 21).

I tried the a) version of the script headers above and it saved okay and appeared to run properly at startup. Screenshot of the YAML that worked:

If anyone knows what the different script headers are for I would appreciate their feedback.

Due to some problem with the sensors in systemmonitor not being created, I haven’t proved to myself that the swapfile is actually operating, but the file is there and has survived a number of RPi reboots since 27 Jan 23. I’ll report back when I can get the systemmonitor sensors working. I guessed it may need an MQTT configuration for each sensor but I don’t see that in my sensor configuration in the past?
Cheers,
PK.

Hey @pkelloway so your screenshot is EXACTLY what you want to see when you come back to the YAML config. I gave the full command up in the first post. When you paste it and hit save, HA will automatically “indent” everything and give you that - >- at the beginning. This means everything worked correctly and your long command was accepted by the HA YAML validation (and slight re-format). I was pretty sure that this was your exact problem when you posted on GitHub. You just have to take my command and replace the original init_commands:[] that you see in the default YAML.

Sounds like you’re good to go… if you want to see it working then either use Glances add-in or open the shell that you installed from my OP and type in htop — look for swap usually at the top of either one of the tools.

Thanks JZhass for your response and information about the HA YAML validation, its script reformat and the added headers. Yep, I’m good to go for now. I’ve a few too many things on the go right now so I haven’t had time to try to Glances add-on or the htop. But I will, and I’ll update the post.
Cheers,
PK.

Seems like I have everything configured correctly but my swap shows no use. Any ideas on what I may have done wrong?

Looks right to me… your system just didn’t need to use it but you have plenty available now which this solution achieves.

Thank you for the guide. I have a dumb question: in case I have an hybrid storage (SD card + SSD as data disk), is the swap file stored on the SD or on the SSD?

It’s wherever the /backup folder is located since that’s where the file is created. Remember you are welcome to modify that (in the few places) with the command… you don’t have to run that command as is. Feel free to change it and point to whatever folder/disk you want.

Thank you, Then I’m good to go. Great solution! Works great.

So this is great, thanks. What I don’t understand though, is do I have to set the add on ‘Start on boot’?
What I think is happening is that every time HAOS restarts, the addon is restarted and the new swapfile config is applied. Is that right?

Is this problem with the swap file filling up just a result of migrating the Rpi from an SD config to an SSD as some of the other comments imply? If I just did a clean install of HAOS onto the SSD, would this problem go away and avoid me having to have the ssh addon installed with the script running each time?

Of course you start it on boot because you want that swap file available for other stuff that you’re running.

If you’re running plain old vanilla HAOS then you will not need this. As soon as you add a few integrations and some are not “optimized”, they will chew through the default HAOS swap allocation in a matter of a few hours. So if you’re running a super simple HAOS with a few small integrations you will likely not experience the issue. However, try compiling ESP Home and you will quickly fail. This is where the “room to grow” with memory comes into play… the more you add to HAOS the more likely you will blow it out of memory. And no the problem will not go away until you remove the memory hog components/integrations that you’ve added to HAOS.

I highly recommend this (obviously :slight_smile:) because it’s a safety-net. You never know when a component version has a small bug that chews up memory but you have no idea which integration that is, and more importantly, you still need/want that integration even if it’s buggy with memory management.

For crazy reasons (crash, power outage, etc…), I lost my swap after a full restore but my instance was not able to load fully for me to add the init_commands in the add-on. It was out of memory and crashing.

To solve the issue, I resized the hassos-data partition and added a swap partition, see all the commands and results below, hope this might help someone in a similar situation.

omelette@omelette:~/Downloads$ sudo e2fsck -f /dev/sdc8
e2fsck 1.46.5 (30-Dec-2021)
Pass 1: Checking inodes, blocks, and sizes
Inode 135489 extent tree (at level 1) could be shorter.  Optimize<y>? yes
Pass 1E: Optimizing extent trees
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information

hassos-data: ***** FILE SYSTEM WAS MODIFIED *****
hassos-data: 144352/3874816 files (0.0% non-contiguous), 878750/15468283 blocks
omelette@omelette:~/Downloads$ sudo resize2fs /dev/sdc8 48G
resize2fs 1.46.5 (30-Dec-2021)
Resizing the filesystem on /dev/sdc8 to 12582912 (4k) blocks.
The filesystem on /dev/sdc8 is now 12582912 (4k) blocks long.

omelette@omelette:~/Downloads$ sudo fdisk /dev/sdc

Welcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

A hybrid GPT was detected. You have to sync the hybrid MBR manually (expert command 'M').

Command (m for help): p

Disk /dev/sdc: 59.69 GiB, 64088965120 bytes, 125173760 sectors
Disk model: SD  Transcend   
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 6433A305-2E32-4085-975B-138CE6B4F6B2

Device       Start       End   Sectors  Size Type
/dev/sdc1     2048     67583     65536   32M Microsoft reserved
/dev/sdc2    67584    116735     49152   24M Linux filesystem
/dev/sdc3   116736    641023    524288  256M Linux filesystem
/dev/sdc4   641024    690175     49152   24M Linux filesystem
/dev/sdc5   690176   1214463    524288  256M Linux filesystem
/dev/sdc6  1214464   1230847     16384    8M Linux filesystem
/dev/sdc7  1230848   1427455    196608   96M Linux filesystem
/dev/sdc8  1427456 125173726 123746271   59G Linux filesystem

Command (m for help): d
Partition number (1-8, default 8): 8

Partition 8 has been deleted.

Command (m for help): n
Partition number (8-128, default 8): 
First sector (1427456-125173726, default 1427456): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (1427456-125173726, default 125173726): +48G

Created a new partition 8 of type 'Linux filesystem' and of size 48 GiB.
Partition #8 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: N

Command (m for help): n
Partition number (9-128, default 9): 9
First sector (102090752-125173726, default 102090752): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (102090752-125173726, default 125173726): +4G

Created a new partition 9 of type 'Linux filesystem' and of size 4 GiB.

Command (m for help): t
Partition number (1-9, default 9): 
Partition type or alias (type L to list all): swap

Changed type of partition 'Linux filesystem' to 'Linux swap'.

Command (m for help): w
The device contains hybrid MBR -- writing GPT only.

The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

omelette@omelette:~/Downloads$ sudo mkswap /dev/sdc9
Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)
no label, UUID=2b162ac5-3c33-457b-a7ee-bd30258d038a

Glances:

Hello everyone,

I just wanted to share that I wasn’t very keen on using an addon that wasn’t dedicated to the task of increasing swap memory in Home Assistant, even though it was a great workaround (thanks JZhass!). So, I spent a few hours creating a simple addon to accomplish this task, and I’ve shared it on my Github repo. I hope this can help a few people out: GitHub - TazzerMAN/increase_swap_addon: This add-on increases the swap space on your system initially created for Rasberry Pi.

Let me know if you have any questions or feedback.

11 Likes

Works like a charm!
Thanks!!

1 Like

Awesome! This is a perfect way to go… I just found somebody else’s workaround and built a few IF statements to ease the task but your add-on takes it to a new level. Thanks for knocking it out and actually giving us all a super-simple example of an add-on.

@TazzerMAN, I had it set to 2GB and it worked great. Then I changed to 3GB, rebooted and restarted everything including the add-on but it’s still keeping the original size of the file though the radio button is different now. Looks like a bug but small one since 2GB is more than plenty to get folks operational for many other components to leverage.

1 Like