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.

131 lines
3.6 KiB

  1. #!/usr/bin/env sh
  2. . /etc/log2ram.conf
  3. LOG_NAME="log2ram.log"
  4. isSafe () {
  5. [ -d $HDD_LOG/ ] || echo "ERROR: $HDD_LOG/ doesn't exist! Can't sync."
  6. [ -d $HDD_LOG/ ] || exit 1
  7. }
  8. syncToDisk () {
  9. isSafe
  10. if [ "$USE_RSYNC" = true ]; then
  11. rsync -aXv --inplace --no-whole-file --delete-after $RAM_LOG/ $HDD_LOG/ 2>&1 | tee -a $LOG2RAM_LOG
  12. else
  13. cp -rfup --preserve=context $RAM_LOG/ -T $HDD_LOG/ 2>&1 | tee -a $LOG2RAM_LOG
  14. fi
  15. }
  16. syncFromDisk () {
  17. isSafe
  18. TP_SIZE=$SIZE
  19. if [ "$ZL2R" = true ]; then
  20. TP_SIZE=$LOG_DISK_SIZE
  21. then
  22. if [ ! -z "$(du -sh -t "$TP_SIZE" $HDD_LOG/ | cut -f1)" ]; then
  23. echo "ERROR: RAM disk for "$HDD_LOG/" 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 for "$HDD_LOG/" 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 -aXv --inplace --no-whole-file --delete-after $HDD_LOG/ $RAM_LOG/ 2>&1 | tee -a $LOG2RAM_LOG
  33. else
  34. cp -rfup --preserve=context $HDD_LOG/ -T $RAM_LOG/ 2>&1 | tee -a $LOG2RAM_LOG
  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. IFS=';'
  58. for i in $PATH_DISK; do
  59. PATH_FIRST_PART=$( echo ${i%/*} )
  60. PATH_LAST_PART=$( echo ${i##/*/} )
  61. RAM_LOG=$i
  62. HDD_LOG=$PATH_FIRST_PART/hdd.$PATH_LAST_PART
  63. LOG2RAM_LOG="${RAM_LOG}/${LOG_NAME}"
  64. [ -d $HDD_LOG/ ] || mkdir $HDD_LOG/
  65. mount --bind $RAM_LOG/ $HDD_LOG/
  66. mount --make-private $HDD_LOG/
  67. wait_for $HDD_LOG
  68. if [ "$ZL2R" = true ]; then
  69. createZramLogDrive
  70. mount -t ext4 -o nosuid,noexec,nodev,user=log2ram /dev/zram${RAM_DEV} ${RAM_LOG}/
  71. else
  72. mount -t tmpfs -o nosuid,noexec,nodev,mode=0755,size=${SIZE} log2ram $RAM_LOG/
  73. fi
  74. wait_for $RAM_LOG
  75. syncFromDisk
  76. done
  77. ;;
  78. stop)
  79. IFS=';'
  80. for i in $PATH_DISK; do
  81. PATH_FIRST_PART=$( echo ${i%/*} )
  82. PATH_LAST_PART=$( echo ${i##/*/} )
  83. RAM_LOG=$i
  84. HDD_LOG=$PATH_FIRST_PART/hdd.$PATH_LAST_PART
  85. LOG2RAM_LOG="${RAM_LOG}/${LOG_NAME}"
  86. syncToDisk
  87. #ZRAM_LOG=$(awk '$2 == "/var/log" {print $1}' /proc/mounts)
  88. #ZRAM_LOG=$(echo ${ZRAM_LOG} | grep -o -E '[0-9]+')
  89. umount -l $RAM_LOG/
  90. umount -l $HDD_LOG/
  91. # Unsure as even with Root permision denied
  92. #echo ${ZRAM_LOG} > /sys/class/zram-control/hot_remove
  93. done
  94. ;;
  95. write)
  96. IFS=';'
  97. for i in $PATH_DISK; do
  98. PATH_FIRST_PART=$( echo ${i%/*} )
  99. PATH_LAST_PART=$( echo ${i##/*/} )
  100. RAM_LOG=$i
  101. HDD_LOG=$PATH_FIRST_PART/hdd.$PATH_LAST_PART
  102. LOG2RAM_LOG="${RAM_LOG}/${LOG_NAME}"
  103. syncToDisk
  104. done
  105. ;;
  106. *)
  107. echo "Usage: log2ram {start|stop|write}" >&2
  108. exit 1
  109. ;;
  110. esac