summaryrefslogtreecommitdiffstats
path: root/kernel/update_kernel
blob: 01b1bca66e57b2d3976b69b29e17af6cb723b144 (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
#!/usr/bin/perl

use MDK::Common;
use list_modules;

my $RPMS = '/export/media/main';
my $rpm = 'rpm --nosignature';


my $MOVE = $ARGV[0] eq '--move' && shift;

@ARGV <= 1 or die "usage: ./update_kernel [--move] [<kernel rpm>]\n";


mkdir 'all.kernels'; # make sure "all.kernels" directory exists
eval { rm_rf('all.modules') }; #- not used anymore

my $main = chomp_(cat_('all.kernels/.main'));
my $main_BOOT = chomp_(cat_('all.kernels/.main-BOOT'));

if (@ARGV) {
    install_kernel($ARGV[0]);
} else {
    update_kernel_from_repository($RPMS, '2.6', 0);
    update_kernel_from_repository($RPMS, '2.6', 1);
#    update_kernel_from_repository($RPMS, '2.4', 1);
}


sub system_verbose { print join(' ', @_), "\n"; system(@_) }
sub sys { &system_verbose; $? and die }

sub rpm2version {
    my ($kernel_rpm) = @_;
    `$rpm -qpl $kernel_rpm` =~ m!/boot/vmlinuz-(.*)! && $1 or die "can't find vmlinuz in $kernel_rpm\n";
}

sub update_kernel_from_repository {
    my ($RPMS, $main_ver, $is_BOOT) = @_;
    my $rpm_wildcard = 'kernel-' . ($is_BOOT ? 'BOOT-' : '') . $main_ver . '*.rpm';
    my ($kernel_rpm) = my @kernels = glob("$RPMS/$rpm_wildcard");
    @kernels <= 1 or die "more than one kernel match $rpm_wildcard";
    @kernels >= 1 or return; # die "no kernel match $rpm_wildcard";

    install_kernel($kernel_rpm, 1);
}

sub install_kernel {
    my ($kernel_rpm, $o_update) = @_;

    my $kern_ver = rpm2version($kernel_rpm);
    my $dir = "all.kernels/$kern_ver";

    -d $dir and return if $o_update;

    my $is_BOOT = $kern_ver =~ /BOOT/;
    my ($main_ver) = $kern_ver =~ /^(2\.\d)/;
    if (my @previous = grep { ($is_BOOT xor !/BOOT/) && /^\Q$main_ver/ } all('all.kernels')) {
	warn "Removing previous kernels ", join(' ', @previous), "\n";
        rm_rf("all.kernels/$_") foreach @previous;
    }

    if (!$main || !-d "all.kernels/$main") {
	$main = $kern_ver;
	output('all.kernels/.main', "$main\n");
    }
    if ($is_BOOT && (!$main_BOOT || !-d "all.kernels/$main_BOOT")) {
	$main_BOOT = $kern_ver;
	output('all.kernels/.main-BOOT', "$main_BOOT\n");
    }

    warn "Installing kernel $kern_ver\n";

    eval { rm_rf($dir) };
    mkdir_p("$dir/modules");
    sys("rpm2cpio $kernel_rpm | (cd $dir ; cpio -id)");

    rename "$dir/boot/vmlinuz-$kern_ver", "$dir/vmlinuz" or die "can't find vmlinuz\n";

    sys("find $dir -name '*.gz' | xargs gunzip");

    my $ext = module_extension($kern_ver);

    open(my $F, "find $dir -name '*.$ext' |");
    my $file; while ($file = <$F>) {
	chomp($file);
	rename $file, "$dir/modules/" . basename($file) or warn "conflict for $file\n";
    }

    if (kernel_is_26($kern_ver)) {
        warn "no stripping on 2.6 since it breaks modules\n";
    } else {
        print STDERR "stripping $kern_ver: ";
        sys("./strip_modules $dir/modules/*.$ext");
        print STDERR "done\n";
    }

    sys('perl', 'modules.pl', 'remove_unneeded_modules', $kern_ver) if $kern_ver !~ /BOOT/;

    sys('perl', 'modules.pl', 'make_modules_per_image', $kern_ver);
    sys('perl', 'modules.pl', 'make_modules_description', $kern_ver) if $kern_ver eq $main;

    if (!$MOVE) {
        print STDERR "packdrake $kern_ver: ";
        sys("cd $dir/modules ; ls *.$ext | packdrake -b9s ../modules.cz 400000");
        print STDERR "done\n";
    }

    eval { rm_rf("$dir$_") } foreach qw(/boot /lib /usr /modules);
}