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
|
#!/usr/bin/env perl
use v5.40.0;
use strict;
use warnings;
use File::Basename qw/dirname/;
use File::pushd qw/pushd/;
use Getopt::Long;
use YAML::XS;
my $help;
my $ebuild_tree;
my $spec;
my $suffix;
my $jobs = 4;
my $skip_initial_setup;
Getopt::Long::Configure( "bundling", "no_ignore_case" );
GetOptions(
'repo|r=s' => \$ebuild_tree,
'spec|s=s' => \$spec,
'suffix|S=s' => \$suffix,
'jobs|j=s' => \$jobs,
'skip-initial-setup' => \$skip_initial_setup,
'help' => \$help
);
if ( !$help ) {
$spec or warn 'no spec file';
$ebuild_tree or warn 'no ebuild tree';
}
if ( $help || !$spec || !$ebuild_tree ) {
print <<'EOF';
perl scripts/build.pl [-h|--help] <-r|--repo> <ebuild tree> <-s|--spec> <spec file>
Options:
<-r|--repo <ebuild tree>>: Select the ebuild repository
<-s|--spec <spec file>>: Select spec file
[-S|--suffix <generated stage suffix>]: Select spec file
[-j|--jobs <number of jobs>]: Select number of jobs
[--skip-initial-setup]: Avoids creating the ebuild tree and uncompressing the source stage for faster debugging
[-h|--help]: Show this help.
EOF
exit 0;
}
$suffix = $suffix // `date -u +%Y%m%dT%H%M%SZ`;
chomp $suffix;
die 'Now try it as root' if $< != 0;
my $algabuild_dir = '/var/tmp/algabuild';
my $tmp = "$algabuild_dir/tmp";
system mkdir => -pv => $tmp;
my $stage_dir = "$algabuild_dir/stages";
system mkdir => -pv => $stage_dir;
die "$spec does not exist" if !-e $spec;
my $yaml = YAML::XS::LoadFile($spec);
my $repo_name = $yaml->{repo_name} // 'gentoo';
my $prefix = $yaml->{prefix} or die 'No stage prefix in yaml';
my $stage_name = $prefix . $suffix;
my $source = $yaml->{source} or die 'No source stage in yaml';
my $profile = $yaml->{profile} or die 'No profile in yaml';
my $rebuild_all = $yaml->{rebuild_all};
my $source_stage = "$stage_dir/$source";
die "$source_stage does not exist" if !-e $source_stage;
my %image_types = (
iso => sub {
say 'Creating iso';
create_iso();
},
stage => sub {
say 'Creating stage';
create_stage();
},
binpkg => sub {
say 'Creating binpkgs for the whole system';
create_binpkgs();
},
);
my $image_type = $yaml->{image_type} // 'stage';
my $callback = $image_types{$image_type};
if ( !defined $callback ) {
die "$image_type image_type not recognized";
}
$callback->();
sub generate_root_tmp {
my $tmp_new_stage = shift;
if ( !$skip_initial_setup ) {
system qw{tar -C}, $tmp_new_stage, '-xvpf', $source_stage,
qw{--numeric-owner --xattrs-include=*.*};
system qw{rsync --exclude=.git -a -P}, "$ebuild_tree/",
"$tmp_new_stage/var/db/repos/$repo_name/";
}
system qw{cp -Lv /etc/resolv.conf}, "$tmp_new_stage/etc";
if ( $yaml->{repos_conf} ) {
open my $fh, '>', "$tmp_new_stage/usr/share/portage/config/repos.conf"
or die 'Unable to write repos.conf';
print $fh $yaml->{repos_conf};
close $fh;
}
}
sub prepare_root {
my $commands = $yaml->{commands};
if ( defined $commands && ref $commands ne 'ARRAY' ) {
die "List of commands is not an array";
}
local $ENV{MAKEOPTS} = "-j$jobs";
my $link_profile = `readlink /etc/portage/make.profile`;
chomp $link_profile;
if ( $link_profile && $link_profile =~ s/gentoo/$repo_name/g ) {
if ( system rm => -v => '/etc/portage/make.profile' ) {
die 'Unable to delete profile';
}
if ( system ln => -sv => $link_profile => '/etc/portage/make.profile' )
{
die "Unable to link profile to $link_profile";
}
if ( system qw{emerge -uUDN @world @system} ) {
die 'Unable to finish previous profile';
}
if ( system qw{emerge @preserved-rebuild} ) {
die 'Unable to finish previous profile';
}
my $profile_for_grep = $profile =~ s/^[^\/]*://r;
if ( !system qw{grep -R clang},
"/var/db/repos/$repo_name/profiles/$profile_for_grep" )
{
install_clang();
}
}
if ( system qw{eselect profile set}, $profile ) {
die "Could not set profile $profile";
}
my $is_binpkg = $yaml->{image_type} eq 'binpkg';
if ($rebuild_all || $is_binpkg) {
die "Could not build the system"
if system( qw{emerge -e --with-bdeps=y @world @system},
$is_binpkg ? ('--buildpkg') : () );
}
else {
if ( $yaml->{break_circular} ) {
local $ENV{USE} = $yaml->{break_circular}{uses};
my $tmp_use_file =
'/etc/portage/package.use/999-temporal-cycle-breaker';
open my $fh, '>', $tmp_use_file;
say $fh $yaml->{break_circular}{package_use};
$fh->flush;
install_clang_if_needed();
die "Could not build the system"
if system qw{emerge --noreplace --with-bdeps=y @world @system};
die "Could not build the system"
if system qw{emerge -uUDN --with-bdeps=y @world @system};
if ( system rm => $tmp_use_file ) {
die 'Failed to delete tmp cycle breaker package.use file';
}
}
install_clang_if_needed();
die "Could not build the system"
if system qw{emerge --noreplace --with-bdeps=y @world @system};
die "Could not build the system"
if system qw{emerge -uUDN --with-bdeps=y @world @system};
}
die "Could not set the default editor"
if system qw{emerge --noreplace vim};
die "Could not set the default editor"
if system qw{eselect editor set vim};
system qw{emerge --depclean --with-bdeps=y};
my @commands = @{ $commands // [] };
for my $command (@commands) {
say "Running command $command";
if ( system $command ) {
die "($command) failed with exit code not 0";
}
}
system qw{rm /etc/resolv.conf};
system qw{touch /success};
}
sub create_binpkgs {
my $tmp_new_stage = "$tmp/$stage_name";
my $rootfs_path = "$tmp_new_stage-root";
system qw{mkdir -pv}, $rootfs_path;
generate_root_tmp($rootfs_path);
mount_eval(
$rootfs_path,
sub {
prepare_root();
}
);
finish_root($rootfs_path);
if (system qw{rsync --mkpath -a -P}, "$rootfs_path/var/cache/binpkgs/", "$stage_dir/$stage_name/") {
die 'Copying binpkgs failed';
}
}
sub create_iso {
my $tmp_new_stage = "$tmp/$stage_name";
my $rootfs_path = "$tmp_new_stage-rootfs";
system qw{mkdir -pv}, $rootfs_path;
my $contents_path = "$tmp_new_stage-contents";
system qw{mkdir -pv}, $contents_path;
generate_root_tmp($rootfs_path);
mount_eval(
$rootfs_path,
sub {
prepare_root();
}
);
finish_root($rootfs_path);
system qw{rm}, "$contents_path/rootfs.squashfs";
system qw{cp -v}, $source_stage, $rootfs_path;
if (system qw{mksquashfs}, $rootfs_path, "$contents_path/rootfs.squashfs") {
die "Couldn't generate squashfs";
}
my $grub_dir = "$contents_path/boot/grub";
system qw{mkdir -pv}, $grub_dir;
open my $fh, '>', "$grub_dir/grub.cfg";
my ($kver) = glob("$rootfs_path/boot/kernel-*");
die "No kernel found in $rootfs_path/boot\n" unless $kver;
$kver =~ s{.*/kernel-}{};
say $fh <<"EOF";
set timeout=5
set default=0
menuentry "AlgaOS" {
linux /boot/kernel-$kver root=live:LABEL=ALGAOS rd.live.dir=/ rd.live.squashimg=rootfs.squashfs rd.live.overlay.overlayfs=1 rd.live.debug=1 rd.systemd.show_status=1 rd.systemd.log_level=debug quiet splash
initrd /boot/initramfs-$kver.img
};
EOF
if (system "cp -v $rootfs_path/boot/kernel* $contents_path/boot/") {
die 'Unable to copy kernel';
}
if (system "cp -v $rootfs_path/boot/initramfs* $contents_path/boot/") {
die 'Unable to copy initramfs';
}
if (system qw{grub-mkrescue -iso-level 3 -volid ALGAOS -o}, "$stage_dir/$stage_name.iso", "$contents_path") {
die 'Unable to create ISO with grub-mkrescue"';
}
}
sub create_stage {
my $tmp_new_stage = "$tmp/$stage_name";
system mkdir => -pv => $tmp_new_stage;
generate_root_tmp($tmp_new_stage);
mount_eval(
$tmp_new_stage,
sub {
prepare_root();
}
);
finish_root($tmp_new_stage);
system qw{tar -C}, $tmp_new_stage, qw{-cvJf},
"$stage_dir/$stage_name.tar.xz",
qw{--numeric-owner --xattrs-include=*.* .};
}
sub finish_root {
my $tmp_new_stage = shift;
die "System not marked successful" if !-e "$tmp_new_stage/success";
for my $transfer (@{$yaml->{transfer} // []}) {
my ($user, $group, $source, $dest) = @$transfer;
if (system qw{rsync -P -a}, "--chown=$user:$group", qw{--mkpath}, $source, "$tmp_new_stage/$dest") {
die "Transfer failed $source -> $dest";
}
}
system rm => "$tmp_new_stage/success";
system "rm -rf $tmp_new_stage/var/cache/distfiles/*";
system "rm -rf $tmp_new_stage/var/db/repos/*";
}
sub install_clang_if_needed {
my $new_packages = `emerge -pv --noreplace --with-bdeps=y \@world`;
if ( $new_packages =~ /spidermonkey|firefox/ ) {
install_clang();
}
}
sub install_clang {
if ( system qw{emerge --noreplace llvm-core/clang} ) {
die "Failed to install clang";
}
}
sub mount_eval {
my $tmp_new_stage = shift;
my $callback = shift;
my $chdir = pushd $tmp_new_stage;
local $SIG{INT} = 'ignore';
system qw{mount -t proc proc proc};
system qw{mount -t sysfs sysfs sys};
system qw{mount --rbind /dev dev};
system qw{mount --make-rslave dev};
system qw{mount --make-rslave sys};
system qw{mount --make-rslave proc};
my $pid = fork;
if ( !$pid ) {
chroot $tmp_new_stage;
chdir '/';
eval { $callback->(); };
if ($@) {
warn "error: $@";
exit 1;
}
exit 0;
}
waitpid $pid, 0;
sleep 5;
system 'umount -R dev sys proc';
if ( $? == -1 ) {
die "waitpid failed: $!";
}
if ( $? & 127 ) {
die "child died from signal " . ( $? & 127 );
}
my $exit = $? >> 8;
die "child exited with status $exit" if $exit != 0;
}
|