-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathKIWIMiniIsoPlugin.pm
304 lines (280 loc) · 8.58 KB
/
KIWIMiniIsoPlugin.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
################################################################
# Copyright (c) 2014, 2015 SUSE LLC
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program (see the file LICENSE); if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
################################################################
package KIWIMiniIsoPlugin;
use strict;
use warnings;
use base "KIWIBasePlugin";
use Data::Dumper;
use Config::IniFiles;
use File::Find;
use FileHandle;
use Carp;
use File::Basename qw /dirname/;
sub new {
# ...
# Create a new KIWIMiniIsoPlugin object
# ---
my $class = shift;
my $handler = shift;
my $config = shift;
my $configpath;
my $configfile;
my $this = KIWIBasePlugin -> new($handler);
bless ($this, $class);
if ($config =~ m{(.*)/([^/]+)$}x) {
$configpath = $1;
$configfile = $2;
}
if ((! $configpath) || (! $configfile)) {
$this->logMsg("E",
"wrong parameters in plugin initialisation\n"
);
return;
}
## plugin content:
#-----------------
#[base]
# name = KIWIEulaPlugin
# order = 3
# defaultenable = 1
#
#[target]
# targetfile = content
# targetdir = $PRODUCT_DIR
# media = (list of numbers XOR "all")
#
my $ini = Config::IniFiles -> new (
-file => "$configpath/$configfile"
);
my $name = $ini->val('base', 'name');
my $order = $ini->val('base', 'order');
my $enable = $ini->val('base', 'defaultenable');
# if any of those isn't set, complain!
if (not defined($name)
or not defined($order)
or not defined($enable)
) {
$this->logMsg("E",
"Plugin ini file <$config> seems broken!\n"
);
return;
}
$this->name($name);
$this->order($order);
if($enable != 0) {
$this->ready(1);
}
return $this;
}
sub execute {
my $this = shift;
if(not ref($this)) {
return;
}
if($this->{m_ready} == 0) {
return 0;
}
my $repoloc = $this->collect()->productData()->getOpt("REPO_LOCATION");
my $ismini = $this->collect()->productData()->getVar("FLAVOR");
if(not defined($ismini)) {
$this->logMsg("W", "FLAVOR not set?");
return 0;
}
if ($ismini !~ m{mini}i) {
$this->logMsg("I",
"Nothing to do for media type <$ismini>"
);
return 0;
}
my ($srv, $path);
if (not defined($repoloc)) {
$this->logMsg("I",
"<REPO_LOCATION> is unset, boot protocol will be set to 'slp'!"
);
} else {
if ($repoloc =~ m{^https?://([^/]+)/(.+)}x) {
($srv, $path) = ($1, $2);
}
if(not defined($srv) or not defined($path)) {
$this->logMsg("W",
"Parsing repo-location=<$repoloc> failed!"
);
return 0;
}
}
my @rootfiles;
find(
sub { find_cb($this, '.*/root$', \@rootfiles) },
$this->handler()->collect()->basedir()
);
$this->removeRepoData();
my @isolxfiles;
find(
sub { find_cb($this, '.*/isolinux.cfg$', \@isolxfiles) },
$this->handler()->collect()->basedir()
);
$this -> updateInitRDNET("./etc/linuxrc.d/10_repo", "defaultrepo=$repoloc\n");
my @gfxbootfiles;
find(
sub { find_cb($this, '.*/gfxboot\.cfg$', \@gfxbootfiles) },
$this->handler()->collect()->basedir()
);
if (!@gfxbootfiles) {
my $msg = "No gfxboot.cfg file found! "
. "This _MIGHT_ be ok for S/390. "
. "Please verify <installation-images> package(s)";
$this->logMsg("W", $msg);
return 0;
}
return $this -> updateGraphicsBootConfig (
\@gfxbootfiles, $repoloc, $srv, $path
);
}
sub removeRepoData {
my $this = shift;
my $basedir = $this->handler()->collect()->basedir();
$this->logMsg("I", "removing repodata from <$basedir>");
system("find", $basedir, "-name", "repodata", "-a", "-type", "d", "-exec", "rm", "-rv", "{}", ";");
return 0;
}
sub updateGraphicsBootConfig {
my $this = shift;
my $gfxbootfiles = shift;
my $repoloc = shift;
my $srv = shift;
my $path = shift;
foreach my $cfg(@{$gfxbootfiles}) {
$this->logMsg("I", "Processing file <$cfg>: ");
my $F = FileHandle -> new();
if (! $F -> open($cfg)) {
$this->logMsg("E", "Cant open file <$cfg>!");
next;
}
my @lines = <$F>;
$F -> close();
chomp(@lines);
my $install = -1;
my $ihs = -1;
my $ihp = -1;
my $i = -1;
foreach my $line(@lines) {
$i++;
next if $line !~ m{^install}x;
if($line =~ m{^install=.*}x) {
$install = $i;
}
if ($line =~ m{^install.http.server=+}x) {
$ihs = $i;
}
if($line =~ m{^install.http.path=+}x) {
$ihp = $i;
}
}
if($srv) {
# store the repo location but don't change the default install method
if($ihs == -1) {
push @lines, "install.http.server=$srv";
} else {
$lines[$ihs] =~ s{^(install.http.server).*}{$1=$srv}x;
}
if($ihp == -1) {
push @lines, "install.http.path=$path";
} else {
$lines[$ihp] =~ s{^(install.http.path).*}{$1=$path}x;
}
}
unlink $cfg;
if (! $F -> open(">$cfg")) {
$this->logMsg("E", "Cant open file for writing <$cfg>!");
return 1;
}
foreach(@lines) {
print $F "$_\n";
}
$F -> close();
}
return 0;
}
# borrowed from obs with permission from [email protected] to license as
# GPLv2+
sub _makecpiohead {
my ($name, $s) = @_;
return "07070100000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000b00000000TRAILER!!!\0\0\0\0" if !$s;
# magic ino
my $h = "07070100000000";
# mode S_IFREG
$h .= sprintf("%08x", oct(100000) | $s->[2]&oct(777));
# uid gid nlink
$h .= "000000000000000000000001";
$h .= sprintf("%08x%08x", $s->[9], $s->[7]);
$h .= "00000000000000000000000000000000";
$h .= sprintf("%08x", length($name) + 1);
$h .= "00000000$name\0";
$h .= substr("\0\0\0\0", (length($h) & 3)) if length($h) & 3;
my $pad = '';
$pad = substr("\0\0\0\0", ($s->[7] & 3)) if $s->[7] & 3;
return ($h, $pad);
}
# append a config snippet to initrd that instructs linuxrc to use
# download.opensuse.org
# https://bugzilla.opensuse.org/show_bug.cgi?id=916175
sub updateInitRDNET {
my ($this, $file, $content) = @_;
$this -> logMsg("I", "prepare initrd for NET iso");
my $zipper = KIWIGlobals -> instance() -> getKiwiConfig() -> {IrdZipperCommand};
# FIXME: looks like IrdZipperCommand is not configured correctly
# in openSUSE product files to match installation-images so
# hardcode for now
$zipper = "xz --check=crc32";
my ($cpio, $pad) = _makecpiohead($file, [0, 0, oct(644), 1, 0, 0, 0, length($content), 0, 0, 0]);
$cpio .= $content;
$cpio .= $pad if $pad;
$cpio .= _makecpiohead();
my @initrdfiles;
find(
sub { find_cb($this, '.*/initrd$', \@initrdfiles) },
$this->handler()->collect()->basedir()
);
$this -> logMsg("E", "no initrds found!") unless @initrdfiles;
for my $initrd (@initrdfiles) {
$this -> logMsg("I", "updating $initrd");
my $fh = FileHandle -> new();
if (! $fh -> open("|$zipper -c >> $initrd")) {
#if (! $fh -> open(">$initrd.append")) {
croak "Cant launch $zipper for $initrd: $!";
}
print $fh $cpio;
$fh -> close();
}
return;
}
sub find_cb {
my $this = shift;
return if not ref($this);
my $pat = shift;
my $listref = shift;
if(not defined($listref) or not defined($pat)) {
return;
}
if($File::Find::name =~ m{$pat}x) {
push @{$listref}, $File::Find::name;
}
return $this;
}
1;