#! /bin/sh # /sbin/dphys-swapfile - automatically set up an swapfile # author franklin, last modification 2004.06.11 # This script is copyright ETH Zuerich Physics Departement, # use under either BSD or GPL license # get ready to work PATH=/sbin:/bin:/usr/sbin:/usr/bin export PATH # what we are NAME=dphys-swapfile # this is what we want, 2 times RAM size SWAPFACTOR=2 # this is a (outdated?) kernel limit (in MBytes), do not overrun it MAXSWAP=2048 # where we want the swapfile to be, this is the default CONF_SWAPFILE=/var/swap # check user config file(s), let user overwride settings, swap file filename CONFFILE=/etc/${NAME} DCONFFILE=/etc/default/${NAME} if [ -f ${CONFFILE} ] ; then . ${CONFFILE} fi if [ -f ${DCONFFILE} ] ; then . ${DCONFFILE} fi case "$1" in setup) # (re-)size/-generate # compute automatic optimal size /bin/echo -n "computing size" # this seems to be the nearest to physical RAM size, lacks about 60k KCORESIZE=`/bin/ls -l /proc/kcore | /usr/bin/awk '{ print $5 }'` # make MBytes which rounded down will be exactly 1 too few, so add 1 MEMSIZE=`/usr/bin/expr ${KCORESIZE} / 1048576 + 1` # default, without config file overwriding, swap=2*RAM CONF_SWAPSIZE=`/usr/bin/expr ${MEMSIZE} '*' ${SWAPFACTOR}` # re-check user config file(s), so late so can overwrite computed size if [ -f ${CONFFILE} ] ; then . ${CONFFILE} fi if [ -f ${DCONFFILE} ] ; then . ${DCONFFILE} fi # announce end resulting config /bin/echo -n ": want ${CONF_SWAPFILE}=${CONF_SWAPSIZE}MByte" # check for legitimate swap size and restrict to it if [ ${CONF_SWAPSIZE} -gt ${MAXSWAP} ] ; then /bin/echo -n ", limiting to kernel limit: ${MAXSWAP}MBytes" CONF_SWAPSIZE=${MAXSWAP} fi # we will be later starting, and in between possible deleting/rebuilding # so switch off any allready running swapfile, to avoid errors # test here for running, as else also error from swapoff if [ x`/sbin/swapon -s | /bin/grep ${CONF_SWAPFILE} | \ /usr/bin/cut -f 1 -d " "` != x ] ; then /sbin/swapoff ${CONF_SWAPFILE} 2>&1 > /dev/null fi # compare existing swapfile (if one exists) to see if it needs replacing if [ -f ${CONF_SWAPFILE} ] ; then /bin/echo -n ", checking existing" # we need bytes for comparing with existing swap file SWAPBYTES=`/usr/bin/expr ${CONF_SWAPSIZE} '*' 1048576` FILEBYTES=`/bin/ls -l ${CONF_SWAPFILE} | /usr/bin/awk '{ print $5 }'` # wrong size, get rid of existing swapfile, after remake if [ x${FILEBYTES} != x${SWAPBYTES} ] ; then # updates to this section need duplicating in postrm script # can not simply make subroutine here and call that from postrm # as this script is deleted before postrm purge is called /bin/echo -n ": deleting wrong size file (${FILEBYTES})" # deactivate before deleting $0 swapoff # and the file is also gone ... /bin/rm ${CONF_SWAPFILE} # remove any entry for mounting existing swapfile from /etc/fstab # do this first, so no deadlock if reboot and attempted swapon /bin/grep -v "^${CONF_SWAPFILE}" /etc/fstab > /etc/.fstab /bin/mv /etc/.fstab /etc/fstab else /bin/echo -n ": keeping it" fi fi # if no swapfile (or possibly old one got deleted) make one if [ ! -f ${CONF_SWAPFILE} ] ; then /bin/echo -n ", generating swapfile ..." # first deleting existing mount lines, if any there (same code as above) /bin/grep -v "^${CONF_SWAPFILE}" /etc/fstab > /etc/.fstab /bin/mv /etc/.fstab /etc/fstab /bin/dd if=/dev/zero of=${CONF_SWAPFILE} bs=1048576 \ count=${CONF_SWAPSIZE} 2> /dev/null /sbin/mkswap ${CONF_SWAPFILE} > /dev/null # ensure that only root can read possibly critical stuff going in here /bin/chmod 600 ${CONF_SWAPFILE} # do not mount swapfile via fstab, because S35mountall.sh is already done # so just add warning comment line that swapfile is not in fstab # and so gets mounted by this script # get rid of possibly existing comment about swapfile mounted by script /bin/grep -v "^# a swapfile" /etc/fstab > /etc/.fstab /bin/grep -v ${NAME} /etc/.fstab > /etc/fstab # add new comment about this /bin/echo -e "# a swapfile is not a swappartition, no swapon|off" \ "from here, use ${NAME} swap[on|off] for that" >> /etc/fstab # and inform user what we did /bin/echo -n " of ${CONF_SWAPSIZE}MBytes" fi /bin/echo ;; swapon) # as there can be no swapon in /etc/fstab, do it from here # this is due to no possible insertion of code (at least in Debian) # between mounting of /var (where swap file most likely resides) # and executing swapon, where the file already needs to be existing if [ -f ${CONF_SWAPFILE} ] ; then /sbin/swapon ${CONF_SWAPFILE} 2>&1 > /dev/null else /bin/echo "$0: ERROR: swap file ${CONF_SWAPFILE} missing!" fi ;; swapoff) # as there can also be no swapoff in /etc/fstab, do it from here if [ x`/sbin/swapon -s | /bin/grep ${CONF_SWAPFILE} | \ /usr/bin/cut -f 1 -d " "` != x ] ; then /sbin/swapoff ${CONF_SWAPFILE} 2>&1 > /dev/null fi ;; uninstall) # note: there is no install), as setup) can run from any blank system # it auto-installs as side effect of recomputing and checking size # deactivate before deleting $0 swapoff if [ -f ${CONF_SWAPFILE} ] ; then # reclaim the file space /bin/rm ${CONF_SWAPFILE} fi # and get rid of comment about swapfile mounting /bin/grep -v "^# a swapfile" /etc/fstab > /etc/.fstab /bin/grep -v ${NAME} /etc/.fstab > /etc/fstab ;; *) /bin/echo "Usage: $0 {setup|swapon|swapoff|uninstall}" exit 1 ;; esac exit 0