summaryrefslogtreecommitdiffstats
path: root/boot-env/OpenSLX/BootEnvironment/Preboot.pm
blob: b06de7d2d9bccfa68506b873b6e5ca926e529c60 (plain) (blame)
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
# Copyright (c) 2008 - OpenSLX GmbH
#
# This program is free software distributed under the GPL version 2.
# See http://openslx.org/COPYING
#
# If you have any feedback please consult http://openslx.org/feedback and
# send your suggestions, praise, or complaints to feedback@openslx.org
#
# General information about OpenSLX can be found at http://openslx.org/
# -----------------------------------------------------------------------------
# BootEnvironment::Preboot.pm
#    - provides general preboot implementation of the BootEnvironment API.
# -----------------------------------------------------------------------------
package OpenSLX::BootEnvironment::Preboot;

use strict;
use warnings;

use base qw(OpenSLX::BootEnvironment::Base);

use Clone qw(clone);
use File::Basename;
use File::Path;

use OpenSLX::Basics;
use OpenSLX::ConfigDB qw(:support);
use OpenSLX::Utils;

sub initialize
{
    my $self   = shift;
    my $params = shift;
    
    return if !$self->SUPER::initialize($params);

    $self->{'original-path'} = "$openslxConfig{'public-path'}/preboot";
    $self->{'target-path'}   = "$openslxConfig{'public-path'}/preboot.new";

    $self->{'requires-default-client-config'} = 0;
        # we do not need a default.tgz since there's always an explicit client
    
    if (!$self->{'dry-run'}) {
        mkpath([$self->{'original-path'}]);
        rmtree($self->{'target-path'});
        mkpath("$self->{'target-path'}/client-config");
    }

    return 1;
}

sub writeBootloaderMenuFor
{
    my $self             = shift;
    my $client           = shift;
    my $externalClientID = shift;
    my $systemInfos      = shift || [];

    $self->_prepareBootloaderConfigFolder() 
        unless $self->{preparedBootloaderConfigFolder};

    my $prebootSystemInfo
        = clone($self->_pickSystemWithNewestKernel($systemInfos));

    $self->_createImages($client, $prebootSystemInfo);

    my $externalClientName   = externalConfigNameForClient($client);
    my $bootloaderPath       = "$self->{'target-path'}/bootloader";
    my $bootloaderConfigPath = "$bootloaderPath/$externalClientName";
    mkpath($bootloaderConfigPath)   unless $self->{'dry-run'};
    my $menuFile = "$bootloaderConfigPath/bootmenu.dialog";
    
    my $clientAppend = $client->{attrs}->{kernel_params_client} || '';
    vlog(1, _tr("writing bootmenu %s", $menuFile));

    # set label for each system
    foreach my $info (@$systemInfos) {
        my $label = $info->{label} || '';
        if (!length($label) || $label eq $info->{name}) {
            $label = $info->{name};
        }
        $info->{label} = $label;
    }
    my $bootmenuEntries = '';
    my $entryState = 'on';
    my $counter = 1;
    foreach my $info (sort { $a->{label} cmp $b->{label} } @$systemInfos) {
        my $vendorOSName = $info->{'vendor-os'}->{name};
        my $kernelName   = basename($info->{'kernel-file'});
        my $append       = $info->{attrs}->{kernel_params} || '';
        $append .= " $clientAppend";
        $bootmenuEntries .= qq{ "$counter" "$info->{label}" };
        $entryState = 'off';

        # create a file containing the boot-configuration for this system
        my $systemDescr = unshiftHereDoc(<<"        End-of-Here");
            label="$info->{label}"
            kernel="$vendorOSName/$kernelName"
            initramfs="$vendorOSName/$info->{'initramfs-name'}"
            append="$append"
        End-of-Here
        my $systemFile = "$bootloaderConfigPath/$info->{name}";
        spitFile(
            $systemFile, $systemDescr, { 'io-layer' => 'encoding(iso8859-1)' } 
        )    unless $self->{'dry-run'};
        slxsystem(qq{ln -sf $info->{name} $bootloaderConfigPath/$counter});
        $counter++;
    }

    my $entryCount = @$systemInfos;
    my $bootmenu = unshiftHereDoc(<<"    End-of-Here");
        --no-cancel --menu "OpenSLX Boot Menu" 20 65 $entryCount $bootmenuEntries
    End-of-Here
    
    if (!$self->{'dry-run'}) {
        # default to iso encoding, let's see how uclibc copes with it ...
        spitFile($menuFile, $bootmenu, { 'io-layer' => 'encoding(iso8859-1)' });

        # copy the preboot script into the folder to be tared
        my $prebootBasePath 
            = "$openslxConfig{'base-path'}/share/boot-env/preboot";
        slxsystem(qq{cp $prebootBasePath/preboot.sh $bootloaderConfigPath/});
        slxsystem(qq{cp -r $prebootBasePath/preboot-scripts $bootloaderConfigPath/});
        slxsystem(qq{chmod a+x $bootloaderConfigPath/preboot.sh});

        # create a tar which can/will be downloaded by prebooting clients
        my $tarCMD 
            = qq{cd $bootloaderConfigPath; tar -czf "${bootloaderConfigPath}.env" *};
        slxsystem($tarCMD);
        rmtree($bootloaderConfigPath);
    }

    return 1;
}

sub _createImages
{
    my $self   = shift;
    my $client = shift;
    my $info   = shift;

    my %mediaMap = (
        'cd'     => 'CD',
    );
    my $prebootMedia = $client->{attrs}->{preboot_media} || '';
    if (!$prebootMedia) {
        warn _tr(
            "no preboot-media defined for client %s, no images will be generated!",
            $client->{name}
        );
        return 0;
    }
    foreach my $mediumName (split m{, }, $prebootMedia) {
        my $moduleName = $mediaMap{$mediumName}
            or die _tr(
                "'%s' is not one of the supported preboot-medias (cd)", 
                $mediumName
            );

        my $prebootMedium = instantiateClass(
            "OpenSLX::BootEnvironment::Preboot::$moduleName"
        );
        $prebootMedium->initialize($self);
        $prebootMedium->createImage($client, $info);
    }
    
    return 1;
}

sub _prepareBootloaderConfigFolder
{
    my $self = shift;
    
    my $bootloaderPath = "$self->{'target-path'}/bootloader";
    if (!$self->{'dry-run'}) {
        rmtree($bootloaderPath);
        mkpath($bootloaderPath);
    }

    $self->{preparedBootloaderConfigFolder} = 1;

    return 1;
}

sub _pickSystemWithNewestKernel
{
    my $self        = shift;
    my $systemInfos = shift;

    my $systemWithNewestKernel;
    my $newestKernelFileSortKey = '';
    foreach my $system (@$systemInfos) {
        next unless $system->{'kernel-file'} =~ m{
            (?:vmlinuz|x86)-(\d+)\.(\d+)\.(\d+)(?:\.(\d+))?-(\d+(?:\.\d+)?)
        }x;
        my $sortKey 
            = sprintf("%02d.%02d.%02d.%02d-%2.1f", $1, $2, $3, $4||0, $5);
        if ($newestKernelFileSortKey lt $sortKey) {
            $systemWithNewestKernel  = $system;
            $newestKernelFileSortKey = $sortKey;
        }
    }

    if (!defined $systemWithNewestKernel) {
        die _tr("unable to pick a system to be used for preboot!");
    }
    return $systemWithNewestKernel;
}

1;