Release new version 2.13.0
[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 ################################################################################
8
9 set -e # exit on any error
10
11 . /etc/grml/script-functions
12 . /etc/grml/lsb-functions
13 . /etc/grml/autoconfig.functions
14
15 check4root || exit 1
16 check4progs dd blkid swapon || exit 2
17
18 checkbootparam "anyswap" && export ANYSWAP='yes' || export ANYSWAP=""
19
20 case $1 in
21    --force)
22      ANYSWAP='yes'
23      ;;
24    -h*|--h*)
25      ewarn 'grml-swapon: activate swap partitions defined in /etc/fstab'
26      eindent
27        ewarn 'Usage: grml-swapon [--help|-h] [--force]'
28        ewarn 'Tip: execute grml-rebuildfstab for rebuilding /etc/fstab'
29      eoutdent
30      eend 1
31      ;;
32 esac
33
34 einfo "Searching for swap partition(s) as requested."
35 eindent
36
37 [ -n "$ANYSWAP" ] && einfo "Option --force set, forcing usage of swap partitions."
38
39 while read p m f relax; do
40   case "$p" in *fd0*|*proc*|*sys*|*\#*) continue;; esac
41   partoptions="users,exec"
42   fnew=""
43   case "$f" in swap)
44      case "$(dd if=$p bs=1 count=6 skip=4086 2>/dev/null)" in
45              S1SUSP|S2SUSP|pmdisk|[zZ]*)
46                 if [ -n "$ANYSWAP" ] ; then
47                   einfo "Using swap partition ${WHITE}${p}${NORMAL} [bootoption anyswap found]."
48                   swapon $p 2>>$DEBUG ; eend $?
49                 else
50                   ewarn "Suspend signature on ${WHITE}${p}${NORMAL} found, not using as swap. Force usage via option: --force"
51                 fi
52                ;;
53              *)
54                 if [[ "$p" == LABEL* ]] ; then
55                    p=$(blkid -t $p | awk -F: '{print $1}')
56                 fi
57                 if grep -q $p /proc/swaps ; then
58                    ewarn "Not using swap partition ${WHITE}${p}${NORMAL} as it is already in use." ; eend 0
59                 else
60                    einfo "Using swap partition ${WHITE}${p}${NORMAL}."
61                    swapon $p 2>>$DEBUG ; eend $?
62                 fi
63                ;;
64      esac
65    continue
66    ;;
67   esac
68  done <<EOT
69 $(cat /etc/fstab)
70 EOT
71
72 eoutdent