swapfile/etc-init.d-swapfile

103 lines
2.4 KiB
Plaintext
Raw Permalink Normal View History

2023-06-08 17:39:16 -03:00
#!/bin/sh /etc/rc.common
2023-07-03 18:15:27 -03:00
# (C) 2016 - 2023 Fernao Vellozo | fernao@disroot.org
2023-06-08 17:39:16 -03:00
# This is free software under the terms of GPLv3
2023-07-03 18:15:27 -03:00
# v 1.0.5
2023-06-08 17:39:16 -03:00
2023-07-03 18:15:27 -03:00
# Service priority in the system
2023-06-08 17:39:16 -03:00
START=90
EXTRA_COMMANDS="status create"
EXTRA_HELP=" status Check service status
create Create a swapfile"
2023-07-03 18:15:27 -03:00
# if version is 19 or lower, use spaces on the help menu above
# if version 21 or higher, use tabs
#
# overlay partition is the default value
# it's better to change it and use other place to store the swapfile
2023-06-08 17:39:16 -03:00
2023-07-01 23:36:23 -03:00
SWPATH="/overlay/swapfile"
2023-06-08 17:39:16 -03:00
boot() {
if [ -f $SWPATH ]; then
swapon $SWPATH
logger "swapfile started on boot"
else
logger "ERROR: swapfile not found"
fi
}
start() {
if [ -f $SWPATH ]; then
swapon $SWPATH
logger "swapfile started"
echo "SwapFile started"
else
logger "ERROR: swapfile not found"
echo "ERROR: SwapFile not found at "$SWPATH
fi
}
restart() {
if [ -f $SWPATH ]; then
swapoff $SWPATH
swapon $SWPATH
logger "swapfile stopped, reloading..."
echo "SwapFile Reloaded"
else
logger "ERROR: swapfile not found"
echo "ERROR: SwapFile not found at "$SWPATH
fi
}
stop() {
if [ -f $SWPATH ]; then
swapoff $SWPATH
logger "swapfile stopped"
echo "SwapFile stopped "$SWPATH
else
logger "ERROR: swapfile not found"
echo "ERROR: SwapFile not found at "$SWPATH
fi
}
create() {
RAMTOTAL=`fgrep MemTotal /proc/meminfo | awk '{print $2}'`
SWAPTOTAL=`fgrep SwapTotal /proc/meminfo | awk '{print $2}'`
2023-07-03 18:15:27 -03:00
OVERLAYSIZE=`df | grep overlayfs | awk '{print $4}'`
if [ "$OVERLAYSIZE" -lt "$RAMTOTAL" ]; then
2023-07-01 23:36:23 -03:00
echo "ERROR: Not enough storage for a swapfile, quitting..."
exit 1
elif [ $SWAPTOTAL != 0 ]; then
2023-06-08 17:39:16 -03:00
echo "ERROR: Swap already in use, quitting..."
2023-07-03 18:15:27 -03:00
exit 1
elif [ -f $SWPATH ]; then
echo "ERROR: File already exists, quitting..."
exit 1
2023-06-08 17:39:16 -03:00
else
2023-07-03 18:15:27 -03:00
echo "Creating Swap File of "$RAMTOTAL
echo "Please wait..."
dd if=/dev/zero of=$SWPATH bs=1024 count=$RAMTOTAL
mkswap -L SWAPFILE $SWPATH
chmod 600 $SWPATH
2023-06-08 17:39:16 -03:00
/etc/init.d/swapfile enable
logger "SwapFile created"
2023-07-03 18:15:27 -03:00
echo "SwapFile successfully created and enabled to run after the next reboot"
/etc/init.d/swapfile start
echo; free
2023-06-08 17:39:16 -03:00
fi
}
status() {
SWAPTOTAL=`fgrep SwapTotal /proc/meminfo | awk '{print $2}'`
if [ $SWAPTOTAL == 0 ]; then
echo "No swap memory available"
else
echo $SWAPTOTAL "KB of swap are available"
if [ -f $SWPATH ]; then
echo "Swapfile is located at "$SWPATH
else
echo "Swap memory has been loaded, but not from here"
fi
fi
}