Control: tag -1 + pending patch

Hi Petter,

Petter Reinholdtsen wrote:
> What about setting some limitation on the amount of free space dphys-swapfile
> will assign to s awap file?  It could never try to create a file larger than
> the amount of free space, and probably stay below 50% or perhaps as high as
> 80% of the free space available.

Indeed. That kind of check came to my mind recently during SHA2017,
too.

I've now written a patch (preliminary version without documentation
below) and check if upstream would accept such a patch. I definitely
prefer such a change over having to add some debconf foo to a so far
debconf-less package.

Description: check available disk space first to avoid filling up disks
Author: Axel Beckert <a...@debian.org>
Debian-Bug: https://bugs.debian.org/596187

--- a/dphys-swapfile
+++ b/dphys-swapfile
@@ -29,6 +29,9 @@
 #   but is also sensible on 64bit to prevent filling /var or even / partition
 CONF_MAXSWAP=2048
 
+# restrict size to this percentage of the remaining disk space.
+CONF_MAXDISK_PCT=50
+
 
 ### ------ actual implementation from here on
 # no user settings any more below this point
@@ -78,6 +81,20 @@
     # announce end resulting config
     echo -n "want ${CONF_SWAPFILE}=${CONF_SWAPSIZE}MByte"
 
+    if [ "${CONF_MAXDISK_PCT}" != "" ] ; then
+      # check for free disk size percentage and don't overstep it
+      AVAIL_KB=$(/bin/df --output=avail "$(dirname "${CONF_SWAPFILE}")" | tail 
-1)
+      if [ -n "${CONF_SWAPFILE}" -a -e "${CONF_SWAPFILE}" ]; then
+          CURRENT_SIZE_B=$(stat --printf='%s' "${CONF_SWAPFILE}")
+          AVAIL_KB=$(echo "${AVAIL_KB} ${CURRENT_SIZE_B} 1024 / + p q" | dc)
+      fi
+      HALF_AVAIL_MB=$(echo "${AVAIL_KB} 2048 / p q" | dc)
+      if [ "${CONF_SWAPSIZE}" -gt "${HALF_AVAIL_MB}" ] ; then
+        echo -n ", restricting to ${CONF_MAXDISK_PCT}% of remaining disk size: 
${HALF_AVAIL_MB}MBytes"
+        CONF_SWAPSIZE="${HALF_AVAIL_MB}"
+      fi
+    fi
+
     if [ "${CONF_MAXSWAP}" != "" ] ; then
       # check for swap size limit and restrict to it
       if [ "${CONF_SWAPSIZE}" -gt "${CONF_MAXSWAP}" ] ; then

                Regards, Axel
-- 
 ,''`.  |  Axel Beckert <a...@debian.org>, http://people.debian.org/~abe/
: :' :  |  Debian Developer, ftp.ch.debian.org Admin
`. `'   |  4096R: 2517 B724 C5F6 CA99 5329  6E61 2FF9 CD59 6126 16B5
  `-    |  1024D: F067 EA27 26B9 C3FC 1486  202E C09E 1D89 9593 0EDE

Reply via email to