-
Notifications
You must be signed in to change notification settings - Fork 5
/
Machine.pm
313 lines (267 loc) · 10 KB
/
Machine.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# functions to manipulate remote test machine
# Copyright (c) 2018-2022 Alexander Bluhm <[email protected]>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package Machine;
use strict;
use warnings;
use Carp;
use Date::Parse;
use File::Copy;
use POSIX;
use Logcmd;
use parent 'Exporter';
our @EXPORT= qw(createhost power_down power_up reboot
install_pxe upgrade_pxe sysupgrade_fetch get_bsdcons get_version
checkout_cvs update_cvs diff_cvs clean_cvs patch_cvs update_ports
make_kernel make_build
align_kernel gap_kernel sort_kernel reorder_kernel
get_bsdnm
);
# XXX explicit IP address in source code
our $testmaster = "10.0.1.1";
my ($user, $host, %sysctl);
sub createhost {
($user, $host) = @_;
}
# power down, up and reboot machine
sub power_down {
logcmd('ssh', "$host\@$testmaster", "powerdown");
}
sub power_up {
logcmd('ssh', "$host\@$testmaster", "powerup");
}
sub reboot {
logcmd('ssh', "$host\@$testmaster", "reboot");
}
# pxe install machine
sub install_pxe {
my ($release) = @_;
logcmd('ssh', "$host\@$testmaster", "install",
$release ? ("-r", $release) : ());
}
sub upgrade_pxe {
logcmd('ssh', "$host\@$testmaster", "upgrade");
}
sub sysupgrade_fetch {
logcmd('ssh', "$user\@$host", "sysupgrade -fkns");
}
# console output since last OpenBSD kernel boot
sub get_bsdcons {
logcmd({
cmd => ['ssh', "$host\@$testmaster", "bsdcons"],
outfile => "bsdcons-$host.txt.new",
});
rename("bsdcons-$host.txt.new", "bsdcons-$host.txt") or
die "Rename 'bsdcons-$host.txt.new' to 'bsdcons-$host.txt' failed: $!";
}
# get version information
sub get_version {
my @sshcmd = (('ssh', "$user\@$host", 'sysctl'),
qw(kern.version hw.machine hw.ncpu hw.ncpuonline));
logmsg "Command '@sshcmd' started.\n";
open(my $ctl, '-|', @sshcmd)
or die "Open pipe from '@sshcmd' failed: $!";
open(my $version, '>', "version-$host.txt.new")
or die "Open 'version-$host.txt.new' for writing failed: $!";
%sysctl = ();
my $prevkey;
local $_;
while (defined($_ = <$ctl>)) {
if (m{^([\w.]+)=(.*)}) {
$sysctl{$1} = $2;
$prevkey = $1;
} else {
$sysctl{$prevkey} .= "\n$_";
}
print $version $_;
}
close($ctl) or die $! ?
"Close pipe from '@sshcmd' failed: $!" :
"Command '@sshcmd' failed: $?";
logmsg "Command '@sshcmd' finished.\n";
close($version)
or die "Close 'version-$host.txt.new' after writing failed: $!";
rename("version-$host.txt.new", "version-$host.txt") or
die "Rename 'version-$host.txt.new' to 'version-$host.txt' failed: $!";
return %sysctl;
}
# cvs checkout, update, diff
sub checkout_cvs {
my ($release) = @_;
foreach (qw(src ports xenocara)) {
logcmd('ssh', "$user\@$host",
"cd /usr && cvs -qRd /mount/openbsd/cvs co $_/Makefile")
}
my $tag = $release || "";
$tag =~ s/(\d+)\.(\d+)/ -rOPENBSD_${1}_${2}_BASE/;
logcmd('ssh', "$user\@$host", "cd /usr/src && cvs -qR up -PdA$tag");
logcmd('ssh', "$user\@$host", "cd /usr/src && make obj");
}
sub update_cvs {
my ($release, $date, $path) = @_;
cvs_update("src", @_);
$path = $path ? " -C$path" : "";
logcmd('ssh', "$user\@$host", "cd /usr/src && make$path obj");
}
sub cvs_update {
my ($repo, $release, $date, $path, $flags) = @_;
my $tag = $release || "";
$tag =~ s/(\d+)\.(\d+)/ -rOPENBSD_${1}_${2}_BASE/;
$tag = strftime(" -D%FZ%T", gmtime(str2time($date))) if $date;
$tag ||= "AC"; # for step checkouts per date preserve quirk patches
$tag = "$flags$tag" if $flags;
$path = $path ? " $path" : "";
logcmd('ssh', "$user\@$host", "cd /usr/$repo && cvs -qR up -Pd$tag$path");
}
sub diff_cvs {
my ($path) = @_;
$path = $path ? " $path" : "";
my @sshcmd = ('ssh', "$user\@$host",
"cd /usr/src && cvs -qR diff -up$path");
logmsg "Command '@sshcmd' started.\n";
open(my $cvs, '-|', @sshcmd)
or die "Open pipe from '@sshcmd' failed: $!";
open(my $diff, '>', "diff-$host.txt.new")
or die "Open 'diff-$host.txt.new' for writing failed: $!";
local $_;
while (<$cvs>) {
print $diff $_;
}
close($cvs) or do {
die "Close pipe from '@sshcmd' failed: $!" if $!;
# cvs diff returns 0 without and 1 with differences
die "Command '@sshcmd' failed: $?" if $? != 0 && $? != (1<<8);
};
logmsg "Command '@sshcmd' finished.\n";
close($diff)
or die "Close 'diff-$host.txt.new' after writing failed: $!";
rename("diff-$host.txt.new", "diff-$host.txt")
or die "Rename 'diff-$host.txt.new' to 'diff-$host.txt' failed: $!";
}
sub clean_cvs {
my ($path) = @_;
$path = $path ? " $path" : "";
logcmd('ssh', "$user\@$host", "cd /usr/src && cvs -qR up -C$path");
}
sub patch_cvs {
my ($file, $path) = @_;
$path = $path ? "/$path" : "";
logcmd({
cmd => ['ssh', "$user\@$host",
"cd /usr/src$path && patch -ENufF0 -p0"],
infile => $file,
});
}
sub update_ports {
cvs_update("ports", @_);
logcmd('ssh', "$user\@$host", "rm -rf /usr/ports/pobj");
logcmd('ssh', "$user\@$host",
"[ ! -f /usr/ports/infrastructure/mk/bsd.port.subdir.mk ] || ".
"make -C /usr/ports fix-permissions");
}
# make /usr/src
sub make_kernel {
my $version = $sysctl{'kern.version'};
$version =~ m{:[\w/]*/sys/(arch/\w+/compile/GENERIC[\w./]*)$}m
or die "No kernel path in version: $version";
my $path = $1;
my $ncpu = $sysctl{'hw.ncpuonline'};
my $jflag = $ncpu > 1 ? " -j ".($ncpu+1) : "";
logcmd('ssh', "$user\@$host", "cd /usr/src/sys/$path && make config");
logcmd('ssh', "$user\@$host", "cd /usr/src/sys/$path && make clean")
if loggrep(qr/you must run "make clean"/);
logcmd('ssh', "$user\@$host", "cd /usr/src/sys/$path && if [ -s CVS/Tag ]".
"; then echo -n 'cvs : '; cat CVS/Tag; fi >obj/version");
logcmd('ssh', "$user\@$host", "cd /usr/src/sys/$path && ".
"time nice make$jflag bsd");
logcmd('ssh', "$user\@$host", "cd /usr/src/sys/$path && make install");
# disable kernel relinking, load after reboot may change perform result
logcmd('ssh', "$user\@$host", "rm /var/db/kernel.SHA256");
}
sub make_build {
my $ncpu = $sysctl{'hw.ncpuonline'};
my $jflag = $ncpu > 1 ? " -j ".($ncpu+1) : "";
logcmd('ssh', "$user\@$host", "cd /usr/src && time nice make$jflag build");
}
# make relink kernel
sub align_kernel {
my ($src, $dst, $file);
$src = "/usr/src/sys/arch/amd64/compile/GENERIC.MP/obj/Makefile";
$dst = "/usr/share/relink/kernel/GENERIC.MP/Makefile";
$file = "/root/perform/patches/makefile-norandom.diff";
logcmd('ssh', "$user\@$host", "cp $src $dst");
logcmd('ssh', "$user\@$host", "patch -NuF0 -p0 $dst <$file");
$file = "/root/perform/patches/makefile-linkalign.diff";
logcmd('ssh', "$user\@$host", "patch -NuF0 -p0 $dst <$file");
$file = "/usr/share/relink/kernel/GENERIC.MP/ld.script";
logcmd('ssh', "$user\@$host", "rm $file");
$file = "/usr/share/relink/kernel/GENERIC.MP";
logcmd('ssh', "$user\@$host", "make -C $file ld.script");
$src = "/usr/src/sys/conf/makegap.sh";
$dst = "/usr/share/relink/kernel/GENERIC.MP/makegap.sh";
$file = "/root/perform/patches/makegap-norandom.diff";
logcmd('ssh', "$user\@$host", "cp $src $dst");
logcmd('ssh', "$user\@$host", "patch -NuF0 -p0 $dst <$file");
}
sub gap_kernel {
my ($src, $dst, $file);
$src = "/usr/src/sys/arch/amd64/compile/GENERIC.MP/obj/Makefile";
$dst = "/usr/share/relink/kernel/GENERIC.MP/Makefile";
$file = "/root/perform/patches/makefile-norandom.diff";
logcmd('ssh', "$user\@$host", "cp $src $dst");
logcmd('ssh', "$user\@$host", "patch -NuF0 -p0 $dst <$file");
$src = "/usr/src/sys/conf/makegap.sh";
$dst = "/usr/share/relink/kernel/GENERIC.MP/makegap.sh";
logcmd('ssh', "$user\@$host", "cp $src $dst");
}
sub sort_kernel {
my ($src, $dst, $file);
$src = "/usr/src/sys/arch/amd64/compile/GENERIC.MP/obj/Makefile";
$dst = "/usr/share/relink/kernel/GENERIC.MP/Makefile";
$file = "/root/perform/patches/makefile-norandom.diff";
logcmd('ssh', "$user\@$host", "cp $src $dst");
logcmd('ssh', "$user\@$host", "patch -NuF0 -p0 $dst <$file");
$src = "/usr/src/sys/conf/makegap.sh";
$dst = "/usr/share/relink/kernel/GENERIC.MP/makegap.sh";
$file = "/root/perform/patches/makegap-norandom.diff";
logcmd('ssh', "$user\@$host", "cp $src $dst");
logcmd('ssh', "$user\@$host", "patch -NuF0 -p0 $dst <$file");
}
sub reorder_kernel {
my $cksum = "/var/db/kernel.SHA256";
logcmd('ssh', "$user\@$host", "sha256 -h /var/db/kernel.SHA256 /bsd");
logcmd('ssh', "$user\@$host", "cat /var/db/kernel.SHA256");
logcmd('ssh', "$user\@$host", "/usr/libexec/reorder_kernel");
logcmd('ssh', "$user\@$host", "cat /var/db/kernel.SHA256");
logcmd('ssh', "$user\@$host", "rm /var/db/kernel.SHA256");
}
# get name list of kernel, addresses influence performance
sub get_bsdnm {
my @sshcmd = ('ssh', "$user\@$host", 'nm', '/bsd');
logmsg "Command '@sshcmd' started.\n";
open(my $nm, '-|', @sshcmd)
or die "Open pipe from '@sshcmd' failed: $!";
open(my $fh, '>', "nm-bsd-$host.txt.new")
or die "Open 'nm-bsd-$host.txt.new' for writing failed: $!";
print $fh sort <$nm>;
close($nm) or die $! ?
"Close pipe from '@sshcmd' failed: $!" :
"Command '@sshcmd' failed: $?";
logmsg "Command '@sshcmd' finished.\n";
close($fh)
or die "Close 'nm-bsd-$host.txt.new' after writing failed: $!";
rename("nm-bsd-$host.txt.new", "nm-bsd-$host.txt") or
die "Rename 'nm-bsd-$host.txt.new' to 'nm-bsd-$host.txt' failed: $!";
}
1;