Skip to contentSkip to conversation

logrotate: Add systemd support

PATCH Superseded Romain Perier 2 participants 6 messages Mar 24, 2017 – Apr 5, 2017

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 logrotate: Add systemd support

Currently, this recipe only supports daily scheduling via a cron job. This commit adds support for systemd, including systemd service and systemd timer. When the corresponding distro feature is enabled the systemd variant will be used. The timer granularity and its accuracy are also configurable.

---

Changes in v2:
- Make the systemd timer configurable (as this is distribution or target specific)

 .../logrotate/logrotate/logrotate.service          |  9 ++++++++
 .../logrotate/logrotate/logrotate.timer            |  7 ++++++
 meta/recipes-extended/logrotate/logrotate_3.9.1.bb | 27 ++++++++++++++++++++--
 3 files changed, 41 insertions(+), 2 deletions(-)
 create mode 100644 meta/recipes-extended/logrotate/logrotate/logrotate.service
 create mode 100644 meta/recipes-extended/logrotate/logrotate/logrotate.timer
meta/recipes-extended/logrotate/logrotate/logrotate.service new file +9 -0
new file mode 100644
index 0000000..3edb8ef
@@ -0,0 +1,9 @@
+[Unit]
+Description=Rotate log files
+
+[Service]
+Type=oneshot
+ExecStart=/usr/sbin/logrotate /etc/logrotate.conf
+Nice=19
+IOSchedulingClass=best-effort
+IOSchedulingPriority=7
meta/recipes-extended/logrotate/logrotate/logrotate.timer new file +7 -0
new file mode 100644
index 0000000..c616b43
@@ -0,0 +1,7 @@
+[Unit]
+Description=Daily rotation of log files
+
+[Timer]
+OnCalendar=@TIMER_BASIS@
+AccuracySec=@TIMER_ACCURACY@
+Persistent=true
meta/recipes-extended/logrotate/logrotate_3.9.1.bb +25 -2
index 5f1a601..6953996 100644
@@ -14,6 +14,8 @@ SRC_URI = "https://fedorahosted.org/releases/l/o/logrotate/logrotate-${PV}.tar.g
            file://act-as-mv-when-rotate.patch \
            file://update-the-manual.patch \
            file://disable-check-different-filesystems.patch \
+           ${@bb.utils.contains('DISTRO_FEATURES', 'systemd', 'file://logrotate.service', '', d)} \
+           ${@bb.utils.contains('DISTRO_FEATURES', 'systemd', 'file://logrotate.timer', '', d)} \
             "
 
 SRC_URI[md5sum] = "4492b145b6d542e4a2f41e77fa199ab0"
@@ -47,6 +49,17 @@ EXTRA_OEMAKE = "\
 # INSTALL=install and BASEDIR=/usr.
 OS_NAME = "Linux"
 
