Debian Codegraph @

setup/files/misc/mkswapfile

raw symbols SCIP
1 #!/bin/bashscip-shell filesystem . . `/bin/bash`.
2 # mkswapfile This shell script takes care of starting and stopping
3 # the mkswapfile.
4 #
5 # chkconfig: 2345 35 65
6 # description: mkswapfile is a script to create swap file for DRBL client. This service must after netfs
7 #
8 # Author: Blake, Kuo-Lien Huang
9 # License: GPL
10 # Description:
11 # start|stop mkswapfile in drbl client.
12 #
13 # For SuSE insserv
14 ### BEGIN INIT INFO
15 # Provides: mkswapfile
16 # Required-Start: $network $portmap $remote_fs
17 # Required-Stop: $network $portmap $remote_fs
18 # X-UnitedLinux-Should-Start:
19 # X-UnitedLinux-Should-Stop:
20 # Default-Start: 2 3 4 5
21 # Default-Stop: 0 1 6
22 # Description: Start the swapfile
23 ### END INIT INFO
24
25 # Modified by Steven Shiau <steven _at_ clonezilla org>, Ceasar Sun <ceasar _at_ nchc org tw> to use it in DRBL
26 # This file is borrowed from Knoppix (http://www.knoppix.org).
27
28 # Setting
29 SWAP_FILE=drbl.swap
30
31 # Source function library.
32 [scip-shell system . . `[`. -f /etc/rc.d/init.d/functionsscip-shell filesystem . . `/etc/rc.d/init.d/functions`. ] && . /etc/rc.d/init.d/functionsscip-shell source . . `/etc/rc.d/init.d/functions`.
33
34 # For SuSE
35 [scip-shell system . . `[`. -e /etc/rc.statusscip-shell filesystem . . `/etc/rc.status`. ] && . /etc/rc.statusscip-shell source . . `/etc/rc.status`.
36
37 # Load DRBL setting and functions
38 DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}"
39 . $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions
40
41 # Source mkswapfile configuration.
42 # It contains the maxswapsize (MB)
43 # like: maxswapsize=512
44 [scip-shell system . . `[`. ! -f $SYSCONF_PATH/mkswapfile ] && exit 1
45 . $SYSCONF_PATH/mkswapfile
46 [scip-shell system . . `[`. -z "$maxswapsize" ] && exit 1
47
48 newswapfilescip-shell shell shell-project 0.0.0 newswapfile().() {
49 total_swap_size=0
50 # Maximum swap file size
51 maxswapsize=$1
52 # Find exist swap partitions
53 for p in `fdisk -l | grepscip-shell system . . grep. "Linux swap" | cutscip-shell system . . cut. -d" " -f1`; do
54 echoscip-shell system . . echo. "Using the existing swap partition in $p"
55 swaponscip-shell system . . swapon. $p
56 exist_swap_size=`catscip-shell system . . cat. /proc/swapsscip-shell filesystem . . `/proc/swaps`. | grepscip-shell system . . grep. $p | awkscip-shell system . . awk. -F" " '{print $3}'`
57 exist_swap_size_in_MB=`exprscip-shell system . . expr. $exist_swap_size /scip-shell filesystem . . `/`. 1000`
58 echoscip-shell system . . echo. "$p with size $exist_swap_size_in_MB (MB)."
59 total_swap_size=`exprscip-shell system . . expr. $exist_swap_size_in_MB + $total_swap_size`
60 [scip-shell system . . `[`. $total_swap_size -ge $maxswapsize ] && return 0
61 done
62
63 # Create swap files in exist partitions
64 avail_part="$(LC_ALL=C fdisk -l 2>/dev/nullscip-shell filesystem . . `/dev/null`. | grepscip-shell system . . grep. "^\/dev\/" | awkscip-shell system . . awk. '{ if($2=="*" && $6!="f" && $6!="0" && $6!="5") print $1; if($2!="*" && $5!="f" && $5!="0" && $5!="5") print $1; }')"
65 for p in $avail_part; do
66 part_fs="$(LC_ALL=C ocs-get-dev-info $p type filesystem)"
67 rc=$?
68 [scip-shell system . . `[`. "$rc" -gt 0 ] && continue
69 case "$part_fs" in *[Ss][Ww][Aa][Pp]*|*extended*) continue ;; esac
70 # If NTFS, skip it since it's readonly if mounting via kernel. If the future maybe we will use ntfs-3g.
71 if [scip-shell system . . `[`. -n "$(echoscip-shell system . . echo. "$part_fs" | grepscip-shell system . . grep. -iE "ntfs")" ]; then
72 echoscip-shell system . . echo. "Skip creating swap file in NTFS partition $p..."
73 continue
74 fi
75 # Initial value
76 Swap_2_create=0
77 SwapSize_Need=0
78 exist_size_in_MB=0
79 #
80 SwapSize_Need=`exprscip-shell system . . expr. $maxswapsize - $total_swap_size`
81 d="/tmp/${p##*/}"
82 f="$d/$SWAP_FILE"
83 mkdirscip-shell system . . mkdir. -p $d
84 # Mount the partition, check the size, create the swap file
85 if mountscip-shell system . . mount. $p $d; then
86 if [scip-shell system . . `[`. -f "$f" ]; then
87 exist_size_in_bytes="$(statscip-shell system . . stat. -c "%s" $f)"
88 exist_size_in_MB="$(echoscip-shell system . . echo. "scale=0; $exist_size_in_bytes /1024/1024" |bc -l)"
89 echoscip-shell system . . echo. "exist_size_in_MB = $exist_size_in_MB"
90 fi
91 ## check the partition size, use 60% for swap
92 AVAIL=$(dfscip-shell system . . df. --block-size=1M $d | awkscip-shell system . . awk. '/^\/dev\//{print $4}scip-shell filesystem . . `/^\/dev\//{print $4}`.')
93 AVAIL=`exprscip-shell system . . expr. $AVAIL + $exist_size_in_MB`
94 AVAIL=$(echoscip-shell system . . echo. "scale=0; $AVAIL * 3 / 5 " |bc -l)
95 if [scip-shell system . . `[`. "$SwapSize_Need" -gt "$AVAIL" ]; then
96 Swap_2_create=$AVAIL
97 else
98 Swap_2_create=$SwapSize_Need
99 fi
100 # when the swap file if not exist, or the existing swapfile size
101 # is not matched, create it.
102 if [scip-shell system . . `[`. ! -f "$f" -o "$exist_size_in_MB" -ne "$Swap_2_create" ]; then
103 echoscip-shell system . . echo. "Creating a swap file $f with size $Swap_2_create MB..."
104 ddscip-shell system . . dd. if=/dev/zeroscip-shell filesystem . . `/dev/zero`. of="$f" bs=1M count="$Swap_2_create"
105 retv=$?
106 if [scip-shell system . . `[`. "$retv" -eq 0 ]; then
107 [scip-shell system . . `[`. -e "$f" ] && chmodscip-shell system . . chmod. 600 $f 2>/dev/nullscip-shell filesystem . . `/dev/null`.
108 else
109 # maybe the partition filesystem is ntfs, which is normally ro.
110 echoscip-shell system . . echo. "The swap file $f can NOT be created!"
111 fi
112 else
113 echoscip-shell system . . echo. "Found an existing swap file with size $exist_size_in_MB MB (It makes the total swap file size is $maxswapsize MB), use that!"
114 fi
115 # force to format the file as swapfile no matter it is or not.
116 [scip-shell system . . `[`. -e "$f" ] && mkswapscip-shell system . . mkswap. -v1 "$f" && swaponscip-shell system . . swapon. -v "$f" &
117 fi
118
119 # check if the total_swap_size is greater than $maxswapsize
120 total_swap_size=`exprscip-shell system . . expr. $Swap_2_create + $total_swap_size`
121 [scip-shell system . . `[`. "$total_swap_size" -ge "$maxswapsize" ] && return 0
122 done
123 if [scip-shell system . . `[`. "$total_swap_size" -eq 0 ]; then
124 echoscip-shell system . . echo. "No writable partition is available. Skip creating swap file!"
125 fi
126 }
127
128 rmswapfilescip-shell shell shell-project 0.0.0 rmswapfile().() {
129 for p in `fdisk -l | grepscip-shell system . . grep. "^\/dev\/" | awkscip-shell system . . awk. '{ if($2=="*" && $6!="f" && $6!="0") print $1; if($2!="*" && $5!="f" && $5!="0") print $1; }'`
130 do
131 d="/tmp/${p##*/}"
132 f="$d/$SWAP_FILE"
133 if [scip-shell system . . `[`. -f "$f" ]; then
134 /sbin/swapoffscip-shell filesystem . . `/sbin/swapoff`. -v "$f"
135 # do not remove, so that next time we can use, this should be better,
136 # otherwise client has to spend some time to dd again.
137 #rm -f $f
138 fi
139 if [scip-shell system . . `[`. "$(mountscip-shell system . . mount. | grepscip-shell system . . grep. "$d")" != "" ]; then
140 umountscip-shell system . . umount. $d
141 fi
142 done
143 swapoffscip-shell system . . swapoff. -a
144 }
145
146 ###
147 case "$1" in
148 "start")
149 #echo "Creating swap file (Upto $maxswapsize Mb) if the harddisk is available... This may take few minitues."
150 if [scip-shell system . . `[`. -e /etc/debian_versionscip-shell filesystem . . `/etc/debian_version`. ]; then
151 # Debian
152 echoscip-shell system . . echo. -n "Starting mkswapfile services... "
153 start-stop-daemonscip-shell system . . start-stop-daemon. --start --quiet \
154 --pidfile /var/run/mkswapfile.pidscip-shell filesystem . . `/var/run/mkswapfile.pid`. --exec $0 -- newswapfile $maxswapsize
155 elif [scip-shell system . . `[`. -e /etc/SuSE-releasescip-shell filesystem . . `/etc/SuSE-release`. ]; then
156 # SuSE
157 echoscip-shell system . . echo. -n "Starting mkswapfile services... "
158 newswapfilescip-shell shell shell-project 0.0.0 newswapfile(). $maxswapsize
159 rc_status -v
160 else
161 # RH-like
162 action $"Starting mkswapfile services: " $0 newswapfile $maxswapsize
163 RETVAL=$?
164 [scip-shell system . . `[`. "$RETVAL" = 0 ] && touchscip-shell system . . touch. /var/lock/subsys/mkswapfilescip-shell filesystem . . `/var/lock/subsys/mkswapfile`.
165 fi
166 ;;
167 "stop")
168 #echo "Removing swap file... This may take few minitues."
169 if [scip-shell system . . `[`. -e /etc/debian_versionscip-shell filesystem . . `/etc/debian_version`. ]; then
170 # Debian
171 echoscip-shell system . . echo. -n "Stopping mkswapfile services... "
172 $0 rmswapfile
173 echoscip-shell system . . echo. "done!"
174 elif [scip-shell system . . `[`. -e /etc/SuSE-releasescip-shell filesystem . . `/etc/SuSE-release`. ]; then
175 # SuSE
176 echoscip-shell system . . echo. -n "Stopping mkswapfile services... "
177 rmswapfilescip-shell shell shell-project 0.0.0 rmswapfile(). $maxswapsize
178 rc_status -v
179 else
180 # RH-like
181 action $"Stopping mkswapfile services: " $0 rmswapfile
182 RETVAL=$?
183 [scip-shell system . . `[`. "$RETVAL" = 0 ] && rmscip-shell system . . rm. -f /var/lock/subsys/mkswapfilescip-shell filesystem . . `/var/lock/subsys/mkswapfile`.
184 fi
185 ;;
186 "newswapfile")
187 newswapfilescip-shell shell shell-project 0.0.0 newswapfile(). $maxswapsize
188 ;;
189 "rmswapfile")
190 rmswapfilescip-shell shell shell-project 0.0.0 rmswapfile().
191 ;;
192 restart)
193 $0 stop
194 $0 start
195 ;;
196 *)
197 echoscip-shell system . . echo. $"Usage: $0 {start|stop|restart}"
198 exit 1
199 esac
200
201 exit 0