]> git.proxmox.com Git - pve-container.git/commitdiff
try to randomize crontab entries
authorDietmar Maurer <dietmar@proxmox.com>
Thu, 7 May 2015 06:59:37 +0000 (08:59 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Thu, 7 May 2015 06:59:37 +0000 (08:59 +0200)
src/PVE/LXCSetup/Base.pm
src/test/run_tests.pl
src/test/test-debian-001/etc/crontab [new file with mode: 0644]
src/test/test-debian-001/etc/crontab.exp [new file with mode: 0644]

index 40ce6c17460f318723edeb86d9ea3e8d8cf00cee..5141b9e4255c21003203a583b558b420cd87fd93 100644 (file)
@@ -258,6 +258,33 @@ sub set_user_password {
     }
 }
 
+my $randomize_crontab = sub {
+    my ($self, $conf) = @_;
+
+    my $rootdir = $self->{rootdir};
+
+    my @files = <$rootdir/etc/cron.d/*>;
+
+    my $crontab_fn = "$rootdir/etc/crontab";
+    unshift @files, $crontab_fn if -f $crontab_fn;
+    
+    foreach my $filename (@files) {
+       my $data = PVE::Tools::file_get_contents($filename);
+       my $new = '';
+       foreach my $line (split(/\n/, $data)) {
+           # we only randomize minutes for root crontab entries
+           if ($line =~ m/^\d+(\s+\S+\s+\S+\s+\S+\s+\S+\s+root\s+\S.*)$/) {
+               my $rest = $1;
+               my $min = int(rand()*59);
+               $new .= "$min$rest\n";
+           } else {
+               $new .= "$line\n";
+           }
+       }
+       PVE::Tools::file_set_contents($filename, $new);
+   }
+};
+
 sub pre_start_hook {
     my ($self, $conf) = @_;
 
@@ -273,6 +300,9 @@ sub post_create_hook {
     my ($self, $conf, $root_password) = @_;
 
     $self->template_fixup($conf);
+    
+    &$randomize_crontab($self, $conf);
+    
     $self->set_user_password($conf, 'root', $root_password);
     $self->setup_init($conf);
     $self->setup_network($conf);
index ff68f8a796230d10173879c4bcf9641c73bdb298..236576eb0d84496fb9fc68fd5850ed0294178b36 100755 (executable)
@@ -40,9 +40,10 @@ sub run_test {
     for (my $i = 0; $i < 2; $i++) {
        # run tests twice, to make sure scripts are idempotent
        
+       srand(0);
        $lxc_setup->post_create_hook('$TEST$ABCDEF');
 
-       my @testfiles = qw(/etc/hostname /etc/hosts /etc/inittab /etc/network/interfaces /etc/resolv.conf /etc/passwd /etc/shadow /etc/sysconfig/network /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth1 /etc/sysconfig/network-scripts/ifcfg-eth2 /etc/sysconfig/network-scripts/ifcfg-eth3 /etc/init/start-ttys.conf /etc/init/tty.conf /etc/init/power-status-changed.conf /etc/securetty);
+       my @testfiles = qw(/etc/hostname /etc/hosts /etc/inittab /etc/network/interfaces /etc/resolv.conf /etc/passwd /etc/shadow /etc/sysconfig/network /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth1 /etc/sysconfig/network-scripts/ifcfg-eth2 /etc/sysconfig/network-scripts/ifcfg-eth3 /etc/init/start-ttys.conf /etc/init/tty.conf /etc/init/power-status-changed.conf /etc/securetty /etc/crontab);
        foreach my $fn (@testfiles) {
            next if !-f "$testdir/$fn.exp";
            test_file("$testdir/$fn.exp", "$rootfs/$fn");
diff --git a/src/test/test-debian-001/etc/crontab b/src/test/test-debian-001/etc/crontab
new file mode 100644 (file)
index 0000000..95edd9b
--- /dev/null
@@ -0,0 +1,15 @@
+# /etc/crontab: system-wide crontab
+# Unlike any other crontab you don't have to run the `crontab'
+# command to install the new version when you edit this file
+# and files in /etc/cron.d. These files also have username fields,
+# that none of the other crontabs do.
+
+SHELL=/bin/sh
+PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
+
+# m h dom mon dow user command
+17 *   * * *   root    cd / && run-parts --report /etc/cron.hourly
+25 6   * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
+47 6   * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
+52 6   1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
+#
diff --git a/src/test/test-debian-001/etc/crontab.exp b/src/test/test-debian-001/etc/crontab.exp
new file mode 100644 (file)
index 0000000..f57dc8d
--- /dev/null
@@ -0,0 +1,15 @@
+# /etc/crontab: system-wide crontab
+# Unlike any other crontab you don't have to run the `crontab'
+# command to install the new version when you edit this file
+# and files in /etc/cron.d. These files also have username fields,
+# that none of the other crontabs do.
+
+SHELL=/bin/sh
+PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
+
+# m h dom mon dow user command
+10 *   * * *   root    cd / && run-parts --report /etc/cron.hourly
+44 6   * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
+5 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
+51 6   1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
+#