From: Elan Ruusamäe Date: Thu, 19 Jun 2014 19:58:35 +0000 (+0300) Subject: add AUTOSWAP feature X-Git-Tag: 0.4.12~1 X-Git-Url: http://git.pld-linux.org/?p=projects%2Frc-scripts.git;a=commitdiff_plain;h=03e0c0fad83a7a6fc0033ead0f1805d6bda4d855 add AUTOSWAP feature inspired from initscripts 9.39-1 --- diff --git a/doc/sysconfig.txt b/doc/sysconfig.txt index 063a332b..635d0545 100644 --- a/doc/sysconfig.txt +++ b/doc/sysconfig.txt @@ -1,6 +1,13 @@ Files in /etc/sysconfig ======================= +/etc/sysconfig/system: + + AUTOSWAP=yes|no + Set to 'yes' to enable automatic swapon of all partitions with + the proper swap magic. This allows setting up swap without editing + /etc/fstab. + /etc/sysconfig/keyboard: KEYTABLE= diff --git a/lib/functions b/lib/functions index e32f2d08..1b1b9726 100644 --- a/lib/functions +++ b/lib/functions @@ -174,8 +174,7 @@ is_empty_file() { # returns OK if $1 contains $2 strstr() { - local a=$2 - [ "${1#*$a*}" = "$1" ] && return 1 + [ "${1#*$2*}" = "$1" ] && return 1 return 0 } diff --git a/rc.d/rc.sysinit b/rc.d/rc.sysinit index 0b9d5c72..e14ad740 100755 --- a/rc.d/rc.sysinit +++ b/rc.d/rc.sysinit @@ -102,6 +102,21 @@ parse_cmdline() { done } +# resolve a device node to its major:minor numbers in decimal or hex +get_numeric_dev() { + local dev=$1 enc=${2:-hex} res + + res=$(stat -Lc "%t:%T" "$dev") + if [ "$enc" = dec ]; then + local oifs=$IFS + IFS=":" + set -- $res + IFS=$oifs + res=$((0x$1)):$((0x$2)) + fi + echo -n $res +} + # setup SELINUX variable init_selinux() { # user knows! @@ -162,6 +177,32 @@ relabel_selinux() { echo $SELINUX > $selinuxfs/enforce } +# Enable automatic swapon of all partitions with the proper swap magic. +# This allows setting up swap without editing /etc/fstab. +enable_autoswap() { + local swappartitions=$(blkid -t TYPE=swap -o device) + if [ -z "$swappartitions" ]; then + return + fi + + local curswap=$(awk '/^\/dev/ { print $1 }' /proc/swaps | while read x; do echo -n " "; get_numeric_dev $x; echo -n " "; done) + + local partition + for partition in $swappartitions; do + [ ! -e $partition ] && continue + majmin=$(get_numeric_dev $partition) + if ! strstr "$curswap" " $majmin "; then + run_cmd "$(nls 'Enabling local swap partitions: %s' $partition)" swapon $partition + fi + done +} + + +enable_swap() { + run_cmd "Activating swap" swapon -a "$@" + is_yes "$AUTOSWAP" && enable_autoswap +} + # Remove duplicate entries from mtab (for vserver guest use only) clean_vserver_mtab() { > /etc/mtab.clean @@ -494,7 +535,7 @@ if ! is_yes "$VSERVER" && [[ "$container" != lxc* ]]; then fi # Start up swapping - run_cmd "Activating swap partitions" swapon -a -e + enable_swap -e # Initialize USB controllers usb=0 @@ -1005,9 +1046,7 @@ chown root:root /tmp/.ICE-unix is_yes "$SELINUX" && restorecon /tmp/.ICE-unix >/dev/null 2>&1 if ! is_yes "$VSERVER"; then - run_cmd "Enabling swap space" true - # Right, now turn on swap in case we swap to files - swapon -a >/dev/null 2>&1 + enable_swap emit --no-wait all-swaps # If a SCSI tape has been detected, load the st module unconditionally diff --git a/sysconfig/system b/sysconfig/system index f86eb736..0734c41e 100644 --- a/sysconfig/system +++ b/sysconfig/system @@ -80,6 +80,9 @@ EVMS_AIX=no # EVMS OS/2 LVM volumes EVMS_OS2=no +# Set to 'yes' to allow probing for devices with swap signatures +AUTOSWAP=no + # LVM2 # disable if your only LVM volume is rootfs started on initrd and want faster startup LVM2=yes