Skip to content

Instantly share code, notes, and snippets.

@hereisderek
Created October 27, 2022 02:53
Show Gist options
  • Save hereisderek/bce1de71596ec1a272185c13dbbdb9de to your computer and use it in GitHub Desktop.
Save hereisderek/bce1de71596ec1a272185c13dbbdb9de to your computer and use it in GitHub Desktop.
create ramdisk on startup
#!/bin/bash
### BEGIN INIT INFO
# Provides: zram
# Required-Start: $all
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop:
# Short-Description: your description here
### END INIT INFO
#
# to enable:
# chmod a+x /etc/init.d/zram.sh && systemctl daemon-reload && systemctl enable --now zram
ZRAM_TEMP=/media/local/zram_temp/zram
start() {
modprobe zram num_devices=3 || (
echo "unable to load zram module, exiting.."
exit 1
)
sleep 3
[[ -b /dev/zram0 ]] || {
echo "block device /dev/zram0 does not exist, existing..."
exit 1
}
# compressed
echo lz4 > /sys/block/zram0/comp_algorithm
echo 4G > /sys/block/zram0/disksize
echo 4G > /sys/block/zram0/mem_limit
# uncompressed
#echo lzo > /sys/block/zram1/comp_algorithm
#echo 2G > /sys/block/zram1/disksize
#echo 2G > /sys/block/zram1/mem_limit
# for zfs cache
echo lzo > /sys/block/zram2/comp_algorithm
echo 2G > /sys/block/zram2/disksize
echo 2G > /sys/block/zram2/mem_limit
# yes|mke2fs -q -m 0 -b 4096 -t ext4 -O sparse_super -L zram /dev/zram0
mkfs.ext4 -F -q -m 0 -b 4096 -t ext4 -O sparse_super -L zram /dev/zram0
mkdir -p /mnt/zram
#umount /mnt/zram
mount -t ext4 -o exec,relatime,nosuid /dev/zram0 /mnt/zram && \
[[ -d "$ZRAM_TEMP" ]] && cp -rau "$ZRAM_TEMP"/* /mnt/zram
chmod -R 777 /mnt/zram
_mount
}
_mount() {
[[ -d /mnt/zram ]] || exit 0
apt clean
mkdir -p /mnt/zram/mount/{cache,tmp}
cp -rau /var/cache /mnt/zram/mount/
mount --bind /mnt/zram/mount/cache /var/cache
mount --bind /mnt/zram/mount/tmp /tmp
}
_umount() {
umount -l /var/cache
umount -l /tmp
}
mkdir_zram_temp() {
[[ -d "$ZRAM_TEMP" ]] \
&& mkdir -p "$ZRAM_TEMP"/lcx/{proxy,media01/{logs,transcode}} \
&& chmod -R 777 "$ZRAM_TEMP" \
&& echo "zram temp created"
}
stop() {
timeout 3 umount -l /mnt/zram || true
_umount
umount -f /mnt/zram
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
# code to check status of app comes here
# example: status program_name
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
esac
exit 0
# vim /etc/init.d/zram
# chmod a+x /etc/init.d/zram && systemctl daemon-reload && systemctl enable --now zram
# update-rc.d zram defaults && service zram restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment