Last active
April 8, 2020 15:57
-
-
Save ivanstepanovftw/e858a4c5906d4b24b483335d8db58cad to your computer and use it in GitHub Desktop.
swap daemon for Arch Linux
This file contains hidden or 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
[Unit] | |
Description=autoswap | |
After=local-fs.target | |
RequiresMountsFor=/ | |
RequiresMountsFor=/run | |
RequiresMountsFor=/sys | |
RequiresMountsFor=/var | |
[Service] | |
Type=oneshot | |
RemainAfterExit=yes | |
TimeoutStopSec=600 | |
Nice=-19 | |
OOMScoreAdjust=-1000 | |
CPUAccounting=true | |
ProtectHome=read-only | |
Environment=DISKSIZE=8G | |
Environment=COMP_ALGORITHM=zstd | |
ExecStartPre=modprobe zram num_devices=1 | |
ExecStart=/usr/bin/sh /usr/bin/autoswap start | |
ExecStop=/usr/bin/sh /usr/bin/autoswap stop | |
[Install] | |
WantedBy=local-fs.target |
This file contains hidden or 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
#!/usr/bin/env bash | |
# Further reading: https://kernel.googlesource.com/pub/scm/linux/kernel/git/next/linux-next/+/refs/heads/akpm/Documentation/blockdev/zram.txt | |
function check() { rc=$?; LINE=$1; [[ $rc != 0 ]] && echo "${BASH_SOURCE[0]}:${LINE} exitted with return code ${rc}" && exit $rc; } | |
function usage() { echo -e 'Usage: systemctl enable|disable|start|restart|stop autoswap\nor: DISKSIZE=8G COMP_ALGORITHM=zstd autoswap start|stop\n\nConfiguration file: /etc/systemd/system/autozram.service'; } | |
function start() { | |
[[ -z ${DISKSIZE} || -z ${COMP_ALGORITHM} ]] && usage && exit 1 | |
device=$(zramctl -f -s ${DISKSIZE} -a ${COMP_ALGORITHM}) || check $LINENO | |
mkswap --label 'zram swap' "${device}" || check $LINENO | |
swapon --priority 10 "${device}" || check $LINENO | |
} | |
function stop() { | |
find /dev -name "zram*" -exec swapoff {} \; | |
find /dev -name "zram*" -exec zramctl -r {} \; | |
} | |
# MAIN | |
[[ $UID != 0 ]] && usage && exit 1 | |
case $1 in | |
start) | |
start;; | |
stop) | |
stop;; | |
restart) | |
stop; start;; | |
*) | |
usage;; | |
esac | |
swapon |
This file contains hidden or 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
#!/usr/bin/env bash | |
# Execute: | |
# bash <(curl --tlsv1.2 -s https://gist.githubusercontent.com/ivanstepanovftw/e858a4c5906d4b24b483335d8db58cad/raw/install.sh) | |
function check() { rc=$?; LINE=$1; [[ $rc != 0 ]] && echo "${BASH_SOURCE[0]}:${LINE} exitted with return code ${rc}." && exit $rc; } | |
curl --tlsv1.2 -L https://gist.githubusercontent.com/ivanstepanovftw/e858a4c5906d4b24b483335d8db58cad/raw/autoswap.service -o /etc/systemd/system/autoswap.service || check $LINENO | |
curl --tlsv1.2 -L https://gist.githubusercontent.com/ivanstepanovftw/e858a4c5906d4b24b483335d8db58cad/raw/autoswap.sh -o /usr/bin/autoswap || check $LINENO | |
systemctl enable autoswap || check $LINENO | |
systemctl start autoswap || check $LINENO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ubuntu 19.04