1
0
mirror of https://github.com/azlux/log2ram.git synced 2023-10-10 13:37:24 +02:00

Compare commits

...

72 Commits
v1.3 ... master

Author SHA1 Message Date
azlux
1a61e1eea5
bookworm repo 2023-08-21 15:13:19 +02:00
azlux
a7d00632b0
Update LICENSE with good email 2023-06-27 14:53:18 +02:00
azlux
2e8d06b807
Added more output on start failure
Merge pull request #203 from a1466d44-d3dc-4c0b-90c7-315b088731d7/master
2023-02-07 11:02:14 +01:00
97b69f63-601b-4697-90db-7cdf2e2a0ea5
cd96c3c3e8 Added more helpfull output to the huge file failue in the syncFromDisk function, and an example command to flush the systemd journal 2023-01-31 22:56:10 +01:00
HyP3r
bf8f16bba6
Added awareness of systemd-journald (#200)
* Reformatted and refactored some files

* Added awareness of systemd-journald
- Added dependency for bash in debian control file, because this implementation uses bash arrays
- Changed in all shell scripts the interpreter to bash
- Replaced "service log2ram stop" with "systemctl stop log2ram"
- Replaced "-a" with "] && ["
- Increased SIZE and LOG_DISK_SIZE to 128M and 256M because journald can use a lot of space
- Introduced a new switch JOURNALD_AWARE
2022-12-13 15:11:13 +01:00
azlux
553ebb0cb3
Reformatted and refactored some files
Merge pull request #199 from HyP3r-/refactor
2022-12-06 14:23:52 +01:00
Andreas Fendt
e16ea0a4dd Reformatted and refactored some files 2022-12-01 13:58:54 +01:00
Ben Lachman
c21abf6994
Check to see if conf file already exists before installing it. (#191)
* Update install.sh

* Add existing conf path.
2022-09-11 23:41:35 +02:00
Andrea Pitto
2c4ea43ad1
Typos and rewording (#193) 2022-09-11 23:40:50 +02:00
Azlux
a342af9ed8 Fix ansible variable name 2022-03-04 15:34:41 +01:00
Azlux
fd7cebbd1d check if folder exist
Fix for #147
2022-03-04 13:54:33 +01:00
Guilherme Lima
fbd8868ae1
Update ansible-playbook (#183)
- Adds support for missing options in the /etc/log2ram.conf (USE_RSYNC and MAIL)
- apt-key command has been deprecated, as mentioned by issue #179. Changing it to use a keyring in /usr/share/keyrings
2022-02-24 02:14:41 +01:00
azlux
5b097683af
Merge pull request #182 from twojstaryzdomu/use_rsync
Restore RSYNC for backwards compatibility
2022-02-07 06:49:15 +01:00
twojstaryzdomu
9a4cbf54c2 Removed parameter history of USE_RSYNC from log2ram.conf 2022-02-06 19:16:45 +01:00
twojstaryzdomu
7f810f6f25 Restore USE_RSYNC variable for backwards compatibility 2022-02-03 12:13:37 +01:00
azlux
500e87f68c
Small typo change
fix #181
2022-01-27 17:04:11 +01:00
Azlux
18226e084f Rollback tests 2022-01-05 22:33:53 +01:00
Azlux
2c3f86134d Fix flush log, rare race condition
No obvious, should fix the issue
2022-01-05 22:25:07 +01:00
azlux
83b373d925
Fix #179 2022-01-03 20:01:35 +01:00
azlux
6ee7907b80
Modify zram service 2021-12-09 12:34:19 +01:00
azlux
d2da092618
added an empty OnCalendar= directive and move timer to 5 min
Merge pull request #166 from fleopaulD/master
2021-09-02 12:21:18 +02:00
azlux
8df9bf7d19
Merge pull request #168 from TupaNegreiros/patch-1
Update README.md
2021-08-30 22:38:43 +02:00
azlux
2204345300
Change buster to bullseye 2021-08-30 01:17:03 +02:00
Tupã Negreiros
fbb75ab5af
Update README.md
Restart journald command
2021-08-18 19:09:24 -03:00
azlux
65a7a84623
root privileges required for apt
Merge pull request #164 from pb66/patch-1
2021-08-03 17:26:36 +02:00
fleopaulD
1c729d8049 added an empty OnCalendar= directive and move timer to 5 min 2021-07-31 00:13:34 +02:00
Paul
a5d8ce4f9d
root privileges required for apt
Need to use `sudo apt` if not root user. proceeding 2 lines already incorporate `sudo` commands so for consistency these lines should be `sudo` too.
2021-07-26 19:00:43 +01:00
azlux
2977823e71
Merge pull request #162 from rgm3/master
Update ansible playbook
2021-07-06 17:21:49 +02:00
rgm
193c41573c Update ansible playbook
Use new trusted.gpg.d keyring for apt key, improve idempotency,
only update cache when necessary, only restart service when necessary.
2021-07-05 08:12:23 -05:00
Rasmus Bondesson
f570ff6e4b
README: Small fixed and addition of troubleshooting tips (#160)
- Move "Is it working?" section to after "Install"
- Adjust "Is it working?" a bit, mention "systemctl status"
- Wording
- Add Troubleshooting section with journal cleanup tips
2021-06-23 16:17:05 +02:00
azlux
e41b7630fc
ansible playbook to install log2ram
Merge pull request #156 from smu/master
2021-06-23 16:15:47 +02:00
Stefan Muthers
a30bf5664e ansible playbook to install log2ram 2021-05-08 20:57:14 +02:00
Christopher Nethercott
82554106c9
Update README.md (#149) 2021-02-21 22:31:36 +01:00
Azlux
28dd509071 Better readme
fix #135
2020-10-29 10:05:37 +01:00
Azlux
d83aa8d631 rm old cron files
preparing new version
2020-10-01 00:03:56 +02:00
azlux
c92e706d2d move timer to 5 min
daily hit in the same time as logrotate.
2020-09-30 10:01:58 +02:00
MegaV0lt
549288f8d4
Support path with spaces (#130)
* Add quotes and support path with spaces
* Using findmnt instead of mount | grep
findmnt is part of util-linux and should be available on almost all systems
2020-09-30 09:52:39 +02:00
azlux
00a929d88c fix part of #121 2020-09-09 13:27:16 +02:00
azlux
39ad7923e4
Merge pull request #120 from casperklein/patch-1
Typo fixed
2020-07-27 13:47:59 +02:00
azlux
ef291999ba
[FEATURE] Replaces CRON usages by a proper systemd timer
Merge pull request #117 from HorlogeSkynet/feature/systemd_timer
2020-07-27 13:47:17 +02:00
Casper
f3673d1ded
Typo fixed 2020-07-27 13:16:07 +02:00
azlux
a344e45e3c
Adding apt uninstall method 2020-07-23 01:33:54 +02:00
Samuel FORESTIER
6185780cf7 Hide error if not installed as a package or if dpkg is not available 2020-07-22 20:42:40 +00:00
Samuel FORESTIER
0fdf8827e5 Silence errors when logrotate is not installed
Co-authored-by: emwe1 <emwe1@users.noreply.github.com>
2020-07-22 20:40:02 +00:00
Samuel FORESTIER
a5c7f553df
Merge branch 'master' into feature/systemd_timer 2020-07-22 20:26:52 +00:00
Azlux
1af7db2b28 rsync by default #115 + fix #119 2020-07-22 22:07:02 +02:00
Samuel FORESTIER
c399b4039e Replaces CRON usages by a proper systemd timer
> Closes #84
> Closes #77
2020-07-02 12:04:14 +02:00
Azlux
324902da44 rollback #111 2020-05-22 10:45:30 +02:00
Azlux
1406472d8a Small check in case config not good 2020-05-21 20:37:27 +02:00
Azlux
6d5b84ba75 fix indent 2020-05-20 02:04:34 +02:00
azlux
0e09169876
work even on a readonly filesystem
Merge pull request #111 from organic-ip/master
2020-05-20 02:01:45 +02:00
organic-ip
013ab903e5 indentation fix 2020-05-19 23:27:13 +02:00
organic-ip
2264d58597 add lighttpd.service in list 2020-05-14 23:23:26 +02:00
organic-ip
4db08b8e52 work with mount point read-only mode 2020-05-14 23:20:20 +02:00
azlux
3efc8f35cf
preserve context make issue on many OS
fix #108
2020-05-09 12:17:02 +02:00
Azlux
31e4409fc3 typo fix #107 2020-05-07 13:46:01 +02:00
Azlux
881eea145f add size check for zram
fix #99
2020-05-07 11:52:44 +02:00
Azlux
f6432fa708 Fix for #57 2020-05-04 22:11:17 +02:00
Azlux
f6e81bc8c1 Additionnal path
feature for #96
2020-05-03 21:51:44 +02:00
Azlux
853ae88d3f rollback #83 2020-02-05 14:47:31 +01:00
Azlux
b99c49fb8e better build 2020-02-05 13:56:21 +01:00
Azlux
e20ee3cfc2 fix typo 2020-02-05 13:45:10 +01:00
azlux
f3d21dc0a7
Merge pull request #85 from zkemble/master
Make log2ram shutdown after journald has shutdown
2020-01-02 17:02:18 +01:00
Azlux
c5b8abaa1c changelog added 2019-12-20 16:24:11 +01:00
Azlux
443a35ef9e version consistancy 2019-12-20 13:23:25 +01:00
Azlux
fe13834ed2 execution right for build 2019-12-19 17:58:37 +01:00
Azlux
615a78f00e typo fix 2019-12-19 17:07:49 +01:00
Azlux
13d0058086 Debian packages
Yeah !
2019-12-19 16:51:27 +01:00
azlux
1b8025aa91
Merge pull request #93 from folti/master
start service before syslog-ng service too
2019-09-26 14:48:56 +02:00
Tamas Pal
7cf6164364 start service before syslog-ng service too
prevent conflicts and failed startup of the syslog-ng daemon
if it's been used instead of the default rsyslog.
2019-09-26 12:43:24 +02:00
David MICHEL
0dc5827f5c typo fix + fix rsync command
fix for #92 , bad fuzzy parameter
2019-09-20 14:18:54 +02:00
Zak Kemble
87540578cc Make log2ram shutdown after journald has shutdown
#83 At system shutdown log2ram should wait until journald has shutdown before syncing back to disk, otherwise it will sync an unclosed journal log.
2019-07-26 23:59:56 +01:00
17 changed files with 534 additions and 158 deletions

View File

@ -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
View File

@ -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
``` ```

View 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
View 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
View File

@ -0,0 +1 @@
/etc/log2ram.conf

11
debian/control vendored Executable file
View 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
View 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
View 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
View 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

View File

@ -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
View File

@ -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
View 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
View File

@ -0,0 +1,9 @@
[Unit]
Description=Daily Log2Ram writing activities
[Timer]
OnCalendar=*-*-* 23:55:00
Persistent=true
[Install]
WantedBy=timers.target

View File

@ -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

View File

@ -1,3 +0,0 @@
#!/usr/bin/env sh
systemctl reload log2ram

View File

@ -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

View File

@ -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