home-assistant / operating-system

:beginner: Home Assistant Operating System
Apache License 2.0
4.75k stars 956 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!

PandJoKell commented 1 year ago

I haven't had a chance to try it out yet. Hope to do so in next couple of weeks and report back then. Cheers, Phil.

On 28 Nov 2022, at 13:50, Vinz @.***> wrote:

actually it worked for me after updating to v12.1.3

— Reply to this email directly, view it on GitHub https://github.com/home-assistant/operating-system/issues/968#issuecomment-1329801817, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJSK5PMKVH5DOEN6SGSLITLWKUSI5ANCNFSM4TNVWLUA. You are receiving this because you were mentioned.

jlunz commented 1 year ago

Here is how I did it, and since I failed for a few times I want to share my learnings.

My setup is a Raspberry Pi Zero 2W with HassOS 8.5 (https://github.com/home-assistant/operating-system/issues/2220). Image was flashed to a 32GB microSD-Card and booted once to confirm it is working and also that the resize on first boot happened.

I wanted to add a 1G Swap Partition after resizing 'hassos-data'.

Partition layout before:

Partition layout after resize of 'hassos-data' + adding a 1G Swap Partition:

I did the resize and re-partitioning on my Desktop with a card reader. I tried different tools (Gnome Disks, parted), but all modified images did no longer boot, bootcode.bin was no longer found / executed (Activity LED stayed off + Checked with enabled BOOT_UART)

Wanting to know why fdisk works and parted not, I searched for an explanation.

tl;dr parted automatically convert(s) the hybrid MBR to a conventional protective MBR, this is the problem. You can examine the content with fdisk / gdisk, look for the option protective MBR data / hybrid MBR.

Click me for details before and after resize # Before resize ``` gdisk Disk\ Image\ of\ sdb\ \(2023-01-19\ 2055\).img.orig GPT fdisk (gdisk) version 1.0.9 Partition table scan: MBR: hybrid BSD: not present APM: not present GPT: present Found valid GPT with hybrid MBR; using GPT. Command (? for help): p Disk Disk Image of sdb (2023-01-19 2055).img.orig: 62333952 sectors, 29.7 GiB Sector size (logical): 512 bytes Disk identifier (GUID): B21260BC-1889-4BAC-A825-A0F789E4F9DC Partition table holds up to 128 entries Main partition table begins at sector 2 and ends at sector 33 First usable sector is 34, last usable sector is 62333918 Partitions will be aligned on 2048-sector boundaries Total free space is 2014 sectors (1007.0 KiB) Number Start (sector) End (sector) Size Code Name 1 2048 67583 32.0 MiB 0C01 hassos-boot 2 67584 116735 24.0 MiB 8300 hassos-kernel0 3 116736 641023 256.0 MiB 8300 hassos-system0 4 641024 690175 24.0 MiB 8300 hassos-kernel1 5 690176 1214463 256.0 MiB 8300 hassos-system1 6 1214464 1230847 8.0 MiB 8300 hassos-bootstate 7 1230848 1427455 96.0 MiB 8300 hassos-overlay 8 1427456 62333918 29.0 GiB 8300 hassos-data Command (? for help): r Recovery/transformation command (? for help): o Disk size is 62333952 sectors (29.7 GiB) MBR disk identifier: 0x00000000 MBR partitions: Number Boot Start Sector End Sector Status Code 1 * 2048 67583 primary 0x0C 2 1 2047 primary 0xEE ``` # After resize ``` gdisk Disk\ Image\ of\ sdb\ \(2023-01-19\ 2055\).img GPT fdisk (gdisk) version 1.0.9 Partition table scan: MBR: protective BSD: not present APM: not present GPT: present Found valid GPT with protective MBR; using GPT. Command (? for help): p Disk Disk Image of sdb (2023-01-19 2055).img: 62333952 sectors, 29.7 GiB Sector size (logical): 512 bytes Disk identifier (GUID): B21260BC-1889-4BAC-A825-A0F789E4F9DC Partition table holds up to 128 entries Main partition table begins at sector 2 and ends at sector 33 First usable sector is 34, last usable sector is 62333918 Partitions will be aligned on 2048-sector boundaries Total free space is 10126269 sectors (4.8 GiB) Number Start (sector) End (sector) Size Code Name 1 2048 67583 32.0 MiB 0C01 hassos-boot 2 67584 116735 24.0 MiB 8300 hassos-kernel0 3 116736 641023 256.0 MiB 8300 hassos-system0 4 641024 690175 24.0 MiB 8300 hassos-kernel1 5 690176 1214463 256.0 MiB 8300 hassos-system1 6 1214464 1230847 8.0 MiB 8300 hassos-bootstate 7 1230848 1427455 96.0 MiB 8300 hassos-overlay 8 1427456 50255871 23.3 GiB 8300 hassos-data 9 50255872 52209663 954.0 MiB 8200 Command (? for help): r Recovery/transformation command (? for help): o Disk size is 62333952 sectors (29.7 GiB) MBR disk identifier: 0x00000000 MBR partitions: Number Boot Start Sector End Sector Status Code 1 * 1 62333951 primary 0xEE ```
Click me for details how to fix it # Recreate hybrid MBR using gdisk ``` gdisk Disk\ Image\ of\ sdb\ \(2023-01-19\ 2055\).img GPT fdisk (gdisk) version 1.0.9 Partition table scan: MBR: protective BSD: not present APM: not present GPT: present Found valid GPT with protective MBR; using GPT. Command (? for help): p Disk Disk Image of sdb (2023-01-19 2055).img: 62333952 sectors, 29.7 GiB Sector size (logical): 512 bytes Disk identifier (GUID): B21260BC-1889-4BAC-A825-A0F789E4F9DC Partition table holds up to 128 entries Main partition table begins at sector 2 and ends at sector 33 First usable sector is 34, last usable sector is 62333918 Partitions will be aligned on 2048-sector boundaries Total free space is 10126269 sectors (4.8 GiB) Number Start (sector) End (sector) Size Code Name 1 2048 67583 32.0 MiB 0C01 hassos-boot 2 67584 116735 24.0 MiB 8300 hassos-kernel0 3 116736 641023 256.0 MiB 8300 hassos-system0 4 641024 690175 24.0 MiB 8300 hassos-kernel1 5 690176 1214463 256.0 MiB 8300 hassos-system1 6 1214464 1230847 8.0 MiB 8300 hassos-bootstate 7 1230848 1427455 96.0 MiB 8300 hassos-overlay 8 1427456 50255871 23.3 GiB 8300 hassos-data 9 50255872 52209663 954.0 MiB 8200 Command (? for help): r Recovery/transformation command (? for help): h WARNING! Hybrid MBRs are flaky and dangerous! If you decide not to use one, just hit the Enter key at the below prompt and your MBR partition table will be untouched. Type from one to three GPT partition numbers, separated by spaces, to be added to the hybrid MBR, in sequence: 1 Place EFI GPT (0xEE) partition first in MBR (good for GRUB)? (Y/N): N Creating entry for GPT partition #1 (MBR partition #1) Enter an MBR hex code (default 0C): Set the bootable flag? (Y/N): Y Unused partition space(s) found. Use one to protect more partitions? (Y/N): N Recovery/transformation command (? for help): o Disk size is 62333952 sectors (29.7 GiB) MBR disk identifier: 0x00000000 MBR partitions: Number Boot Start Sector End Sector Status Code 1 * 2048 67583 primary 0x0C 2 1 2047 primary 0xEE Recovery/transformation command (? for help): w Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING PARTITIONS!! Do you want to proceed? (Y/N): y OK; writing new GUID partition table (GPT) to Disk Image of sdb (2023-01-19 2055).img. Warning: The kernel is still using the old partition table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) The operation has completed successfully. ```

With all that said, creating the swap partition was all I did, it was recognized automatically.

[    7.616672] Adding 976892k swap on /dev/mmcblk0p9.  Priority:-2 extents:1 across:976892k SSFS
...
[    8.797583] Adding 109116k swap on /dev/zram0.  Priority:-3 extents:1 across:109116k SSFS
...

Please also note that this is just an experiment, for a robust and long running system you might want to adjust the swap priority.

JZ-SmartThings commented 1 year ago

@jlunz that looks like so much work and who knows what will happen when you update the core or the OS --- meaning are you going to have to go through this gyration again? Seems like way too much work when compared to my little technique, which just works and puts this swap need behind you. Try this:

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

jlunz commented 1 year ago

@JZ-SmartThings Absolutely, I wouldn't recommend it to someone who is interested in a stable and long-term working system. As I wrote, this is / was an experiment and I felt that sharing the hybrid MBR story was worth my reply. (I couldn't find a lot of references explaining what was going on)

