prepare 8.66 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-2010 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
5
6
# All rights reserved.
#
7
use English;
8
use Getopt::Std;
9
10
11
12
13
14
15
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";
16
my @DBFILES     = ();
Ryan Jackson's avatar
Ryan Jackson committed
17
my @SYSVARDIRS  = ("lib/dhcp", "lib/dhclient", "lib/dhcp3", "spool/clientmqueue",
Mike Hibler's avatar
Mike Hibler committed
18
		   "log/audit", "log/account", "log/mysql", "log/ntpstats");
Mike Hibler's avatar
Mike Hibler committed
19
my @VARDIRS	= ("logs", "db", "jails", "boot", "lock");
20
my @SOCKETS	= ("/dev/log", "/var/run/acpid.socket");
21
my $FSTAB       = "/etc/fstab";
22
23
my $LOGDIR      = "/var/log";
my $RUNDIR      = "/var/run";
Mike Hibler's avatar
Mike Hibler committed
24
my $HISTORY     = ".bash_history";
25
my $MAILFILE    = "/var/spool/mail/root";
Mike Hibler's avatar
Mike Hibler committed
26
27
my $NTPDRIFT1   = "/etc/ntp/drift";
my $NTPDRIFT2   = "/var/lib/ntp/drift";
28
29
my $SFSUSERS    = "/etc/sfs/sfs_users";
my $SFSHOSTKEY  = "/etc/sfs/sfs_host_key";
30
my $VARACCTDIR  = "/var/account";
Mike Hibler's avatar
Mike Hibler committed
31
my $IFTAB       = "/etc/iftab";
32

Mike Hibler's avatar
Mike Hibler committed
33
34
35
36
37
38
39
40
41
42
43
44
45
#
# 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
		   );

46
47
# Drag in path stuff so we can find emulab stuff.
BEGIN { require "/etc/emulab/paths.pm"; import emulabpaths; }
48
49
50
51
52
53

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

54
55
56
57
58
59
#
# Load the OS independent support library. It will load the OS dependent
# library and initialize itself. 
# 
use libsetup;

60
61
62
63
64
65
66
67
68
69
70
71
#
# XXX Elabinelab hack: option to not unmount NFS filesystems.
#     The setup script assumes that /share is mounted after this runs.
#
my $noumount = 0;
my %options = ();
if (getopts("N", \%options)) {
    if (defined($options{"N"})) {
	$noumount = 1;
    }
}

72
73
74
#
# First clean up the node as it would be if free.
#
75
cleanup_node(1);
76

77
78
79
print "Running $BINDIR/rc/rc.config to clean up ...\n";
system("$BINDIR/rc/rc.config reset");

80
81
82
83
84
85
print "Removing old DB files ...\n";
foreach my $dbfile (@DBFILES) {
    if (-e $dbfile) {
	unlink($dbfile) or
	    die("Could not unlink $dbfile");
    }
86
87
}

88
89
90
91
92
#
# Attempt to unmount all NFS filesystem if not already unmounted.
# This allows us to clean up /users and /proj.  We only do the cleanup if
# if unmount succeeds and even then, we do it in a safe way.
#
93
if (!$noumount && -e "/share/freebsd") {
94
    print "Unmounting NFS filesystems ...\n";
95
    if (!system("umount -at nfs")) {
96
97
98
99
100
101
102
103
104
105
106
107
	#
	# Note that this command will not remove anything if the NFS unmounts
	# failed (and we didn't catch it) because the directories in question
	# will not be empty.
	#
	system("rmdir /users/* /proj/* /groups/* /scratch/*");
    } else {
	print "Could not determine if NFS filesystems are unmounted, ".
	      "NOT cleaning up /proj and /users\n";
    }
}

108
109
110
#
# Run anacron to bring everything up to date. It never runs again.
#
Mike Hibler's avatar
Mike Hibler committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# XXX anacron, at least later versions, insists on sending mail and there
# is no way to disable it.  Since FC6 doesn't run sendmail, anacrons attempts
# to send mail will hang and eventually timeout.  This makes things take a
# lot longer, so we hack and temporarily move sendmail while we run anacron!
#
if (-x "/usr/sbin/sendmail") {
    my $didrename = 0;
    print "Running cron jobs (might see failed attempts to mail output).\n";
    if (rename("/usr/sbin/sendmail", "/usr/sbin/_sendmail")) {
	$didrename = 1;
    }
    system("anacron -d -f -s -n");
    if ($didrename) {
	rename("/usr/sbin/_sendmail", "/usr/sbin/sendmail");
    }
} else {
    print "Running cron jobs.\n";
    system("anacron -d -f -s -n");
}
130

