prepare 5.86 KB
Newer Older
1
#!/usr/bin/perl -w
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2
3
#
# EMULAB-COPYRIGHT
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
use English;
require 'ctime.pl';

10
11
12
# Drag in path stuff so we can find emulab stuff.
BEGIN { require "/etc/emulab/paths.pm"; import emulabpaths; }

13
14
15
16
17
#
# 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";
18
my @DBFILES     = ();
19
my @VARDIRS	= ("logs", "db", "jails", "boot", "lock");
20
my $FSTAB       = "/etc/fstab";
21
22
my $LOGDIR      = "/var/log";
my $RUNDIR      = "/var/run";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
23
my $ACCTDIR     = "/var/account";
24
my $HISTORY     = "/root/.history";
25
my $MAILDIR     = "/var/mail";
26
my $LEASES      = "/var/db/dhclient.leases*";
27
my $NTPDRIFT    = "/etc/ntp.drift";
28
29
my $SFSUSERS    = "/etc/sfs/sfs_users";
my $SFSHOSTKEY  = "/etc/sfs/sfs_host_key";
30
my $MOUNTINFO	= "/var/db/mounttab";
31

32
33
34
35
36
37
38
39
40
41
42
43
#
# 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");


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
#
# Build the locate database. I hate that its never there!
#
74
if (-x "/usr/libexec/locate.updatedb") {
75
76
77
78
    print "Building the locate database ... Be patient, will ya!\n";
    system("/usr/libexec/locate.updatedb");
}

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
104
105
106
#
# 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);

107
108
109
110
111
112
113
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) {
114
        if (($file =~ /\.\d+$/) || ($file =~ /\.\d\.gz$/) ||
115
	    ($file =~ /\.\d\.bz2$/) || ($file =~ /\.(today|yesterday)$/)) {
116
117
118
119
120
121
122
123
124
125
126
127
            # 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);

128
129
130
131
132
133
134
135
136
137
138
139
140
141
#
# Must remove /var/db/mounttab or else at boot time the node will attempt
# to contact all the servers listed in the file to tell them that the
# indicated filesystems are no longer mounted by this node.  Normally this
# is harmless however, if the listed server is not reachable in the image's
# new environment (e.g., you are making an image for another testbed), each
# and every one of the listed server/fs pairs will hang for 5 minutes or so.
#
print "Removing $MOUNTINFO ...\n";
if (-f $MOUNTINFO) {
    unlink($MOUNTINFO) or
        die("Could not unlink $MOUNTINFO: $!");
}    

Mike Hibler's avatar
Mike Hibler committed
142
print "Removing root's history and .saves files...\n";
143
144
145
if (-f $HISTORY) {
    unlink($HISTORY) or
        die("Could not unlink $HISTORY: $!");
Mike Hibler's avatar
Mike Hibler committed
146
    system("rm -f /root/.saves-*");
147
148
}

149
150
print "Cleaning mail spool files ...\n";
system("rm -rf $MAILDIR/*");
151

152
153
154
155
156
157
#
# 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";
158
if (-f $NTPDRIFT) {
159
160
161
162
    open(DRIFT, ">$NTPDRIFT") or
	die("Could not open $NTPDRIFT: $!");
    print DRIFT "0\n";
    close(DRIFT);
163
164
}

165
print "Cleaning .pid files ...\n";
166
system("rm -rf $RUNDIR/*.pid /var/spool/*/*.pid");
167

168
169
170
print "Clearing utmp file ...\n";
system("rm -rf $RUNDIR/utmp");

171
172
173
174
175
print "Clearing other $RUNDIR stuff ...\n";
system("rm -f $RUNDIR/*.db $RUNDIR/*.boot $RUNDIR/*.hints $RUNDIR/*.lock");
system("rm -f $RUNDIR/*.sock $RUNDIR/*.sockets");
system("rm -rf $RUNDIR/sudo/*");

176
print "Cleaning out /tmp ...\n";
177
system("rm -rf /tmp/* /tmp/.??*");
178

179
180
181
print "Cleaning out /local/logs ...\n";
system("rm -rf /local/logs/* /local/logs/.??*");

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

185
186
187
print "Cleaning out old accounting files ...\n";
system("rm -rf $ACCTDIR/*");

188
189
190
191
192
193
194
#
# Urk.  Old copies of passwd files are stored here...
#
print "Cleaning out /var/backups ...\n";
system("rm -rf /var/backups/*");


195
196
197
print "Removing SFS files ...\n";
if (-f $SFSUSERS) {
    system("rm -f $SFSUSERS ${SFSUSERS}.pub");
198
    system("rm -f $SFSUSERS.old ${SFSUSERS}.pub.old");
199
200
201
202
}
if (-f $SFSHOSTKEY) {
    system("rm -f $SFSHOSTKEY");
}
203
system("rm -f /var/sfs/sockets/*.sock");
204
205
206
207
208
209
210
211
212


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

213
214
215
216
217
218
219
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");
    }
}
Mike Hibler's avatar
Mike Hibler committed
220
system("rm -f /etc/start_if*");
221
222
223

exit 0;