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

# this script takes kernels rpms from RPMS/ and create various files in all_kernels/
# for each rpms in RPMS, it does:
#   RPMS/kernel-VER-*.rpm -> 
#     all.kernels/VER/all_modules.tar  (used for all.rdz)
#     all.kernels/VER/*.mar            (used for other rdz)
#     all.kernels/VER/modules.cz       (used for stage2)
#     all.kernels/VER/modules.dep
#     all.kernels/VER/vmlinuz
#
# if RPMS is empty, this script tries to find kernels in $main_repository

use MDK::Common;
use list_modules;

my $main_repository = '/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";

eval { rm_rf('all.kernels') }; #- remove old things
mkdir 'all.kernels';

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

if (@ARGV) {
    install_kernel($ARGV[0]);
} elsif (! -d 'RPMS') {
    update_kernel_from_repository($main_repository, '2.6', 0);
    update_kernel_from_repository($main_repository, '2.6', 1);
    install_kernel(glob("$main_repository/kernel-xbox*"), 1) if arch() =~ /i.86/;
}

extract_kernel($_) foreach glob("RPMS/*.rpm");

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 ($repository, $main_ver, $is_BOOT) = @_;
    my $rpm_wildcard = 'kernel-' . ($is_BOOT ? 'BOOT-' : (arch() =~ /i.86/ ? 'i586-up-1GB-' : '')) . $main_ver . '*.rpm';
    my @kernels = glob("$repository/$rpm_wildcard");
    
    install_kernel($_, 1) foreach @kernels;
}

sub install_kernel {
    my ($kernel_rpm) = @_;
    my $basename = basename($kernel_rpm);
    warn "Installing rpm $basename in RPMS\n";
    mkdir 'RPMS';
    cp_af($kernel_rpm, "RPMS/$basename");

    if (!$main) {
	$main = rpm2version($kernel_rpm);
	output('RPMS/.main', "$main\n");
    }
    if (!$main_BOOT && $kernel_rpm =~ /BOOT/) {
	$main_BOOT = rpm2version($kernel_rpm);
	output('RPMS/.main-BOOT', "$main_BOOT\n");
    }
}

sub extract_kernel {
    my ($kernel_rpm) = @_;

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

    warn "Extracting 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);
}