131
132
133
#
# Create locate and makewhatis DBs, which are no longer in weekly cron.
#
Mike Hibler's avatar
Mike Hibler committed
134
135
136
137
138
139
140
141
if (-x "/etc/cron.pend/makewhatis.cron") {
    print "Building makewhatis DB. This could take a few minutes ...\n";
    system("/etc/cron.pend/makewhatis.cron");
}
if (-x "/etc/cron.pend/slocate.cron") {
    print "Building locate DB. This could take a few minutes ...\n";
    system("/etc/cron.pend/slocate.cron");
}
142

143
144
145
146
147
#
# 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.
#
148
149
150
151
152
153
if (-e $FSTAB) {
    print "Removing old $DUMPDATES file ...\n";
    if (-e $DUMPDATES) {
	unlink($DUMPDATES) or
	    die("Could not unlink $DUMPDATES");
    }
154

155
156
157
158
159
160
161
162
163
164
165
166
    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";
	    }
167
168
	}
    }
169
170
    close(DMP);
    close(TAB);
171
172
}

173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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);

193
194
195
196
197
print "Removing accounting files ...\n";
if ( -f "$LOGDIR/pacct" ) {
    unlink("$LOGDIR/pacct");
}

198
199
200
201
202
203
204
205
206
207
208
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: $!");
	    }
209
210
	}
    }
211
    closedir(VARACCT);
212
213
}

214
print "Removing root's history ...\n";
Mike Hibler's avatar
Mike Hibler committed
215
216
217
218
219
220
221
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: $!");
222
223
}

224
225
226
227
228
229
print "Removing root's mailfile ...\n";
if (-f $MAILFILE) {
    unlink($MAILFILE) or
        die("Could not unlink $MAILFILE: $!");
}

Mike Hibler's avatar
Mike Hibler committed
230
231
232
233
234
235
236
237
238
#
# Remove the iftab file (maps ifaces to mac addrs and renames ifaces according
# to the map -- screws us up).
#
if ( -f $IFTAB ) {
    unlink($IFTAB);
    print "Removed $IFTAB.\n";
}

Mike Hibler's avatar
Mike Hibler committed
239
240
241
242
243
#
# 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.
#
Mike Hibler's avatar
Mike Hibler committed
244
245
246
247
248
249
250
251
print "Resetting NTP drift ...\n";
my $driftfile = $NTPDRIFT1;
if (! -f $driftfile) {
    $driftfile = $NTPDRIFT2;
}
if (-f $driftfile) {
    open(DRIFT, ">$driftfile") or
	die("Could not open $driftfile: $!");
Mike Hibler's avatar
Mike Hibler committed
252
253
    print DRIFT "0\n";
    close(DRIFT);
254
255
}

256
257
print "Clearing out $RUNDIR ...\n";
system("rm -rf $RUNDIR/*.pid $RUNDIR/sudo/* $RUNDIR/pump.sock");
258

259
print "Clearing out /tmp ...\n";
260
system("rm -rf /tmp/* /tmp/.??*");
261

262
263
264
print "Cleaning out /local/logs ...\n";
system("rm -rf /local/logs/* /local/logs/.??*");

265
266
267
print "Removing SFS files ...\n";
if (-f $SFSUSERS) {
    system("rm -f $SFSUSERS ${SFSUSERS}.pub");
268
    system("rm -f $SFSUSERS.old ${SFSUSERS}.pub.old");
269
270
271
272
}
if (-f $SFSHOSTKEY) {
    system("rm -f $SFSHOSTKEY");
}
273

Mike Hibler's avatar
Mike Hibler committed
274
275
276
277
278
279
280
print "Clearing out directories in /var ...\n";
foreach my $dir (@SYSVARDIRS) {
    if (-d "/var/$dir") {
	system("rm -rf /var/$dir/*");
    }
}

Mike Hibler's avatar
Mike Hibler committed
281
282
283
284
# and clean out /var/tmp...hope there isn't anything we should be leaving!
print "Cleaning up /var/tmp ....\n";
system("rm -rf /var/tmp/*");

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

288
289
290
291
292
293
print "Clearing out directories in $VARDIR ...\n";
foreach my $dir (@VARDIRS) {
    if (-d "$VARDIR/$dir") {
	system("rm -rf $VARDIR/$dir/*");
    }
}
294

Mike Hibler's avatar
Mike Hibler committed
295
296
297
298
299
300
301
302
303
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");

304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
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);
319
320
321
322
323
324
325
326
327

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");
    }
}

328
329
# Leave this print statement here; create_image depends on it.
print "prepare ran successfully!\n";
330
331
exit 0;