We start ramlog on distributions with systemd

    From the “Marginal Notes” series. More so as not to forget yourself, but maybe someone will come in handy.

    After purchasing the Raspberry Pi 2, Odroid XU4, which had not lived for a week, was replaced by a leisurely shamanism to install and initial configure the system for itself. What was disappointment when the beloved ramlog refused to not only be installed (unpack with hands, not lazy) , but also to start after a forced "implementation". Desperate and requesting Google, I found out that it is not friends with systemd, from the word "completely".

    Already almost ready to fence something of his own , I came across a German postwhere the "adapted" ramlog was mentioned. Gutting the immediately downloaded image showed that there exactly what I wanted was done. Therefore, instead of inventing my bicycle, I suggest using the already prepared one.

    How does it work


    Both the old and the new versions of ramlog work on the same principle: download / var / log into memory at boot and write it to disk by command or shutdown.

    The difference between the new version is the use of the systemd startup mechanism and the storage of logs in the archive, which radically simplified the code at the cost of archiving and unchanged files. Well, instead of launching its own RAM disk, tmpfs is used, which, in which case, will go into a swap (and we have it on zram and there is a good chance that there will be no access to the disk)

    Installation


    1. We create the ramlog-a service (/ usr / bin / ramlog):
      / usr / bin / ramlog
      #!/bin/sh
      . /lib/lsb/init-functions
      start() {
          log_begin_msg "RAMLOG: Read files from disk.."
          tar xfz /var/ram_log.tar.gz -C /
          log_end_msg 0
      }
      stop() {
          log_begin_msg "RAMLOG: Write files to disk.."
          tar cfz /var/ram_log.tar.gz --directory=/ var/log/
          log_end_msg 0
      }
      case "$1" in
          start)
              start
              ;;
          stop)
              stop
              ;;
          flush)
              stop
              ;;
          *)
               echo "Usage: $0 {start|stop|flush}"
               exit 1
      esac
      


    2. Create an entry for systemd (/etc/systemd/system/ramlog.service):
      Hidden text
      [Unit]
      Description=Ramlog
      After=local-fs.target
      Before=cron.service syslog.service
      [Service]
      Type=oneshot
      RemainAfterExit=yes
      ExecStart=/usr/bin/ramlog start
      ExecStop=/usr/bin/ramlog stop
      [Install]
      WantedBy=multi-user.target
      

    3. Add a record to the CRON to periodically save logs:
      # ...
      # каждые 15 минут, настроить по вкусу
      */15 * * * * /usr/bin/crontab flush >/dev/null 2>&1
      

    4. Edit / etc / fstab, transferring / var / log to tmpfs:
      tmpfs /var/log tmpfs nodev,nosuid 0 0
      

    5. Install the service:
      # insserv
      # systemctl enable ramlog.service
      

    6. We start the service
      # systemctl start ramlog.service
      

      Now, at the next reboot, the contents of / var / log will be saved in /var/var_log.tar.gz, and loaded already in tmpfs
    7. ... Profit!


    Wishlist


    It is possible to redo the logic of work by analogy with the original ramlog - you only need to do rsync instead of packaging to save. What is "more profitable" when working with a flash drive - who knows?

    The idea of ​​compressed tmpfs was in the air, but somehow nothing has been googled up yet reasonable.

    You can pave the saving of any other folders, including network drives. Unless you have to deal with the systemd parameters to configure the start order of this case.

    Also popular now: