├── LICENSE ├── README.md ├── zswap.service └── zswap.sh /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 bluerider 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # zswap 2 | 3 | Zswap extends the amount of usable ram one has. It does this by utilizing compressed zram block devices.It is not related to the kernel zswap module! 4 | 5 | Zswap defaults to use a single multithreaded lzo compressed half-ram swap disk 6 | Edit the following parameters in zswap.sh to configure zswap: 7 | 8 | Parameter Usage | Example 9 | ------------|-------------------------------------------|--------------- 10 | size | Set the size a block device(K,M,G) | size=(1G) 11 | algo | Set the compression algorithm (lzo|lz4) | algo=(lzo) 12 | threads | Set the # of threads to compress with | threads=(2) 13 | num_devices | Set the # of swap devices from zswap | num_devices=1 14 | 15 | Use the following commands to control zswap: 16 | 17 | Command | Usage 18 | ---------------------------|------------------------------------ 19 | zswap.sh start | Start zswap 20 | zswap.sh stop | Stop zswap 21 | zswap.sh restart | Restart zswap 22 | systemctl enable zswap | Start zswap on boot using systemd 23 | systemctl start zswap | Start zswap using systemd 24 | systemctl stop zswap | Stop zswap using systemd 25 | systemctl restart zswap | Restart zswap using systemd 26 | -------------------------------------------------------------------------------- /zswap.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=Zram-based swap (compressed RAM block devices) 3 | 4 | [Service] 5 | Type=oneshot 6 | ExecStart=/usr/lib/systemd/scripts/zswap.sh start 7 | ExecStop=/usr/lib/systemd/scripts/zswap.sh stop 8 | ExecReload=/usr/lib/systemd/scripts/zswap.sh restart 9 | RemainAfterExit=yes 10 | 11 | [Install] 12 | WantedBy=multi-user.target 13 | -------------------------------------------------------------------------------- /zswap.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ## mem_size is total ram size in megabytes 4 | ## size : array : accepts integer sizes in K,M,G 5 | ## num_devices : integer : number of devices requested 6 | ## threads : array : accepts integers for # threads to use 7 | ## algo : array : accepts lzo or lz4 8 | mem_array=($(free -m)) 9 | declare -r mem_size=${mem_array[7]} 10 | unset mem_array; 11 | declare -a size=($[mem_size/2]M) 12 | declare -a algo=(lzo) 13 | declare -a threads=($(nproc)) 14 | declare -r num_devices=1 15 | 16 | makeSwap() { 17 | mkswap "$1" 18 | swapon --priority 100 "$1" 19 | } 20 | 21 | stopSwap() { 22 | swapoff "$1"; 23 | zramctl -r "$1"; 24 | } 25 | 26 | resetSwap() { 27 | swapoff "$1"; 28 | swapon -f --priority 100 "$1"; 29 | } 30 | 31 | ## makeSwap function accepts integer as $1 32 | enableZmodule() { 33 | makeSwap "$(zramctl -f -s ${size[$1]} -a ${algo[$1]} -t ${threads[$1]})" 34 | } 35 | 36 | case $1 in 37 | start) 38 | count() { 39 | echo $# 40 | } 41 | ## if no zram devices. ensure there is 2 more than needed 42 | if [ ! -b "/dev/zram0" ]; then 43 | modprobe zram num_devices=$[num_devices+2]; 44 | fi; 45 | ## check if there are enough devices; there are 7 lines per device in zramctl 46 | if [[ $[$(count /dev/zram*)-$(count $(zramctl -n --raw))/7] -ge $num_devices ]]; then 47 | for ((i=0;i<$num_devices;i++)); do 48 | enableZmodule $i; 49 | done; 50 | else 51 | echo "Not enough zram devices"; 52 | fi;; 53 | stop) 54 | for a in $(swapon --noheadings); do 55 | if [[ "$a" == /dev/zram* ]]; then 56 | stopSwap "$a"; 57 | fi; 58 | done;; 59 | restart) 60 | for a in $(swapon --noheadings); do 61 | if [[ "$a" == /dev/zram* ]]; then 62 | resetSwap "$a"; 63 | fi; 64 | done;; 65 | *) 66 | echo "Not a valid option";; 67 | esac; 68 | --------------------------------------------------------------------------------