/
ks.pm
executable file
·1218 lines (1020 loc) · 35.8 KB
/
ks.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ${license-info}
# ${developer-info
# ${author-info}
# ${build-info}
package NCM::Component::ks;
use strict;
use warnings;
use NCM::Component;
use EDG::WP4::CCM::Property;
use EDG::WP4::CCM::Element qw (unescape);
use NCM::Filesystem;
use NCM::Partition qw (partition_compare);
use NCM::BlockdevFactory qw (build);
use FileHandle;
use LC::Exception qw (throw_error);
use Data::Dumper;
use Exporter;
use CAF::FileWriter;
use Sys::Hostname;
use Text::Glob qw(match_glob);
our @ISA = qw (NCM::Component Exporter);
our $EC = LC::Exception::Context->new->will_store_all;
our $this_app = $main::this_app;
# Modules that may be interesting for hooks.
our @EXPORT_OK = qw (ksuserhooks ksinstall_rpm);
# PAN paths for some of the information needed to generate the
# Kickstart.
use constant { KS => "/system/aii/osinstall/ks",
HOSTNAME => "/system/network/hostname",
DOMAINNAME => "/system/network/domainname",
FS => "/system/filesystems/",
PART => "/system/blockdevices/partitions",
REPO => "/software/repositories",
PRESCRIPT => "/system/aii/osinstall/ks/pre_install_script",
PREHOOK => "/system/aii/hooks/pre_install",
PREENDHOOK => "/system/aii/hooks/pre_install_end",
POSTREBOOTSCRIPT => "/system/aii/osinstall/ks/post_reboot_script",
POSTREBOOTHOOK => "/system/aii/hooks/post_reboot",
POSTREBOOTENDHOOK => "/system/aii/hooks/post_reboot_end",
POSTSCRIPT => "/system/aii/osinstall/ks/post_install_script",
POSTHOOK => "/system/aii/hooks/post_install",
ANACONDAHOOK => "/system/aii/hooks/anaconda",
PREREBOOTHOOK => "/system/aii/hooks/pre_reboot",
PKG => "/software/packages/",
ACKURL => "/system/aii/osinstall/ks/ackurl",
ACKLIST => "/system/aii/osinstall/ks/acklist",
CARDS => "/hardware/cards/nic",
SPMAPROXY => "/software/components/spma/proxy",
SPMA => "/software/components/spma",
SPMA_OBSOLETES => "/software/components/spma/process_obsoletes",
ROOTMAIL => "/system/rootmail",
AII_PROFILE => "/system/aii/osinstall/ks/node_profile",
CCM_PROFILE => "/software/components/ccm/profile",
CCM_TRUST => "/software/components/ccm/trust",
CCM_KEY => "/software/components/ccm/key_file",
CCM_CERT => "/software/components/ccm/cert_file",
CCM_CA => "/software/components/ccm/ca_file",
CCM_WORLDR => "/software/components/ccm/world_readable",
CCM_DBFORMAT => "/software/components/ccm/dbformat",
EMAIL_SUCCESS => "/system/aii/osinstall/ks/email_success",
NAMESERVER => "/system/network/nameserver/0",
FORWARDPROXY => "forward",
END_SCRIPT_FIELD => "/system/aii/osinstall/ks/end_script",
BASE_PKGS => "/system/aii/osinstall/ks/base_packages",
DISABLED_REPOS => "/system/aii/osinstall/ks/disabled_repos",
LOCALHOST => hostname(),
ENABLE_SSHD => "enable_sshd",
};
# Base package path for user hooks.
use constant MODULEBASE => "AII::";
use constant USEMODULE => "use " . MODULEBASE;
# use this syslogheader for remote AII scripts logging:
# 190 = local7.info
use constant LOG_ACTION_SYSLOGHEADER => '<190>AII: ';
# awk command to prefix LOG_ACTION_SYSLOGHEADER and
# to insert sleep (usleep by initscripts), throtlles to 40 lines per sec
use constant LOG_ACTION_AWK =>
"awk '{print \"".LOG_ACTION_SYSLOGHEADER."\"\$0; fflush(); system(\"usleep 25000 >& /dev/null\");}'";
# Configuration variable for the osinstall directory.
use constant KSDIROPT => 'osinstalldir';
# Opens the kickstart file and sets its handle as the default.
sub ksopen
{
my ($self, $cfg) = @_;
my $host = $cfg->getElement (HOSTNAME)->getValue;
my $domain = $cfg->getElement (DOMAINNAME)->getValue;
my $ksdir = $this_app->option (KSDIROPT);
$self->debug(3,"Kickstart file directory = $ksdir");
my $ks = CAF::FileWriter->open ("$ksdir/$host.$domain.ks",
mode => 0664, log => $this_app);
select ($ks);
}
# Prints the opening here doc statement for the post-reboot
# script. It's usefult to separate this, as other plug-ins might want
# only the post_reboot script.
sub kspostreboot_hereopen
{
print <<EOF;
cat <<End_Of_Post_Reboot > /etc/rc.d/init.d/ks-post-reboot
EOF
}
# Prints the heredoc closing the KS postreboot statement.
sub kspostreboot_hereclose
{
print <<EOF;
End_Of_Post_Reboot
EOF
}
# Configures the network, allowing both DHCP and static boots.
sub ksnetwork
{
my ($tree, $config) = @_;
if ($tree->{bootproto} eq 'dhcp') {
$this_app->debug (5, "Node configures its network via DHCP");
print "network --bootproto=dhcp\n";
return;
}
my $dev = $config->getElement("/system/aii/nbp/pxelinux/ksdevice")->getValue;
my $fqdn = $config->getElement (HOSTNAME)->getValue . "." .
$config->getElement (DOMAINNAME)->getValue;
return unless $dev =~ m/eth\d+/;
$this_app->debug (5, "Node will boot from $dev");
my $net = $config->getElement("/system/network/interfaces/$dev")->getTree;
unless (exists ($net->{ip})) {
$this_app->error ("Static boot protocol specified ",
"but no IP given to the interface");
return;
}
my $mtu = exists($net->{mtu}) ? "--mtu=$net->{mtu} " : "";
my $gw = '--gateway=';
if (exists($net->{gateway})) {
$gw .= $net->{gateway};
} elsif ($config->elementExists ("/system/network/default_gateway")) {
$gw .= $config->getElement ("/system/network/default_gateway")->getValue;
} else {
# This is a recipe for disaster
# Best guess is that no gateway is needed.
$this_app->debug (5, "No gateway defined for dev $dev and ",
" using static network description.",
"Let's hope everything is reachable through a ",
"direct route.");
$gw='';
print <<EOF;
## No gateway defined for dev $dev and using static network description.
## Lets hope all is reachable through direct route.
EOF
};
my $ns = $config->getElement(NAMESERVER)->getValue;
print <<EOF;
network --bootproto=static --ip=$net->{ip} --netmask=$net->{netmask} $gw --nameserver=$ns --device=$dev --hostname=$fqdn $mtu
EOF
}
# Instantiates and executes the user hooks for a given path.
sub ksuserhooks
{
my ($config, $path) = @_;
return unless $config->elementExists ($path);
$this_app->debug (5, "User defined hooks for $path");
my $el = $config->getElement ($path);
$path =~ m(/system/aii/hooks/([^/]+));
my $method = $1;
while ($el->hasNextElement) {
my $nel = $el->getNextElement;
my $tree = $nel->getTree;
my $nelpath = $nel->getPath->toString;
# Catch bad guys
if ($tree->{module} !~ m/^[_a-zA-Z]\w+$/) {
$this_app->error ("Invalid identifier specified as a hook module. ",
"Skipping");
next;
}
my $modulename = MODULEBASE . $tree->{module};
$this_app->debug (5, "Loading " . $modulename);
eval ("use " . $modulename);
if ($@) {
# Fallback: try without the AII:: prefix
my $orig_error = $@;
$modulename = $tree->{module};
$this_app->debug (5, "Loading " . $modulename);
eval ("use " . $modulename);
# Report the original error message if the fallback failed
throw_error ("Couldn't load module $tree->{module}: $orig_error")
if $@;
}
my $hook = eval ($modulename . "->new");
throw_error ("Couldn't instantiate object of class $tree->{module}")
if $@;
$this_app->debug (5, "Running $tree->{module}->$method");
$hook->$method ($config, $nelpath);
}
}
# Prints to the Kickstart all the non-partitioning directives.
sub kscommands
{
my $config = shift;
my $tree = $config->getElement(KS)->getTree;
my @packages = @{$tree->{packages}};
my $installtype = $tree->{installtype};
if ($installtype =~ /http/) {
my ($proxyhost, $proxyport, $proxytype) = proxy($config);
if ($proxyhost) {
if ($proxyport) {
$proxyhost .= ":$proxyport";
}
if ($proxytype eq "reverse") {
$installtype =~ s{(https?)://([^/]*)/}{$1://$proxyhost/};
}
}
}
print <<EOF;
install
$installtype
text
reboot
timezone --utc $tree->{timezone}
rootpw --iscrypted $tree->{rootpw}
EOF
if (exists $tree->{repo}) {
print "repo $_ \n" foreach @{$tree->{repo}};
}
if ($tree->{enable_sshd}) {
print "sshpw --username=root $tree->{rootpw} --iscrypted \n";
}
if (exists($tree->{logging})) {
print "logging --host=$tree->{logging}->{host} ",
"--port=$tree->{logging}->{port}";
print " --level=$tree->{logging}->{level}" if $tree->{logging}->{level};
print "\n";
if(exists($tree->{logging}->{send_aiilogs}) && $tree->{logging}->{send_aiilogs}) {
# requirement for usleep
push(@packages, 'initscripts');
push(@packages, 'nc') if ($tree->{logging}->{method} eq 'netcat');
}
}
print "bootloader --location=$tree->{bootloader_location}";
print " --driveorder=", join(',', @{$tree->{bootdisk_order}})
if exists $tree->{bootdisk_order} && @{$tree->{bootdisk_order}};
print " --append=\"$tree->{bootloader_append}\""
if exists $tree->{bootloader_append};
print "\n";
if (exists $tree->{xwindows}) {
print "xconfig ";
while (my ($key, $val) = each %{$tree->{xwindows}}) {
if ($key eq "startxonboot") {
print "--$key " if $val;
} else {
print "--$key=$val ";
}
}
print "\n";
} else {
print "skipx\n" unless exists $tree->{xwindows};
}
print "key $tree->{installnumber}\n" if exists $tree->{installnumber};
print "auth ", join(" ", map("--$_", @{$tree->{auth}})), "\n";
print "lang $tree->{lang}\n";
print "langsupport ", join (" ", @{$tree->{langsupport}}), "\n"
if exists $tree->{langsupport} and @{$tree->{langsupport}}[0] ne "none";
print "keyboard $tree->{keyboard}\n";
print "mouse $tree->{mouse}\n" if exists $tree->{mouse};
print "selinux --$tree->{selinux}\n" if exists $tree->{selinux};
print "firewall --", $tree->{firewall}->{enabled}? "enabled":"disabled",
" ";
print "--trusted $_ " foreach @{$tree->{firewall}->{trusted}};
print "--$_ " foreach @{$tree->{firewall}->{services}};
print "--port $_ " foreach @{$tree->{firewall}->{ports}};
print "\n";
ksnetwork ($tree, $config);
print "driverdisk --source=$_\n" foreach @{$tree->{driverdisk}};
print "zerombr yes\n" if $tree->{clearmbr};
if (exists ($tree->{ignoredisk}) &&
scalar (@{$tree->{ignoredisk}})) {
print "ignoredisk --drives=",
join (',', @{$tree->{ignoredisk}}), "\n";
}
## disable and enable services, if any
my @services;
push(@services, "--disabled=".join(',', @{$tree->{disable_service}})) if
(exists($tree->{disable_service}) && @{$tree->{disable_service}});
push(@services, "--enabled=".join(',', @{$tree->{enable_service}})) if
(exists($tree->{enable_service}) && @{$tree->{enable_service}});
print "services ", join (' ', @services), "\n" if (@services);
print "%packages ", join(" ",@{$tree->{packages_args}}), "\n",
join ("\n", @packages), "\n",
$config->getElement(END_SCRIPT_FIELD)->getValue(), "\n";
}
# Writes the mountpoint definitions and LVM and MD settings
sub ksmountpoints
{
my $config = shift;
# Skip the remainder if "/system/filesystems" is undefined
return unless ( $config->elementExists (FS) );
my $tree = $config->getElement(KS)->getTree;
my %ignoredisk;
if (exists ($tree->{ignoredisk}) &&
scalar (@{$tree->{ignoredisk}})) {
foreach my $disk (@{$tree->{ignoredisk}}) {
$ignoredisk{$disk} = 1;
}
}
print <<EOF;
# Mountpoint and block device definition. At this stage, LVM and MD
# settings are defined for Anaconda to be able to use them. All block
# devices (LVM and MD included) are actually created on the %pre
# phase.
EOF
my $fss = $config->getElement (FS);
while ($fss->hasNextElement) {
my $fs = $fss->getNextElement;
my $fstree = NCM::Filesystem->new ($fs->getPath->toString,
$config);
next if (exists $fstree->{block_device}->{holding_dev} &&
exists $ignoredisk{$fstree->{block_device}->{holding_dev}->{devname}});
$this_app->debug (5, "Pre-processing filesystem $fstree->{mountpoint}");
$fstree->print_ks;
}
}
# Prints the code for downloading and executing an user script.
sub ksuserscript
{
my ($config, $path) = @_;
return unless $config->elementExists ($path);
my $url = $config->getElement ($path)->getValue;
$this_app->debug (5, "User defined script to be fetched ",
"from $url for path $path");
print <<EOS;
pushd /root
wget --output-document=userscript $url
chmod +x userscript
./userscript
rm -f userscript
popd
EOS
}
# Takes care of the install phase, where all the fixed directives are
# placed.
sub install
{
my ($self, $config) = @_;
print <<EOF;
# Kickstart generated by AII's ks.pm.
# Do not edit.
#
# AII is part of Quattor, see more information on Quattor and its license
# at http://www.quattor.org
EOF
ksmountpoints ($config);
# User hooks must be included before the commands because the latter
# ends with the %postconfig section
ksuserhooks ($config, ANACONDAHOOK);
kscommands ($config);
}
# Create the action to be taken on the log files
# logfile is the path to the log file
sub log_action {
my ($config, $logfile, $wait_for_network) = @_;
my $tree = $config->getElement(KS)->getTree;
my @logactions;
push(@logactions, "exec >$logfile 2>&1");
my $consolelogging = 1; # default behaviour
if (exists($tree->{logging})) {
$consolelogging = $tree->{logging}->{console} if(exists($tree->{logging}->{console}));
if (exists($tree->{logging}->{send_aiilogs}) && $tree->{logging}->{send_aiilogs}) {
# network must be functional
# (not needed in %pre and %post; we can rely on anaconda for that)
push(@logactions, "wait_for_network $tree->{logging}->{host}")
if ($wait_for_network);
my $method = $tree->{logging}->{method};
my $protocol = $tree->{logging}->{protocol};
my $actioncmd;
if ($method eq 'netcat') {
push(@logactions,'# Send messages to $protocol syslog server via netcat');
# use netcat to log to syslog port
my $nccmd = 'nc';
$nccmd .= ' -u' if ($protocol eq 'udp');
$actioncmd = "| $nccmd $tree->{logging}->{host} $tree->{logging}->{port}";
} elsif ($method eq 'bash') {
push(@logactions,"# Send messages to $protocol syslog server via bash /dev/$protocol");
# use netcat to log to UDP syslog port
# this assumes that the %pre, %post and post-reboot are bash
$actioncmd = "> /dev/$protocol/$tree->{logging}->{host}/$tree->{logging}->{port}";
}
my $action = "(tail -f $logfile | ".LOG_ACTION_AWK." $actioncmd) &";
push(@logactions, $action);
# insert extra sleep to get all started before any output is send
push(@logactions, 'sleep 1');
}
}
if ($consolelogging) {
push(@logactions, '# Make sure messages show up on the serial console',
"tail -f $logfile > /dev/console &");
}
push(@logactions,''); # add trailing newline
return join("\n", @logactions)
}
# Takes care of the pre-install script, in which the
sub pre_install_script
{
my ($self, $config) = @_;
my $logfile = '/tmp/pre-log.log';
my $logaction = log_action($config, $logfile);
print <<EOF;
%pre
# Pre-installation script.
#
# Block devices are created here instead of using Anaconda's
# directives to allow better control. Anaconda doesn't guarantee that
# the devices it creates will have the exact name it is told to. For
# instance, if you define 4 primary partitions, it will create 3
# primary, one extended and your /dev/foo4 will be silently renamed to
# /dev/foo5.
$logaction
echo 'Begin of pre section'
set -x
EOF
print <<'EOF';
# Hack for RHEL 6: force re-reading the partition table
#
# fdisk often fails to re-read the partition table on RHEL 6, so we have to do
# it explicitely. We also have to make sure that udev had enough time to create
# the device nodes.
rereadpt () {
[ -x /sbin/udevadm ] && udevadm settle
# hdparm can still fail with EBUSY without the wait...
sleep 2
hdparm -q -z "$1"
[ -x /sbin/udevadm ] && udevadm settle
# Just in case...
sleep 2
}
# Align the start of a partition
align () {
local disk path n align_sect START ALIGNED
# By passing disk/path/n separately, we don't have to worry about part_prefix
disk="$1"
path="$2"
n="$3"
align_sect="$4"
START=`fdisk -ul $disk | awk '{if ($1 == "'$path'") print $2 == "*" ? $3: $2}'`
ALIGNED=$((($START + $align_sect - 1) / $align_sect * $align_sect))
if [ $START != $ALIGNED ]; then
echo "-----------------------------------"
echo "Aligning $path: old start sector: $START, new: $ALIGNED"
fdisk $disk <<end_of_fdisk
x
b
$n
$ALIGNED
w
end_of_fdisk
rereadpt $disk
fi
}
wipe_metadata () {
local path clear SIZE START
path="$1"
clear="$2"
SIZE=`fdisk -s "$path"`
let START=$SIZE/1024-$clear
dd if=/dev/zero of="$path" bs=1M count=$clear 2>/dev/null
dd if=/dev/zero of="$path" bs=1M seek=$START 2>/dev/null
}
EOF
# Hook handling should come here, to allow NIKHEF to override
# partitioning.
ksuserhooks ($config, PREHOOK);
$self->ksprint_filesystems ($config);
# Is this needed if we are allowing for hooks?
ksuserscript ($config, PRESCRIPT);
ksuserhooks ($config, PREENDHOOK);
my $end = $config->getElement(END_SCRIPT_FIELD)->getValue();
print <<EOF;
# De-activate logical volumes. Needed on RHEL6, see:
# https://bugzilla.redhat.com/show_bug.cgi?id=652417
lvm vgchange -an
echo 'End of pre section'
$end
EOF
}
# Prints the code needed for removing and creating partitions, block
# devices and filesystems
sub ksprint_filesystems
{
my ($self, $config) = @_;
# Skip the remainder if "/system/filesystems" is not defined
return unless ( $config->elementExists (FS) );
my $fss = $config->getElement (FS);
my @filesystems = ();
# Destroy what needs to be destroyed.
my $clear = [];
if ($config->elementExists ("/system/aii/osinstall/ks/clearpart")) {
$clear = $config->getElement ("/system/aii/osinstall/ks/clearpart")->getTree;
}
foreach (@$clear) {
my $disk = build ($config, "physical_devs/".$self->escape($_));
$disk->clearpart_ks;
}
while ($fss->hasNextElement) {
my $fs = $fss->getNextElement;
my $fstree = NCM::Filesystem->new ($fs->getPath->toString,
$config);
$fstree->del_pre_ks;
push (@filesystems, $fstree);
}
# Create what needs to be created.
$fss = $config->getElement (PART);
my @part = ();
while ($fss->hasNextElement) {
my $p = $fss->getNextElement;
my $pt = NCM::Partition->new ($p->getPath->toString,
$config);
push (@part, $pt);
}
# Partitions go first, as of bug #26137
$_->create_pre_ks foreach (sort partition_compare @part);
foreach (sort partition_compare @part) {
$_->align_ks if $_->can('align_ks');
}
$_->create_ks foreach @filesystems;
# Ensure that all LVMs are active before formatting anything, or
# they won't appear during the reinstallation process.
if ($config->elementExists("/system/blockdevices/logical_volumes")) {
print <<EOF;
lvm vgscan --mknodes
lvm vgchange -ay
EOF
}
$_->format_ks foreach @filesystems;
}
# Prints the statements needed to install a given set of RPMs
sub ksinstall_rpm
{
my ($config, @pkgs) = @_;
my $disabled = $config->getElement(DISABLED_REPOS)->getTree();
my $cmd = "yum -c /tmp/aii/yum/yum.conf -y install ";
$cmd .= " --disablerepo=" . join(",", @$disabled) . " " if @$disabled;
print $cmd, join("\\\n ", @pkgs),
"|| fail 'Unable to install packages'\n";
}
sub proxy
{
my ($config) = @_;
my ($proxyhost, $proxyport, $proxytype);
if ($config->elementExists (SPMAPROXY)) {
my $spma = $config->getElement (SPMA)->getTree;
my $proxy_host = $spma->{proxyhost};
my @proxies = split /,/,$proxy_host;
if (scalar(@proxies) == 1) {
# there's only one proxy specified
$proxyhost = $spma->{proxyhost};
} elsif (scalar(@proxies) > 1) {
# optimize by picking the responding server as the proxy
my ($me) = grep { /\b@(LOCALHOST)\b/ } @proxies;
$me ||= $proxies[0];
$proxyhost = $me;
}
if (exists $spma->{proxyport}) {
$proxyport = $spma->{proxyport};
}
if (exists $spma->{proxytype}) {
$proxytype = $spma->{proxytype};
}
}
return ($proxyhost, $proxyport, $proxytype);
}
# Prints the header functions and definitions of the post_reboot
# script.
sub kspostreboot_header
{
my $config = shift;
# TODO is it ok to rename this logfile?
my $logfile = '/root/ks-post-reboot.log';
my $logaction = log_action($config, $logfile, 1);
$logaction =~ s/\$/\\\$/g;
my $hostname = $config->getElement (HOSTNAME)->getValue;
my $domain = $config->getElement (DOMAINNAME)->getValue;
my $fqdn = "$hostname.$domain";
my $rootmail = $config->getElement (ROOTMAIL)->getValue;
print <<EOF;
#!/bin/bash
# Script to run at the first reboot. It installs the base Quattor RPMs
# and runs the components needed to get the system correctly
# configured.
hostname $fqdn
# Function to be called if there is an error in this phase.
# It sends an e-mail to $rootmail alerting about the failure.
fail() {
echo "Quattor installation on $fqdn failed: \\\$1"
sendmail -t <<End_of_sendmail
From: root\@$fqdn
To: $rootmail
Subject: [\\`date +'%x %R %z'\\`] Quattor installation on $fqdn failed: \\\$1
\\`cat $logfile\\`
------------------------------------------------------------
\\`ls -tr /var/log/ncm 2>/dev/null|xargs tail /var/log/spma.log\\`
.
End_of_sendmail
exit 1
}
# Function to be called if the installation succeeds. It sends an
# e-mail to $rootmail alerting about the installation success.
success() {
echo "Quattor installation on $fqdn succeeded"
sendmail -t <<End_of_sendmail
From: root\@$fqdn
To: $rootmail
Subject: [\\`date +'%x %R %z'\\`] Quattor installation on $fqdn succeeded
Node $fqdn successfully installed.
.
End_of_sendmail
}
# Wait for functional network up by testing DNS lookup via nslookup.
wait_for_network () {
# Wait up to 2 minutes until the network comes up
i=0
while ! nslookup \\\$1 > /dev/null
do
sleep 1
let i=\\\$i+1
if [ \\\$i -gt 120 ]
then
fail "Network does not come up (nslookup \\\$1)"
fi
done
}
# Ensure that the log file doesn't exist.
[ -e $logfile ] && \\
fail "Last installation went wrong. Aborting. See logfile $logfile."
$logaction
echo 'Begin of ks-post-reboot'
set -x
wait_for_network $fqdn
EOF
}
sub ksquattor_config
{
my $config = shift;
# Hack to prevent ncm-network failing when the order of interfaces does not
# match its expectations
my $clear_netcfg = 0;
if ($config->elementExists("/system/network/set_hwaddr") &&
$config->getElement("/system/network/set_hwaddr")->getValue &&
$config->elementExists("/system/aii/nbp/pxelinux/ksdevicemode") &&
$config->getElement("/system/aii/nbp/pxelinux/ksdevicemode")->getValue eq 'mac') {
$clear_netcfg = 1;
}
print <<EOF;
# SPMA transactions may open HUGE amounts of files at the same time.
ulimit -n 8192
cat <<End_Of_CCM_Conf > /etc/ccm.conf
# Base configuration for CCM
# Generated by AII Kickstart Generator
EOF
if ($config->elementExists (AII_PROFILE)) {
print "profile ", $config->getElement (AII_PROFILE)->getValue, "\n";
} else {
print "profile ", $config->getElement (CCM_PROFILE)->getValue, "\n";
}
print "key_file ", $config->getElement (CCM_KEY)->getValue, "\n"
if $config->elementExists (CCM_KEY);
print "cert_file ", $config->getElement (CCM_CERT)->getValue, "\n"
if $config->elementExists (CCM_CERT);
print "ca_file ", $config->getElement (CCM_CA)->getValue, "\n"
if $config->elementExists (CCM_CA);
print "world_readable ", $config->getElement (CCM_WORLDR)->getValue, "\n"
if $config->elementExists (CCM_WORLDR);
print "dbformat ", $config->getElement (CCM_DBFORMAT)->getValue, "\n"
if $config->elementExists (CCM_DBFORMAT);
print "trust ", $config->getElement (CCM_TRUST)->getValue, "\n"
if $config->elementExists (CCM_TRUST);
print "End_Of_CCM_Conf\n";
print <<EOF;
/usr/sbin/ccm-initialise || fail "CCM initialization failed with code \\\$?"
/usr/sbin/ccm-fetch || fail "ccm-fetch failed with code \\\$?"
# we want nscd running in case the NSS config
# gets modified (ncm-ncd will be bound to nscd for
# lookups and therefore an nscd bounce will cause
# ncm-ncd to immediately notice the changes, which
# means that components that rely on valid data in
# nss will work)
service nscd start
sleep 5 # give nscd time to initialize
EOF
if ($clear_netcfg) {
print <<EOF;
rm -f /etc/udev.d/rules.d/70-persistent-net.rules
rm -f /etc/sysconfig/network-scripts/ifcfg-eth*
EOF
}
print <<EOF;
/usr/sbin/ncm-ncd --verbose --configure spma || fail "ncm-ncd --configure spma failed"
/usr/sbin/ncm-ncd --verbose --configure --all
EOF
}
sub kspostreboot_tail
{
my $config = shift;
ksuserscript ($config, POSTREBOOTSCRIPT);
print "\nsuccess\n" if $config->elementExists (EMAIL_SUCCESS) &&
$config->getElement (EMAIL_SUCCESS)->getTree;
print <<EOF;
rm -f /etc/rc.d/rc3.d/S86ks-post-reboot
echo 'End of ks-post-reboot'
sleep 1
shutdown -r now
EOF
}
# Prints the post_reboot script.
sub post_reboot_script
{
my ($self, $config) = @_;
kspostreboot_header ($config);
ksuserhooks ($config, POSTREBOOTHOOK);
ksquattor_config ($config);
ksuserhooks ($config, POSTREBOOTENDHOOK);
kspostreboot_tail ($config);
}
# The way Anaconda handles the bootloader and MBR can screw software
# RAID. This fixes it.
sub ksfix_grub
{
print <<EOF;
BOOT_ARRAY=`df /boot | awk '/dev/{print \$1}'`
if [ -n "\$BOOT_ARRAY" ] ; then
# Select only active disks (skip spares)
case "\$BOOT_ARRAY" in
/dev/md* )
level=`mdadm --query \$BOOT_ARRAY|awk '/raid/ {print \$3}'`
DISKS=`mdadm --query --detail \$BOOT_ARRAY | \\
awk '/active sync/{print \$7}'| \\
sed 's!/dev/!!g
s/,/ /g
s/[0-9]//g'`
for d in \$DISKS
do
dpart=`mdadm --query --detail \$BOOT_ARRAY | \\
awk '/dev\\/'\$d'[0-9]/ {print \$7}'| \\
sed 's!/dev/'\$d'!!g'`
dpart=`expr \$dpart - 1`
eval PART_\$d=\$dpart
done
;;
/dev/sd*|/dev/hd*)
DISKS=`echo "\$BOOT_ARRAY" | \\
sed 's!/dev/!!g
s/,/ /g
s/[0-9]//g'`
dpart=`echo "\$BOOT_ARRAY" | sed 's!/dev/'\$DISKS'!!g'`
dpart=`expr \$dpart - 1`
eval PART_\$DISKS=\$dpart
;;
* )
DISKS=""
;;
esac
i=0
for d in \$DISKS
do
eval dpart=\\\$PART_\$d
echo bootspec setting grub on /dev/\$d: to hd\$i,\$dpart
cat <<EOGRUB | /sbin/grub --batch
device (hd\$i) /dev/\$d
root (hd\$i,\$dpart)
setup (hd\$i)
quit
EOGRUB
# For raid1, hd number must be different for each member
if [ -n "\$level" -a "\$level" = "raid1" ]
then
i=`expr \$i + 1`
fi
done
fi
# remove splash image as it breaks on Thumpers and is otherwise useless
sed -i '/splashimage/d' /boot/grub/grub.conf
# Restrict the ability to boot from only the first disks (hd0,
# hd1). Thumpers boot from sdac or sdy, but those are GRUB's hd0 and
# hd1.
perl -pi -e 's/hd(\\d+)/"hd".(\$1 > 1 ? 0:\$1)/e' /boot/grub/grub.conf
EOF
}
sub yum_setup
{
my ($self, $config) = @_;
my $obsoletes = $config->getElement (SPMA_OBSOLETES)->getTree();
my $repos = $config->getElement (REPO)->getTree();
print <<EOF;
mkdir -p /tmp/aii/yum/repos
cat <<end_of_yum_conf > /tmp/aii/yum/yum.conf
[main]
cachedir=/var/cache/yum/\$basearch/\$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
gpgcheck=1
plugins=1
installonly_limit=3
clean_dependencies_on_remove=1
reposdir=/tmp/aii/yum/repos
obsoletes=$obsoletes
end_of_yum_conf
cat <<end_of_repos > /tmp/aii/yum/repos/aii.repo
EOF
my ($phost, $pport, $ptype) = proxy($config);
foreach my $repo (@$repos) {
if ($ptype && $ptype eq 'reverse') {
$repo->{protocols}->[0]->{url} =~ s{://[^/]*}{://$phost:$pport};
}
print <<EOF;
[$repo->{name}]
enabled=1
baseurl=$repo->{protocols}->[0]->{url}
name=$repo->{name}
gpgcheck=0
skip_if_unavailable=1
EOF
if ($ptype && $ptype eq 'forward') {
print <<EOF;
proxy=http://$phost:$pport/
EOF
}
if (exists($repo->{priority})) {
print <<EOF;
priority=$repo->{priority}
EOF
}
}
print "end_of_repos\n";
}
sub process_pkgs
{
my ($self, $pkg, $ver) = @_;
my @ret;
if (%$ver) {
while (my ($version, $arch) = each(%$ver)) {