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.

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