summaryrefslogtreecommitdiffstats
path: root/lib/MGA/DrakISO/BuildISO.pm
blob: 8771939336cc62a48842c06092d744480c3bed03 (plain)
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
# Copyright (C) 2005 Mandriva
#                    Olivier Blin <oblin@mandriva.com>
# Copyright (C) 2017 Mageia
#                    Martin Whitaker <mageia@martin-whitaker.me.uk>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# 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; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

# SYNOPSIS
# --------
# This module provides a function to build the ISO image. All the files
# to go on the ISO should be stored in or linked into the build directory
# before performing this step.

package MGA::DrakISO::BuildISO;

use strict;

use MDK::Common;
use common;

use MGA::DrakISO::ISOBuild;
use MGA::DrakISO::Loopback;
use MGA::DrakISO::Utils;

use MGA::DrakISO::BuildLoop;

use Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(build_iso);

# This is the top-level function called to build an iso image. It generates
# the list of file assignments before calling build_iso_image() to do the
# actual work.
#
sub build_iso {
    my ($build) = @_;

    my $loopbacks = $build->get_builddir('loopbacks');
    my $files     = $build->get_builddir('files');

    build_iso_image(
        $build,
        '/boot=' . $build->get_builddir('boot'),
        if_($build->{settings}{arch} eq 'x86_64',
            '/EFI=' . $build->get_builddir('EFI'),
        ),
        if_(-d $loopbacks,
            map {
                '/loopbacks' . $_ . '=' . $loopbacks . $_;
            } list_selected_loopbacks($build)
        ),
        if_(-d $files,
            map {
                $_ . '=' . $files . '/' . $_;
            } all($files)
        ),
    );
}

sub list_selected_loopbacks {
    my ($build) = @_;
    my @pack = $build->{settings}{pack} ? @{$build->{packs}{$build->{settings}{pack}} || []} : ();
    my @pack_modules = grep { member($_->{name}, @pack) } list_loopback_modules($build);
    (map { $loop_types{$_->{type}}{is_loopback} && $_->{path} ? $_->{path} . $loop_types{$_->{type}}{extension} : () } @{$build->{mount}{dirs} || []}),
    (map { '/modules/' . $_->{name} . $loop_types{$_->{type}}{extension} } @pack_modules);
}

# This function builds a hybrid ISO capable of both legacy and UEFI boot. The
# ISO contains a primary iso9660 partition (which is the only thing visible
# when booting from DVD) and a secondary ESP partition (which is only used
# for USB UEFI boot).
#
# For legacy boot, the system boots the grub2 El Torito image. For DVD boot,
# this is located via the El Torito catalogue. For USB boot, it is located
# via the grub2 MBR hybrid boot image, which is stored in the MBR. In both
# cases the initial grub2 root location is the iso9660 partition, so the
# initial grub2 configuration file will be read from that partition.
#
# For UEFI boot, the system boots the grub2 EFI image. For DVD boot, this
# is located via the El Torito catalogue, and we store it in the iso9660
# partition. For USB boot, it must be located in the /EFI/BOOT directory
# in the ESP. For DVD boot the initial grub2 root location is the iso9660
# partition, for USB boot it is the ESP partition, and the initial grub2
# configuration file will be located accordingly.
#
sub build_iso_image {
    my ($build, @opts) = @_;

    my $mbr_image = $build->get_absolute_path($build->{media}{mbr_boot_img}) // '/usr/lib/grub/i386-pc/boot_hybrid.img';
    -e $mbr_image or die "cannot find MBR boot image $mbr_image\n";

    my $esp_image = $build->get_builddir('images') . '/esp.img';
    -e $esp_image or die "cannot find ESP image $esp_image\n";

    my $build_dir = $build->get_builddir('dist');
    mkdir_p($build_dir);

    my $dest = $build_dir . '/' . $build->get_name . '.iso';

    my $xorriso_version = `xorriso -version 2> /dev/null | head -1`;
    chomp($xorriso_version);

    my $date_file = $build_dir . '/DATE.txt';
    run_('date', '>', $date_file);

    run_('xorrisofs',
            if_($::verbose < 2, '-quiet'),
            '-pad', '-l', '-R', '-J',
            '-publisher', $build->{settings}{vendor},
            '-preparer',  'drakiso',
            '-sysid',     'Linux',
            '-appid',     $xorriso_version,
            '-volid',     $build->{media}{label},
            '-follow-links',
            '-graft-points',
            '-hide-rr-moved',
            '--sort-weight', 0, '/',
            '--sort-weight', 1, '/boot',
            # for hybrid MBR boot
            '--grub2-mbr', $mbr_image,
            '-b', '/boot/grub2/eltorito.img',
            '-no-emul-boot',
            '-boot-load-size', 4,
            '-boot-info-table',
            '--grub2-boot-info',
            if_($build->{settings}{arch} eq 'x86_64',
                # for DVD UEFI boot
                '--efi-boot', '--interval:appended_partition_2:all::',
                # for USB UEFI boot
                '-part_like_isohybrid',
                '-iso_mbr_part_type', '0x00',
                '-append_partition', 2, '0xef', $esp_image,
            ),
            '-o', $dest,
            @opts,
            '/DATE.txt=' . $date_file,
    ) or die "unable to run xorrisofs\n";

    my $filename = basename($dest);
    run_('mgaiso-addmd5', '>', '/dev/null', '2>', '/dev/null', $dest);
    run_({ chdir => $build_dir }, 'md5sum', '>', $dest . '.md5', $filename);
    run_({ chdir => $build_dir }, 'sha1sum', '>', $dest . '.sha1', $filename);
    run_({ chdir => $build_dir }, 'sha512sum', '>', $dest . '.sha512', $filename);
    if (my $suffix = $build->get_set_suffix) {
        if (my ($prefix, $ext) = $dest =~ /(.*)(\.[^.]+)$/) {
            my $link = $prefix . $suffix . $ext;
            linkf($dest, $link);
            print "linked as $link\n";
        }
    }
}

1;