summaryrefslogtreecommitdiffstats
path: root/src/installer/OpenSLX/OSExport/FileSystem/SquashFS.pm
blob: 77034c9c7f8aaa140883c7e33f073c5801bb6e46 (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
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
# Copyright (c) 2006, 2007 - 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/
# -----------------------------------------------------------------------------
# SquashFS.pm
#    - provides SquashFS-specific overrides of the OpenSLX::OSExport::ExportType
#      API.
# -----------------------------------------------------------------------------
package OpenSLX::OSExport::FileSystem::SquashFS;

use strict;
use warnings;

use base qw(OpenSLX::OSExport::FileSystem::Base);

use File::Basename;
use OpenSLX::Basics;
use OpenSLX::ConfigDB qw(:support);
use OpenSLX::OSExport::FileSystem::Base 1;
use OpenSLX::Utils;

################################################################################
### interface methods
################################################################################
sub new
{
    my $class = shift;
    my $self = {'name' => 'sqfs',};
    return bless $self, $class;
}

sub initialize
{
    my $self        = shift;
    my $engine      = shift;
    my $blockDevice = shift || confess('need to pass in block-device!');

    $self->SUPER::initialize($engine);

    $self->{'block-device'} = $blockDevice;
    my $exportBasePath = "$openslxConfig{'public-path'}/export";
    $self->{'export-path'} = "$exportBasePath/sqfs/$engine->{'vendor-os-name'}";
    return;
}

sub exportVendorOS
{
    my $self   = shift;
    my $source = shift;

    my $includeExcludeList = $self->_determineIncludeExcludeList();
    # in order to do the filtering as part of mksquashfs, we need to map
    # our internal (rsync-)filter format to regexes:
    $includeExcludeList =
      $self->_mapRsyncFilter2Regex($source, $includeExcludeList);
    vlog(1, _tr("using include-exclude-filter:\n%s\n", $includeExcludeList));
    my $target = $self->{'export-path'};

    my $sourceTime = (stat($source))[9] || 0;
    my $targetTime = (stat($target))[9] || 0;
    vlog(2, "source-time=$sourceTime target-time=$targetTime");

    if ($targetTime && $sourceTime < $targetTime) {
        vlog(
            0,
            "!!! creation of squashfs skipped, as vendor-OS hasn't changed since last export!\n" 
              . "!!! Use 'touch $source' to force an export."
        );
    } else {
        $self->_createSquashFS($source, $target, $includeExcludeList);
    }
    $self->_addBlockDeviceTagToExport($target);
    return;
}

sub purgeExport
{
    my $self = shift;

    my $target = $self->{'export-path'};
    if ($self->_removeBlockDeviceTagFromExport($target)) {
        # no more tags, we can remove the image:
        if (slxsystem("rm $target")) {
            vlog(0, _tr("unable to remove export '%s'!", $target));
            return 0;
        }
    }
    return 1;
}

sub checkRequirements
{
    my $self         = shift;
    my $vendorOSPath = shift;

    # determine most appropriate kernel version ...
    my $kernelVer = $self->_pickKernelVersion($vendorOSPath);

    # ... and check if that kernel-version provides all the required modules
    my @blockModNames = $self->{'block-device'}->requiredBlockDeviceModules();
    foreach my $blockModName (@blockModNames) {
        my $blockMod =
          $self->_locateKernelModule($vendorOSPath, "$blockModName.ko",
            ["$vendorOSPath/lib/modules/$kernelVer/kernel/drivers/block"]);
        if (!defined $blockMod) {
            warn _tr(
                "unable to find blockdevice-module '%s' for kernel version '%s'.",
                $blockModName, $kernelVer
            );
            return;
        }
    }
    my $squashfsMod = $self->_locateKernelModule(
        $vendorOSPath,
        'squashfs.ko',
        [
            "$vendorOSPath/lib/modules/$kernelVer/kernel/fs/squashfs",
            "$vendorOSPath/lib/modules/$kernelVer/kernel/fs"
        ]
    );
    if (!defined $squashfsMod) {
        warn _tr("unable to find squashfs-module for kernel version '%s'.",
            $kernelVer);
        return;
    }
    return 1;
}

sub addExportToConfigDB
{
    my $self      = shift;
    my $export    = shift;
    my $openslxDB = shift;

    $export->{port} = $self->{'block-device'}->getExportPort($openslxDB);

    my $res = $openslxDB->addExport($export);
    return $res;
}

sub generateExportURI
{
    my $self     = shift;
    my $export   = shift;
    my $vendorOS = shift;

    my $URI = $self->{'block-device'}->generateExportURI($export);
    $URI .= '/squashfs';
    return $URI;
}

sub requiredFSMods
{
    my $self = shift;

    my @mods = $self->{'block-device'}->requiredBlockDeviceModules();
    push @mods, 'squashfs ';
    return @mods;
}

sub requiredFSTools
{
    my $self = shift;

    return $self->{'block-device'}->requiredBlockDeviceTools();
}

sub showExportConfigInfo
{
    my $self   = shift;
    my $export = shift;

    $self->{'block-device'}->showExportConfigInfo($export);
    return;
}

################################################################################
### implementation methods
################################################################################

#################################################################################
#  Branch to choose the appropriate squashfs version according to target system #
################################################################################

sub _createSquashFS
{
    my $self               = shift;
    my $source             = shift;
    my $target             = shift;
    my $includeExcludeList = shift;


    my @versionString = split(/-/, $self->{engine}->{'vendor-os-name'});
    my $mksquashfsVersion = $self->{engine}->{distro}->getSquashfsVersion($versionString[1]);

    vlog(0, "found version string $versionString[1] out of $self->{engine}->{'vendor-os-name'}");
    vlog(0, "use squashfs version: $mksquashfsVersion");

    $self->_createSquashFS_prepare($target);

    my $mksquashfsParams;

    $mksquashfsVersion == '3.2' && do 
        {$mksquashfsParams = $self->_createSquashFS_setup_3_2($includeExcludeList)}; 
    $mksquashfsVersion == '3.3' && do 
        {$mksquashfsParams = $self->_createSquashFS_setup_3_3($includeExcludeList)}; 
    $mksquashfsVersion == '4.0' && do 
        {$mksquashfsParams = $self->_createSquashFS_setup_4($includeExcludeList)}; 

    $self->_createSquashFS_run($source, $target, $mksquashfsParams);    

    return;
}

sub _createSquashFS_prepare
{ 
    my $self               = shift;
    my $target             = shift;

    system("rm -f $target");
    # mksquasfs isn't significantly faster if fs already exists, but it
    # causes the filesystem to grow somewhat, so we remove it in order to
    # get the smallest FS-file possible.

    my $baseDir = dirname($target);
    if (!-e $baseDir) {
        if (system("mkdir -p $baseDir")) {
            die _tr("unable to create directory '%s', giving up! (%s)\n", 
                $baseDir, $!);
        }
    }
    return;
}


sub _createSquashFS_run {
    my $self               = shift;
    my $source             = shift;
    my $target             = shift;
    my $params             = shift;
   
    # ... invoke mksquashfs ...
    vlog(0, _tr("invoking mksquashfs..."));
    my $mksquashfsBinary = 
         "$openslxConfig{'base-path'}/share/squashfs/$params->{binary}";
    my $res = 
         system("$mksquashfsBinary $source $target $params->{cmdlineOptions}");
    unlink($params->{tmpfile});
    # ... remove filter file if done
    if ($res) {
        die _tr(
            "unable to create squashfs for source '%s' as target '%s', giving up! (%s)",
            $source, $target, $!);
    }
    return;
}

sub _createSquashFS_setup_4
{ 
    my $self               = shift;
    my $includeExcludeList = shift;

    # dump filter to a file ...
    my $filterFile = "/tmp/slx-nbdsquash-filter-$$";
    my $includeFile = "/tmp/slx-nbdsquash-includes-$$";
    spitFile($filterFile, $includeExcludeList);

    slxsystem("sed $filterFile -e '/^-/d' -e 's/^+[ \t]*//' > $includeFile");
    slxsystem("sed -i $filterFile -e '/^+/d' -e 's/^-[ \t]*//'");
   
    my $result = {
        binary          => "mksquashfs_4",
        cmdlineOptions  => "-wildcards -ef $filterFile",
        tmpfile         => "$filterFile"
    };

    return $result;
}


sub _createSquashFS_setup_3_3
{
    my $self               = shift;
    my $includeExcludeList = shift;

    # dump filter to a file ...
    my $filterFile = "/tmp/slx-nbdsquash-filter-$$";
    my $includeFile = "/tmp/slx-nbdsquash-includes-$$";
    spitFile($filterFile, $includeExcludeList);

    slxsystem("sed $filterFile -e '/^-/d' -e 's/^+[ \t]*//' > $includeFile");
    slxsystem("sed -i $filterFile -e '/^+/d' -e 's/^-[ \t]*//'");

    my $result = {
        binary          => "mksquashfs_3_3",
        cmdlineOptions  => "-wildcards -ef $filterFile",
        tmpfile         => "$filterFile"
    };

    return $result;
}


sub _createSquashFS_setup_3_2
{
    my $self               = shift;
    my $includeExcludeList = shift;

    # dump filter to a file ...
    my $filterFile = "/tmp/slx-nbdsquash-filter-$$";
    spitFile($filterFile, $includeExcludeList);

    my $result = {
        binary          => "mksquashfs_3_2",
        cmdlineOptions  => "-ff $filterFile",
        tmpfile         => "$filterFile"
    };

    return $result;
}

sub _determineIncludeExcludeList
{
    my $self = shift;

    # Rsync uses a first match strategy, so we mix the local specifications
    # in front of the filterset given by the package (as the local filters
    # should always overrule the vendor filters):
    my $distroName      = $self->{engine}->{'distro-name'};
    my $localFilterFile =
      "$openslxConfig{'config-path'}/distro-info/$distroName/export-filter";
    my $includeExcludeList 
        = slurpFile($localFilterFile, { failIfMissing => 0 });
    $includeExcludeList .= $self->{engine}->{distro}->{'export-filter'};
    $includeExcludeList =~ s[^\s+][]igms;
    # remove any leading whitespace, as rsync doesn't like it
    return $includeExcludeList;
}

sub _mapRsyncFilter2Regex
{
    my $self        = shift;
    my $sourcePath  = shift;
    my $rsyncFilter = shift;

    return join(
        "\n",
        map {
            if ($_ =~ m[^([-+]\s*)(.+?)\s*$])
            {
                my $action = $1;
                my $regex  = $2;
                $regex =~ s[\*\*][.+]g;
                # '**' matches everything
                $regex =~ s[\*][[^/]+]g;
                # '*' matches anything except slashes
                $regex =~ s[\?][[^/]?]g;
                # '*' matches any single char except slash
                $regex =~ s[\?][[^/]?]g;
                # '*' matches any single char except slash
                $regex =~ s[\.][\\.]g;
                # escape any dots
                if (substr($regex, 0, 1) eq '/') {
                    # absolute path given, need to extend by source-path:
                    "$action^$sourcePath$regex\$";
                } else {
                    # filename pattern given, need to anchor to the end only:
                    "$action$regex\$";
                }
            } else {
                $_;
            }
          }
          split "\n",
        $rsyncFilter
    );
}

sub _addBlockDeviceTagToExport
{
    my $self   = shift;
    my $target = shift;

    my $tagName = "$target" . '@' . lc($self->{'block-device'}->{name});
    linkFile(basename($target), $tagName);
    return;
}

sub _removeBlockDeviceTagFromExport
{
    my $self   = shift;
    my $target = shift;

    my $tagName = "$target" . '@' . lc($self->{'block-device'}->{name});
    slxsystem("rm $tagName");
    # now find out whether or not there are any other tags left:
    my $vendorOSName = basename($target);
    opendir(DIR, dirname($target));
    my @tags = grep { /^$vendorOSName\@/ } readdir(DIR);
    return @tags ? 0 : 1;
    # return 1 if no more tags (i.e. it is safe to remove the image)
}

1;