JZ-SmartThings commented 1 year ago

@jlunz definitely and very interesting actually. I just wouldn't do it due to complexity and unknown future up-keep :) kinda sad that we don't have this at the HASS OS level. More importantly, the default swap allocation is way too small for real-world use.

PandJoKell commented 1 year ago

Hi HAOS Swapfile fans,

Have been tardy in not trying the more permanent fix Swap file issue solution by @Biont's posting on 14 Oct 2021, mainly because of unfamiliarity with Linux udev for managing partitions. My HA system (HAOS installed version 9.5) runs on a RPI 3B+ with a 32GB SD card for system and data storage. I haven't tried mounting a USB SSD per @Biont's posting which has the side benefit of creating a new data partition labelled "hassos-data" during the migration procedure. Pending a better understanding of udev

I've been trying to re-create the swap area on HAOS startup per JZSmartthings posts over the last couple of years. It uses the community SSH & Web Terminal addon with an init_commands script to allocate a 2GB swap file in the Backup folder on startup. I couldn't get the community SSH & Web Terminal addon to save the script (both UI mode and 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). Got {'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': True, 'ssh': {'allowagent..."

I then tried running the individual commands via ssh checking the proper file creation occurred. The following are 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 /backup//_swap.swap' 'ls -al' confirmed the file 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

I then 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': 'John131150', 'sftp': False, 'username': 'pkelloway'}, 'zsh': True, 'log_level': 'debug'}

The init_commands script worked without hitch a year or so ago but not now. So this post is to enquire 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? Have other HA swapfile fans had issues with the init_commands in the Community addon SSH & Web Terminal lately and if so has anybody found a way to make it?

JZ-SmartThings commented 1 year ago

@PandJoKell if you feel like troubleshooting it, please post it on the community thread on HA rather than GitHub. Post your screenshot of the YAML config. I'll also update mine and see if it breaks... so strange, but every time folks come with problems, turns out it's typically user error :) not insinuating, just informing.

PandJoKell commented 1 year ago

Thanks JZ-SmartThings,

Will post it on the community thread with the community addon YAML config. Could be I have made an error, so no problem with informing me :-).

Cheers, @PandJoKell

On 31 Jan 2023, at 07:38, JZ-SmartThings @.***> wrote:

@PandJoKell https://github.com/PandJoKell if you feel like troubleshooting it, please post it on the community thread on HA rather than GitHub. Post your screenshot of the YAML config. I'll also update mine and see if it breaks... so strange, but every time folks come with problems, turns out it's typically user error :) not insinuating, just informing.

— Reply to this email directly, view it on GitHub https://github.com/home-assistant/operating-system/issues/968#issuecomment-1409300727, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJSK5PJYDGEEW6674P66UELWVARDTANCNFSM4TNVWLUA. You are receiving this because you were mentioned.

omelettedufromagee commented 1 year ago

@jlunz You saved me. Due to many circomstances, I lost the SWAP created from the init_commands and my instance was not able to load fully before it crashes due to memory issues.

I decided to create a Swap partition directly on the microSD card as @jlunz mentionned.

Below are the steps I did using resize2fs, fdisk and mkswap. It might help others in need. I wish we could have different flavors of the HassOS img with SWAP or some boot config file to add more SWAP, at least enough to match the available RAM.

`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? 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`