Use dh_clean instead of dh_prep to get rid of need for debhelper 7
[live-initramfs-grml.git] / bin / live-swapfile
1 #!/bin/sh
2
3 # File: live-swapfile - create and use a swap file
4 # Copyright: (C) 2009 Daniel Baumann <daniel@debian.org>
5 # License: GPL-3+
6
7 set -e
8
9 # Options
10 _SWAP_DIRECTORY="${_SWAP_DIRECTORY:-/live/swap}"
11 _SWAP_FILE="${_SWAP_FILE:-swapfile.img}"
12
13 _SWAP_SIZE="${_SWAP_SIZE:-auto}"
14 _SWAP_FACTOR="${_SWAP_FACTOR:-2}"
15
16 _SWAP_PURGE="${_SWAP_PURGE:-true}"
17 _FORCE="${_FORCE:-true}"
18
19 case "${1}" in
20         add)
21                 # Reading size of physical memory
22                 _MEM_TOTAL_KB="$(awk '/^MemTotal: / { print $2 }' /proc/meminfo)"
23                 _MEM_TOTAL_MB="$(expr ${_MEM_TOTAL_KB} / 1024)"
24
25                 echo "Found ${_MEM_TOTAL_MB} MB physical memory."
26
27                 # Setting size of new swapfile
28                 if [ -z "${_SWAP_SIZE}" ] || [ "${_SWAP_SIZE}" = "auto" ]
29                 then
30                         _SWAP_SIZE_KB="$(expr ${_MEM_TOTAL_KB} '*' ${_SWAP_FACTOR})"
31                         _SWAP_SIZE_MB="$(expr ${_SWAP_SIZE_KB} / 1024)"
32                 else
33                         _SWAP_SIZE_MB="${_SWAP_SIZE}"
34                 fi
35
36                 echo "Requesting ${_SWAP_SIZE_MB} MB swapfile."
37
38                 # Reading size of old swapfile
39                 if [ -e "${_SWAP_DIRECTORY}/${_SWAP_FILE}" ]
40                 then
41                         _SWAP_FILESIZE="$(ls -hl ${_SWAP_DIRECTORY}/${_SWAP_FILE} | awk '{ print $5 }')"
42
43                         echo "Found ${_SWAP_FILESIZE} MB swapfile."
44                 fi
45
46                 # Creating new swap file
47                 if [ "${_SWAP_FILESIZE}" != "${_SWAP_SIZE_MB}M" ]
48                 then
49                         if [ "${_FORCE}" = "true" ]
50                         then
51                                 # Removing old swapfile
52                                 rm -f "${_SWAP_DIRECTORY}/${_SWAP_FILE}"
53
54                                 echo "Creating ${_SWAP_SIZE_MB} MB swapfile."
55
56                                 mkdir -p "${_SWAP_DIRECTORY}"
57
58                                 # Unfortunately, swapon does not support files
59                                 # with holes, therefore we cannot preallocate.
60                                 dd if=/dev/zero of="${_SWAP_DIRECTORY}/${_SWAP_FILE}"  bs=1024k count="${_SWAP_SIZE_MB}"
61                         else
62                                 echo "Exit."
63                                 return 1
64                         fi
65                 fi
66
67                 echo "Enabling ${_SWAP_DIRECTORY}/${_SWAP_FILE}."
68
69                 mkswap "${_SWAP_DIRECTORY}/${_SWAP_FILE}"
70                 swapon "${_SWAP_DIRECTORY}/${_SWAP_FILE}"
71                 ;;
72
73         rm|remove)
74                 if grep -qs "${_SWAP_DIRECTORY}/${_SWAP_FILE}" /proc/swaps
75                 then
76                         echo "Disabling ${_SWAP_DIRECTORY}/${_SWAP_FILE}."
77
78                         swapoff "${_SWAP_DIRECTORY}/${_SWAP_FILE}"
79                 fi
80
81                 if [ "${_SWAP_PURGE}" = "true" ]
82                 then
83                         echo "Removing ${_SWAP_DIRECTORY}/${_SWAP_FILE}."
84
85                         rm -f "${_SWAP_DIRECTORY}/${_SWAP_FILE}"
86
87                         __DIRECTORY="${_SWAP_DIRECTORY}"
88                         while [ "${__DIRECTORY}" != "/" ]
89                         do
90                                 rmdir --ignore-fail-on-non-empty "${__DIRECTORY}"
91                                 __DIRECTORY="$(dirname ${__DIRECTORY})"
92                         done
93                 fi
94                 ;;
95
96         *)
97                 echo "Usage: ${0} {add|remove}"
98                 exit 1
99                 ;;
100 esac