File: Initrd.pm

package info (click to toggle)
systemconfigurator 2.0.10-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 724 kB
  • ctags: 313
  • sloc: perl: 7,423; makefile: 48
file content (127 lines) | stat: -rw-r--r-- 3,348 bytes parent folder | download | duplicates (3)
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
package Initrd;

#   $Id: Initrd.pm,v 1.17 2002/12/18 16:29:31 sdague Exp $

#   Copyright (c) 2001-2002 International Business Machines

#   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 of the License, 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

#   Sean Dague <sean@dague.net>
#   Vasilios Hoffman <greekboy@users.sourceforge.net>

use strict;
use Carp;
use POSIX;
use File::Copy;
use Util::Log qw(:all);
use Util::Cmd qw(:all);
use base qw(Exporter);
use vars qw(@EXPORT $VERSION @rdtypes);
use Initrd::SuSE;
use Initrd::RH;
use Initrd::Debian;

$VERSION = sprintf("%d.%02d", q$Revision: 1.17 $ =~ /(\d+)\.(\d+)/);

=head1 NAME

Initrd - builds initial ramdisks for kernels

=head1 SYNOPSIS

Initrd::setup($config);

=head1 DESCRIPTION

The Initrd module generates initial ramdisks for kernels, provided there is enough userland tools and kernel support to allow this.

=head1 AUTHOR

  Sean Dague <sean@dague.net>
  Vasilios Hoffman <greekboy@users.sourceforge.net>

=head1 SEE ALSO

L<Config>, 
L<perl>

=cut

sub setup {

    my $config = shift;

    my (@ramdisks,@files) = ();

    foreach my $rdtype (@rdtypes) {
        verbose("Testing for ramdisk type '$rdtype'");
        if($rdtype->footprint()) {
            verbose("Setting up ramdisk type '$rdtype'");
            @files = setup_ramdisks($config, $rdtype);
            last if scalar(@files);
        }
    }
    return @files;
}

# Pretty simple, we pass in the config and the ramdisk type.
# If the kernel file exists, and the ramdisk doesn't, we try to build it.
# If the ramdisk now exists we add it to $config, which should propogate
# back up.

sub setup_ramdisks {
    my ($config, $rdtype) = @_;
    
    my @files = ();
    my @kernels = get_kernels($config);

    foreach my $kernel (@kernels) {
        my $kernelfile = $config->get($kernel . "_path");
        my $ramdisk = $config->get($kernel . "_initrd");
        my $rootdev = get_rootdev($config, $kernel);
        verbose("Setting up kernel: $kernelfile for root device: $rootdev");
        if(-e $kernelfile and !$ramdisk) {
            $ramdisk = $rdtype->setup($kernelfile, $rootdev);
            
            if($ramdisk and -e $ramdisk) {
                $config->set($kernel . "_initrd", $ramdisk);
                push @files, $ramdisk;
            }
        }
    }
    return @files;
}

sub get_rootdev {
    my ($config, $kernel) = @_;
    my $rootdev = $config->get($kernel . "_rootdev");
    $rootdev ||= $config->get('boot_rootdev');
    return $rootdev;
}

sub get_kernels {
    my $config = shift;
    my %kernels = $config->varlist("^kernel");
    my @keys = grep(/kernel\d+_path/, (sort keys %kernels));
    my @kernels = map {s/_path//; $_} @keys;
    return @kernels;
}

1;