+inherit systemd
+
+SYSTEMD_AUTO_ENABLE = "disable"
+SYSTEMD_SERVICE_${PN} = "\
+    ${PN}.service \
+    ${PN}.timer \
+"
+
+LOGROTATE_SYSTEMD_TIMER_BASIS ?= "daily"
+LOGROTATE_SYSTEMD_TIMER_ACCURACY ?= "12h"
+
 do_compile_prepend() {
     # Make sure the recompile is OK
     rm -f ${B}/.depend
@@ -55,9 +68,19 @@ do_compile_prepend() {
 do_install(){
     oe_runmake install DESTDIR=${D} PREFIX=${D} MANDIR=${mandir}
     mkdir -p ${D}${sysconfdir}/logrotate.d
-    mkdir -p ${D}${sysconfdir}/cron.daily
     mkdir -p ${D}${localstatedir}/lib
     install -p -m 644 examples/logrotate-default ${D}${sysconfdir}/logrotate.conf
-    install -p -m 755 examples/logrotate.cron ${D}${sysconfdir}/cron.daily/logrotate
     touch ${D}${localstatedir}/lib/logrotate.status
+
+    # Install systemd unit files
+    if [ "${@bb.utils.contains('DISTRO_FEATURES', 'systemd', 'systemd', '', d)}" = "systemd" ]; then
+        install -d ${D}/${systemd_system_unitdir}
+        install -m 0644 ${WORKDIR}/logrotate.service ${D}/${systemd_system_unitdir}/logrotate.service
+        install -m 0644 ${WORKDIR}/logrotate.timer ${D}/${systemd_system_unitdir}/logrotate.timer
+        sed -i -e 's,@TIMER_BASIS@,${LOGROTATE_SYSTEMD_TIMER_BASIS},g' ${D}${systemd_system_unitdir}/logrotate.timer
+        sed -i -e 's,@TIMER_ACCURACY@,${LOGROTATE_SYSTEMD_TIMER_ACCURACY},g' ${D}${systemd_system_unitdir}/logrotate.timer
+    else
+        mkdir -p ${D}${sysconfdir}/cron.daily
+        install -p -m 0755 examples/logrotate.cron ${D}${sysconfdir}/cron.daily/logrotate
+    fi
 }
Signature · 1 line
1.8.3.1
#1 lore raw

ping

Quoted 100 lines · Romain Perier

Currently, this recipe only supports daily scheduling via a cron job. This commit adds support for systemd, including systemd service and systemd timer. When the corresponding distro feature is enabled the systemd variant will be used. The timer granularity and its accuracy are also configurable.

---

Changes in v2:
- Make the systemd timer configurable (as this is distribution or target specific)

 .../logrotate/logrotate/logrotate.service          |  9 ++++++++
 .../logrotate/logrotate/logrotate.timer            |  7 ++++++
 meta/recipes-extended/logrotate/logrotate_3.9.1.bb | 27 ++++++++++++++++++++--
 3 files changed, 41 insertions(+), 2 deletions(-)
 create mode 100644 meta/recipes-extended/logrotate/logrotate/logrotate.service
 create mode 100644 meta/recipes-extended/logrotate/logrotate/logrotate.timer
meta/recipes-extended/logrotate/logrotate/logrotate.service new file +9 -0
new file mode 100644
index 0000000..3edb8ef
@@ -0,0 +1,9 @@
+[Unit]
+Description=Rotate log files
+
+[Service]
+Type=oneshot
+ExecStart=/usr/sbin/logrotate /etc/logrotate.conf
+Nice=19
+IOSchedulingClass=best-effort
+IOSchedulingPriority=7
meta/recipes-extended/logrotate/logrotate/logrotate.timer new file +7 -0
new file mode 100644
index 0000000..c616b43
@@ -0,0 +1,7 @@
+[Unit]
+Description=Daily rotation of log files
+
+[Timer]
+OnCalendar=@TIMER_BASIS@
+AccuracySec=@TIMER_ACCURACY@
+Persistent=true
meta/recipes-extended/logrotate/logrotate_3.9.1.bb +25 -2
index 5f1a601..6953996 100644
@@ -14,6 +14,8 @@ SRC_URI = "https://fedorahosted.org/releases/l/o/logrotate/logrotate-${PV}.tar.g
            file://act-as-mv-when-rotate.patch \
            file://update-the-manual.patch \
            file://disable-check-different-filesystems.patch \
+           ${@bb.utils.contains('DISTRO_FEATURES', 'systemd', 'file://logrotate.service', '', d)} \
+           ${@bb.utils.contains('DISTRO_FEATURES', 'systemd', 'file://logrotate.timer', '', d)} \
             "
 
 SRC_URI[md5sum] = "4492b145b6d542e4a2f41e77fa199ab0"
@@ -47,6 +49,17 @@ EXTRA_OEMAKE = "\
 # INSTALL=install and BASEDIR=/usr.
 OS_NAME = "Linux"
 
+inherit systemd
+
+SYSTEMD_AUTO_ENABLE = "disable"
+SYSTEMD_SERVICE_${PN} = "\
+    ${PN}.service \
+    ${PN}.timer \
+"
+
+LOGROTATE_SYSTEMD_TIMER_BASIS ?= "daily"
+LOGROTATE_SYSTEMD_TIMER_ACCURACY ?= "12h"
+
 do_compile_prepend() {
     # Make sure the recompile is OK
     rm -f ${B}/.depend
@@ -55,9 +68,19 @@ do_compile_prepend() {
 do_install(){
     oe_runmake install DESTDIR=${D} PREFIX=${D} MANDIR=${mandir}
     mkdir -p ${D}${sysconfdir}/logrotate.d
-    mkdir -p ${D}${sysconfdir}/cron.daily
     mkdir -p ${D}${localstatedir}/lib
     install -p -m 644 examples/logrotate-default ${D}${sysconfdir}/logrotate.conf
-    install -p -m 755 examples/logrotate.cron ${D}${sysconfdir}/cron.daily/logrotate
     touch ${D}${localstatedir}/lib/logrotate.status
+
+    # Install systemd unit files
+    if [ "${@bb.utils.contains('DISTRO_FEATURES', 'systemd', 'systemd', '', d)}" = "systemd" ]; then
+        install -d ${D}/${systemd_system_unitdir}
+        install -m 0644 ${WORKDIR}/logrotate.service ${D}/${systemd_system_unitdir}/logrotate.service
+        install -m 0644 ${WORKDIR}/logrotate.timer ${D}/${systemd_system_unitdir}/logrotate.timer
+        sed -i -e 's,@TIMER_BASIS@,${LOGROTATE_SYSTEMD_TIMER_BASIS},g' ${D}${systemd_system_unitdir}/logrotate.timer
+        sed -i -e 's,@TIMER_ACCURACY@,${LOGROTATE_SYSTEMD_TIMER_ACCURACY},g' ${D}${systemd_system_unitdir}/logrotate.timer
+    else
+        mkdir -p ${D}${sysconfdir}/cron.daily
+        install -p -m 0755 examples/logrotate.cron ${D}${sysconfdir}/cron.daily/logrotate
+    fi
 }
#2 lore raw
Quoted 2 lines · Romain Perier

ping

Is this available in newer upstream versions of logrotate? Should you send it there first?

Alex

#3 lore raw

Hello,

Quoted 9 lines · Alexander Kanavin
Quoted 2 lines · Romain Perier

ping

Is this available in newer upstream versions of logrotate? Should you send it there first?

Alex

No, not yet.
Mhhh, we have variables specific to the recipe in the .service and .timer, but I think that I can still send a generic version of these files on upstream and use sed anyway from the recipe...

Romain

#4 lore raw

Add yocto and Alexander to Cc: (sorry)

Romain

Quoted 18 lines · Romain Perier

Hello,

Quoted 8 lines · Alexander Kanavin
Quoted 2 lines · Romain Perier

ping

Is this available in newer upstream versions of logrotate? Should you send it there first?

Alex

No, not yet.
Mhhh, we have variables specific to the recipe in the .service and .timer, but I think that I can still send a generic version of these files on upstream and use sed anyway from the recipe...

Romain

#5 lore raw

I have just realized, systemd service and timer is already present and supported on upstream (like cron, in examples/). At least in 3.11.0 and master.

I will propose an update of this recipe to 3.11.0 (the last release) and then add support for systemd.

Regards,

Romain

Quoted 24 lines · Romain Perier

Add yocto and Alexander to Cc: (sorry)

Romain

Quoted 18 lines · Romain Perier

Hello,

Quoted 8 lines · Alexander Kanavin
Quoted 2 lines · Romain Perier

ping

Is this available in newer upstream versions of logrotate? Should you send it there first?

Alex

No, not yet.
Mhhh, we have variables specific to the recipe in the .service and .timer, but I think that I can still send a generic version of these files on upstream and use sed anyway from the recipe...

Romain