prepare 6.02 KB
Newer Older
1
#!/usr/bin/perl -w
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2
3
#
# EMULAB-COPYRIGHT
Mike Hibler's avatar
Mike Hibler committed
4
# Copyright (c) 2000-2005 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
5
6
# All rights reserved.
#
7
8
9
10
11
12
13
14
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";
15
my @DBFILES     = ();
Mike Hibler's avatar
Mike Hibler committed
16
17
my @SYSVARDIRS  = ("lib/dhcp", "spool/clientmqueue");
my @VARDIRS	= ("logs", "db", "jails", "boot", "lock");
18
my $FSTAB       = "/etc/fstab";
19
20
my $LOGDIR      = "/var/log";
my $RUNDIR      = "/var/run";
Mike Hibler's avatar
Mike Hibler committed
21
my $HISTORY     = ".bash_history";
22
my $MAILFILE    = "/var/spool/mail/root";
23
my $NTPDRIFT    = "/etc/ntp/drift";
24
25
my $SFSUSERS    = "/etc/sfs/sfs_users";
my $SFSHOSTKEY  = "/etc/sfs/sfs_host_key";
26
my $VARACCTDIR  = "/var/account";
27

Mike Hibler's avatar
Mike Hibler committed
28
29
30
31
32
33
34
35
36
37
38
39
40
#
# Dead wood in $BINDIR
# If you remove/move a script, list it here so that it goes away when the
# image is remade.
#
my @DEADFILES	= ("rc.agents", "rc.delayagent", "rc.delta", "rc.healthd",
		   "rc.injail", "rc.ipod", "rc.mfs", "rc.progagent",
		   "rc.setup", "rc.slothd", "rc.testbed",
		   "batchcmddone", "bootsetup", "install-tarfile",
		   "jailsetup", "update_delays", "rc/rc.linux",
		   "sethostname" # replaced by sethostname.dhclient on linux
		   );

41
42
# Drag in path stuff so we can find emulab stuff.
BEGIN { require "/etc/emulab/paths.pm"; import emulabpaths; }
43
44
45
46
47
48

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

49
50
51
52
53
54
#
# Load the OS independent support library. It will load the OS dependent
# library and initialize itself. 
# 
use libsetup;

55
56
57
#
# First clean up the node as it would be if free.
#
58
cleanup_node(1);
59

60
61
62
print "Running $BINDIR/rc/rc.config to clean up ...\n";
system("$BINDIR/rc/rc.config reset");

63
64
65
66
67
68
print "Removing old DB files ...\n";
foreach my $dbfile (@DBFILES) {
    if (-e $dbfile) {
	unlink($dbfile) or
	    die("Could not unlink $dbfile");
    }
69
70
}

71
72
73
#
# Run anacron to bring everything up to date. It never runs again.
#
74
print "Running cron jobs.\n";
75
76
system("anacron -d -f -s -n");

77
78
79
80
81
82
83
84
#
# Create locate and makewhatis DBs, which are no longer in weekly cron.
#
print "Building makewhatis DB. This could take a few minutes ...\n";
system("/etc/cron.pend/makewhatis.cron");
print "Building locate DB. This could take a few minutes ...\n";
system("/etc/cron.pend/slocate.cron");

85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#
# 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 $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);

113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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);

133
134
135
136
137
print "Removing accounting files ...\n";
if ( -f "$LOGDIR/pacct" ) {
    unlink("$LOGDIR/pacct");
}

138
139
140
141
142
143
144
145
146
147
148
if (-d $VARACCTDIR) {
    opendir(VARACCT,$VARACCTDIR) or
	die("Could not open directory $VARACCTDIR: $!");

    while ($dirent = readdir(VARACCT)) {
	my $file = $VARACCTDIR . "/" . $dirent;
	if (-f $file) {
	    if ($file =~ /acct/) {
		unlink($file) or
		    die ("Could not unlink $file: $!");
	    }
149
150
	}
    }
151
    closedir(VARACCT);
152
153
}

154
print "Removing root's history ...\n";
Mike Hibler's avatar
Mike Hibler committed
155
156
157
158
159
160
161
if (-f "/$HISTORY") {
    unlink("/$HISTORY") or
        die("Could not unlink /$HISTORY: $!");
}
if (-f "/root/$HISTORY") {
    unlink("/root/$HISTORY") or
        die("Could not unlink /root/$HISTORY: $!");
162
163
}

164
165
166
167
168
169
print "Removing root's mailfile ...\n";
if (-f $MAILFILE) {
    unlink($MAILFILE) or
        die("Could not unlink $MAILFILE: $!");
}

Mike Hibler's avatar
Mike Hibler committed
170
171
172
173
174
175
#
# Just removing the drift file results in no drift being tracked,
# so set it to zero instead.  Not ideal, but at least the clock will
# stabilize eventually this way.
#
print "Resetting drift in $NTPDRIFT ...\n";
176
if (-f $NTPDRIFT) {
Mike Hibler's avatar
Mike Hibler committed
177
178
179
180
    open(DRIFT, ">$NTPDRIFT") or
	die("Could not open $NTPDRIFT: $!");
    print DRIFT "0\n";
    close(DRIFT);
181
182
}

183
184
print "Clearing out $RUNDIR ...\n";
system("rm -rf $RUNDIR/*.pid $RUNDIR/sudo/* $RUNDIR/pump.sock");
185

186
print "Clearing out /tmp ...\n";
187
system("rm -rf /tmp/* /tmp/.??*");
188

189
190
191
print "Cleaning out /local/logs ...\n";
system("rm -rf /local/logs/* /local/logs/.??*");

192
193
194
print "Removing SFS files ...\n";
if (-f $SFSUSERS) {
    system("rm -f $SFSUSERS ${SFSUSERS}.pub");
195
    system("rm -f $SFSUSERS.old ${SFSUSERS}.pub.old");
196
197
198
199
}
if (-f $SFSHOSTKEY) {
    system("rm -f $SFSHOSTKEY");
}
200

Mike Hibler's avatar
Mike Hibler committed
201
202
203
204
205
206
207
print "Clearing out directories in /var ...\n";
foreach my $dir (@SYSVARDIRS) {
    if (-d "/var/$dir") {
	system("rm -rf /var/$dir/*");
    }
}

208
209
210
# take these out since they are large and RHL regens them anyway
system("rm -f /var/lib/rpm/__db*");

211
212
213
214
215
216
print "Clearing out directories in $VARDIR ...\n";
foreach my $dir (@VARDIRS) {
    if (-d "$VARDIR/$dir") {
	system("rm -rf $VARDIR/$dir/*");
    }
}
217

Mike Hibler's avatar
Mike Hibler committed
218
219
220
221
222
223
224
225
226
print "Clearing out old Emulab scripts and binaries in $BINDIR ...\n";
foreach my $file (@DEADFILES) {
    if (-f "$BINDIR/$file") {
	unlink("$BINDIR/$file") or
	    warn("*** could not remove $BINDIR/$file\n");
    }
}
system("rm -f /etc/pump.conf");

227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
print "Removing backup files in /etc\n";

opendir(ETC,"/etc") or
    die ("Couldn't open /etc: $!");

while ($dirent = readdir(ETC)) {
    my $file = "/etc/" . $dirent;
    if (-f $file) {
	if (($file =~ /~$/) || ($file =~ /-$/)) {
	    unlink($file) or
		die ("Couldn't unlink $file: $!");
	}
    }
}
closedir(ETC);