prepare 6.28 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     = ();
16
my @SYSVARDIRS  = ("lib/dhcp", "spool/clientmqueue", "log/audit");
Mike Hibler's avatar
Mike Hibler committed
17
my @VARDIRS	= ("logs", "db", "jails", "boot", "lock");
18
my @SOCKETS	= ("/dev/log", "/var/run/acpid.socket");
19
my $FSTAB       = "/etc/fstab";
20
21
my $LOGDIR      = "/var/log";
my $RUNDIR      = "/var/run";
Mike Hibler's avatar
Mike Hibler committed
22
my $HISTORY     = ".bash_history";
23
my $MAILFILE    = "/var/spool/mail/root";
24
my $NTPDRIFT    = "/etc/ntp/drift";
25
26
my $SFSUSERS    = "/etc/sfs/sfs_users";
my $SFSHOSTKEY  = "/etc/sfs/sfs_host_key";
27
my $VARACCTDIR  = "/var/account";
28

Mike Hibler's avatar
Mike Hibler committed
29
30
31
32
33
34
35
36
37
38
39
40
41
#
# 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
		   );

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

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

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

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

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

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

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

78
79
80
81
82
83
84
85
#
# 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");

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
113
#
# 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);

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

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

139
140
141
142
143
144
145
146
147
148
149
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: $!");
	    }
150
151
	}
    }
152
    closedir(VARACCT);
153
154
}

155
print "Removing root's history ...\n";
Mike Hibler's avatar
Mike Hibler committed
156
157
158
159
160
161
162
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: $!");
163
164
}

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

Mike Hibler's avatar
Mike Hibler committed
171
172
173
174
175
176
#
# 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";
177
if (-f $NTPDRIFT) {
Mike Hibler's avatar
Mike Hibler committed
178
179
180
181
    open(DRIFT, ">$NTPDRIFT") or
	die("Could not open $NTPDRIFT: $!");
    print DRIFT "0\n";
    close(DRIFT);
182
183
}

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

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

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

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

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

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

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

Mike Hibler's avatar
Mike Hibler committed
219
220
221
222
223
224
225
226
227
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");

228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
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);
243
244
245
246
247
248
249
250
251
252
253

print "Removing assorted unix-domain socket files\n";
foreach my $file (@SOCKETS) {
    if (-e "$file" && ! -f "$file") {
	unlink("$file") or
	    warn("*** could not remove $file\n");
    }
}

exit 0;