Skip to content

Instantly share code, notes, and snippets.

@ivanstepanovftw
Last active April 8, 2020 15:57
Show Gist options
  • Save ivanstepanovftw/e858a4c5906d4b24b483335d8db58cad to your computer and use it in GitHub Desktop.
Save ivanstepanovftw/e858a4c5906d4b24b483335d8db58cad to your computer and use it in GitHub Desktop.
swap daemon for Arch Linux
[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
#!/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
#!/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
@ivanstepanovftw
Copy link
Author

Ubuntu 19.04

sudo apt install zram-config
sudo init-zram-swapping

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment