Last active
August 6, 2018 05:25
-
-
Save yangxuan8282/88dd030ebe32ec03e57884d3602ee25e to your computer and use it in GitHub Desktop.
bash script to add zram for rpi
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
#!/bin/bash | |
function addswap { | |
SIZE=${1:-2G} | |
fallocate -l $SIZE /swapfile | |
chmod 600 /swapfile | |
mkswap /swapfile | |
swapon /swapfile | |
} |
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
#!/bin/bash | |
# run as root: sudo su | |
# source addzram; addzram | |
#set -xe | |
function delzram { | |
swapoff /dev/zram* | |
for (( i=0; i<$( nproc ); i++)); do | |
echo 1 > /sys/block/zram"$i"/reset | |
done | |
rmmod zram | |
} | |
function convert_to_bytes { | |
ZRAM_PER_CORE=$(( $( echo $1 | numfmt --from=iec ) / $( nproc ) )) | |
} | |
function check_comp_algorithm { | |
if grep -qw lz4hc /sys/block/zram0/comp_algorithm; then | |
export COMPRESSION_ALGORITHM="lz4hc" | |
elif grep -qw lz4 /sys/block/zram0/comp_algorithm; then | |
export COMPRESSION_ALGORITHM="lz4" | |
elif grep -qw lzo /sys/block/zram0/comp_algorithm; then | |
export COMPRESSION_ALGORITHM="lzo" | |
elif grep -qw deflate /sys/block/zram0/comp_algorithm; then | |
export COMPRESSION_ALGORITHM="deflate" | |
else | |
echo "No valid compression algorithm found."; | |
delzram; | |
exit 1; | |
fi | |
} | |
function get_comp_algorithm { | |
if [ -z "$COMPRESSION_ALGORITHM" ]; then | |
check_comp_algorithm | |
else | |
export COMPRESSION_ALGORITHM=$COMPRESSION_ALGORITHM | |
fi | |
} | |
function addzram { | |
swapoff -a | |
modprobe zram num_devices=$( nproc ) | |
TOTAL_ZRAM_SIZE=${1:-4G} | |
COMPRESSION_ALGORITHM=$2 | |
convert_to_bytes $TOTAL_ZRAM_SIZE | |
get_comp_algorithm $COMPRESSION_ALGORITHM | |
echo 128000 > /proc/sys/vm/min_free_kbytes | |
echo 70 > /proc/sys/vm/swappiness | |
for (( i=0; i<$( nproc ); i++)); do | |
echo "$(( $(nproc) -1))" > /sys/devices/virtual/block/zram"$i"/max_comp_streams | |
echo 1 > /sys/block/zram"$i"/reset | |
echo $COMPRESSION_ALGORITHM > /sys/block/zram"$i"/comp_algorithm | |
echo $ZRAM_PER_CORE > /sys/block/zram"$i"/disksize | |
mkswap /dev/zram"$i" | |
swapon /dev/zram"$i" -p 10 | |
done | |
} |
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
#!/bin/bash | |
# need to be tested | |
# need to compiled kernel with `CONFIG_ZSWAP` | |
function addzswap { | |
ZSWAP_SIZE={1:-25} | |
echo 1 > /sys/module/zswap/parameters/enabled | |
echo $ZSWAP_SIZE > /sys/module/zswap/parameters/max_pool_percent | |
echo z3fold > /sys/module/zswap/parameters/zpool | |
echo lz4 > /sys/module/zswap/parameters/compressor | |
} |
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
#!/bin/bash | |
function delswap { | |
swapoff -a | |
rm -f /swapfile | |
} |
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
#!/bin/bash | |
function delzram { | |
swapoff /dev/zram* | |
for (( i=0; i<$( nproc ); i++)); do | |
echo 1 > /sys/block/zram"$i"/reset | |
done | |
rmmod zram | |
} |
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
#!/bin/bash | |
# need to be tested | |
function delzswap { | |
echo 0 > /sys/module/zswap/parameters/enabled | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment