summaryrefslogtreecommitdiffstats
path: root/tools/draklive
blob: 5555b2f5b45847d99ac1dcb9e7ea94b8629bd5e9 (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
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
#!/usr/bin/perl

# draklive $Id$

# Copyright (C) 2005 Mandriva
#                    Olivier Blin <oblin@mandriva.com>
#
# 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.

use lib qw(/usr/lib/libDrakX);
use MDK::Common;
use list_modules;
use modules;
use run_program;
use POSIX qw(strftime);
use Cwd 'abs_path';
use Getopt::Long;
use Pod::Usage;

my $dir_distrib_sqfs = {
    mountpoint => '/distrib',
    type => 'squashfs',
    source => 'distrib.sqfs',
    build_from => '/',
};
my $dir_memory = {
    mountpoint => '/memory',
    type => 'tmpfs',
};

my %predefined = (
    mounts => {
        simple_union => {
            root => '/union',
            overlay => 'unionfs',
            dirs => [
                $dir_memory,
                { mountpoint => '/media' },
            ],
        },
        squash_union => {
            root => '/union',
            overlay => 'unionfs',
            dirs => [
                {
                    mountpoint => '/system',
                    type => 'loopfs',
                    pre_allocate => '100k',
                    source => 'system.loop'
                },
                $dir_distrib_sqfs,
            ],
        },
        volatile_squash_union => {
            root => '/union',
            overlay => 'unionfs',
            dirs => [
                $dir_memory,
                $dir_distrib_sqfs,
            ],
        },
        multi_squash_union => {
            root => '/union',
            overlay => 'unionfs',
            dirs => [
                $dir_memory,
                {
                    mountpoint => '/system',
                    type => 'squashfs',
                    source => 'system.sqfs'
                },
                $dir_distrib_sqfs,
            ],
        },
    },
    media => {
        usb => {
            storage => 'usb',
            fs => 'vfat',
            sleep => 15,
            source => 'LABEL=MDVUSBROOT',
            mountpoint => '/media',
        },
        cdrom => {
            storage => 'cdrom',
            fs => 'iso9660',
            source => 'LABEL=MDVCDROOT',
            mountpoint => '/media',
        },
    },
);

my %custom = (
    media => {
        nfs => sub {
            my ($module, $client, $source) = @_;
            {
                modules => [ $module ],
                fs => 'nfs',
                pre => "ifconfig eth0 $client up",
                source => $source,
                mountpoint => '/media',
            };
        },
    },
);

sub nls_modules {
    my ($live) = @_;
    if_($live->{media}{fs} eq 'vfat', 'nls_cp437'), #- default FAT codepage
    map { "nls_$_" } (map { "iso8859-$_" } 1..7, 9, 13..15), 'utf8';
}

sub fs_module {
    my ($live) = @_;
    $live->{media}{fs} eq 'iso9660' ? 'isofs' : $live->{media}{fs};
}

sub progress_start {
    my ($total, $time, $o_exp_divide) = @_;
    {
        total => $total,
        current => 0,
        start_time => $time,
        exp_divide => $o_exp_divide,
        maxl => length($total) - $o_exp_divide,
    };
}

sub progress_show_incr {
    my ($progress, $incr, $time) = @_;
    $progress->{current} += $incr;
    my $elapsed_time = $time - $progress->{start_time};
    my $eta = int($elapsed_time*$progress->{total}/$progress->{current});
    printf("\r%3d%% (%$progress->{maxl}s/%-$progress->{maxl}s), %8s/%8s (ETA)",
           int(100*$progress->{current}/$progress->{total}),
           (map { substr($_, 0, length($_)-$progress->{exp_divide}) } $progress->{current}, $progress->{total}),
           (map { strftime("%H:%M:%S", gmtime($_)) } $elapsed_time, $eta));
}

sub progress_end() { print "\n" }

my $loop_number = 0;
my %loop = (
    squashfs => {
        read_only => 1,
        modules => [ qw(loop squashfs) ],
        build => sub {
            my ($dest, $root) = @_;
            my $total = first(split /\s/, `du -sb $root`);
            print "have to process " . int($total/1000000) . " MB\n";
            my $progress = progress_start($total, time(), 6);
            open(my $OUTPUT, '-|', 'mksquashfs', $root, $dest, '-info');
            {
                local $_; #- avoid outside $_ to be overwritten
                while (<$OUTPUT>) {
                    if (/^mksquashfs: file .*, uncompressed size (\d+) bytes, (?:DUPLICATE)?$/) {
                        progress_show_incr($progress, $1, time());
                    }
                }
            }
            progress_end();
        },
        mount => sub {
            my ($live, $dir) = @_;
            my @mnt = (
                "/bin/losetup -r /dev/loop$loop_number $live->{media}{mountpoint}/$dir->{source}",
                "mount -o ro -t squashfs /dev/loop$loop_number $dir->{mountpoint}");
            $loop_number++;
            @mnt;
        },
    },
    loopfs => {
        modules => [],
        create => sub {
            my ($dest, $size) = @_;
            run_('dd', "of=$dest", 'count=0', "seek=$size", 'bs=1k');
            run_('mke2fs', '-F', $dest);
        },
        mount => sub {
            my ($live, $dir) = @_;
            my @mnt = (
                "losetup /dev/loop$loop_number $live->{media}{mountpoint}/$dir->{source}",
                "mount -t ext2 /dev/loop$loop_number $dir->{mountpoint}");
            $loop_number++;
            @mnt;
        },
    },
    tmpfs => {
        mount => sub {
            my ($_live, $dir) = @_;
            "mount -t tmpfs none $dir->{mountpoint}";
        },
    },
);

my %overlay = (
    unionfs => {
        modules => [ qw(unionfs) ],
        mount => sub {
            my ($live) = @_;
            #- build dirs list: "dir1=ro:dir2:ro:dir3=rw"
            my $dirs = join(':',
                            map { "$_->{mountpoint}=" .
                                    ($_->{type} && !$loop{$_->{type}}{read_only} ? 'rw' : 'ro');
                                } @{$live->{mount}{dirs} || []});
            "mount -o dirs=$dirs -t unionfs unionfs $live->{mount}{root}";
        },
    },
);

my %storage = (
    cdrom => {
        read_only => 1,
        modules => 'disk/cdrom|hardware_raid|sata|scsi bus/usb disk/raw',
        create => \&create_cdrom_master,
        record => \&record_cdrom_master,
    },
    usb => {
        modules => 'bus/usb disk/raw|usb',
        create => undef,
        record => \&record_usb_master,
    },
);

my %moddeps;
sub load_moddeps {
    my ($root, $kernel_path) = @_;
    my $get_modname = sub { first($_[0] =~ m!^$kernel_path/kernel/(?:.*/|)(.*?)\.k?o!) };
    %moddeps = (map {
	my ($f, $deps) = split ':';
	my $modname = $get_modname->($f);
	$modname => { full => $f, deps => [ map { $get_modname->($_) } split ' ', $deps ] };
    } cat_($root . $kernel_path . '/modules.dep'));
}
sub moddeps_closure {
    my ($module) = @_;
    my @deps = @{$moddeps{$module}{deps}};
    (map { moddeps_closure($_) } @deps), @deps;
}

sub run_ {
    print "running " . join(' ', @_) . "\n";
    run_program::run(@_);
}

sub create_initrd {
    my ($live) = @_;

    rm_rf($live->{initrd_tree}) if -e $live->{initrd_tree};

    mkdir_p($live->{initrd_tree} . $_) foreach
      qw(/bin /dev /lib /proc /sys),
      $live->{media}{mountpoint},
      (map { $_->{mountpoint} } @{$live->{mount}{dirs} || []}),
      $live->{mount}{root};

    # cp_f($live->{system}{root} . '/sbin/nash', $live->{initrd_tree} . '/bin/');
    #- use nash from cooker for now, label support
    cp_f('/sbin/nash', $live->{initrd_tree} . '/bin/');
    #- needed to mount loopbacks read-only
    cp_f('/lib/tls/libc.so.6', $live->{initrd_tree} . '/lib/');
    cp_f('/lib/ld-linux.so.2', $live->{initrd_tree} . '/lib/');
    cp_f('/sbin/losetup', $live->{initrd_tree} . '/bin/');

    if ($live->{media}{fs} eq 'nfs') {
        cp_f('/sbin/ifconfig', $live->{initrd_tree} . '/bin/');
        cp_f('/bin/mount', $live->{initrd_tree} . '/bin/');
        if ($live->{debug}) {
            cp_f('/bin/ping', $live->{initrd_tree} . '/bin/');
            cp_f('/lib/libresolv.so.2', $live->{initrd_tree} . '/lib/');
        }
    }
    if ($live->{debug}) {
        cp_f('/usr/bin/strace', $live->{initrd_tree} . '/bin/');
        cp_f('/usr/bin/busybox', $live->{initrd_tree} . '/bin');
        my @l = map { /functions:/ .. /^$/ ? do { s/\s//g; split /,/ } : () } `busybox`;
        shift @l;
        symlink('busybox', $live->{initrd_tree} . "/bin/$_") foreach @l;
    }

    require devices;
    devices::make($live->{initrd_tree} . "/dev/$_") foreach
        qw(console initrd null ram systty),
        (map { "tty$_" } 0..5),
        (map { "loop$_" } 0..7);

    load_moddeps($live->{system}{root}, "/lib/modules/" . $live->{system}{kernel});

    my ($modules, $skipped) = partition { exists $moddeps{$_} }
      uniq(map { modules::cond_mapping_24_26($_) } category2modules($storage{$live->{media}{storage}}{modules}));
    my ($extra_modules, $missing) = partition { exists $moddeps{$_} }
      nls_modules($live),
      fs_module($live),
      @{$live->{media}{modules} || []},
      (map { @{$loop{$_}{modules} || []} } uniq(map { $_->{type} } grep { $_->{type} } @{$live->{mount}{dirs} || []})),
      ($live->{mount}{overlay} ? @{$overlay{$live->{mount}{overlay}}{modules} || []} : ());

    @$missing and die "missing mandatory modules:" . join("\n", '', sort(@$missing));
    push @$modules, @$extra_modules;

    my @module_deps = uniq(map { moddeps_closure($_) } @$modules);
    run_('gzip', '>', $live->{initrd_tree} . "/lib/$_.ko", '-dc', $live->{system}{root} . $moddeps{$_}{full})
      foreach @module_deps, @$modules;

    @$skipped and warn "skipped modules:" . join("\n", '', sort(@$skipped));

    create_initrd_linuxrc($live, @module_deps, @$modules);
    compress_initrd_tree($live);
    add_splash($live);
    $live->{copy_initrd} and cp_f($live->{boot_dir} . '/initrd.gz', $live->{copy_initrd});
}

sub create_initrd_linuxrc {
    my ($live, @modules) = @_;
    my $target = $live->{mount}{root} || $live->{media}{mountpoint};
    output_with_perm($live->{initrd_tree} . '/linuxrc', 0755,
                     join("\n",
                          "#!/bin/nash",
                          (map { "insmod /lib/$_.ko" } @modules),
                          if_($live->{media}{sleep}, "sleep $live->{media}{sleep}"),
                          #- required for labels
                          "mount -t proc none /proc",
                          #- required for cdrom labels
                          "mount -t sysfs none /sys",
                          if_($live->{debug}, "/bin/sh"),
                          if_($live->{media}{pre}, deref_array($live->{media}{pre})),
                          ($live->{media}{fs} eq 'nfs' ? '/bin/mount -n -o ro,nolock' : 'mount') .
                            ($storage{$live->{media}{storage}}{read_only} && " -o ro") .
                            " -t $live->{media}{fs} $live->{media}{source} $live->{media}{mountpoint}",
                          (map { $loop{$_->{type}}{mount}->($live, $_) } grep { $_->{type} } @{$live->{mount}{dirs} || []}),
                          ($live->{mount}{overlay} ? $overlay{$live->{mount}{overlay}}{mount}->($live) : ()),
                          "echo 0x0100 > /proc/sys/kernel/real-root-dev",
                          "umount /sys",
                          "umount /proc",
                          ($live->{mount}{overlay} ?
                             # don't move to /initrd but /live, or else the overlay will be unmounted
                             ("mkdir -p $target/live", "pivot_root $target $target/live") :
                               "pivot_root $target $target/initrd"),
                          if_($live->{post}, deref_array($live->{post})),
                          ""));
}

#- builds $live->{boot_dir} . '/initrd.gz'
sub compress_initrd_tree {
    my ($live) = @_;

    my $size = run_program::get_stdout("du -ks $live->{initrd_tree} | awk '{print \$1}'") + 250;
    my $inodes = run_program::get_stdout("find $live->{initrd_tree} | wc -l") + 1250;
    $size = int($size + $inodes / 10) + 1; #- 10 inodes needs 1K
    my $initrd = $live->{boot_dir} . "/initrd";

    mkdir_p($live->{boot_dir});
    run_('dd', 'if=/dev/zero', "of=$initrd", 'bs=1k', "count=$size");
    run_('mke2fs', '-q', '-m', 0, '-F', '-N', $inodes, '-s', 1, $initrd);
    mkdir_p($live->{mnt});
    run_('mount', '-o', 'loop', '-t', 'ext2', $initrd, $live->{mnt});
    cp_af(glob("$live->{initrd_tree}/*"), $live->{mnt});
    rm_rf($live->{mnt} . "/lost+found");
    run_('umount', $live->{mnt});
    run_('gzip', '-f', '-9', $initrd);
}

sub add_splash {
    my ($live) = @_;
    if ($live->{system}{vga_mode} && $live->{system}{splash} ne 'no') {
	require bootloader;
	my $initrd = "$live->{boot_dir}/initrd.gz";
	my $tmp_initrd = '/tmp/initrd.gz';
        cp_f($initrd, $live->{system}{root} . $tmp_initrd);
        {
            local $::prefix = $live->{system}{root};
            bootloader::add_boot_splash($tmp_initrd, $live->{system}{vga_mode});
        }
        cp_f($live->{system}{root} . $tmp_initrd, $initrd);
	unlink($live->{system}{root} . $tmp_initrd);
    }
}

sub build_syslinux_cfg {
    my ($live) = @_;
    #- fastboot is needed to avoid fsck
    my $append = "fastboot splash=silent vga=$live->{system}{vga_mode}";
    qq(default live
prompt 1
timeout 40
display live.msg
label live
    kernel vmlinuz
    append initrd=initrd.gz $append
);
}

sub install_system {
    my ($live) = @_;
    run_('drakx-in-chroot',
         $live->{system}{repository},
         $live->{system}{root},
         if_($live->{system}{auto_install}, '--auto_install', abs_path($live->{system}{auto_install})),
         if_($live->{system}{patch}, '--defcfg', abs_path($live->{system}{patch})))
      or die "unable to install system chroot";

    run_('urpmi',
         '--root',
         $live->{system}{root},
         map { abs_path($_) } @{$live->{system}{rpms}}) if @{$live->{system}{rpms}};

    #- make sure harddrake is run
    #- (do it in chroot, or else Storable from the build box may write an incompatible config file)
    system("chroot $live->{system}{root} " .
           "perl -MStorable -e \"Storable::store({ UNKNOWN => {} }, '/etc/sysconfig/harddrake2/previous_hw')\"");

    #- interactive mode can lead to race in initscripts
    #- (don't use addVarsInSh from MDK::Common, it breaks shell escapes)
    substInFile { s/^PROMPT=.*/PROMPT=no/ } $live->{system}{root} . '/etc/sysconfig/init';

    #- disable first boot wizard
    output($live->{system}{root} . '/etc/sysconfig/firstboot', 'FIRSTBOOT=no');
    #- enable drakx-finish-install
    output($live->{system}{root} . '/etc/sysconfig/finish-install', 'FINISH_INSTALL=yes');

    #- preselect guest user in kdm
    my $kdm_cfg = '/usr/share/config/kdm/kdmrc';
    update_gnomekderc($live->{system}{root} . $kdm_cfg,
                      'X-:0-Greeter' => (PreselectUser => 'Default', DefaultUser => 'guest')) if -f $kdm_cfg;
}

sub create_loopback_files {
    my ($live) = @_;
    mkdir_p($live->{images_dir});
    foreach (grep { $_->{build_from} } @{$live->{mount}{dirs} || []}) {
        my $tree = $live->{system}{root} . $_->{build_from};
        my $dest = $live->{images_dir} . '/' . $_->{source};
        unlink($dest);
        $loop{$_->{type}}{build}->($dest, $tree);
    }
    foreach (grep { $_->{pre_allocate} } @{$live->{mount}{dirs} || []}) {
        my $dest = $live->{images_dir} . '/' . $_->{source};
        unlink($dest);
        $loop{$_->{type}}{create}->($dest, $_->{pre_allocate});
    }
}

sub get_media_label {
    my ($live) = @_;
    first($live->{media}{source} =~ /^LABEL=(.*)$/);
}

sub get_media_device {
    my ($live) = @_;
    return $live->{media}{device} if $live->{media}{device};
    my $label = get_media_label($live) or return $live->{media}{source};
    my $device = chomp_(`readlink -f /dev/disk/by-label/$label`)
      or die "unable to find device for /dev/disk/by-label/$label";
    $device;
}

sub prepare_bootloader {
    my ($live) = @_;
    mkdir_p($live->{boot_dir});
    cp_f($live->{system}{root} . '/boot/vmlinuz-' . $live->{system}{kernel}, $live->{boot_dir} . '/vmlinuz');
    my $msg = $live->{system}{root} . '/boot/message-graphic';
    cp_f($msg, $live->{boot_dir} . '/live.msg') if -f $msg;
    output($live->{boot_dir} . '/syslinux.cfg', build_syslinux_cfg($live));
}

sub create_cdrom_master {
    my ($live) = @_;
    my $dest = $live->{images_dir} . '/live.iso';
    my $label = get_media_label($live) or die "the source device must be described by a label";
    run_('mkisofs', '-pad', '-l', '-R', '-J', '-v', '-v',
           '-V', $label, #'-A', $application, '-p', $preparer, '-P', $publisher,
           '-b', 'isolinux/isolinux.bin',
           '-c', 'isolinux/boot.cat',
           '-hide-rr-moved', '-no-emul-boot',
           '-boot-load-size', 4, '-boot-info-table',
           '-o', $dest,
           '-graft-points',
           'isolinux/isolinux.bin=/usr/lib/syslinux/isolinux-graphic.bin',
           'isolinux/isolinux.cfg=' . $live->{boot_dir} . '/syslinux.cfg',
           (map { 'isolinux/=' . $live->{boot_dir} . '/' . $_ } qw(vmlinuz initrd.gz live.msg)),
           (map { $live->{images_dir} . '/' . $_->{source} } grep { $_->{build_from} } @{$live->{mount}{dirs}}));
}

sub create_master {
    my ($live) = @_;
    if (my $create = $storage{$live->{media}{storage}}{create}) {
        $create->($live);
    } else {
        warn "not implemented yet";
    }
}

sub record_cdrom_master {
    my ($live, $o_refresh_boot_only) = @_;
    $o_refresh_boot_only and die "record boot isn't possible for cdrom master";
    $live->{media}{device} or die "no device defined in media configuration";
    my $src = $live->{images_dir} . '/live.iso';
    run_('cdrecord', '-v', 'dev=' . $live->{media}{device}, $src);
}

sub record_usb_master {
    my ($live, $o_refresh_boot_only) = @_;
    my $label = get_media_label($live);
    if ($live->{media}{device} && $label) {
        run_('mkdosfs', '-n', $label, $live->{media}{device})
          or die "unable to format device $live->{media}{device}";
    }
    my $device = get_media_device($live);
    mkdir_p($live->{mnt});
    run_('mount', $device, $live->{mnt})
      or die "unable to mount $device";
    cp_f(map { $live->{boot_dir} . "/$_" } qw(vmlinuz initrd.gz live.msg), $live->{mnt});
    unless ($o_refresh_boot_only) {
        foreach (grep { $_->{build_from} || $_->{pre_allocate} } @{$live->{mount}{dirs} || []}) {
            print "copying $_->{source}\n";
            cp_f($live->{images_dir} . '/' . $_->{source}, $live->{mnt});
        }
    }
    run_('umount', $live->{mnt});
    #- use syslinux -s, "safe, slow and stupid" version of SYSLINUX
    run_('syslinux', '-s', $device) or die "unable to run syslinux on $device";
}

sub record_master {
    my ($live, $o_refresh_boot_only) = @_;
    if (my $record = $storage{$live->{media}{storage}}{record}) {
        $record->($live, $o_refresh_boot_only);
    } else {
        warn "not implemented yet";
    }
}

sub record_boot {
    my ($live) = @_;
    record_master($live, 'refresh');
}

sub complete_config {
    my ($live) = @_;

    #- set unsupplied config dirs
    $live->{workdir} ||= '/tmp/draklive';
    $live->{boot_dir} ||= $live->{workdir} . "/boot";
    $live->{initrd_tree} ||= $live->{workdir} . "/initrd";
    $live->{images_dir} ||= $live->{workdir} . "/images";
    $live->{mnt} ||= $live->{workdir} . "/mnt";

    #- check for minimum requirements
    ref $live->{media} or die "no media definition";
    ref $live->{system} or die "no system definition";
    $live->{system}{kernel} or die "no kernel has been configured";
    mkdir_p($live->{workdir});
}

sub clean {
    my ($live) = @_;
    rm_rf($_) foreach grep { -e $_ } $live->{workdir}, $live->{system}{root};
}

my @actions = (
    { name => 'clean', do => \&clean },
    { name => 'install', do => \&install_system },
    { name => 'initrd', do => \&create_initrd },
    { name => 'boot', do => \&prepare_bootloader },
    { name => 'loop', do => \&create_loopback_files },
    { name => 'master', do => \&create_master },
    { name => 'record', do => \&record_master },
    { name => 'record_boot', do => \&record_boot },
);
my @all = qw(install initrd boot loop master);

my %live;
GetOptions(
    "help" => sub { pod2usage('-verbose' => 1) },
    "all" => sub { $_->{to_run} = 1 foreach grep { member($_->{name}, @all) } @actions },
    (map { $_->{name} => \$_->{to_run} } @actions),
    "device:s" => sub { $live{media}{device} = $_[1] },
    "config:s" => sub {
        my $path = $_[1];
        #- don't use do(), since it can't see lexicals in the enclosing scope
        my $cfg = eval(cat_($path)) or die "unable to load $path";
        add2hash(\%live, $cfg);
        print "loaded $path as config file\n";
    },
) or pod2usage();
unless (keys(%live)) {
    warn 'no live definition';
    pod2usage();
}
complete_config(\%live);

require standalone;
every { !$_->{to_run} } @actions and die 'nothing to do';
foreach (grep { $_->{to_run} } @actions) {
    print qq(* entering step "$_->{name}"\n);
    $_->{do}->(\%live);
    print qq(* step "$_->{name}" done\n);
}

__END__

=head1 NAME

draklive - A live distribution mastering tool

=head1 SYNOPSIS

draklive [options]

 Options:
   --help           long help message

   --install        install selected distribution in chroot
   --initrd         build initrd
   --boot           prepare bootloader files
   --loop           build compressed loopback files
   --master         build master image

   --all            run all steps, from installation to mastering

   --clean          clean installation chroot and work directory
   --record         install live on selected media
   --record_boot    install bootloader only on selected media

   --device <dev>   use this device for live recording, formatting
                    it preliminary (not needed if the device
                    already has the required label)
   --config <file>  use this configuration file as live description

Examples:

 draklive --config config/live.cfg --clean

 draklive --config config/live.cfg --all

 draklive --config config/live.cfg --record --device /dev/sdb1

=head1 OPTIONS

=over 8

=item B<--config>

Makes draklive use the next argument as a configuration file.
This file should contain an hash describing the live distribution,
meaning the system (chroot and boot), media (usb, cdrom, nfs),
and mount type (simple R/W union, union with squash files).

Here's a configuration sample:

  {
    system => {
        root => '/chroot/live-move',
        repository => '/mnt/ken/2006.0/i586',
        kernel => '2.6.12-12mdk-i586-up-1GB',
        auto_install => 'config/auto_inst.cfg.pl',
        patch => 'config/patch-2006-live.pl',
        rpms => [
             'rpms/unionfs-kernel-2.6.12-12mdk-i586-up-1GB-1.1.1.1.20051124.1mdk-1mdk.i586.rpm'
        ],
        vga_mode => 788,
    },
    media => $predefined{media}{usb},
    mount => $predefined{mounts}{squash_union}
  };

=back

=head1 DESCRIPTION

B<draklive> builds a live distribution according to a
configuration file, creates a master image,
and optionnally installs it on a device.

See L<http://qa.mandriva.com/twiki/bin/view/Main/DrakLive>

=head1 AUTHOR

Olivier Blin <oblin@mandriva.com>

=cut