Skip to content

Commit

Permalink
Add sys-apps/rpi3-expand-swap-1.0-r4.ebuild
Browse files Browse the repository at this point in the history
Underlying increases default max swap percentage to 50%.
Makes it less likely to truncate when used with e.g. PINN.
  • Loading branch information
sakaki- committed Dec 27, 2018
1 parent b26e7ae commit 9c0f745
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
27 changes: 27 additions & 0 deletions sys-apps/rpi3-expand-swap/files/conf.d_rpi3-expand-swap-3
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# /etc/conf.d/rpi3-expand-swap

# Settings for RPi3's main swap; this is set up as a simple swapfile
# on the root partition for simplicity, and you can specify the
# desired characteristics of the this file here.

# Uncomment any of the below if you wish to change them from
# the default values for the RPi3 (shown)

# Max size of swapfile to allocate, in MiB, even if unlimited storage
# were to be available
#
#MAX_SWAPFILE_MiB=1024

# Minimum size of swapfile to accept - if there is insufficient space
# an error will be raised instead
#
#MIN_SWAPFILE_MiB=512

# Percentage of available space on the root that should be allocated
# for the swapfile, subject to the above limits
#
#SWAPFILE_PCT_AVAIL=50

# Location of the swapfile
#
#SWAPFILE="/var/cache/swap/swap1"
81 changes: 81 additions & 0 deletions sys-apps/rpi3-expand-swap/files/init.d_rpi3-expand-swap-4
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/sbin/openrc-run
# Set RPi3's swapfile to a sensible percentage of the
# available free space on the root filesystem, subject
# to upper and lower caps.
#
# Default configuration settings can be overridden in
# /etc/conf.d/rpi3-expand-swap

# Copyright (c) 2018 sakaki <sakaki@deciban.com>
# License: GPL v3+
# NO WARRANTY

description="Automatically manages swapfile size on RPi3"

_expand_swapfile_if_possible() {
# swaps must be OFF before calling this function!
local LMAX_SWAPFILE_MiB="${MAX_SWAPFILE_MiB:-1024}"
local LMIN_SWAPFILE_MiB="${MIN_SWAPFILE_MiB:-512}"
local LSWAPFILE_PCT_AVAIL="${SWAPFILE_PCT_AVAIL:-50}"
local LSWAPFILE="${SWAPFILE:-/var/cache/swap/swap1}"
local LSWAPFILE_KiB=0
if [ -s "${LSWAPFILE}" ]; then
LSWAPFILE_KiB=$(( "$(du --apparent-size -k "${LSWAPFILE}" | awk '{print $1}')"))
fi
local KiB_AVAIL_ON_ROOT="$(df -k / | tail -n 1 | awk '{print $4}')"
# add back size of any existing swapfile to available space tally,
# so we don't end up oscillating each boot once space gets tight
KiB_AVAIL_ON_ROOT=$((LSWAPFILE_KiB + KiB_AVAIL_ON_ROOT))
local MiB_AVAIL_ON_ROOT=$((KiB_AVAIL_ON_ROOT/1024))
local MiB_AVAIL_FOR_SWAP=$((MiB_AVAIL_ON_ROOT*LSWAPFILE_PCT_AVAIL/100))
if ((MiB_AVAIL_FOR_SWAP > LMIN_SWAPFILE_MiB)); then
local MiB_FOR_SWAP=$((MiB_AVAIL_FOR_SWAP < LMAX_SWAPFILE_MiB ? MiB_AVAIL_FOR_SWAP : LMAX_SWAPFILE_MiB))
# don't do anything if swapfile already correct size
if (( LSWAPFILE_KiB == (1024 * MiB_FOR_SWAP) )); then
einfo "Swap file exists and is already correctly sized"
elif (( LSWAPFILE_KiB > (1024 * MiB_FOR_SWAP) )); then
# common case as space gets tight
einfo "Swap file exists, but is too large; truncating"
# this will not create holes
truncate --size="${MiB_FOR_SWAP}M" "${LSWAPFILE}"
mkswap -L "swap1" "${LSWAPFILE}"
else
ewarn "Setting swapfile size to ${MiB_FOR_SWAP} MiB, please wait"
if ((MiB_FOR_SWAP==LMAX_SWAPFILE_MiB && \
MiB_AVAIL_FOR_SWAP>(MiB_FOR_SWAP*2))); then
ewarn "(this process should only be required once)"
fi
rm -f "${LSWAPFILE}"
# yes fallocate is way faster but also, per
# swapon manpage, not safe on ext4, so do it the
# old fashioned way
dd if=/dev/zero of="${LSWAPFILE}" bs=1M count="${MiB_FOR_SWAP}" status=progress
chmod -v 0600 "${LSWAPFILE}"
mkswap -L "swap1" "${LSWAPFILE}"
fi
else
eerror "Insufficient free space to set up swapfile!"
eerror "Please consider running this image on a larger drive."
if ((LSWAPFILE_KiB>0)); then
ewarn "Deleting existing swapfile"
rm -fv "${LSWAPFILE}"
fi
return 1
fi
return 0
}


depend() {
need localmount
before xdm
}

start() {
ebegin "Starting ${SVCNAME}"
swapoff -a
_expand_swapfile_if_possible
RC=$?
swapon -a
eend $RC
}
36 changes: 36 additions & 0 deletions sys-apps/rpi3-expand-swap/rpi3-expand-swap-1.0-r4.ebuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright (c) 2018 sakaki <sakaki@deciban.com>
# License: GPL v3+
# NO WARRANTY

EAPI=6

KEYWORDS="~arm ~arm64"

DESCRIPTION="Service to manage swapfile size on RPi3"
HOMEPAGE="https://github.com/sakaki-/gentoo-on-rpi3-64bit"
SRC_URI=""
LICENSE="GPL-3+"
SLOT="0"
IUSE=""
RESTRICT="mirror"

# required by Portage, as we have no SRC_URI...
S="${WORKDIR}"

DEPEND=""
RDEPEND="${DEPEND}
>=sys-apps/openrc-0.21
>=app-shells/bash-4.0"

src_install() {
newinitd "${FILESDIR}/init.d_${PN}-4" "${PN}"
newconfd "${FILESDIR}/conf.d_${PN}-3" "${PN}"
}

pkg_postinst() {
if [[ -z ${REPLACING_VERSIONS} ]]; then
rc-update add "${PN}" default
elog "The ${PN} service has been added to your default runlevel."
elog "Please check /etc/conf.d/${PN} for settings."
fi
}

0 comments on commit 9c0f745

Please sign in to comment.