You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.4 KiB

  1. #!/usr/bin/env sh
  2. . /etc/log2ram.conf
  3. HDD_LOG=/var/hdd.log
  4. RAM_LOG=/var/log
  5. LOG_NAME="log2ram.log"
  6. LOG2RAM_LOG="${RAM_LOG}/${LOG_NAME}"
  7. LOG_OUTPUT="tee -a $LOG2RAM_LOG"
  8. isSafe () {
  9. [ -d $HDD_LOG/ ] || echo "ERROR: $HDD_LOG/ doesn't exist! Can't sync."
  10. [ -d $HDD_LOG/ ] || exit 1
  11. }
  12. syncToDisk () {
  13. isSafe
  14. if [ "$USE_RSYNC" = true ]; then
  15. rsync -aXWv --delete --links $RAM_LOG/ $HDD_LOG/ 2>&1 | $LOG_OUTPUT
  16. else
  17. cp -rfup $RAM_LOG/ -T $HDD_LOG/ 2>&1 | $LOG_OUTPUT
  18. fi
  19. }
  20. syncFromDisk () {
  21. isSafe
  22. if [ ! -z "$(du -sh -t "$SIZE" $HDD_LOG/ | cut -f1)" ]; then
  23. echo "ERROR: RAM disk too small. Can't sync."
  24. umount -l $RAM_LOG/
  25. umount -l $HDD_LOG/
  26. if [ "$MAIL" = true ]; then
  27. echo "LOG2RAM : No place on RAM anymore, fallback on the disk" | mail -s 'Log2Ram Error' root;
  28. fi
  29. exit 1
  30. fi
  31. if [ "$USE_RSYNC" = true ]; then
  32. rsync -aXWv --delete --links $HDD_LOG/ $RAM_LOG/ 2>&1 | $LOG_OUTPUT
  33. else
  34. cp -rfup $HDD_LOG/ -T $RAM_LOG/ 2>&1 | $LOG_OUTPUT
  35. fi
  36. }
  37. wait_for () {
  38. while ! grep -qs "$1" /proc/mounts; do
  39. sleep 0.1
  40. done
  41. }
  42. createZramLogDrive () {
  43. # Check Zram Class created
  44. if [ ! -d "/sys/class/zram-control" ]; then
  45. modprobe zram
  46. RAM_DEV='0'
  47. else
  48. RAM_DEV=$(cat /sys/class/zram-control/hot_add)
  49. fi
  50. echo ${COMP_ALG} > /sys/block/zram${RAM_DEV}/comp_algorithm
  51. echo ${LOG_DISK_SIZE} > /sys/block/zram${RAM_DEV}/disksize
  52. echo ${SIZE} > /sys/block/zram${RAM_DEV}/mem_limit
  53. mke2fs -t ext4 /dev/zram${RAM_DEV}
  54. }
  55. case "$1" in
  56. start)
  57. [ -d $HDD_LOG/ ] || mkdir $HDD_LOG/
  58. mount --bind $RAM_LOG/ $HDD_LOG/
  59. mount --make-private $HDD_LOG/
  60. wait_for $HDD_LOG
  61. if [ "$ZL2R" = true ]; then
  62. createZramLogDrive
  63. mount -t ext4 -o nosuid,noexec,nodev,user=log2ram /dev/zram${RAM_DEV} ${RAM_LOG}/
  64. else
  65. mount -t tmpfs -o nosuid,noexec,nodev,mode=0755,size=${SIZE} log2ram $RAM_LOG/
  66. fi
  67. wait_for $RAM_LOG
  68. syncFromDisk
  69. ;;
  70. stop)
  71. syncToDisk
  72. #ZRAM_LOG=$(awk '$2 == "/var/log" {print $1}' /proc/mounts)
  73. #ZRAM_LOG=$(echo ${ZRAM_LOG} | grep -o -E '[0-9]+')
  74. umount -l $RAM_LOG/
  75. umount -l $HDD_LOG/
  76. # Unsure as even with Root permision denied
  77. #echo ${ZRAM_LOG} > /sys/class/zram-control/hot_remove
  78. ;;
  79. write)
  80. syncToDisk
  81. ;;
  82. *)
  83. echo "Usage: log2ram {start|stop|write}" >&2
  84. exit 1
  85. ;;
  86. esac