home-assistant / operating-system

:beginner: Home Assistant Operating System
Apache License 2.0
4.58k stars 939 forks source link

How can i increase my swap file size? #968

Open basdejong95 opened 3 years ago

basdejong95 commented 3 years ago

HassOS release with the issue:

HassOS 4.15

Supervisor logs:

Journal logs:

Kernel logs:

Description of problem:

Hi everybody,

I am using home assistant on a RPI 4 with 1GB of ram. My swap file is only 230mb and fills up pretty fast, in the last week or so i had home assistant crashing a lot and i noticed my swap file is almost pretty full. While reading about this there are pretty clear instructions on the internet on how to increase the swap file on a standard RPI, but i cannot find it anywhere for HASSOS. Does anybody know a command or a setting i can change to increase this? I have a 128GB SD card which is A2 class so i dont mind creating a swap file of 1-2GB.

Thank you!

agners commented 3 years ago

Currently Home Assistant OS does not really support that use case. This is also taking a strain on your SD card as swap usually generates quite a bit of read and writes. Since we already have users which have problems with SD cards wearing out, in a lot of cases using swap on SD card does not make sense.

By default HAOS configures a part of the RAM as compressed swap storage (using zram). This helps quite a bit for boards with low memory.

That said, when you access HAOS directly (via SSH port 22222), you can create a swapfile and enable it as swap, but the swap will not be automatically re-enabled after a reboot:

# fallocate -l 4G /mnt/data/test.swap
# mkswap /mnt/data/test.swap
# chmod 0600 /mnt/data/test.swap
# swapon /mnt/data/test.swap
bachy commented 3 years ago

hi, my hassos lives in a rpi3b+ with a 120Go ssd connected via usb i moved the data partition on the ssd my memory is at 80% and swap at 100% i don't have so much devices and entities (357) (discovered HA 2 weeks ago) using rfxtrx, z-wave, zigbee top says that homeassistant is consuming 31%, mysqld 16%, systemd-journald 5%, node-red 5% and supervisor 2.5% i succesfully added a 4G swap file as describe above, i'll see if it prevent my instance to freeze is there any way to make it permanent ? i mean active on reboot i saw that there is nothing on /etc/fstab i know nothing about buildroot thanks

Jpsy commented 3 years ago

Do yourself a favor and switch over to an RPi4 with more than 1GB of RAM. You will only waste time with you trials on 1GB. The difference that more RAM makes is overwhelming.

bachy commented 3 years ago

Yes i know, unfortunatly my z-stick gen5 is not compatible with rpi4 and i'm lazzy to rebuild all my zwave mesh (qubinos wire pilote on the wall behind heaters)

frenck commented 3 years ago

Do yourself a favor and switch over to an RPi4 with more than 1GB of RAM.

Better yet, grab an ODROID N2 or N2+

basdejong95 commented 3 years ago

Do yourself a favor and switch over to an RPi4 with more than 1GB of RAM. You will only waste time with you trials on 1GB. The difference that more RAM makes is overwhelming.

Home assistant says on the getting started website that 1GB ram should work... And it would work if my swap file could be bigger. However there is no way to permanently change this, only the commands from @agners are a manual solution for now?

basdejong95 commented 3 years ago

image

this is my swap file within 1 day of rebooting

JZ-SmartThings commented 3 years ago

@agners that standard solution doesn't seem to work... see the error which suggests that BusyBox and not the full version of swapon is what HassOS seems to be using. Error you get is invalid argument for swapon. FYI, swapon -a ran fine but did not use the newly created swap file either. With my SSD plugged into the RPI3B+, it'd be nice to be able to control the default swap behavior in HassOS. image

QbaF commented 3 years ago

for me image

capstan1 commented 3 years ago

Currently Home Assistant OS does not really support that use case. This is also taking a strain on your SD card as swap usually generates quite a bit of read and writes. Since we already have users which have problems with SD cards wearing out, in a lot of cases using swap on SD card does not make sense.

By default HAOS configures a part of the RAM as compressed swap storage (using zram). This helps quite a bit for boards with low memory.

That said, when you access HAOS directly (via SSH port 22222), you can create a swapfile and enable it as swap, but the swap will not be automatically re-enabled after a reboot:

# fallocate -l 4G /mnt/data/test.swap
# mkswap /mnt/data/test.swap
# chmod 0600 /mnt/data/test.swap
# swapon /mnt/data/test.swap

This works like a treat! On my ssd that should be perfect to prevent filling up the ram completely.

Any idea on how to make this persistent?

dpjrodrigues commented 3 years ago

I do understand what @agners say and I do agree that if you use an SD card increasing the swap is not great but if you use an SSD then shouldn't be a problem.

Since few months ago my pi3b+ is getting very slow and HA ends up not responding. Restart fixes it and then after 1 week or so again the same thing. I’ve added few checks and realized that it may be related with memory… once swap hits close to 100% and RAM 90% this happens. I do not have that many addons and it was working ok before… there are few discussion on potential memory leaks but anyway it is not the purpose of this topic.

I’m running an SSD with my pi so raising the swap size could reduce or even solve some of these issues. The default setting is just 25% of the real device memory (in this case 256Mb) and I think that it is probably having in SDcards in mind (and not SSD)

I’ve managed to change it using the commands below and so far so good… the problem is that this is not persistent (and after restart goes back to original swap size). As far as I understood I would need to change the /usr/libexec/hassos-zram or /usr/lib/systemd/system/zram-swap.service but the filesystem is read-only. Is there any other way to do this?

swapoff /dev/zram0
zramctl --reset /dev/zram0
zramctl --find --size 1024M
mkswap /dev/zram0
swapon /dev/zram0
dpjrodrigues commented 3 years ago

Just a quick update to mention that increasing the swap size is working well so far. Now the RAM is stable 65% - 70% and the swap is between 30% - 35% so not that much from the forced 25% (the problem is that this configuration is lost when restarted)

marcoevcom commented 3 years ago

I got the exact same problem. It would be great to edit somenthing on hassio config file prior to boot to set up as SSD.

capstan1 commented 3 years ago

I found a way to make this "persistant" using the SSH & Web Terminal Addon. When disabling protection mode for the addon you can simply place the swape file inside the Core folders (for example "/config" oder "/config/www") and issue the swapon command as initial command of the addon. The swapfile itself and the permissions survive the restarts.

##### Edit: Confirmed working solution:

Edit: Original post (not optimal, due to increased snapshot size (as described in the next post ))
  • install the adon via [the repository] (https://github.com/hassio-addons/addon-ssh)

  • issue the first three commands inside terminal with changed folders:

    fallocate -l 1G /config/www/swapfile.swap
    mkswap /config/www/swapfile.swap
    chmod 0600 /config/www/swapfile.swap
  • set the third command as init_command in the addons' configuration: swapon /config/www/swapfile.swap

JZ-SmartThings commented 3 years ago

@capstan1 it makes doing HA backups a pain... now there's a huge file rather than a few megs. Can the file reside in any other location outside of HA config folder?

Bellavista commented 3 years ago

I found a way to make this "persistant" using the SSH & Web Terminal Addon. When disabling protection mode for the addon you can simply place the swape file inside the Core folders (for example "/config" oder "/config/www") and issue the swapon command as initial command of the addon. The swapfile itself and the permissions survive the restarts.

fallocate -l 1G /config/www/swapfile.swap
mkswap /config/www/swapfile.swap
chmod 0600 /config/www/swapfile.swap
  • set the third command as init_command in the addons' configuration: swapon /mnt/data/test.swap

HI to all! wonderful .. you are great! i had... an rsp3 + with 1G ram and 200mb of swap file but now.... the swap file is 2Gbit .. I read the post from JZ-SmartThings .... but I thought .... the folder / backup is not part of the backup and I tried to create the swap file in backup folder and i used SSH & Web Terminal Addon like @capstan1 GREAT!!! after a snapshot whith Home Assistant Google Drive Backup and a restart YESSS!!! IT WORKS !!! GREAT! image Cattura

JZ-SmartThings commented 3 years ago

@Bellavista notice that your init_command is not using the same file/location as you have on the swapon command.

We seem to be getting lucky with how swap files are considered/packed by TAR. Notice that 1GB is down to 1MB when compressed:

image

Seems like that 1MB of backup space is indeed a small price to pay for larger swap. However, after looking at init_commands, it made me wonder why not use that to run all of the necessary commands. Something worth trying... thanks for the more detailed steps, the INIT_COMMANDS piece was key for me.

capstan1 commented 3 years ago

Hi, indeed, the snapshot size increases when putting the swapfile inside a folder that gets snapshoted. @JZ-SmartThings: This 1 MB is probably close after restarting. As I understand this, the 1GB is reserved space, hence a compressed file is small. But as swap fills up with time, the snapshots also increase in size. My snapshots grew from around 70MB so 160-200Mb during 1 week of uptime.

I changed the location to the /mediafolder and excluded this folder from my snapshots (very simple with the Google Drive Addon). Then the snapshots' size don't increase. As @Bellavista mentioned, the /backup folder should be a good choice, too!

However, after looking at init_commands, it made me wonder why not use that to run all of the necessary commands. Something worth trying...

Surely, you could put all commands inside the init_commands. But as the swapfile itself is persistent throughout restarts, it's just not necessary. Don't know if it adds a lot of workload to a restart, though. Only advantage I can see: it's also executed after restoring a snapshot on a fresh install. So you won't need to SSH into the host once after restoring.

JZ-SmartThings commented 3 years ago

@capstan1 and @Bellavista I'm so glad you both posted. It all led me to the final ONE-LINER resolution :) which is ultimately the best and most maintainable approach. Basically use the INIT_COMMANDS to run an if statement in the shell and you never really need to visit that shell yourself to make this work. It survives reboots and doesn't make my backups larger because I simply store it in the /backup folder anyway like suggested. It has a bit logic which will re-create the file if it's not there already, etc.

BTW @capstan1 that makes perfect sense... swap file will be all zeros until it's actually used. Now that the uptime is longer it is indeed impacting the backup size like I initially suspected.

Thanks again to you both! Here's the one-line solution which belongs on the Configuration page of the SSH & Web Terminal add-on in HA.

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 ; else swapon /backup/_swap.swap; fi
QbaF commented 3 years ago

I am not sure why we have to act as an underground/resistance movement but You did it well;) Thank You.

JZ-SmartThings commented 3 years ago

@QbaF underground teamwork indeed! We also owe some gratitude to @agners who came with the right ammunition!

Bellavista commented 3 years ago

Great job from everyone. I am one of those users who knows little ... indeed nothing ... about programming and linux or yamal commands, I only have a scholastic knowledge ... (but I'm not young ... ;-) internet has been a turn fatal! Well! The effect of the 2GB swap gave the system a lot of stability! Maybe 2 gb is too much I based on the double of system memory rule .... Still great result !!! Thank you all ps. Thank you @agners

JZ-SmartThings commented 3 years ago

Please see the on-going thread here: https://community.home-assistant.io/t/how-to-increase-the-swap-file-size-on-home-assistant-os/272226

I modified the command in the 1st post to have a bit more logic where it checks if swap is already on with dmesg and if so NOT to repeat the command. Otherwise the SSH add-on fails a restart.

Pasting it one last time here and will continue with the community post:

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
johnsmithmad commented 3 years ago

Thank you guys, it helped me a lot. I've a raspberry pi 3b and HASSIO was unstable and unavailable almost one time per day ...

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

agamemnen commented 2 years ago

I can't understand it. how to do it: disable "protection mode" in the addon? Explain step by step.

PS Home Assistant OS 6.2 supervisor-2021.09.0 core-2021.9.6

agamemnen commented 2 years ago

how to do it: disable "protection mode" in the addon?

I will answer my own question. Maybe it will be useful to someone and will save time.

There are several addons. We need exactly:SSH & Web Terminal!

It just has a separate setting: disable "protection mode".

After its activation and if you register the necessary parameters in the config, then everything works!

bkbartk commented 2 years ago

great solution, thanks I tried to do a simular thing using shell_command, but I kept running into permission issues.

Biont commented 2 years ago

This is all very nice, but I would still prefer a native solution that does not require us to install an unofficial addon. Looking through the documentation, it turns out that this OS isn't completely read-only. We can define network configs, kernel modules and more importantly, udev rules! And all those are persistent!

So here is what I did:


This is of course a bit shaky since it relies on the block device being added as /dev/sda. I am absolutely not an expert, so this udev rule can certainly be written in a much more robust way. I hope this helps anyone else looking for a solution.

PS: Perhaps it can be improved by introducing partition labels. Maybe the devs would be open to automatically swapon any partition labelled hassos-swap? Just as an idea.

Vinz87 commented 2 years ago

I get the output swapon: /backup/_swap.swap: Operation not permitted when trying to execute the command swapon /backup/_swap.swap, anyone knows why?

PandJoKell commented 2 years ago

Hi fellow HA users, Big, big thanks to the gurus who have contributed to this HA critical thread. I'm running HAOS on a RPi3B+, 128GB SD card, no portable drive or SSD, running HA OS 7.0, supervisor-2021.12.2, docker 20.10.9. I have the same HASSIO instability problems and I have almost given up on HA. I'm trying some of the ideas in this thread and are hitting some roadblocks that have prompted me to make this post. I'm a crusty old electrical engineer with fair bit of real-time SCADA computing experience. With limited RAM in old minis and mainframes swap files were common to keep the systems alive with some real time performance penalty to manage. Sizing the swap file was critical to the stability of the system. I find the modern computer jargon is quite challenging for me at times but I have some basic questions/observations:

  1. Why use the SSH & Web Terminal addon as the place to put the init_commands? Could any other addons that are initiated at restart be used?
  2. In the SSH & Web Terminal addon, thanks to agememnen, I added "protection mode: disable" in the 3rd line of the Configuration to put it into the required disabled "protection mode". On checking the addon log I couldn't find a specific note indicating protection mode disabled but no errors in the log so I assumed it worked. Also I couldn't find this as a setting option in the Documentation tab for the addon (version 9.2.1). Did I miss it somewhere?
  3. Before solving the protection mode disable problem, when I tried to add the init_command as per JZ-SmartThings to the Configuration options in the SSH & Web Terminal addon I got a permissions error and the Configuration reverted back to my last saved version. Perhaps others may have seen this error?
  4. I may have missed this in the above thread but I assume that once the addon is configured as above, I do need to restart the system to get it to create the swap file and allocate the ram?
  5. Should we be concerned that much of the accumulated knowledge in the thread may be lost if/when the thread goes stale? I looked at lots of disparate (and often not so helpful) posts on github (and elsewhere) before finding this thread which I consider to be the "jewel in the crown". I note also the Oct 2021 post by Biont with an alternative approach which I haven't got to yet, but it may be a better overall method. How can this knowledge base be preserved for others? Do we need to keep this thread fresh because it seems to me to be critical to the ongoing interests of all HA users and could be lost if it goes stale? Thanks again and keep up the good work.
bkbartk commented 2 years ago

A lot of questions. Let me try to answer a few.

  1. I tried but couldn’t get it to work in any other way.
  2. protection mode actually grants root priveledges. This is why the security rating is 1. It only is available in the community add on. Not on the other ssh add on.
  3. You need to restart the add on. And make the add on boot on start.

No comment on 3 and 5.

PandJoKell commented 2 years ago

Thanks bkbartk, I'll restart the system and check the swap file sensors which are currently: sensor.swap_free 0.0 sensor.swap_use 227.4 sensor.swap_use 100.0 Cheers.

PandJoKell commented 2 years ago

Terminal & SSH Configuration Options were set as follows: authorized_keys: [] apks: [] password: ...............redacted server: tcp_forwarding: false init_commands:

After a server reboot checked for swap space - no material change. sensor.swap_free 0.1 sensor.swap_use 227.3 sensor.swap_use 99.9

Checked backup directory and no file _swap.swap [core-ssh ~]$ cd ../backup [core-ssh backup]$ ls -al total 3337604 drwxr-xr-x 2 root root 4096 Jan 2 04:07 . drwxr-xr-x 1 root root 4096 Jan 2 09:44 .. -rw-r--r-- 1 root root 243210240 Jan 1 20:07 337e5272.tar -rw-r--r-- 1 root root 276951040 Dec 28 20:06 428863cb.tar -rw-r--r-- 1 root root 256624640 Dec 27 23:20 51177d83.tar -rw-r--r-- 1 root root 331489280 Dec 30 04:07 5879cd2f.tar -rw-r--r-- 1 root root 257064960 Jan 2 04:08 5a814985.tar -rw-r--r-- 1 root root 258293760 Dec 28 04:05 8908753d.tar -rw-r--r-- 1 root root 377118720 Dec 30 20:12 93d34ed5.tar -rw-r--r-- 1 root root 317460480 Dec 29 20:07 9b635441.tar -rw-r--r-- 1 root root 390932480 Dec 31 04:12 db0d8ce8.tar -rw-r--r-- 1 root root 202373120 Dec 31 20:09 e02f9483.tar -rw-r--r-- 1 root root 290304000 Dec 29 04:06 e43966ea.tar -rw-r--r-- 1 root root 215859200 Jan 1 04:07 f8fd67df.tar

Checked Supervisor log and found schema issue with init_commands and protection mode: 22-01-02 10:06:09 WARNING (MainThread) [supervisor.addons.options] Option 'init_commands' does not exist in the schema for Terminal & SSH (core_ssh) 22-01-02 10:06:09 WARNING (MainThread) [supervisor.addons.options] Option 'protection mode' does not exist in the schema for Terminal & SSH (core_ssh) 22-01-02 10:06:09 WARNING (MainThread) [supervisor.addons.options] Option 'init_commands' does not exist in the schema for Terminal & SSH (core_ssh) 22-01-02 10:06:09 WARNING (MainThread) [supervisor.addons.options] Option 'protection mode' does not exist in the schema for Terminal & SSH (core_ssh) 22-01-02 10:08:56 WARNING (MainThread) [supervisor.addons.options] Option 'init_commands' does not exist in the schema for Terminal & SSH (core_ssh) 22-01-02 10:08:56 WARNING (MainThread) [supervisor.addons.options] Option 'protection mode' does not exist in the schema for Terminal & SSH (core_ssh) 22-01-02 10:15:02 WARNING (MainThread) [supervisor.addons.options] Option 'init_commands' does not exist in the schema for Terminal & SSH (core_ssh) 22-01-02 10:15:02 WARNING (MainThread) [supervisor.addons.options] Option 'protection mode' does not exist in the schema for Terminal & SSH (core_ssh) 22-01-02 10:15:11 WARNING (MainThread) [supervisor.addons.options] Option 'init_commands' does not exist in the schema for Terminal & SSH (core_ssh) 22-01-02 10:15:11 WARNING (MainThread) [supervisor.addons.options] Option 'protection mode' does not exist in the schema for Terminal & SSH (core_ssh)

Any thoughts gratefully received.

JZ-SmartThings commented 2 years ago

I wish this thread/bug could be closed because the solution/links in previous posts keeps getting further and further buried. Please do NOT use protected_mode as an entry in the config of the SSH addon. It's a checkmark/slider on the Add-on and you should see it on the Info page of most add-ons. It's not allowed as a YAML entry AFAIK. Screenshot below along with a link that should be used as your source (I've updated the command since the last post here) of the command/script and instructions. I hope people stop continuing to comment and pose questions here and instead go to the HA community to post linked below.

https://community.home-assistant.io/t/how-to-increase-the-swap-file-size-on-home-assistant-os/272226

image

bkbartk commented 2 years ago

Protection mode: disable shouldn’t be Parton the init command. It should only be set with the toggle.

`init_commands:

PandJoKell commented 2 years ago

Thanks bkbartk and JZ-SmartThings for the info,

Apologies for following an out of date link but I didn’t know the newer link existed. I haven’t got the protection mode switch in the INFO tab of any of the installed addons. I must have missed something so I will remove the protection mode disable from the init command, look more closely at the replacement instructions link, and follow the instructions as closely as I can.

Can I get back to you by email if I hit snags that stop me? This RAM memory problem a key system stability issue for me as I may get a day or so operation but gradually the memory map fills up and eventually the system fails and can only be salvaged by a cold host boot.

BTW, short term I am trying to create the swap file in ../backup folder on the installed 128GB SD card. Later, to save the SD card I plan to shift the swap file, tar backups, etc to a portable disk as soon as I get time to read up how and go through the shift.

Thanks for your help, Phil.

On 2 Jan 2022, at 20:56, bkbartk @.***> wrote:

Protection mode: disable shouldn’t be Parton the init command. It should only be set with the toggle.

`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 packages: [] share_sessions: false ssh: allow_agent_forwarding: false allow_remote_port_forwarding: false allow_tcp_forwarding: false authorized_keys: [] …..` — Reply to this email directly, view it on GitHub https://github.com/home-assistant/operating-system/issues/968#issuecomment-1003690451, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJSK5PPU27JR6XFAGBFH7ALUUAOM3ANCNFSM4TNVWLUA. Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub. You are receiving this because you commented.

JZ-SmartThings commented 2 years ago

@PandJoKell please go to the link below and follow the instructions to a tee... specifically the name of the add-on that you're installing --- bullet 3. You want the unofficial SSH & Web Terminal not what you're trying to use:

https://community.home-assistant.io/t/how-to-increase-the-swap-file-size-on-home-assistant-os/272226

bkbartk commented 2 years ago

image you need the one in the green square, not the other one, You can use both together, but for increasing the swap file you need the community add-on. image there you will find the toggle in the red square.

PandJoKell commented 2 years ago

Got it thanks JZ-SmarthThings. Before yesterday I was using the wrong SSH now using the community one and with success. I’m also posting on the new thread. Cheers.

On 5 Jan 2022, at 13:24, JZ-SmartThings @.***> wrote:

@PandJoKell https://github.com/PandJoKell please go to the link below and follow the instructions to a tee... specifically the name of the add-on that you're installing --- bullet 3. You want the unofficial SSH & Web Terminal not what you're trying to use:

https://community.home-assistant.io/t/how-to-increase-the-swap-file-size-on-home-assistant-os/272226 https://community.home-assistant.io/t/how-to-increase-the-swap-file-size-on-home-assistant-os/272226 — Reply to this email directly, view it on GitHub https://github.com/home-assistant/operating-system/issues/968#issuecomment-1005327205, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJSK5PM5H5KSUHWP4HQHADLUUOTWHANCNFSM4TNVWLUA. Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub. You are receiving this because you were mentioned.

PandJoKell commented 2 years ago

Got it thanks bkbartk. Till yesterday I was using the wrong SSH but now using the community add-on with success. Also I’m posting on the new thread. Cheers.

On 6 Jan 2022, at 02:38, bkbartk @.***> wrote:

https://user-images.githubusercontent.com/17070805/148244745-d2d3930c-bd1d-498d-b265-e8bf90e53894.png you need the one in the green square, not the other one, You can use both together, but for increasing the swap file you need the community add-on. https://user-images.githubusercontent.com/17070805/148245057-78c95d7a-0461-49f6-8c21-354a4e56c0c6.png there you will find the toggle in the red square.

— Reply to this email directly, view it on GitHub https://github.com/home-assistant/operating-system/issues/968#issuecomment-1005830490, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJSK5PNSXMIOQ256RE5BF7DUURQWBANCNFSM4TNVWLUA. Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub. You are receiving this because you were mentioned.

ce3a commented 1 year ago

This is all very nice, but I would still prefer a native solution that does not require us to install an unofficial addon. Looking through the documentation, it turns out that this OS isn't completely read-only. We can define network configs, kernel modules and more importantly, udev rules! And all those are persistent!

@Biont thanks for sharing this. I did something similar.

For now, I am using a RPI 4 with 1GB of RAM (8GB versions are hard to get currently) and a 240 GB SSD. I decided to spend 16 GB for a SWAP partition (maybe thats too much, but I have plenty of disk space).

Here is what I did:

BlackRapsbeere commented 1 year ago
* Launched HA and logged in to port `22222`.

* Ran `mkswap /dev/sda9`.

* Rebooted, I checked that it actually worked:
  ![grafik](https://user-images.githubusercontent.com/5941722/196285696-5d55d212-fabd-47ca-8881-2e9bd66cb258.png)

So you are telling the mkswap command stays persistent if referencing a whole partition? Or do you additionally use udev-rules to swapon this partition?

The swapsize, location and swappiness definitely need to be editable by the user via homeassistant-GUI and get persistent.

PandJoKell commented 1 year ago

 Hi Folks, I am interested in this latest development from @Biont and Sergej Sawazki but haven’t looked into the details or tried it out yet. I’ve been using the community SSH & Terminal addon approach for a year or so and only trouble I’ve had was when I upgraded the addon, loosing the swap file configuration. But once I got the config back and reinstated it everything has worked since then. I use HassOS with an RPi 3B+ and a 2GB swap file on a 64GB SSD. My HA has been very stable and the UI much faster, although lots of trend graphs slow it down a bit with some taking up to a minute at times. An occasional restart seems to clear things out if it gets slow. I’m sure that I would have had to upgrade my RPi to an RPi 4B+ with 4GB RAM had this community addon method not been devised. It’s been a saviour for my HA. I plan to look into the new method and try it out soon on my dev system that uses an RPi 3B. Till then thanks for including me in the email thread. Cheers, Phil K.

Sent from my iPad

On 18 Oct 2022, at 08:32, Sergej Sawazki @.***> wrote:  This is all very nice, but I would still prefer a native solution that does not require us to install an unofficial addon. Looking through the documentation, it turns out that this OS isn't completely read-only. We can define network configs, kernel modules and more importantly, udev rules! And all those are persistent!

@Biont thanks for sharing this. I did something similar.

For now, I am using a RPI 4 with 1GB of RAM (8GB versions are hard to get currently) and a 240 GB SSD. I decided to spend 16 GB for a SWAP partition (maybe thats too much, but I have plenty of disk space).

Here is what I did:

Resized the hassos-data partition to make space for a SWAP partition. Created the SWAP partition as partition number 9 at the end of the disk. Lessens learned: don't use parted for that (HA will not boot), use fdisk instead. In order to get ssh access to port 22222(!), created directory CONFIG inside the hassos-boot partition and copied my public key to file authorized_keys in this directory. Launched HA and logged in to port 22222. Ran mkswap /dev/sda9. Rebooted, I checked that it actually worked:

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.

krystof-k commented 1 year ago

@ce3a thank you, I tried it your way and it worked all fine on Raspberry Pi 3B+ v1.3.

Here is my approach step by step. I also upgraded the SD card to a larger one in the process.

You will need either a larger SD card with a USB reader or a USB drive (sized at least as the SD card you're using).

⚠️ Warning! First, I did it yesterday and I'm not sure this guide is 100% correct. Always consult Google in case you are not sure about something. Second, if you make a mistake (or I did), you can lose all your data, always backup first. No guarantees!

  1. In case you are exchanging SD card, connect both old and new to a computer and clone it (I did it using dd on macOS)
  2. Setup SSH access
  3. Connect the old SD card / USB drive
  4. Stop all Docker containers (docker kill $(docker ps -q))
  5. Unmount no. 8 (hassio-data) partition (check lsblk for the name): umount /dev/sda8
  6. In case you are using the USB drive, clone the partition to the drive: dd if=/dev/sda8 of=/dev/yourexternalpartition (or just mount the drive and copy the files)
  7. Now delete the no. 8 partition using fdisk: fdisk /dev/sda, d, 8
  8. Next, create a new (hassio-data) partition while leaving space for SWAP partition: n, p, 8, use the default start sector and then set the size for the data, e.g. +50G, N
  9. Create a new swap partition: n, p, 9, keep default start and end sector (fills up the rest of the space)
  10. Change type to swap for the partition: t, 9, swap
  11. Check the result p, write partitions and quit fdisk w
  12. Format the data partition mkfs.ext4 /dev/sda8
  13. Restore the data from the old SD card / USB drive: dd if=/dev/yourexternalpartition of=/dev/sda9 (or again just mount & copy the files)
  14. Create swap mkswap /dev/sda9
  15. Reboot and check Home Assistant booted correctly & is using your new swap partition

Hope this helps!

hlfbt commented 1 year ago

@Biont

KERNEL=="sd?", SUBSYSTEM=="block", DEVPATH=="*usb1/1-1*", NAME="sda", RUN+="/usr/sbin/swapon /dev/sda2"

PS: Perhaps it can be improved by introducing partition labels. Maybe the devs would be open to automatically swapon any partition labelled hassos-swap? Just as an idea.

Thanks for the idea of using udev for this! Changing your udev rule a bit makes it match on any swap partition with the hassos-swap label:

ACTION=="add", SUBSYSTEM=="block", ENV{ID_FS_TYPE}=="swap", ENV{ID_FS_LABEL}=="hassos-swap", RUN+="/usr/sbin/swapon /dev/disk/by-label/hassos-swap"

Adding the label to the swap partition on my external drive worked perfectly in combination with this rule.

Vinz87 commented 1 year ago

Hi, did anyone succeed in using the swap command with the latest version 12.1.2 of the SSH & Terminal add-on?

koger23 commented 1 year ago

Hi, did anyone succeed in using the swap command with the latest version 12.1.2 of the SSH & Terminal add-on?

Hi, this is the first time I try increasing swap, and addon version is 12.1.2 and does not work.

svh1985 commented 1 year ago

It doesn't work with the 12.1.3 version of the plugin.

Please make it a part of the Native Home Assistant OS settings. WTH? 😜 Definitely helps to run Home Assistant on RPi 4 with just 2GB memory and an SSD.

Vinz87 commented 1 year ago

actually it worked for me after updating to v12.1.3