Last active
November 2, 2022 08:56
-
-
Save jimklimov/09afe6cc6b7b85d7ba4ad4c1a386ccf2 to your computer and use it in GitHub Desktop.
/etc/init.d/swap-noauto for Linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
### /etc/init.d/swap-noauto for Linux | |
### Copyright (C) 2020-2022 by Jim Klimov | |
### Partially based on https://unix.stackexchange.com/a/594006 | |
### and https://wiki.archlinux.org/index.php/ZFS#Swap_volume | |
### BEGIN INIT INFO | |
# Provides: swap | |
# Required-Start: $local_fs | |
# Required-Stop: $local_fs | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Should-Start: | |
# Should-Stop: | |
# Short-Description: SWAP (noauto) | |
# Description: provide "noauto" swap space, okay if not mountable | |
### END INIT INFO | |
# EXAMPLE SETUP: | |
# zfs create -b $(getconf PAGESIZE) \ | |
# -o logbias=throughput -o sync=always \ | |
# -o primarycache=metadata \ | |
# -o com.sun:auto-snapshot=false \ | |
# -o dedup=off -o checksum=off -o compression=off \ | |
# -V 8G rpool/swap | |
# mkswap -f /dev/zvol/rpool/swap | |
# swapon /dev/zvol/rpool/swap | |
# | |
# In /etc/fstab: | |
# | |
# # https://unix.stackexchange.com/a/594006 | |
# # Swap out tmpfs build workspaces, shared libs and other data, if needed | |
# # noauto to avoid congestion if dual-booting to illumos (mkswap needed after) | |
# /dev/zvol/rpool/swap none swap discard,noauto 0 0 | |
SWAPDEVS="`grep -w noauto /etc/fstab | grep -w swap | awk '{print $1}' | grep -v '#' | tr '\n' ' '`" | |
[ -n "$SWAPDEVS" ] || exit 0 | |
(command -v swapon && command -v swapoff) >/dev/null 2>/dev/null || exit 0 | |
# Report to terminal before systemd wraps take over | |
case $1 in | |
status) | |
echo "Expected swap devices: $SWAPDEVS" | |
swapon -s | |
echo "Swap device listing completed" | |
;; | |
esac | |
. /lib/lsb/init-functions | |
case $1 in | |
start) | |
RES=0 | |
for S in $SWAPDEVS ; do | |
swapon "$S" || { | |
RES=1 | |
echo "FAILED to enable swap on '$S', is it formatted for Linux?" >&2 | |
} | |
done | |
log_end_msg $RES | |
;; | |
stop) | |
RES=0 | |
for S in $SWAPDEVS ; do | |
swapoff "$S" || { | |
RES=1 | |
echo "FAILED to disable swap on '$S', is it attached and enough memory free?" >&2 | |
} | |
done | |
log_end_msg $RES | |
;; | |
reload|restart|force-reload) | |
$0 stop | |
sleep 1 | |
$0 start | |
;; | |
status) | |
echo "Expected swap devices: $SWAPDEVS" | |
swapon -s | |
echo "Swap device listing completed" | |
exit 0 | |
;; | |
*) | |
echo "Usage: /etc/init.d/swap-noauto {start|stop|reload|restart|force-reload|status}" | |
exit 1 | |
;; | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment