How to increase the swap file size on Home Assistant OS

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

Anyone noticed increased swap and memory use after upgrade to HAOS 10.
My RAM use jumps to 97% after upgrade to HAOS 10 and even 10.1 doesn’t solve it.

image

even @falstaff321 suggestion doesn’t solve the issue `

swapoff /mnt/data/swapfile
dd if=/dev/zero of="/mnt/data/swapfile" bs=1M count=2048
mkswap /mnt/data/swapfile
chmod 600 /mnt/data/swapfile
swapon /mnt/data/swapfile

update:
Swap use also in over drive

image

I’ve upgraded and don’t have that issue. It kinda suggests that one of your add-ons or a device integration is the cause. I would try disabling some temporarily and enabling one at a time while watching swap use.

Thanks a lot for your feedback!

I just integrated in the last update this which corrects the behavior of the addon:

  • Support for resizing the swap file when the configuration changes.
  • Automatically remove old swap file when changing the swap location in the configuration.

I hope you like it :slight_smile:

1 Like

Nice work @TazzerMAN !!!

Minor inconvenience… I started the container after upgrading and had the default swap of like 800MB. Then I restarted, still 800. I then changed my 3072 back to 2048 and started, got my 2048. Then went to 3GB and restarted the container/add-on again and got my 3GB of swap. Works with minor quirks but no complaints, I like it :slight_smile:

1 Like

Hello,

Did anyone else have the issue that the default swapfile that HAOS created after first installation is huge (11GB), leaving only 500mb left in a 16gb ssd, causing to not restore a backup and continue?

So I want to reduce the swapfile but I can’t install any addons. I created a new topic, and because this is the most related topic that I found, I hope it’s ok to share my weird problem here.

Thanks very much for the Add-On. I can finally apply ESPHome updates without my Raspberry Pi 4 2GB locking up.

1 Like

Thanks! Works perfect :+1:
However, first install gave an error. After reboot, I was surprised to see addon was installed and working. :grin:

1 Like

Hi @TazzerMAN , or somebody else,

I’m using the addon but like to go back to default, so back to standard.
The reason why is because (I’m not 100% sure) my RPi powers off after x-hours after using the addon.
Can you let me know how to do that??

Thanks! Everything worked as advertised. Made my day. My RPi 3B will live another day.

1 Like

Uninstall and reboot.
Should be enough.

Brilliant, this should be an official addon! @frenck

1 Like

Does this mean its successful? How do I check?