forked from home-assistant/operating-system
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make swap size configurable (home-assistant#3882)
Allow configuration of the swap size via /etc/default/haos-swapfile file. By setting the SWAPSIZE variable in this file, swapfile get recreated on the next reboot to the defined size. Size can be either in bytes or with optional units (B/K/M/G, accepting some variations but always interpreted as power of 10). The size is then rounded to 4k block size. If no override is defined or the value can't be parsed, it falls back to previously used 33% of system RAM. Fixes home-assistant#968
- Loading branch information
Showing
6 changed files
with
89 additions
and
15 deletions.
There are no files selected for viewing
Empty file.
13 changes: 13 additions & 0 deletions
13
buildroot-external/rootfs-overlay/usr/lib/systemd/system/etc-default.mount
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[Unit] | ||
Description=Persistent /etc/default directory | ||
Requires=mnt-overlay.mount | ||
After=mnt-overlay.mount | ||
|
||
[Mount] | ||
What=/mnt/overlay/etc/default | ||
Where=/etc/default | ||
Type=None | ||
Options=bind | ||
|
||
[Install] | ||
WantedBy=hassos-bind.target |
4 changes: 2 additions & 2 deletions
4
buildroot-external/rootfs-overlay/usr/lib/systemd/system/haos-swapfile.service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
[Unit] | ||
Description=HAOS swap | ||
DefaultDependencies=no | ||
Requires=mnt-data.mount | ||
After=mnt-data.mount [email protected] | ||
Requires=etc-default.mount mnt-data.mount | ||
After=etc-default.mount mnt-data.mount [email protected] | ||
Before=mnt-data-swapfile.swap | ||
|
||
[Service] | ||
|
1 change: 1 addition & 0 deletions
1
buildroot-external/rootfs-overlay/usr/lib/systemd/system/mnt-data-swapfile.swap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
[Unit] | ||
Description=HAOS swap file | ||
ConditionFileNotEmpty=/mnt/data/swapfile | ||
|
||
[Swap] | ||
What=/mnt/data/swapfile | ||
|
61 changes: 48 additions & 13 deletions
61
buildroot-external/rootfs-overlay/usr/libexec/haos-swapfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,59 @@ | ||
#!/bin/sh | ||
set -e | ||
|
||
swapfile="/mnt/data/swapfile" | ||
size2kilobytes() { | ||
bytes="$(echo "$1" | awk \ | ||
'BEGIN{IGNORECASE = 1} | ||
function tobytes(n,b,p) {printf "%u\n", n*b^p/1024} | ||
/[0-9]B?$/{tobytes($1, 1, 0); next}; | ||
/K(i?B)?$/{tobytes($1, 2, 10); next}; | ||
/M(i?B)?$/{tobytes($1, 2, 20); next}; | ||
/G(i?B)?$/{tobytes($1, 2, 30); next}; | ||
{print -1}')" | ||
echo "$bytes" | ||
} | ||
|
||
if [ -f /etc/default/haos-swapfile ]; then | ||
# shellcheck disable=SC1091 | ||
. /etc/default/haos-swapfile | ||
fi | ||
SWAPFILE="/mnt/data/swapfile" | ||
|
||
# Swap size in kilobytes (as it's also what meminfo shows) | ||
SWAPSIZE="$(size2kilobytes "${SWAPSIZE}")" | ||
|
||
if [ -z "${SWAPSIZE}" ] || [ "${SWAPSIZE}" = "-1" ]; then | ||
# Default to 33% of total memory | ||
SWAPSIZE="$(awk '/MemTotal/{ print int($2 * 0.33) }' /proc/meminfo)" | ||
echo "[INFO] Using default swapsize of 33% RAM (${SWAPSIZE} kB)" | ||
fi | ||
|
||
# Swap space in 4k blocks | ||
swapsize="$(awk '/MemTotal/{ print int($2 * 0.33 / 4) }' /proc/meminfo)" | ||
SWAPSIZE_BLOCKS=$((SWAPSIZE / 4)) | ||
|
||
if [ "${SWAPSIZE_BLOCKS}" -lt 10 ]; then | ||
echo "[INFO] Requested swap size smaller than 40kB, disabling swap" | ||
|
||
if [ ! -s "${swapfile}" ] || [ "$(stat "${swapfile}" -c '%s')" -lt $((swapsize * 4096)) ]; then | ||
# Check free space (in 4k blocks) | ||
if [ "$(stat -f /mnt/data -c '%f')" -lt "${swapsize}" ]; then | ||
echo "[WARNING] Not enough space to allocate swapfile" | ||
exit 1 | ||
fi | ||
if [ -f "${SWAPFILE}" ]; then | ||
echo "[INFO] Removing existing swapfile" | ||
rm -f "${SWAPFILE}" | ||
fi | ||
|
||
echo "[INFO] Creating swapfile of size $((swapsize *4))k" | ||
umask 0077 | ||
dd if=/dev/zero of="${swapfile}" bs=4k count="${swapsize}" | ||
exit 0 | ||
fi | ||
|
||
if ! swaplabel "${swapfile}" > /dev/null 2>&1; then | ||
/usr/lib/systemd/systemd-makefs swap "${swapfile}" | ||
if [ ! -s "${SWAPFILE}" ] || [ "$(stat "${SWAPFILE}" -c '%s')" -ne $((SWAPSIZE_BLOCKS * 4096)) ]; then | ||
# Check free space (in 4k blocks) | ||
if [ "$(stat -f /mnt/data -c '%f')" -lt "${SWAPSIZE_BLOCKS}" ]; then | ||
echo "[ERROR] Not enough space to allocate swapfile" | ||
exit 1 | ||
fi | ||
|
||
echo "[INFO] Creating swapfile of size ${SWAPSIZE} kB (rounded to ${SWAPSIZE_BLOCKS} blocks)" | ||
umask 0077 | ||
dd if=/dev/zero of="${SWAPFILE}" bs=4k count="${SWAPSIZE_BLOCKS}" | ||
fi | ||
|
||
if ! swaplabel "${SWAPFILE}" > /dev/null 2>&1; then | ||
/usr/lib/systemd/systemd-makefs swap "${SWAPFILE}" | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters