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.

158 lines
4.1 KiB

  1. #!/usr/bin/env sh
  2. . /etc/log2ram.conf
  3. if [ -z "$PATH_DISK" ]; then
  4. PATH_DISK='/var/log'
  5. fi
  6. LOG_NAME='log2ram.log'
  7. NO_RSYNC=${USE_RSYNC#true}
  8. isSafe () {
  9. [ -d "$HDD_LOG" ] || echo "ERROR: $HDD_LOG/ doesn't exist! Can't sync."
  10. [ -d "$HDD_LOG" ] || exit 1
  11. }
  12. remountOriginal() {
  13. OPTION="$1"
  14. mount -o remount,"$OPTION" "$HDD_LOG"
  15. }
  16. syncToDisk () {
  17. isSafe
  18. if [ -z "${NO_RSYNC}" -a -x "$(command -v rsync)" ]; then
  19. rsync -aXv --inplace --no-whole-file --delete-after "$RAM_LOG"/ "$HDD_LOG"/ 2>&1 | tee -a "$LOG2RAM_LOG"
  20. else
  21. cp -rfup "$RAM_LOG"/ -T "$HDD_LOG"/ 2>&1 | tee -a "$LOG2RAM_LOG"
  22. fi
  23. }
  24. syncFromDisk () {
  25. isSafe
  26. TP_SIZE=$SIZE
  27. if [ "$ZL2R" = true ]; then
  28. TP_SIZE=$LOG_DISK_SIZE
  29. fi
  30. if [ -n "$(du -sh -t "$TP_SIZE" "$HDD_LOG"/ | cut -f1)" ]; then
  31. echo "ERROR: RAM disk for \"$HDD_LOG/\" too small. Can't sync."
  32. umount -l "$RAM_LOG"/
  33. umount -l "$HDD_LOG"/
  34. if [ "$MAIL" = true ]; then
  35. echo "LOG2RAM : No place on RAM for \"$HDD_LOG/\" anymore, fallback on the disk" | mail -s 'Log2Ram Error' root;
  36. fi
  37. exit 1
  38. fi
  39. if [ -z "${NO_RSYNC}" -a -x "$(command -v rsync)" ]; then
  40. rsync -aXv --inplace --no-whole-file --delete-after "$HDD_LOG"/ "$RAM_LOG"/ 2>&1 | tee -a "$LOG2RAM_LOG"
  41. else
  42. cp -rfup "$HDD_LOG"/ -T "$RAM_LOG"/ 2>&1 | tee -a "$LOG2RAM_LOG"
  43. fi
  44. }
  45. wait_for () {
  46. while ! findmnt "$1" > /dev/null; do
  47. sleep 0.1
  48. done
  49. while [ ! -f "$1/log2ram.test" ]; do
  50. touch "$1/log2ram.test"
  51. sleep 0.1
  52. done
  53. rm "$1/log2ram.test"
  54. }
  55. createZramLogDrive () {
  56. # Check Zram Class created
  57. if [ ! -d "/sys/class/zram-control" ]; then
  58. modprobe zram
  59. RAM_DEV='0'
  60. else
  61. RAM_DEV=$(cat /sys/class/zram-control/hot_add)
  62. fi
  63. echo "$COMP_ALG" > "/sys/block/zram${RAM_DEV}/comp_algorithm"
  64. echo "$LOG_DISK_SIZE" > "/sys/block/zram${RAM_DEV}/disksize"
  65. echo "$SIZE" > "/sys/block/zram${RAM_DEV}/mem_limit"
  66. mke2fs -t ext4 "/dev/zram${RAM_DEV}"
  67. }
  68. make_log_dir () {
  69. [ -d "$HDD_LOG" ] || mkdir "$HDD_LOG"
  70. }
  71. case "$1" in
  72. start)
  73. IFS=';'
  74. for i in $PATH_DISK; do
  75. # Skip the path if the folder doesn't exist
  76. [ ! -d "$i" ] && continue
  77. PATH_FIRST_PART="${i%/*}"
  78. PATH_LAST_PART="${i##/*/}"
  79. RAM_LOG="$i"
  80. HDD_LOG="${PATH_FIRST_PART}/hdd.${PATH_LAST_PART}"
  81. LOG2RAM_LOG="${RAM_LOG}/${LOG_NAME}"
  82. make_log_dir
  83. mount --bind "$RAM_LOG"/ "$HDD_LOG"/
  84. mount --make-private "$HDD_LOG"/
  85. wait_for "$HDD_LOG"
  86. if [ "$ZL2R" = true ]; then
  87. createZramLogDrive
  88. mount -t ext4 -o nosuid,noexec,noatime,nodev,user=log2ram "/dev/zram${RAM_DEV}" "$RAM_LOG"/
  89. else
  90. mount -t tmpfs -o "nosuid,noexec,noatime,nodev,mode=0755,size=${SIZE}" log2ram "$RAM_LOG"/
  91. fi
  92. wait_for "$RAM_LOG"
  93. syncFromDisk
  94. done
  95. exit 0
  96. ;;
  97. stop)
  98. IFS=';'
  99. for i in $PATH_DISK; do
  100. PATH_FIRST_PART="${i%/*}"
  101. PATH_LAST_PART="${i##/*/}"
  102. RAM_LOG="$i"
  103. HDD_LOG="${PATH_FIRST_PART}/hdd.${PATH_LAST_PART}"
  104. LOG2RAM_LOG="${RAM_LOG}/${LOG_NAME}"
  105. syncToDisk
  106. #ZRAM_LOG=$(awk '$2 == "/var/log" {print $1}' /proc/mounts)
  107. #ZRAM_LOG=$(echo ${ZRAM_LOG} | grep -o -E '[0-9]+')
  108. umount -l "$RAM_LOG"/
  109. umount -l "$HDD_LOG"/
  110. # Unsure as even with Root permision denied
  111. #echo ${ZRAM_LOG} > /sys/class/zram-control/hot_remove
  112. done
  113. exit 0
  114. ;;
  115. write)
  116. IFS=';'
  117. for i in $PATH_DISK; do
  118. PATH_FIRST_PART="${i%/*}"
  119. PATH_LAST_PART="${i##/*/}"
  120. RAM_LOG="$i"
  121. HDD_LOG="${PATH_FIRST_PART}/hdd.${PATH_LAST_PART}"
  122. LOG2RAM_LOG="${RAM_LOG}/${LOG_NAME}"
  123. syncToDisk
  124. done
  125. exit 0
  126. ;;
  127. *)
  128. echo 'Usage: log2ram {start|stop|write}' >&2
  129. exit 1
  130. ;;
  131. esac