prepare 2.89 KB
Newer Older
1
#!/usr/bin/perl -w
2 3 4 5 6 7 8 9
use English;
require 'ctime.pl';

#
# Prepare the node for cutting a new image. Should be run just prior, on
# each of the FreeBSD and Linux partitions.
#
my $DUMPDATES   = "/etc/dumpdates";
10
my @DBFILES     = ("/var/db/testbed.deltas", "/var/db/testbed.tarfiles");
11
my $FSTAB       = "/etc/fstab";
12 13 14
my $LOGDIR      = "/var/log";
my $RUNDIR      = "/var/run";
my $HISTORY     = "/root/.history";
15
my $MAILFILE    = "/var/mail/root";
16
my $LEASES      = "/var/db/dhclient.leases";
17
my $NTPDRIFT    = "/etc/ntp/drift";
18 19

#
20 21 22 23 24
# Load the OS independent support library. It will load the OS dependent
# library and initialize itself. 
# 
use lib "/etc/rc.d/testbed";
use libsetup;
25 26 27 28

#
# Untaint path
#
29
$ENV{'PATH'} = '/bin:/sbin:/usr/bin:/usr/local/bin:/etc/testbed';
30 31 32 33 34 35 36 37 38 39
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Turn off line buffering on output
#
$| = 1;

#
# First clean up the node as it would be if free.
#
40
cleanup_node(1);
41

42 43 44 45 46 47
print "Removing old DB files ...\n";
foreach my $dbfile (@DBFILES) {
    if (-e $dbfile) {
	unlink($dbfile) or
	    die("Could not unlink $dbfile");
    }
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
}

#
# Generate a dumpdates file. Use fstab to find out what filesystems
# need entries, and then put in entries for each filesystem using
# the current date.
#
print "Removing old /etc/dumpdates file ...\n";
if (-e $DUMPDATES) {
    unlink($DUMPDATES) or
	die("Could not unlink $DUMPDATES");
}

print "Creating stub /etc/dumpdates file ...\n";
open(TAB, "$FSTAB") or
    die("Could not open $FSTAB: $!");
open(DMP, ">$DUMPDATES") or
    die("Could not open $DUMPDATES: $!");

while (<TAB>) {
    if ($_ =~ /^([\w\/]+).*(\d).*\d$/) {
	if ($2 != 0) {
	    my $t = localtime(time);
	    print DMP "$1                          0 $t\n";
	}
    }
}
close(DMP);
close(TAB);

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
print "Cleaning logfiles ...\n";
opendir(LOG,$LOGDIR) or
    die("Could not open directory $LOGDIR: $!");

while ($dirent = readdir(LOG)) {
    my $file = $LOGDIR . "/" . $dirent;
    if (-f $file) {
        if (($file =~ /\.\d$/) || ($file =~ /\.\d\.gz$/)) {
            # Remove archived logfiles
            unlink($file) or
                die("Could not unlink $file: $!");
        } elsif (-s $file) {
            # Truncate other logfiles - many do not get re-created
            truncate($file,0) or
                die("Could not truncate $file: $!");
        }
    }
}
closedir(LOG);

print "Removing root's history ...\n";
if (-f $HISTORY) {
    unlink($HISTORY) or
        die("Could not unlink $HISTORY: $!");
}

104 105 106 107 108 109
print "Removing root's mailfile ...\n";
if (-f $MAILFILE) {
    unlink($MAILFILE) or
        die("Could not unlink $MAILFILE: $!");
}

110 111 112 113 114 115
print "Removing $NTPDRIFT ...\n";
if (-f $NTPDRIFT) {
    unlink($NTPDRIFT) or
        die("Could not unlink $NTPDRIFT: $!");
}

116 117 118 119 120
print "Cleaing .pid files ...\n";
system("rm -rf $RUNDIR/*.pid");

print "Cleaing out /tmp ...\n";
system("rm -rf /tmp/* /tmp/.??*");
121 122 123

print "Removing dhclient leases ...\n";
system("rm -f $LEASES");