[Devel] [PATCH] Move cronjobs installation/remove to initscript

Kir Kolyshkin kir at openvz.org
Wed Nov 22 08:34:53 PST 2006


If OpenVZ is not started, it doesn't make sense to execute its
cronjobs every five minutes. This fills up system logs, reduces
notebook battery life etc. Moving cronjobs management to initscript
fixes that.

Stolen from mailman initscript.
---
 etc/cron.d/vpsnetclean |   34 +++++++++++++++++++---------------
 etc/cron.d/vpsreboot   |   34 +++++++++++++++++++---------------
 etc/init.d/vz          |   40 ++++++++++++++++++++++++++++++++++++++++
 etc/init.d/vz-gentoo   |   40 ++++++++++++++++++++++++++++++++++++++++
 src/Makefile           |    2 +-
 vzctl.spec             |    4 +++-
 6 files changed, 122 insertions(+), 32 deletions(-)

diff --git a/etc/cron.d/vpsnetclean b/etc/cron.d/vpsnetclean
index 50397bb..07da8b5 100755
--- a/etc/cron.d/vpsnetclean
+++ b/etc/cron.d/vpsnetclean
@@ -1,21 +1,25 @@
-#  Copyright (C) 2000-2006 SWsoft. All rights reserved.
 #
-#  This program is free software; you can redistribute it and/or modify
-#  it under the terms of the GNU General Public License as published by
-#  the Free Software Foundation; either version 2 of the License, or
-#  (at your option) any later version.
+# -- WARNING -- WARNING -- WARNING -- WARNING -- WARNING -- WARNING --
+# ------------------  EDIT THE CORRECT FILE  -------------------------
 #
-#  This program is distributed in the hope that it will be useful,
-#  but WITHOUT ANY WARRANTY; without even the implied warranty of
-#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-#  GNU General Public License for more details.
+# This file is copied to /etc/cron.d/vpsnetclean from
+# /usr/share/vzctl/cron/vpsnetclean when the vz service is started via its
+# init.d script and the file /etc/cron.d/vpsnetclean is removed when the
+# service is stopped.  Therefore any edits made directly to
+# /etc/cron.d/vpsnetclean will be lost anytime the vz service
+# restarts.
 #
-#  You should have received a copy of the GNU General Public License
-#  along with this program; if not, write to the Free Software
-#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-#
-# Clean VEs network rules (arp,route,iptables)
+# To make changes edit the master copy /usr/share/vzctl/cron/vpsnetclean
+# and then copy the file to /etc/cron.d/ (or restart vz service).
 #
+# The reason this is done this way is because the vz cron jobs
+# should only be invoked if the vz service is enabled and not
+# just as a consequence of installing the vzctl as was the case
+# previously. The file /etc/cron.d/vznetclean cannot simply be linked to
+# the master copy in /usr/share/vzctl/cron/ because for security reasons cron
+# will not process crontab files that are links or writeable by
+# anybody else but root, thus the file must be copied into /etc/cron.d
+# with the right ownership and permissions.
 
+# Clean VEs network rules (arp,route,iptables)
 */5 * * * * root /usr/share/vzctl/scripts/vpsnetclean
-
diff --git a/etc/cron.d/vpsreboot b/etc/cron.d/vpsreboot
index 41a5809..28c5e1a 100755
--- a/etc/cron.d/vpsreboot
+++ b/etc/cron.d/vpsreboot
@@ -1,21 +1,25 @@
-#  Copyright (C) 2000-2006 SWsoft. All rights reserved.
 #
-#  This program is free software; you can redistribute it and/or modify
-#  it under the terms of the GNU General Public License as published by
-#  the Free Software Foundation; either version 2 of the License, or
-#  (at your option) any later version.
+# -- WARNING -- WARNING -- WARNING -- WARNING -- WARNING -- WARNING --
+# ------------------  EDIT THE CORRECT FILE  -------------------------
 #
-#  This program is distributed in the hope that it will be useful,
-#  but WITHOUT ANY WARRANTY; without even the implied warranty of
-#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-#  GNU General Public License for more details.
+# This file is copied to /etc/cron.d/vpsreboot from
+# /usr/share/vzctl/cron/vpsreboot when the vz service is started via its
+# init.d script and the file /etc/cron.d/vpsreboot is removed when the
+# service is stopped.  Therefore any edits made directly to
+# /etc/cron.d/vpsreboot will be lost anytime the vz service
+# restarts.
 #
-#  You should have received a copy of the GNU General Public License
-#  along with this program; if not, write to the Free Software
-#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+# To make changes edit the master copy /usr/share/vzctl/cron/vpsreboot
+# and then copy the file to /etc/cron.d/ (or restart vz service).
 #
-# Start VEs marked as rebooted.
-# If vzagent not installed you can enable reboot functionality by
-# commented out the line below.
+# The reason this is done this way is because the vz cron jobs
+# should only be invoked if the vz service is enabled and not
+# just as a consequence of installing the vzctl as was the case
+# previously. The file /etc/cron.d/vznetclean cannot simply be linked to
+# the master copy in /usr/share/vzctl/cron/ because for security reasons cron
+# will not process crontab files that are links or writeable by
+# anybody else but root, thus the file must be copied into /etc/cron.d
+# with the right ownership and permissions.
 
+# Start VEs marked as rebooted.
 */5 * * * * root /usr/share/vzctl/scripts/vpsreboot
diff --git a/etc/init.d/vz b/etc/init.d/vz
index 1db41d3..aefa47f 100755
--- a/etc/init.d/vz
+++ b/etc/init.d/vz
@@ -76,6 +76,44 @@ elif [ -r /etc/rc.status ]; then
 	fi
 fi
 
+# We used to install vzctl cron jobs when the vzctl rpm was
+# installed, irrespective of whether OpenVZ was actually being
+# run. Although the cron jobs didn't create any problems if someone
+# wasn't running OpenVZ some users complained about the cron log file
+# filling up, resource usage, and power consumption since systems
+# wouldn't really idle. It really only makes sense to run the vzctl
+# cron jobs if the vzctl service is turned on and not just merely
+# having the rpm installed. This init.d script is an obvious place to
+# install or remove the cron jobs based on the service being enabled
+# or not.
+
+SRC_CRONSCRIPT_DIR=/usr/share/vzctl/cron/
+DST_CRONSCRIPT_DIR=/etc/cron.d/mailman/
+
+install_crons()
+{
+	local f ff
+	for ff in $SRC_CRONSCRIPT_DIR/*; do
+		f=$(basename $ff)
+		install -m644 -o root -g root $f $DST_CRONSCRIPT_DIR/$ff
+	done
+}
+
+remove_crons()
+{
+	local f ff
+	for ff in $SRC_CRONSCRIPT_DIR/*; do
+		f=$(basename $ff)
+		cat > $DST_CRONSCRIPT_DIR/$f <<EOF
+# DO NOT EDIT THIS FILE!
+#
+# Contents of this file managed by /etc/init.d/vz script
+# Master copy is $SRC_CRONSCRIPT_DIR/$f
+# Consult that file for documentation
+EOF
+}
+
+
 VEINFO=""
 RETVAL=0
 PARALLEL=4
@@ -402,6 +440,7 @@ function start()
 	
 	setup_net
 	hn_setup
+	install_crons
 	ve_start
 	
 	rm -f $LOCKFILE
@@ -418,6 +457,7 @@ function stop()
 	fi
 
 	ve_stop
+	remove_crons
 	__echo "Stopping OpenVZ: "
 	ip link set ${VZDEV} down 2>/dev/null
 	for MOD in $MODULES; do
diff --git a/etc/init.d/vz-gentoo b/etc/init.d/vz-gentoo
index ae332c4..19868e1 100644
--- a/etc/init.d/vz-gentoo
+++ b/etc/init.d/vz-gentoo
@@ -40,6 +40,43 @@ check_config() {
 	return 0
 }
 
+# We used to install vzctl cron jobs when the vzctl package was
+# installed, irrespective of whether OpenVZ was actually being
+# run. Although the cron jobs didn't create any problems if someone
+# wasn't running OpenVZ some users complained about the cron log file
+# filling up, resource usage, and power consumption since systems
+# wouldn't really idle. It really only makes sense to run the vzctl
+# cron jobs if the vzctl service is turned on and not just merely
+# having the package installed. This init.d script is an obvious place to
+# install or remove the cron jobs based on the service being enabled
+# or not.
+
+SRC_CRONSCRIPT_DIR=/usr/share/vzctl/cron/
+DST_CRONSCRIPT_DIR=/etc/cron.d/mailman/
+
+install_crons()
+{
+	local f ff
+	for ff in $SRC_CRONSCRIPT_DIR/*; do
+		f=$(basename $ff)
+		install -m644 -o root -g root $f $DST_CRONSCRIPT_DIR/$ff
+	done
+}
+
+remove_crons()
+{
+	local f ff
+	for ff in $SRC_CRONSCRIPT_DIR/*; do
+		f=$(basename $ff)
+		cat > $DST_CRONSCRIPT_DIR/$f <<EOF
+# DO NOT EDIT THIS FILE!
+#
+# Contents of this file managed by /etc/init.d/vz script
+# Master copy is $SRC_CRONSCRIPT_DIR/$f
+# Consult that file for documentation
+EOF
+}
+
 setup_ve0() {
 	local msg ve0conf="${VPSCONFDIR}/0.conf"
 
@@ -237,6 +274,8 @@ start() {
 
 	setup_ve0
 
+	install_crons
+
 	start_ve
 }
 
@@ -245,6 +284,7 @@ stop() {
 
 	local mod
 
+	remove_crons
 	stop_ve
 	stop_net
 
diff --git a/src/Makefile b/src/Makefile
index 3db39ce..85719cc 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -25,7 +25,7 @@ #
 LIBSCRIPTSDIR = /usr/lib/vzctl/scripts/
   SCRIPTSDIR = /usr/share/vzctl/scripts
 NETSCRIPTDIR = /etc/sysconfig/network-scripts
-    CRONDDIR = /etc/cron.d
+    CRONDDIR = /usr/share/vzctl/cron
     INITDDIR = /etc/init.d
 UDEVRULESDIR = /etc/udev/rules.d
        VZDIR = /vz
diff --git a/vzctl.spec b/vzctl.spec
index c63fc7d..f9984c2 100644
--- a/vzctl.spec
+++ b/vzctl.spec
@@ -1,5 +1,5 @@
 %define _initddir %_sysconfdir/init.d
-%define _crondir %_sysconfdir/cron.d
+%define _crondir /usr/share/vzctl/cron/
 %define _vzdir /vz
 %define _lockdir %{_vzdir}/lock 
 %define _dumpdir %{_vzdir}/dump 
@@ -69,6 +69,8 @@ rm -rf $RPM_BUILD_ROOT
 %attr(755,root,root) %{_initddir}/vz
 %attr(644,root,root) %config(noreplace) %{_crondir}/vpsreboot
 %attr(644,root,root) %{_crondir}/vpsnetclean
+%attr(644,root,root) %ghost /etc/cron.d/vpsreboot
+%attr(644,root,root) %ghost /etc/cron.d/vpsnetclean
 %dir %attr(755,root,root) %{_lockdir}
 %dir %attr(755,root,root) %{_dumpdir}
 %dir %attr(755,root,root) %{_cachedir}
-- 
1.4.2.4





More information about the Devel mailing list