forked from metan-ucw/runltp-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install_pkg.pm
executable file
·362 lines (280 loc) · 7.08 KB
/
install_pkg.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/usr/bin/env perl
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (c) 2017-2021 Cyril Hrubis <chrubis@suse.cz>
# Copyright (c) 2021 Petr Vorel <pvorel@suse.cz>
#
# Linux Test Project test runner
package install_pkg;
use strict;
use warnings;
sub foo_to_pkg
{
my ($foo, $distro) = @_;
my %pkg_map = (
'git' => 'git',
'unzip' => 'unzip',
'autoconf' => 'autoconf',
'automake' => 'automake',
'pkg-config' => 'pkg-config',
'make' => 'make',
'gcc' => 'gcc',
'bc' => 'bc',
# mkfs.foo
'dosfstools' => 'dosfstools',
'xfsprogs' => 'xfsprogs',
'e2fsprogs' => 'e2fsprogs',
'btrfsprogs' => 'btrfsprogs',
'btrfsprogs-alpine' => 'btrfs-progs',
'btrfsprogs-debian' => 'btrfs-progs',
'btrfsprogs-fedora' => 'btrfs-progs',
# FS quota tools
'quota' => 'quota',
'quota-alpine' => 'quota-tools',
# NFS tools
'nfs-utils' => 'nfs-utils',
'nfs-utils-debian' => 'nfs-kernel-server',
# kernel devel
'kernel-devel' => 'kernel-devel',
'kernel-devel-alpine' => 'linux-headers',
'kernel-devel-debian' => 'linux-headers-`uname -r`',
# devel libs
'libacl-devel-alpine' => 'acl-dev',
'libaio-devel-alpine' => 'libaio-dev',
'libattr-devel-alpine' => 'attr-dev',
'libcap-devel-alpine' => 'libcap-dev',
'libnuma-devel-alpine' => 'numactl-dev',
'pkg-config-alpine' => 'pkgconf',
'libaio-devel-debian' => 'libaio-dev',
'libacl-devel-debian' => 'libacl1-dev',
'libattr-devel-debian' => 'libattr1-dev',
'libcap-devel-debian' => 'libcap-dev',
'libnuma-devel-debian' => 'libnuma-dev',
'libaio-devel-fedora' => 'libaio-devel',
'libacl-devel-fedora' => 'libacl-devel',
'libattr-devel-fedora' => 'libattr-devel',
'libcap-devel-fedora' => 'libcap-devel',
'libnuma-devel-fedora' => 'numactl-devel',
'libaio-devel-suse' => 'libaio-devel',
'libacl-devel-suse' => 'libacl-devel',
'libattr-devel-suse' => 'libattr-devel',
'libcap-devel-suse' => 'libcap-devel',
'libnuma-devel-suse' => 'libnuma-devel',
);
my $pkg = $pkg_map{"$foo-$distro"};
return $pkg if defined $pkg;
return $pkg_map{"$foo"};
}
my @distros = qw(alpine debian fedora suse);
sub detect_distro
{
my ($self) = @_;
for my $distro (@distros) {
if (utils::run_cmd_retry($self, "grep -q $distro /etc/os-release") == 0) {
return $distro;
}
}
print("Unknown distribution!\n");
return;
}
sub pkg_to_m32
{
my ($distro, $pkg_name) = @_;
if ($distro eq "debian") {
#TODO: we need architecture detection for now default to i386
return "$pkg_name:i386";
}
return "$pkg_name-32bit" if ($distro eq "suse");
return;
}
sub setup_m32
{
my ($distro) = @_;
if ($distro eq "debian") {
return ("dpkg --add-architecture i386");
}
return;
}
sub map_pkgs
{
my ($foos, $distro, $m32) = @_;
$foos = [$foos] unless (ref($foos) eq 'ARRAY');
my @pkgs = map { foo_to_pkg($_, $distro) } @{$foos};
@pkgs = map { pkg_to_m32($distro, $_) } @pkgs if ($m32);
return \@pkgs;
}
sub install_pkg
{
my ($distro, $pkgs) = @_;
if ($distro eq "alpine") {
return 'apk add ' . join(' ', @$pkgs);
}
if ($distro eq 'debian') {
return 'apt-get install -y ' . join(' ', @$pkgs);
}
if ($distro eq 'fedora') {
return 'yum install -y ' . join(' ', @$pkgs);
}
if ($distro eq 'suse') {
return 'zypper --non-interactive --ignore-unknown in ' . join(' ', @$pkgs);
}
}
sub update_pkg_db
{
my ($distro) = @_;
if ($distro eq "alpine") {
return "apk update";
}
if ($distro eq "debian") {
return "apt-get update";
}
if ($distro eq "fedora") {
return "yum update -y";
}
if ($distro eq "suse") {
return "zypper --non-interactive ref";
}
return;
}
sub add_build_pkgs
{
my ($required_pkgs) = @_;
# Attempt to install required packages
my @build_pkgs = ('make',
'autoconf',
'automake',
'pkg-config',
'gcc');
push(@$required_pkgs, @build_pkgs);
# We need at least one to get the sources
push(@$required_pkgs, 'git');
push(@$required_pkgs, 'unzip');
# Kernel devel so that we can build modules
push(@$required_pkgs, ('kernel-devel'));
}
sub add_runtime_pkgs
{
my ($required_pkgs) = @_;
# We need mkfs.foo at runtime
my @mkfs = (
'dosfstools',
'xfsprogs',
'e2fsprogs',
'btrfsprogs',
);
push(@$required_pkgs, @mkfs);
# Debian does not install bc by default!
push(@$required_pkgs, 'bc');
# FS quota tests needs quota tools
push(@$required_pkgs, ('quota'));
# NFS tests needs exportfs
push(@$required_pkgs, ('nfs-utils'));
}
sub get_runtime_pkgs
{
my ($pkgs, $distro) = @_;
my $run_pkgs = [];
add_runtime_pkgs($run_pkgs);
$run_pkgs = map_pkgs($run_pkgs, $distro);
push(@$pkgs, @$run_pkgs);
}
sub add_devel_libs
{
my ($required_pkgs) = @_;
# Attempt to install devel libraries
my @devel_libs = (
'libaio-devel',
'libacl-devel',
'libattr-devel',
'libcap-devel',
'libnuma-devel'
);
push(@$required_pkgs, @devel_libs);
}
sub get_build_pkgs
{
my ($pkgs, $distro, $m32) = @_;
my $foos = [];
add_build_pkgs($foos);
add_devel_libs($foos);
$foos = map_pkgs($foos, $distro);
push(@$pkgs, @$foos);
if ($m32) {
my $pkgs32 = [];
add_devel_libs($pkgs32);
push(@$pkgs32, 'gcc');
$pkgs32 = map_pkgs($pkgs32, $distro, $m32);
push(@$pkgs, @$pkgs32);
}
return $pkgs;
}
sub get_install_cmds
{
my ($pkgs, $distro, $m32) = @_;
my @cmds = ();
push(@cmds, setup_m32($distro)) if $m32;
push(@cmds, update_pkg_db($distro));
push(@cmds, install_pkg($distro, $pkgs));
return \@cmds;
}
sub install_ltp_pkgs
{
my ($self, $m32) = @_;
my $distro = detect_distro($self);
return unless defined($distro);
my $pkgs = [];
get_build_pkgs($pkgs, $distro, $m32);
get_runtime_pkgs($pkgs, $distro);
my $cmds = get_install_cmds($pkgs, $distro, $m32);
my @results;
if (utils::run_cmds_retry($self, $cmds, results => \@results) != 0) {
my $last = $results[$#results];
printf("Failed command: %s\n output:\n%s\n",
$last->{cmd}, join("\n ", @{$last->{log}}));
return $last->{ret};
}
return 0;
}
use Getopt::Long;
if (not caller())
{
my $distro;
my $m32;
my $build;
my $run;
my $help;
my $cmd;
GetOptions(
"distro=s" => \$distro,
"m32" => \$m32,
"build" => \$build,
"run" => \$run,
"cmd" => \$cmd,
"help" => \$help,
) or die("Error in argument parsing!\n");
if ($help) {
print("Usage\n-----\n\n");
print("install_pkg.pm --distro DISTRO {at least one of: --build, --run} [--cmd] [--m32]\n\n");
print("install_pkg.pm -h|--help\n\n");
print("Options\n-------\n\n");
print("--help : Print this help\n\n");
print("--distro : Distribution name: " . join(' ', @distros) . "\n\n");
print("--m32 : Include 32bit build dependencies\n\n");
print("--build : Include build dependencies\n\n");
print("--run : Include runtime dependencies\n\n");
print("--cmd : Print command line instead of package list\n\n");
exit 0;
}
die("No distribution selected!\n") unless $distro;
die("Unsupported distribution!\n") unless grep(/^$distro$/i, @distros);
die("No packages selected!\n") unless $build or $run;
my $pkgs = [];
get_build_pkgs($pkgs, $distro, $m32) if $build;
get_runtime_pkgs($pkgs, $distro) if $run;
if ($cmd) {
my $cmds = get_install_cmds($pkgs, $distro, $m32);
print "$_\n" for (@$cmds);
} else {
print(join(' ', @$pkgs) . "\n");
}
}
1;