merged grml-quickconfig nonblocking branch
[grml-scripts.git] / usr_sbin / grml-swapon
1 #!/bin/zsh
2 # Filename:      grml-swapon
3 # Purpose:       activate swap partitions with taking care of suspend signatures
4 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 # Latest change: Son Nov 26 18:04:13 CET 2006 [mika]
8 ################################################################################
9
10 set -e # exit on any error
11
12 . /etc/grml/script-functions
13 . /etc/grml/lsb-functions
14 . /etc/grml/autoconfig.functions
15
16 check4root || exit 1
17 check4progs dd blkid swapon || exit 2
18
19 checkbootparam "anyswap" && export ANYSWAP='yes' || export ANYSWAP=""
20
21 case $1 in
22    --force)
23      ANYSWAP='yes'
24      ;;
25    -h*|--h*)
26      ewarn 'grml-swapon: activate swap partitions defined in /etc/fstab'
27      eindent
28        ewarn 'Usage: grml-swapon [--help|-h] [--force]'
29        ewarn 'Tip: execute grml-rebuildfstab for rebuilding /etc/fstab'
30      eoutdent
31      eend 1
32      ;;
33 esac
34
35 einfo "Searching for swap partition(s) as requested."
36 eindent
37
38 [ -n "$ANYSWAP" ] && einfo "Option --force set, forcing usage of swap partitions."
39
40 while read p m f relax; do
41   case "$p" in *fd0*|*proc*|*sys*|*\#*) continue;; esac
42   partoptions="users,exec"
43   fnew=""
44   case "$f" in swap)
45      case "$(dd if=$p bs=1 count=6 skip=4086 2>/dev/null)" in
46              S1SUSP|S2SUSP|pmdisk|[zZ]*)
47                 if [ -n "$ANYSWAP" ] ; then
48                   einfo "Using swap partition ${WHITE}${p}${NORMAL} [bootoption anyswap found]."
49                   swapon $p 2>>$DEBUG ; eend $?
50                 else
51                   ewarn "Suspend signature on ${WHITE}${p}${NORMAL} found, not using as swap. Force usage via option: --force"
52                 fi
53                ;;
54              *)
55                 if [[ "$p" == LABEL* ]] ; then
56                    p=$(blkid -t $p | awk -F: '{print $1}')
57                 fi
58                 if grep -q $p /proc/swaps ; then
59                    ewarn "Not using swap partition ${WHITE}${p}${NORMAL} as it is already in use." ; eend 0
60                 else
61                    einfo "Using swap partition ${WHITE}${p}${NORMAL}."
62                    swapon $p 2>>$DEBUG ; eend $?
63                 fi
64                ;;
65      esac
66    continue
67    ;;
68   esac
69  done <<EOT
70 $(cat /etc/fstab)
71 EOT
72
73 eoutdent