| 1 | #!/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 | [ -f /etc/rc.d/init.d/functions ] && . /etc/rc.d/init.d/functions |
| 33 | |
| 34 | # For SuSE |
| 35 | [ -e /etc/rc.status ] && . /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 | [ ! -f $SYSCONF_PATH/mkswapfile ] && exit 1 |
| 45 | . $SYSCONF_PATH/mkswapfile |
| 46 | [ -z "$maxswapsize" ] && exit 1 |
| 47 | |
| 48 | 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 | grep "Linux swap" | cut -d" " -f1`; do |
| 54 | echo "Using the existing swap partition in $p" |
| 55 | swapon $p |
| 56 | exist_swap_size=`cat /proc/swaps | grep $p | awk -F" " '{print $3}'` |
| 57 | exist_swap_size_in_MB=`expr $exist_swap_size / 1000` |
| 58 | echo "$p with size $exist_swap_size_in_MB (MB)." |
| 59 | total_swap_size=`expr $exist_swap_size_in_MB + $total_swap_size` |
| 60 | [ $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/null | grep "^\/dev\/" | 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 | [ "$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 [ -n "$(echo "$part_fs" | grep -iE "ntfs")" ]; then |
| 72 | 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=`expr $maxswapsize - $total_swap_size` |
| 81 | d="/tmp/${p##*/}" |
| 82 | f="$d/$SWAP_FILE" |
| 83 | mkdir -p $d |
| 84 | # Mount the partition, check the size, create the swap file |
| 85 | if mount $p $d; then |
| 86 | if [ -f "$f" ]; then |
| 87 | exist_size_in_bytes="$(stat -c "%s" $f)" |
| 88 | exist_size_in_MB="$(echo "scale=0; $exist_size_in_bytes /1024/1024" |bc -l)" |
| 89 | echo "exist_size_in_MB = $exist_size_in_MB" |
| 90 | fi |
| 91 | ## check the partition size, use 60% for swap |
| 92 | AVAIL=$(df --block-size=1M $d | awk '/^\/dev\//{print $4}') |
| 93 | AVAIL=`expr $AVAIL + $exist_size_in_MB` |
| 94 | AVAIL=$(echo "scale=0; $AVAIL * 3 / 5 " |bc -l) |
| 95 | if [ "$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 [ ! -f "$f" -o "$exist_size_in_MB" -ne "$Swap_2_create" ]; then |
| 103 | echo "Creating a swap file $f with size $Swap_2_create MB..." |
| 104 | dd if=/dev/zero of="$f" bs=1M count="$Swap_2_create" |
| 105 | retv=$? |
| 106 | if [ "$retv" -eq 0 ]; then |
| 107 | [ -e "$f" ] && chmod 600 $f 2>/dev/null |
| 108 | else |
| 109 | # maybe the partition filesystem is ntfs, which is normally ro. |
| 110 | echo "The swap file $f can NOT be created!" |
| 111 | fi |
| 112 | else |
| 113 | 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 | [ -e "$f" ] && mkswap -v1 "$f" && swapon -v "$f" & |
| 117 | fi |
| 118 | |
| 119 | # check if the total_swap_size is greater than $maxswapsize |
| 120 | total_swap_size=`expr $Swap_2_create + $total_swap_size` |
| 121 | [ "$total_swap_size" -ge "$maxswapsize" ] && return 0 |
| 122 | done |
| 123 | if [ "$total_swap_size" -eq 0 ]; then |
| 124 | echo "No writable partition is available. Skip creating swap file!" |
| 125 | fi |
| 126 | } |
| 127 | |
| 128 | rmswapfile() { |
| 129 | for p in `fdisk -l | grep "^\/dev\/" | 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 [ -f "$f" ]; then |
| 134 | /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 [ "$(mount | grep "$d")" != "" ]; then |
| 140 | umount $d |
| 141 | fi |
| 142 | done |
| 143 | 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 [ -e /etc/debian_version ]; then |
| 151 | # Debian |
| 152 | echo -n "Starting mkswapfile services... " |
| 153 | start-stop-daemon --start --quiet \ |
| 154 | --pidfile /var/run/mkswapfile.pid --exec $0 -- newswapfile $maxswapsize |
| 155 | elif [ -e /etc/SuSE-release ]; then |
| 156 | # SuSE |
| 157 | echo -n "Starting mkswapfile services... " |
| 158 | newswapfile $maxswapsize |
| 159 | rc_status -v |
| 160 | else |
| 161 | # RH-like |
| 162 | action $"Starting mkswapfile services: " $0 newswapfile $maxswapsize |
| 163 | RETVAL=$? |
| 164 | [ "$RETVAL" = 0 ] && touch /var/lock/subsys/mkswapfile |
| 165 | fi |
| 166 | ;; |
| 167 | "stop") |
| 168 | #echo "Removing swap file... This may take few minitues." |
| 169 | if [ -e /etc/debian_version ]; then |
| 170 | # Debian |
| 171 | echo -n "Stopping mkswapfile services... " |
| 172 | $0 rmswapfile |
| 173 | echo "done!" |
| 174 | elif [ -e /etc/SuSE-release ]; then |
| 175 | # SuSE |
| 176 | echo -n "Stopping mkswapfile services... " |
| 177 | rmswapfile $maxswapsize |
| 178 | rc_status -v |
| 179 | else |
| 180 | # RH-like |
| 181 | action $"Stopping mkswapfile services: " $0 rmswapfile |
| 182 | RETVAL=$? |
| 183 | [ "$RETVAL" = 0 ] && rm -f /var/lock/subsys/mkswapfile |
| 184 | fi |
| 185 | ;; |
| 186 | "newswapfile") |
| 187 | newswapfile $maxswapsize |
| 188 | ;; |
| 189 | "rmswapfile") |
| 190 | rmswapfile |
| 191 | ;; |
| 192 | restart) |
| 193 | $0 stop |
| 194 | $0 start |
| 195 | ;; |
| 196 | *) |
| 197 | echo $"Usage: $0 {start|stop|restart}" |
| 198 | exit 1 |
| 199 | esac |
| 200 | |
| 201 | exit 0 |