gs / update_kernel.pl

#!/usr/bin/env perl
#
# name:      update Gentoo kernel
# author:    Xu Xiaodong <xxdlhy@gmail.com>
# license:   GPL
# created:   2011 Apr  3
# modified:  2011 Oct 28
#

use strict;
use warnings;

use File::Copy;
use Getopt::Long;
use autodie;

my $ver   = '';
my $r_ver = '';
my $help  = 0;

GetOptions(
    'v=s' => \$ver,
    'r=s' => \$r_ver,
    'h'   => \$help,
);

if ($help) {
    ( my $me = $0 ) =~ s!^.*/!!g;

    say <<"USAGE";

    Usage: $me -v <ver> -r <r_ver> -h

    -v version number
    -r revision number
    -h show this help
USAGE

    exit;
}

if ( $ver and $r_ver ) {
    build_kernel( $ver, $r_ver );
    build_module();
    edit_grub( $ver, $r_ver );
}
else {
    say "Oops...";
}

sub build_kernel {
    my ( $v, $r_v ) = @_;

    my $path = '/usr/src/linux';
    chdir $path;

    copy( "../.config", ".config" );
    system "make oldconfig";
    system "make && make modules_install";
    copy( "arch/i386/boot/bzImage", "/boot/kernel-$v-gentoo-r$r_v" );
    copy( ".config",                "../.config" );
}

sub build_module {
    system "module-rebuild rebuild";
}

sub edit_grub {
    my ( $v, $r_v ) = @_;

    my $line = <<"LINE";
title Gentoo Linux $v-r$r_v
root (hd0,0)
kernel /boot/kernel-$v-gentoo-r$r_v root=/dev/sda3
LINE

    open my $grub_conf, '+<', '/boot/grub/grub.conf';
    seek( $grub_conf, 424, 0 );
    say $grub_conf $line;

    close $grub_conf;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.