Dinosaur
New Member
Noting the comments of @ldf, I've had another go, based more closely on on @prpr's enhanced version.
I haven't tried it on a real HD, so it would be good if someone with an HD could give it a spin.
Feedback is welcome.
Don't know what's happened to the indentation
edit: fixed
- Caters for HD and HDR
- I've made the swapfile size configurable (the easy way)
- Separate default values for HD and HDR, 256MiB and 1024MiB respectively.
- Swapfile deleted at stop for HD
- Swapfile retained for HDR
- Swapfile deleted and recreated on start if configured size has changed
I haven't tried it on a real HD, so it would be good if someone with an HD could give it a spin.
Feedback is welcome.
Code:
#!/bin/sh
########### /mod/etc/init.d/S00swapper ###########
set +x
# size of swapspace in MiB. Configure as required
# Separate values for HD and HDR to allow different defaults
HDRswapsize=1024 # default 1024
HDswapsize=256 # default 256
# Constants
HDRmnt='/mnt/hd3/'
HDmnt='/media/drive1/'
swapbasename='.swap0'
squote="'"
model="$(cat /etc/model)"
#model='HD' # fiddle for testing. normally commented out.
case "$model" in
HDR)
mnt="$HDRmnt"
swapsize="$HDRswapsize"
;;
HD)
mnt="$HDmnt"
swapsize="$HDswapsize"
;;
*)
echo "$squote$model$squote in /etc/model is neither HD nor HDR"
exit 1
;;
esac
swapfile="$mnt$swapbasename"
case "$1" in
start)
if grep "$swapfile" /proc/swaps >/dev/null 2>&1 ; then
echo "Swap file already enabled"
exit 0
fi
actualsize=0
[ -f $swapfile ] && actualsize=$(stat -t "$swapfile"|awk '{print $2/1024/1024}')
if [ $actualsize -ne $swapsize ] ; then
[ -f $swapfile ] && rm $swapfile
dd if=/dev/zero of=$swapfile bs=1M count=$swapsize
mkswap $swapfile
fi
swapon $swapfile
echo "Enabled swap file."
;;
stop)
if grep "$swapfile" /proc/swaps >/dev/null 2>&1 ; then
swapoff $swapfile
echo "Disabled swap file."
[ $model == HD ] && rm $swapfile && echo "Swap file deleted"
else
echo "Swap file was not enabled"
exit 1
fi
;;
*)
echo "Must use $squote$0 start$squote or $squote$0 stop$squote"
exit 1
;;
esac
exit 0
Don't know what's happened to the indentation
edit: fixed
Last edited: