mirror of
https://github.com/azlux/log2ram.git
synced 2023-10-10 13:37:24 +02:00
Compare commits
72 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
1a61e1eea5 | ||
|
a7d00632b0 | ||
|
2e8d06b807 | ||
|
cd96c3c3e8 | ||
|
bf8f16bba6 | ||
|
553ebb0cb3 | ||
|
e16ea0a4dd | ||
|
c21abf6994 | ||
|
2c4ea43ad1 | ||
|
a342af9ed8 | ||
|
fd7cebbd1d | ||
|
fbd8868ae1 | ||
|
5b097683af | ||
|
9a4cbf54c2 | ||
|
7f810f6f25 | ||
|
500e87f68c | ||
|
18226e084f | ||
|
2c3f86134d | ||
|
83b373d925 | ||
|
6ee7907b80 | ||
|
d2da092618 | ||
|
8df9bf7d19 | ||
|
2204345300 | ||
|
fbb75ab5af | ||
|
65a7a84623 | ||
|
1c729d8049 | ||
|
a5d8ce4f9d | ||
|
2977823e71 | ||
|
193c41573c | ||
|
f570ff6e4b | ||
|
e41b7630fc | ||
|
a30bf5664e | ||
|
82554106c9 | ||
|
28dd509071 | ||
|
d83aa8d631 | ||
|
c92e706d2d | ||
|
549288f8d4 | ||
|
00a929d88c | ||
|
39ad7923e4 | ||
|
ef291999ba | ||
|
f3673d1ded | ||
|
a344e45e3c | ||
|
6185780cf7 | ||
|
0fdf8827e5 | ||
|
a5c7f553df | ||
|
1af7db2b28 | ||
|
c399b4039e | ||
|
324902da44 | ||
|
1406472d8a | ||
|
6d5b84ba75 | ||
|
0e09169876 | ||
|
013ab903e5 | ||
|
2264d58597 | ||
|
4db08b8e52 | ||
|
3efc8f35cf | ||
|
31e4409fc3 | ||
|
881eea145f | ||
|
f6432fa708 | ||
|
f6e81bc8c1 | ||
|
853ae88d3f | ||
|
b99c49fb8e | ||
|
e20ee3cfc2 | ||
|
f3d21dc0a7 | ||
|
c5b8abaa1c | ||
|
443a35ef9e | ||
|
fe13834ed2 | ||
|
615a78f00e | ||
|
13d0058086 | ||
|
1b8025aa91 | ||
|
7cf6164364 | ||
|
0dc5827f5c | ||
|
87540578cc |
2
LICENSE
2
LICENSE
@ -1,6 +1,6 @@
|
|||||||
The MIT License (MIT)
|
The MIT License (MIT)
|
||||||
|
|
||||||
Copyright (c) 2016, Azlux <azlux@outlook.com>
|
Copyright (c) 2016-2023, Azlux <github@azlux.fr>
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
176
README.md
176
README.md
@ -1,67 +1,104 @@
|
|||||||
# Log2Ram
|
# Log2Ram
|
||||||
Like ramlog for systemd (on debian 8 jessie for example).
|
Log2Ram works just like ramlog for systemd (on Debian 8 Jessie for example).
|
||||||
|
|
||||||
Usefull for **RaspberryPi** for not writing on the SD card all the time. You need it because your SD card doesn't want to suffer anymore!
|
Useful for **RaspberryPi** for not writing on the SD card all the time. You need it because your SD card doesn't want to suffer anymore!
|
||||||
|
|
||||||
Explanations: The script creates a `/var/log` mount point in RAM. So any writing of the log to the `/var/log` folder will not actually be written to disk (in this case to the sd card for a raspberry card) but directly to RAM. By default, every hour, the CRON will launch a synchronization of the RAM to the folder located on the physical disk. The script will also make this copy of RAM to disk in case of machine shutdown (but cannot do it in case of power failure). This way you avoid excessive writing on the SD card.
|
Explanations: The script creates a `/var/log` mount point in RAM. So any writing of the log to the `/var/log` folder will not actually be written to disk (in this case to the SD card on a Raspberry Pi) but directly to RAM. By default, every day the CRON will synchronize the contents in RAM with the folder located on the physical disk. The script will also make this copy of RAM to disk in case of machine shutdowns (but, of course, it still won't do it in case of power failures). This way you can avoid excessive writing on the SD card and extend its life.
|
||||||
|
|
||||||
The script [log2ram](https://github.com/azlux/log2ram) can work on every linux system. So you can use it with your own daemon manager if you don't have systemd.
|
[Log2Ram](https://github.com/azlux/log2ram)'s script works on every Linux system. If you don't have Systemd, you can still use Log2Ram with your own daemon manager.
|
||||||
|
|
||||||
Log2Ram is based on transient log for Systemd here : [A transient /var/log](https://www.debian-administration.org/article/661/A_transient_/var/log)
|
Log2Ram is based on transient /var/log for Systemd. For more information, check [here](https://www.debian-administration.org/article/661/A_transient_/var/log).
|
||||||
|
|
||||||
_____
|
_____
|
||||||
## Menu
|
## Table of Contents
|
||||||
1. [Install](#install)
|
1. [Installation](#installation)
|
||||||
2. [Upgrade](#upgrade)
|
2. [Is it working?](#is-it-working)
|
||||||
3. [Customize](#customize)
|
3. [Upgrading](#upgrading)
|
||||||
4. [It is working ?](#it-is-working)
|
4. [Customization](#customization)
|
||||||
5. [Uninstall](#uninstall-)
|
5. [Troubleshooting](#troubleshooting)
|
||||||
|
6. [Uninstallation](#uninstallation-)
|
||||||
|
|
||||||
## Install
|
## Installation
|
||||||
|
### Via APT (recommended)
|
||||||
|
|
||||||
curl -Lo log2ram.tar.gz https://github.com/azlux/log2ram/archive/master.tar.gz
|
```bash
|
||||||
tar xf log2ram.tar.gz
|
echo "deb [signed-by=/usr/share/keyrings/azlux-archive-keyring.gpg] http://packages.azlux.fr/debian/ bookworm main" | sudo tee /etc/apt/sources.list.d/azlux.list
|
||||||
|
sudo wget -O /usr/share/keyrings/azlux-archive-keyring.gpg https://azlux.fr/repo.gpg
|
||||||
|
sudo apt update
|
||||||
|
sudo apt install log2ram
|
||||||
|
```
|
||||||
|
### Manually
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl -L https://github.com/azlux/log2ram/archive/master.tar.gz | tar zxf -
|
||||||
cd log2ram-master
|
cd log2ram-master
|
||||||
chmod +x install.sh && sudo ./install.sh
|
chmod +x install.sh && sudo ./install.sh
|
||||||
cd ..
|
cd ..
|
||||||
rm -r log2ram-master
|
rm -r log2ram-master
|
||||||
|
```
|
||||||
|
|
||||||
**REBOOT** before installing anything else (for example apache2)
|
For better performances, `RSYNC` is a recommended package.
|
||||||
## Upgrade
|
|
||||||
|
|
||||||
You need to stop log2ram (`service log2ram stop`) and start the [install](#install).
|
**REBOOT** before installing anything else (for example `apache2`)
|
||||||
|
|
||||||
## Customize
|
## Is it working?
|
||||||
#### variables :
|
After installing and rebooting, use systemctl to check if Log2Ram started successfully:
|
||||||
In the file `/etc/log2ram.conf`, there are three variables:
|
|
||||||
|
```bash
|
||||||
|
systemctl status log2ram
|
||||||
|
```
|
||||||
|
|
||||||
|
This will show a color-coded status (green: active/red: failed), as well as the last few log lines. To show the full log (scrolled to the end), run:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
journalctl -u log2ram -e
|
||||||
|
```
|
||||||
|
|
||||||
|
The log is also written to `/var/log/log2ram.log`.
|
||||||
|
|
||||||
|
You can also inspect the mount folder in RAM with:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# df -h | grep log2ram
|
||||||
|
log2ram 40M 532K 40M 2% /var/log
|
||||||
|
```
|
||||||
|
or also:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# mount | grep log2ram
|
||||||
|
log2ram on /var/log type tmpfs (rw,nosuid,nodev,noexec,relatime,size=40960k,mode=755)
|
||||||
|
```
|
||||||
|
|
||||||
|
If you do not get any line as response of these commands, something is not working. Refer to [this section](#is-it-working).
|
||||||
|
|
||||||
|
## Upgrading
|
||||||
|
|
||||||
|
You need to stop Log2Ram (`systemctl stop log2ram`) and execute the [installation](#installation) process. If you used APT, this will be done automatically.
|
||||||
|
|
||||||
|
## Customization
|
||||||
|
|
||||||
|
#### Variables
|
||||||
|
In the file `/etc/log2ram.conf`, there are five variables:
|
||||||
|
|
||||||
- `SIZE`: defines the size the log folder will reserve into the RAM (default is 40M).
|
- `SIZE`: defines the size the log folder will reserve into the RAM (default is 40M).
|
||||||
- `USE_RSYNC`: Can be set to `true` if you prefer ´rsync´ rather than ´cp´. I use the command `cp -u` and `rsync -X`, I don't copy the all folder every time for optimization.
|
- `USE_RSYNC`: (commented out by default = `true`) use `cp` instead of `rsync` (if set to `false`).
|
||||||
- `MAIL`: Disables the error system mail if there is not enough place on RAM (if set to `false`)
|
- `MAIL`: disables the error system mail if there is not enough place on RAM (if set to `false`).
|
||||||
- `ZL2R`: Enable zram compatibility (`false` by default). Check the comment on the config file. See https://github.com/StuartIanNaylor/zram-swap-config to configure a zram space on your raspberry before enable this option.
|
- `PATH_DISK`: activate log2ram for other path than default one. Paths should be separated with a `;`.
|
||||||
|
- `ZL2R`: enable zram compatibility (`false` by default). Check the comment on the config file. See https://github.com/StuartIanNaylor/zram-swap-config to configure a zram space on your raspberry before enable this option.
|
||||||
|
|
||||||
#### refresh time:
|
#### Refresh time
|
||||||
By default Log2Ram writes to the HardDisk every day. If you think this is too much, you can move `/etc/cron.daily/log2ram` in antoiher cron folder, or remove it if you prefer writing logs only at stop/reboot.
|
|
||||||
|
|
||||||
### It is working?
|
By default, Log2Ram writes to disk every day. If you think this is too much, you can run `systemctl edit log2ram-daily.timer` and for example add:
|
||||||
You can now check the mount folder in ram with (You will see lines with log2ram if working)
|
|
||||||
|
```ini
|
||||||
|
[Timer]
|
||||||
|
OnCalendar=
|
||||||
|
OnCalendar=Mon *-*-* 23:55:00
|
||||||
```
|
```
|
||||||
# df -h
|
... Or even disable it altogether with `systemctl disable log2ram-daily.timer`, if you instead prefer Log2Ram to be writing logs only on system stops/reboots.
|
||||||
…
|
|
||||||
log2ram 40M 532K 40M 2% /var/log
|
|
||||||
…
|
|
||||||
|
|
||||||
# mount
|
#### Compressor
|
||||||
…
|
Compressor for ZRAM. Useful for the `COMP_ALG` of ZRAM on the config file.
|
||||||
log2ram on /var/log type tmpfs (rw,nosuid,nodev,noexec,relatime,size=40960k,mode=755)
|
|
||||||
…
|
|
||||||
```
|
|
||||||
|
|
||||||
If you have issue with apache2, you can try to add `apache2.service` next to other services on the `Before` parameter in `/etc/systemd/system/log2ram.service` it will solve the pb
|
|
||||||
|
|
||||||
The log for log2ram will be written at: `/var/log/log2ram.log`
|
|
||||||
|
|
||||||
Compressor for zram. Usefull for the `COMP_ALG` of ZRAM on the config file.
|
|
||||||
|
|
||||||
| Compressor name | Ratio | Compression | Decompress. |
|
| Compressor name | Ratio | Compression | Decompress. |
|
||||||
|------------------------|----------|-------------|-------------|
|
|------------------------|----------|-------------|-------------|
|
||||||
@ -76,9 +113,60 @@ Compressor for zram. Usefull for the `COMP_ALG` of ZRAM on the config file.
|
|||||||
|
|
||||||
###### Now, muffins for everyone!
|
###### Now, muffins for everyone!
|
||||||
|
|
||||||
|
## Troubleshooting
|
||||||
|
|
||||||
## Uninstall :(
|
### Existing content in `/var/log` too large for RAM
|
||||||
(Because sometime we need it)
|
|
||||||
|
One thing that stops Log2Ram from functioning is if `/var/log` is too large before starting Log2Ram the first time. This can happen if logs had been collected for a long time before installing Log2Ram itself. Find the largest directories in `/var/log` (this example command only shows the 3 largest):
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo du -hs /var/log/* | sort -h | tail -n 3
|
||||||
```
|
```
|
||||||
|
|
||||||
|
If the `/var/log/journal` is very large, then there are a lot of system logs. Deletion of old "archived" logs can be fixed by adjusting a setting. Edit the `/etc/systemd/journald.conf` file and add the following option:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
SystemMaxUse=20M
|
||||||
|
```
|
||||||
|
|
||||||
|
**Or** the more radical version of directly flushing the journal to a size that matches log2ram size imediately _(Be aware that this flish flush the systemd journal logs imediately to the given size!)_
|
||||||
|
|
||||||
|
```bash
|
||||||
|
journalctl --vacuum-size=32M
|
||||||
|
```
|
||||||
|
|
||||||
|
This should be set to a value smaller than the size of the RAM volume; for example, half of it could be fine. Then, apply the new setting:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo systemctl restart systemd-journald
|
||||||
|
```
|
||||||
|
|
||||||
|
This should shrink the size of "archived" logs to be below the newly imposed limit. Reboot and check that Log2Ram now works properly:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo reboot
|
||||||
|
```
|
||||||
|
|
||||||
|
Wait until system reboots...
|
||||||
|
|
||||||
|
```bash
|
||||||
|
systemctl status log2ram
|
||||||
|
```
|
||||||
|
|
||||||
|
## Uninstallation :(
|
||||||
|
|
||||||
|
(Because sometimes we need it)
|
||||||
|
|
||||||
|
### Via APT
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo apt remove log2ram
|
||||||
|
```
|
||||||
|
|
||||||
|
You can add the `--purge` argument to remove Log2Ram config files as well.
|
||||||
|
|
||||||
|
### Manually
|
||||||
|
|
||||||
|
```bash
|
||||||
chmod +x /usr/local/bin/uninstall-log2ram.sh && sudo /usr/local/bin/uninstall-log2ram.sh
|
chmod +x /usr/local/bin/uninstall-log2ram.sh && sudo /usr/local/bin/uninstall-log2ram.sh
|
||||||
```
|
```
|
||||||
|
77
ansible_playbook/install_log2ram.yml
Normal file
77
ansible_playbook/install_log2ram.yml
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
#!/usr/bin/env ansible-playbook
|
||||||
|
---
|
||||||
|
# Configure Raspberry Pi to log to RAM, with occasional SD card sync
|
||||||
|
# to reduce SD card writes
|
||||||
|
# Usage: ansible-playbook -e 'log2ram_size=80M' install_log2ram.yml
|
||||||
|
|
||||||
|
- hosts: all
|
||||||
|
gather_facts: true
|
||||||
|
gather_subset: min
|
||||||
|
become: true
|
||||||
|
|
||||||
|
|
||||||
|
vars:
|
||||||
|
log2ram_repo_url: http://packages.azlux.fr
|
||||||
|
log2ram_gpg: https://azlux.fr/repo.gpg
|
||||||
|
log2ram_keyring: /usr/share/keyrings/azlux-archive-keyring.gpg
|
||||||
|
log2ram_apt_repository: "deb [signed-by={{ log2ram_keyring }}] {{ log2ram_repo_url }}/{{ ansible_distribution | lower }} {{ ansible_distribution_release }} main"
|
||||||
|
|
||||||
|
log2ram_size: 40M
|
||||||
|
log2ram_use_rsync: true
|
||||||
|
log2ram_mail: true
|
||||||
|
log2ram_path_disk: /var/log
|
||||||
|
log2ram_use_z2lr: true
|
||||||
|
log2ram_comp_alg: lz4
|
||||||
|
log2ram_log_disk_size: 100M
|
||||||
|
|
||||||
|
|
||||||
|
tasks:
|
||||||
|
- name: Add rsync as pre-requisite
|
||||||
|
apt:
|
||||||
|
name: rsync
|
||||||
|
when: log2ram_use_rsync
|
||||||
|
|
||||||
|
- name: Add gpg key
|
||||||
|
shell:
|
||||||
|
cmd: >
|
||||||
|
curl -fsSL {{ log2ram_gpg }} | gpg --dearmor --yes -o {{ log2ram_keyring }}
|
||||||
|
creates: "{{ log2ram_keyring }}"
|
||||||
|
|
||||||
|
- name: Add apt repository
|
||||||
|
apt_repository:
|
||||||
|
repo: "{{ log2ram_apt_repository }}"
|
||||||
|
filename: log2ram
|
||||||
|
register: log2ram_apt_repo
|
||||||
|
|
||||||
|
- name: Update apt cache
|
||||||
|
apt:
|
||||||
|
update_cache: true
|
||||||
|
when: log2ram_apt_repo is changed
|
||||||
|
|
||||||
|
- name: Install log2ram
|
||||||
|
apt:
|
||||||
|
name: log2ram
|
||||||
|
notify: Restart log2ram
|
||||||
|
|
||||||
|
- name: Set config options
|
||||||
|
lineinfile:
|
||||||
|
path: /etc/log2ram.conf
|
||||||
|
regexp: "{{ item.regexp }}"
|
||||||
|
line: "{{ item.line }}"
|
||||||
|
backrefs: true
|
||||||
|
loop:
|
||||||
|
- {regexp: '^SIZE=(.*)$', line: 'SIZE={{ log2ram_size }}'}
|
||||||
|
- {regexp: 'USE_RSYNC=(.*)$', line: 'USE_RSYNC={{ log2ram_use_rsync }}'}
|
||||||
|
- {regexp: '^MAIL=(.*)$', line: 'MAIL={{ log2ram_mail }}'}
|
||||||
|
- {regexp: '^PATH_DISK=(.*)$', line: 'PATH_DISK="{{ log2ram_path_disk }}"'}
|
||||||
|
- {regexp: '^ZL2R=(.*)$', line: 'ZL2R={{ log2ram_use_z2lr|lower }}'}
|
||||||
|
- {regexp: '^COMP_ALG=(.*)$', line: 'COMP_ALG={{ log2ram_comp_alg }}'}
|
||||||
|
- {regexp: '^LOG_DISK_SIZE=(.*)$', line: 'LOG_DISK_SIZE={{ log2ram_log_disk_size }}'}
|
||||||
|
notify: Restart log2ram
|
||||||
|
|
||||||
|
|
||||||
|
handlers:
|
||||||
|
- name: Restart log2ram
|
||||||
|
systemd:
|
||||||
|
name: log2ram
|
||||||
|
state: restarted
|
41
build-packages.sh
Executable file
41
build-packages.sh
Executable file
@ -0,0 +1,41 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
# Exit the script if any of the commands fail
|
||||||
|
set -e
|
||||||
|
set -u
|
||||||
|
set -o pipefail
|
||||||
|
|
||||||
|
# Set working directory to the location of this script
|
||||||
|
cd "$(dirname "${BASH_SOURCE[0]}")"
|
||||||
|
|
||||||
|
STARTDIR="$(pwd)"
|
||||||
|
DESTDIR="$STARTDIR/pkg"
|
||||||
|
OUTDIR="$STARTDIR/deb"
|
||||||
|
# get version
|
||||||
|
repo="azlux/log2ram"
|
||||||
|
api=$(curl --silent "https://api.github.com/repos/$repo/releases/latest")
|
||||||
|
new=$(echo $api | grep -Po '"tag_name": "\K.*?(?=")')
|
||||||
|
|
||||||
|
# Remove potential leftovers from a previous build
|
||||||
|
rm -rf "$DESTDIR" "$OUTDIR"
|
||||||
|
|
||||||
|
## log2ram
|
||||||
|
# Create directory
|
||||||
|
install -Dm 644 "$STARTDIR/log2ram.service" "$DESTDIR/etc/systemd/system/log2ram.service"
|
||||||
|
install -Dm 644 "$STARTDIR/log2ram-daily.service" "$DESTDIR/etc/systemd/system/log2ram-daily.service"
|
||||||
|
install -Dm 644 "$STARTDIR/log2ram-daily.timer" "$DESTDIR/etc/systemd/system/log2ram-daily.timer"
|
||||||
|
install -Dm 755 "$STARTDIR/log2ram" "$DESTDIR/usr/local/bin/log2ram"
|
||||||
|
install -Dm 644 "$STARTDIR/log2ram.conf" "$DESTDIR/etc/log2ram.conf"
|
||||||
|
install -Dm 644 "$STARTDIR/uninstall.sh" "$DESTDIR/usr/local/bin/uninstall-log2ram.sh"
|
||||||
|
|
||||||
|
# logrotate
|
||||||
|
install -Dm 644 "$STARTDIR/log2ram.logrotate" "$DESTDIR/etc/logrotate.d/log2ram"
|
||||||
|
|
||||||
|
# Build .deb
|
||||||
|
mkdir "$DESTDIR/DEBIAN" "$OUTDIR"
|
||||||
|
cp "$STARTDIR/debian/"* "$DESTDIR/DEBIAN/"
|
||||||
|
|
||||||
|
# Set version
|
||||||
|
sed -i "s/VERSION-TO-REPLACE/$new/" "$DESTDIR/DEBIAN/control"
|
||||||
|
|
||||||
|
dpkg-deb --build "$DESTDIR" "$OUTDIR"
|
1
debian/conffiles
vendored
Normal file
1
debian/conffiles
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/etc/log2ram.conf
|
11
debian/control
vendored
Executable file
11
debian/control
vendored
Executable file
@ -0,0 +1,11 @@
|
|||||||
|
Package: log2ram
|
||||||
|
Version: VERSION-TO-REPLACE
|
||||||
|
Depends: bash (>= 4.3)
|
||||||
|
Section: net
|
||||||
|
Priority: optional
|
||||||
|
Architecture: all
|
||||||
|
Maintainer: Azlux <github@azlux.fr>
|
||||||
|
Description: ramlog like for systemd (Put log into a ram folder)
|
||||||
|
Homepage: https://github.com/azlux/log2ram
|
||||||
|
Bugs: https://github.com/azlux/log2ram/issues
|
||||||
|
Recommends: rsync
|
16
debian/postinst
vendored
Executable file
16
debian/postinst
vendored
Executable file
@ -0,0 +1,16 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
systemctl daemon-reload
|
||||||
|
systemctl enable log2ram.service log2ram-daily.timer
|
||||||
|
|
||||||
|
|
||||||
|
rm -f /etc/cron.daily/log2ram
|
||||||
|
rm -f /etc/cron.hourly/log2ram
|
||||||
|
|
||||||
|
if [ "$1" == "configure" ]; then
|
||||||
|
echo "##### Reboot to activate log2ram #####"
|
||||||
|
echo "##### edit /etc/log2ram.conf to configure options ####"
|
||||||
|
fi
|
||||||
|
|
||||||
|
exit 0
|
6
debian/preinst
vendored
Executable file
6
debian/preinst
vendored
Executable file
@ -0,0 +1,6 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
systemctl -q is-active log2ram.service && systemctl stop log2ram.service
|
||||||
|
systemctl -q is-active log2ram-daily.timer && systemctl stop log2ram-daily.timer
|
||||||
|
rm -rf /var/hdd.log
|
||||||
|
|
||||||
|
exit 0
|
12
debian/prerm
vendored
Executable file
12
debian/prerm
vendored
Executable file
@ -0,0 +1,12 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
case "$1" in
|
||||||
|
upgrade)
|
||||||
|
[ -d /run/systemd/system/ ] && systemctl stop log2ram.service log2ram-daily.timer
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
[ -d /run/systemd/system/ ] && systemctl stop log2ram.service log2ram-daily.timer
|
||||||
|
[ -d /run/systemd/system/ ] && systemctl disable log2ram.service log2ram-daily.timer
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
esac
|
26
install.sh
26
install.sh
@ -1,19 +1,33 @@
|
|||||||
#!/usr/bin/env sh
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
systemctl -q is-active log2ram && { echo "ERROR: log2ram service is still running. Please run \"sudo service log2ram stop\" to stop it."; exit 1; }
|
systemctl -q is-active log2ram && {
|
||||||
[ "$(id -u)" -eq 0 ] || { echo "You need to be ROOT (sudo can be used)"; exit 1; }
|
echo "ERROR: log2ram service is still running. Please run \"sudo systemctl stop log2ram\" to stop it."
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
[ "$(id -u)" -eq 0 ] || {
|
||||||
|
echo "You need to be ROOT (sudo can be used)"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
# log2ram
|
# log2ram
|
||||||
mkdir -p /usr/local/bin/
|
mkdir -p /usr/local/bin/
|
||||||
install -m 644 log2ram.service /etc/systemd/system/log2ram.service
|
install -m 644 log2ram.service /etc/systemd/system/log2ram.service
|
||||||
|
install -m 644 log2ram-daily.service /etc/systemd/system/log2ram-daily.service
|
||||||
|
install -m 644 log2ram-daily.timer /etc/systemd/system/log2ram-daily.timer
|
||||||
install -m 755 log2ram /usr/local/bin/log2ram
|
install -m 755 log2ram /usr/local/bin/log2ram
|
||||||
|
if [ ! -f /etc/log2ram.conf ]; then
|
||||||
install -m 644 log2ram.conf /etc/log2ram.conf
|
install -m 644 log2ram.conf /etc/log2ram.conf
|
||||||
|
fi
|
||||||
install -m 644 uninstall.sh /usr/local/bin/uninstall-log2ram.sh
|
install -m 644 uninstall.sh /usr/local/bin/uninstall-log2ram.sh
|
||||||
systemctl enable log2ram
|
systemctl enable log2ram.service log2ram-daily.timer
|
||||||
|
|
||||||
# cron
|
# logrotate
|
||||||
install -m 755 log2ram.cron /etc/cron.daily/log2ram
|
if [ -d /etc/logrotate.d ]; then
|
||||||
install -m 644 log2ram.logrotate /etc/logrotate.d/log2ram
|
install -m 644 log2ram.logrotate /etc/logrotate.d/log2ram
|
||||||
|
else
|
||||||
|
echo "##### Directory /etc/logrotate.d does not exist. #####"
|
||||||
|
echo "##### Skipping log2ram.logrotate installation. #####"
|
||||||
|
fi
|
||||||
|
|
||||||
# Remove a previous log2ram version
|
# Remove a previous log2ram version
|
||||||
rm -rf /var/log.hdd
|
rm -rf /var/log.hdd
|
||||||
|
171
log2ram
171
log2ram
@ -1,56 +1,104 @@
|
|||||||
#!/usr/bin/env sh
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
. /etc/log2ram.conf
|
. /etc/log2ram.conf
|
||||||
|
|
||||||
HDD_LOG=/var/hdd.log
|
if [ -z "$PATH_DISK" ]; then
|
||||||
RAM_LOG=/var/log
|
PATH_DISK='/var/log'
|
||||||
|
fi
|
||||||
|
|
||||||
LOG_NAME="log2ram.log"
|
LOG_NAME='log2ram.log'
|
||||||
LOG2RAM_LOG="${RAM_LOG}/${LOG_NAME}"
|
NO_RSYNC=${USE_RSYNC#true}
|
||||||
LOG_OUTPUT="tee -a $LOG2RAM_LOG"
|
|
||||||
|
|
||||||
isSafe () {
|
## @fn is_safe()
|
||||||
[ -d $HDD_LOG/ ] || echo "ERROR: $HDD_LOG/ doesn't exist! Can't sync."
|
## @brief Check if hdd log exists
|
||||||
[ -d $HDD_LOG/ ] || exit 1
|
is_safe() {
|
||||||
|
[ -d "$HDD_LOG" ] || echo "ERROR: $HDD_LOG/ doesn't exist! Can't sync."
|
||||||
|
[ -d "$HDD_LOG" ] || exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
syncToDisk () {
|
## @fn journald_logrotate()
|
||||||
isSafe
|
## @brief Logrotate the journal if the current RAM_LOG path is part of the journald directory
|
||||||
|
journald_logrotate() {
|
||||||
|
if ! [ -x "$(command -v journalctl)" ] || ! [ "$JOURNALD_AWARE" = true ]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
if [ "$USE_RSYNC" = true ]; then
|
if journalctl --header | grep "File path" | grep "$RAM_LOG" >/dev/null 2>&1; then
|
||||||
rsync -aXWv --fuzzy --no-whole-file --append --delete-after --links $RAM_LOG/ $HDD_LOG/ 2>&1 | $LOG_OUTPUT
|
journalctl --rotate
|
||||||
|
return 0
|
||||||
else
|
else
|
||||||
cp -rfup $RAM_LOG/ -T $HDD_LOG/ 2>&1 | $LOG_OUTPUT
|
return 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
syncFromDisk () {
|
## @fn sync_to_disk()
|
||||||
isSafe
|
## @brief Sync memory back to hard disk
|
||||||
|
sync_to_disk() {
|
||||||
|
is_safe
|
||||||
|
|
||||||
if [ ! -z "$(du -sh -t "$SIZE" $HDD_LOG/ | cut -f1)" ]; then
|
optional_params=()
|
||||||
echo "ERROR: RAM disk too small. Can't sync."
|
|
||||||
umount -l $RAM_LOG/
|
if journald_logrotate; then
|
||||||
umount -l $HDD_LOG/
|
optional_params+=("--include=journal/*/*@*.journal")
|
||||||
|
optional_params+=("--exclude=journal/*/*")
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -z "${NO_RSYNC}" ] && [ -x "$(command -v rsync)" ]; then
|
||||||
|
rsync -aXv --inplace --no-whole-file --delete-after "${optional_params[@]}" "$RAM_LOG"/ "$HDD_LOG"/ 2>&1 |
|
||||||
|
tee -a "$LOG2RAM_LOG"
|
||||||
|
else
|
||||||
|
cp -rfup "$RAM_LOG"/ -T "$HDD_LOG"/ 2>&1 | tee -a "$LOG2RAM_LOG"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
## @fn sync_from_disk()
|
||||||
|
## @brief Sync hard disk to memory
|
||||||
|
sync_from_disk() {
|
||||||
|
is_safe
|
||||||
|
|
||||||
|
TP_SIZE=$SIZE
|
||||||
|
if [ "$ZL2R" = true ]; then
|
||||||
|
TP_SIZE=$LOG_DISK_SIZE
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "$(du -sh -t "$TP_SIZE" "$HDD_LOG"/ | cut -f1)" ]; then
|
||||||
|
echo "ERROR: RAM disk for \"$HDD_LOG/\" too small. Can't sync."
|
||||||
|
echo -e "File(s) causing issues\n: $(du -sh -t "$TP_SIZE" "$HDD_LOG"/*)"
|
||||||
|
umount -l "$RAM_LOG"/
|
||||||
|
umount -l "$HDD_LOG"/
|
||||||
if [ "$MAIL" = true ]; then
|
if [ "$MAIL" = true ]; then
|
||||||
echo "LOG2RAM : No place on RAM anymore, fallback on the disk" | mail -s 'Log2Ram Error' root;
|
echo "LOG2RAM : No place on RAM for \"$HDD_LOG/\" anymore, fallback on the disk" | mail -s 'Log2Ram Error' root
|
||||||
fi
|
fi
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "$USE_RSYNC" = true ]; then
|
if [ -z "${NO_RSYNC}" ] && [ -x "$(command -v rsync)" ]; then
|
||||||
rsync -aXWv --fuzzy --no-whole-file --append --delete-after --links $HDD_LOG/ $RAM_LOG/ 2>&1 | $LOG_OUTPUT
|
rsync -aXv --inplace --no-whole-file --delete-after "$HDD_LOG"/ "$RAM_LOG"/ 2>&1 | tee -a "$LOG2RAM_LOG"
|
||||||
else
|
else
|
||||||
cp -rfup $HDD_LOG/ -T $RAM_LOG/ 2>&1 | $LOG_OUTPUT
|
cp -rfup "$HDD_LOG"/ -T "$RAM_LOG"/ 2>&1 | tee -a "$LOG2RAM_LOG"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
## @fn wait_for()
|
||||||
|
## @brief Wait for directory and create test file to make sure the directory exists
|
||||||
|
## @param param1 path to the directory
|
||||||
wait_for() {
|
wait_for() {
|
||||||
while ! grep -qs "$1" /proc/mounts; do
|
WAIT_PATH="$1"
|
||||||
|
while ! findmnt "$WAIT_PATH" >/dev/null; do
|
||||||
sleep 0.1
|
sleep 0.1
|
||||||
done
|
done
|
||||||
|
|
||||||
|
while [ ! -f "$WAIT_PATH/log2ram.test" ]; do
|
||||||
|
touch "$WAIT_PATH/log2ram.test"
|
||||||
|
sleep 0.1
|
||||||
|
done
|
||||||
|
rm "$WAIT_PATH/log2ram.test"
|
||||||
}
|
}
|
||||||
|
|
||||||
createZramLogDrive () {
|
## @fn create_zram_log_drive()
|
||||||
|
## @brief Create zram log device
|
||||||
|
create_zram_log_drive() {
|
||||||
# Check Zram Class created
|
# Check Zram Class created
|
||||||
if [ ! -d "/sys/class/zram-control" ]; then
|
if [ ! -d "/sys/class/zram-control" ]; then
|
||||||
modprobe zram
|
modprobe zram
|
||||||
@ -58,44 +106,79 @@ createZramLogDrive () {
|
|||||||
else
|
else
|
||||||
RAM_DEV=$(cat /sys/class/zram-control/hot_add)
|
RAM_DEV=$(cat /sys/class/zram-control/hot_add)
|
||||||
fi
|
fi
|
||||||
echo ${COMP_ALG} > /sys/block/zram${RAM_DEV}/comp_algorithm
|
echo "$COMP_ALG" >"/sys/block/zram${RAM_DEV}/comp_algorithm"
|
||||||
echo ${LOG_DISK_SIZE} > /sys/block/zram${RAM_DEV}/disksize
|
echo "$LOG_DISK_SIZE" >"/sys/block/zram${RAM_DEV}/disksize"
|
||||||
echo ${SIZE} > /sys/block/zram${RAM_DEV}/mem_limit
|
echo "$SIZE" >"/sys/block/zram${RAM_DEV}/mem_limit"
|
||||||
mke2fs -t ext4 /dev/zram${RAM_DEV}
|
mke2fs -t ext4 "/dev/zram${RAM_DEV}"
|
||||||
}
|
}
|
||||||
|
|
||||||
case "$1" in
|
case "$1" in
|
||||||
start)
|
start)
|
||||||
[ -d $HDD_LOG/ ] || mkdir $HDD_LOG/
|
IFS=';'
|
||||||
mount --bind $RAM_LOG/ $HDD_LOG/
|
for i in $PATH_DISK; do
|
||||||
mount --make-private $HDD_LOG/
|
# Skip the path if the folder doesn't exist
|
||||||
wait_for $HDD_LOG
|
[ ! -d "$i" ] && continue
|
||||||
|
|
||||||
|
PATH_FIRST_PART="${i%/*}"
|
||||||
|
PATH_LAST_PART="${i##/*/}"
|
||||||
|
RAM_LOG="$i"
|
||||||
|
HDD_LOG="${PATH_FIRST_PART}/hdd.${PATH_LAST_PART}"
|
||||||
|
LOG2RAM_LOG="${RAM_LOG}/${LOG_NAME}"
|
||||||
|
|
||||||
|
[ -d "$HDD_LOG" ] || mkdir "$HDD_LOG"
|
||||||
|
|
||||||
|
mount --bind "$RAM_LOG"/ "$HDD_LOG"/
|
||||||
|
mount --make-private "$HDD_LOG"/
|
||||||
|
wait_for "$HDD_LOG"
|
||||||
|
|
||||||
if [ "$ZL2R" = true ]; then
|
if [ "$ZL2R" = true ]; then
|
||||||
createZramLogDrive
|
create_zram_log_drive
|
||||||
mount -t ext4 -o nosuid,noexec,nodev,user=log2ram /dev/zram${RAM_DEV} ${RAM_LOG}/
|
mount -t ext4 -o nosuid,noexec,noatime,nodev,user=log2ram "/dev/zram${RAM_DEV}" "$RAM_LOG"/
|
||||||
else
|
else
|
||||||
mount -t tmpfs -o nosuid,noexec,nodev,mode=0755,size=${SIZE} log2ram $RAM_LOG/
|
mount -t tmpfs -o "nosuid,noexec,noatime,nodev,mode=0755,size=${SIZE}" log2ram "$RAM_LOG"/
|
||||||
fi
|
fi
|
||||||
wait_for $RAM_LOG
|
wait_for "$RAM_LOG"
|
||||||
syncFromDisk
|
sync_from_disk
|
||||||
|
done
|
||||||
|
exit 0
|
||||||
;;
|
;;
|
||||||
|
|
||||||
stop)
|
stop)
|
||||||
syncToDisk
|
IFS=';'
|
||||||
|
for i in $PATH_DISK; do
|
||||||
|
PATH_FIRST_PART="${i%/*}"
|
||||||
|
PATH_LAST_PART="${i##/*/}"
|
||||||
|
RAM_LOG="$i"
|
||||||
|
HDD_LOG="${PATH_FIRST_PART}/hdd.${PATH_LAST_PART}"
|
||||||
|
LOG2RAM_LOG="${RAM_LOG}/${LOG_NAME}"
|
||||||
|
|
||||||
|
sync_to_disk
|
||||||
#ZRAM_LOG=$(awk '$2 == "/var/log" {print $1}' /proc/mounts)
|
#ZRAM_LOG=$(awk '$2 == "/var/log" {print $1}' /proc/mounts)
|
||||||
#ZRAM_LOG=$(echo ${ZRAM_LOG} | grep -o -E '[0-9]+')
|
#ZRAM_LOG=$(echo ${ZRAM_LOG} | grep -o -E '[0-9]+')
|
||||||
umount -l $RAM_LOG/
|
umount -l "$RAM_LOG"/
|
||||||
umount -l $HDD_LOG/
|
umount -l "$HDD_LOG"/
|
||||||
# Unsure as even with Root permision denied
|
# Unsure as even with Root permision denied
|
||||||
#echo ${ZRAM_LOG} > /sys/class/zram-control/hot_remove
|
#echo ${ZRAM_LOG} > /sys/class/zram-control/hot_remove
|
||||||
|
done
|
||||||
|
exit 0
|
||||||
;;
|
;;
|
||||||
|
|
||||||
write)
|
write)
|
||||||
syncToDisk
|
IFS=';'
|
||||||
|
for i in $PATH_DISK; do
|
||||||
|
PATH_FIRST_PART="${i%/*}"
|
||||||
|
PATH_LAST_PART="${i##/*/}"
|
||||||
|
RAM_LOG="$i"
|
||||||
|
HDD_LOG="${PATH_FIRST_PART}/hdd.${PATH_LAST_PART}"
|
||||||
|
LOG2RAM_LOG="${RAM_LOG}/${LOG_NAME}"
|
||||||
|
|
||||||
|
sync_to_disk
|
||||||
|
done
|
||||||
|
exit 0
|
||||||
;;
|
;;
|
||||||
|
|
||||||
*)
|
*)
|
||||||
echo "Usage: log2ram {start|stop|write}" >&2
|
echo 'Usage: log2ram {start|stop|write}' >&2
|
||||||
exit 1
|
exit 1
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
6
log2ram-daily.service
Normal file
6
log2ram-daily.service
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Daily Log2Ram writing activities
|
||||||
|
After=log2ram.service
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
ExecStart=/bin/systemctl reload log2ram.service
|
9
log2ram-daily.timer
Normal file
9
log2ram-daily.timer
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Daily Log2Ram writing activities
|
||||||
|
|
||||||
|
[Timer]
|
||||||
|
OnCalendar=*-*-* 23:55:00
|
||||||
|
Persistent=true
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=timers.target
|
25
log2ram.conf
25
log2ram.conf
@ -5,17 +5,29 @@
|
|||||||
# If it's not enough, log2ram will not be able to use ram. Check you /var/log size folder.
|
# If it's not enough, log2ram will not be able to use ram. Check you /var/log size folder.
|
||||||
# The default is 40M and is basically enough for a lot of applications.
|
# The default is 40M and is basically enough for a lot of applications.
|
||||||
# You will need to increase it if you have a server and a lot of log for example.
|
# You will need to increase it if you have a server and a lot of log for example.
|
||||||
SIZE=40M
|
SIZE=128M
|
||||||
|
|
||||||
# This variable can be set to true if you prefer "rsync" rather than "cp".
|
# Select the log syncing method between the log directory on disk and in the RAM.
|
||||||
# I use the command cp -u and rsync -X, so I don't copy the all folder every time for optimization.
|
# The variable may be uncommented out, setting it to false, if "cp" is preferred over "rsync".
|
||||||
# You can choose which one you want. Be sure rsync is installed if you use it.
|
# Currently, this option needs to be unset or set to true for "rsync" to run,
|
||||||
USE_RSYNC=false
|
# with "cp" available for fallback when "rsync" is missing.
|
||||||
|
# In all other cases, setting USE_RSYNC to anything other than true will default to "cp".
|
||||||
|
#USE_RSYNC=false
|
||||||
|
|
||||||
# If there are some errors with available RAM space, a system mail will be send
|
# If there are some errors with available RAM space, a system mail will be send
|
||||||
# Change it to false and you will have only a log if there is no place on RAM anymore.
|
# Change it to false and you will have only a log if there is no place on RAM anymore.
|
||||||
MAIL=true
|
MAIL=true
|
||||||
|
|
||||||
|
# Variable for folders to put in RAM. You need to specify the real folder `/path/folder` , the `/path/hdd.folder` will
|
||||||
|
# be automatically created. Multiple path can be separeted by `;`. Do not add the final `/` !
|
||||||
|
# example : PATH_DISK="/var/log;/home/test/FolderInRam"
|
||||||
|
PATH_DISK="/var/log"
|
||||||
|
|
||||||
|
# Should log2ram consider journald and do a log rotate before copying the log files back? Please note that for this
|
||||||
|
# rsync is mandatory. Also make sure that you have configured SystemMaxUse in journald.conf, because the size of this
|
||||||
|
# ram folder is will probably not be as large as journald will use by default
|
||||||
|
JOURNALD_AWARE=true
|
||||||
|
|
||||||
# **************** Zram backing conf *************************************************
|
# **************** Zram backing conf *************************************************
|
||||||
|
|
||||||
# ZL2R Zram Log 2 Ram enables a zram drive when ZL2R=true ZL2R=false is mem only tmpfs
|
# ZL2R Zram Log 2 Ram enables a zram drive when ZL2R=true ZL2R=false is mem only tmpfs
|
||||||
@ -29,5 +41,4 @@ COMP_ALG=lz4
|
|||||||
# LOG_DISK_SIZE is expected compression ratio of alg chosen multiplied by log SIZE
|
# LOG_DISK_SIZE is expected compression ratio of alg chosen multiplied by log SIZE
|
||||||
# lzo/lz4=2.1:1 compression ratio zlib=2.7:1 zstandard=2.9:1
|
# lzo/lz4=2.1:1 compression ratio zlib=2.7:1 zstandard=2.9:1
|
||||||
# Really a guestimate of a bit bigger than compression ratio whilst minimising 0.1% mem usage of disk size
|
# Really a guestimate of a bit bigger than compression ratio whilst minimising 0.1% mem usage of disk size
|
||||||
LOG_DISK_SIZE=100M
|
LOG_DISK_SIZE=256M
|
||||||
|
|
||||||
|
@ -1,3 +0,0 @@
|
|||||||
#!/usr/bin/env sh
|
|
||||||
|
|
||||||
systemctl reload log2ram
|
|
@ -1,7 +1,7 @@
|
|||||||
[Unit]
|
[Unit]
|
||||||
Description=Log2Ram
|
Description=Log2Ram
|
||||||
DefaultDependencies=no
|
DefaultDependencies=no
|
||||||
Before=basic.target rsyslog.service syslog.target systemd-journald.service sysinit.target shutdown.target zram-swap-conf.service apache2.service
|
Before=basic.target rsyslog.service syslog-ng.service syslog.target systemd-journald.service sysinit.target shutdown.target zram-swap-conf.service apache2.service lighttpd.service
|
||||||
After=local-fs.target
|
After=local-fs.target
|
||||||
Conflicts=shutdown.target reboot.target halt.target
|
Conflicts=shutdown.target reboot.target halt.target
|
||||||
RequiresMountsFor=/var/log /var/hdd.log
|
RequiresMountsFor=/var/log /var/hdd.log
|
||||||
|
20
uninstall.sh
20
uninstall.sh
@ -1,14 +1,18 @@
|
|||||||
#!/usr/bin/env sh
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
if [ "$(id -u)" -eq 0 ]
|
if dpkg -l log2ram 2>/dev/null; then
|
||||||
then
|
echo "Please run : apt remove log2ram"
|
||||||
service log2ram stop
|
exit 1
|
||||||
systemctl disable log2ram
|
fi
|
||||||
rm /etc/systemd/system/log2ram.service
|
|
||||||
|
if [ "$(id -u)" -eq 0 ]; then
|
||||||
|
echo "Not apt installed. Remove will continue with this script..."
|
||||||
|
systemctl stop log2ram.service log2ram-daily.timer
|
||||||
|
systemctl disable log2ram.service log2ram-daily.timer
|
||||||
|
rm -rf /etc/systemd/system/log2ram*
|
||||||
rm /usr/local/bin/log2ram
|
rm /usr/local/bin/log2ram
|
||||||
rm /etc/log2ram.conf
|
rm /etc/log2ram.conf
|
||||||
rm /etc/cron.daily/log2ram
|
rm -f /etc/logrotate.d/log2ram
|
||||||
rm /etc/logrotate.d/log2ram
|
|
||||||
|
|
||||||
if [ -d /var/hdd.log ]; then
|
if [ -d /var/hdd.log ]; then
|
||||||
rm -r /var/hdd.log
|
rm -r /var/hdd.log
|
||||||
|
Loading…
Reference in New Issue
Block a user