Files
Arch-R/projects/ROCKNIX/packages/sysutils/util-linux/scripts/mount-swap
2025-07-13 19:02:44 +01:00

73 lines
1.8 KiB
Bash
Executable File

#!/bin/sh
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2009-2014 Stephan Raue (stephan@openelec.tv)
. /etc/swap.conf
. /etc/profile
if [ -f /storage/.config/swap.conf ]; then
. /storage/.config/swap.conf
fi
if [ -f "$SWAPFILE" ]; then
# Clean up existing swap file if it is smaller than desired or swap type is zram
if [ $(ls -l "$SWAPFILE" | awk '{print $5}') -lt $(($SWAPSIZE*1024*1024)) -o "$SWAP_TYPE" = "zram" ]; then
[[ $(swapon | grep "$SWAPFILE") ]] && swapoff "$SWAPFILE"
rm -rf "$SWAPFILE"
fi
fi
if [ -e /dev/.storage_netboot ] ; then
logger -t Boot "### netbooting... swap disabled ###"
exit 0
fi
if [ ! "$SWAP_ENABLED" = yes ] ; then
logger -t Boot "### swap disabled via configfile ###"
exit 0
fi
SWAP=`blkid -t TYPE="swap" -o device`
case $1 in
create)
logger -t Boot "### creating swap type ${SWAP_TYPE} ###"
if [ "$SWAP_TYPE" = "file" ]; then
if [ -z "$SWAP" -a ! -f "$SWAPFILE" ]; then
mkdir -p `dirname $SWAPFILE`
dd if=/dev/zero of=$SWAPFILE bs=1M count=$SWAPSIZE
chmod 0600 $SWAPFILE
mkswap $SWAPFILE
fi
elif [ "$SWAP_TYPE" = "zram" ]; then
# Load zram module
modprobe zram
# Set up zram device of desired size
zramctl --find --size ${SWAPSIZE}M --algorith zstd
fi
;;
mount)
logger -t Boot "### mounting swap type ${SWAP_TYPE} ###"
if [ "$SWAP_TYPE" = "file" ]; then
[ -z "$SWAP" -a -f "$SWAPFILE" ] && SWAP=$SWAPFILE
for i in $SWAP; do
swapon -p 10000 $i
done
elif [ "$SWAP_TYPE" = "zram" ]; then
ZRAM_DEV=$(zramctl -o NAME | grep zram)
if [ ! -z "$ZRAM_DEV" ]; then
mkswap ${ZRAM_DEV}
swapon ${ZRAM_DEV}
fi
fi
;;
unmount)
swapoff -a
;;
esac