Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Here's a script to help migrating between kernel versions
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Kernel & Hardware
View previous topic :: View next topic  
Author Message
jkcunningham
l33t
l33t


Joined: 28 Apr 2003
Posts: 649
Location: 47.49N 121.79W

PostPosted: Mon Jun 21, 2004 7:50 pm    Post subject: Here's a script to help migrating between kernel versions Reply with quote

It seems like everytime I've compiled a new kernel there are new parameters in its .config, so that I end up going through make menuconfig all over again. Most of the settings are the same as my previous kernel (on the same machine), but I always miss a few settings, so it becomes an iterative process before I get it right. I've used diff to try to prevent this, but it has never been simple.

I got tired of this and wrote a perl script this morning that compares the two configs, leaves new parameters alone, moves old parameter values into the new config where they match, and lists the ones that are obsolete (don't appear in the new config at all).

Here it is. I hope someone else finds it useful.

-Jeff

Code:

#!/usr/bin/perl
## ----------------------------------------------------------------------
## Last Edit Time-stamp: <2004-06-21 12:38:23 jcunningham>
## ----------------------------------------------------------------------
##
##  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 can review the GNU General Public License by writing to the
##  Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
##  MA  02111-1307, USA.
##
## Please direct any comments to:
##
##  email: jeffrey@cunningham.net
##
#########################################################################

sub usage()
{
$0=~/[^\/]*$/;
$exe="$&";
print STDOUT <<EOF;

usage: $exe [-h] <sourcecfg> <targetcfg>
  -h           - (this) help
  <sourcecfg>  - source .config file
  <targetcfg>  - target .config file

This script is designed to ease the process of migrating between kernel
versions by setting one .config\'s parameters from another\'s whenever
they both exist. Parameters in the target .config that don\'t exist in the
source are left alone. The output .config is written to stdout. A log file
\'config.log\' is generated which details changes made and includes a list
of obsolete parameters.

This script is idempotent (i.e. repeated application results in no
additional changes).

Please direct any comments to:
 
  email: jeffrey\@cunningham.net

EOF
}

## Get the command line arguments
use Getopt::Std;
my $opt_string = 'h';
getopts( "$opt_string", \ my %opt ) or usage() and exit;
usage() and exit if $opt{h};
usage() and exit(255) if ($#ARGV+1 != 2);

my $source = $ARGV[0];
my $target = $ARGV[1];

open IF,$source;
my @sline = <IF>;
close IF;

open IF,$target;
my @tline = <IF>;
close IF;

open LOG,">config.log";

foreach $t (@tline)
{
   ($tp,$tv) = get_parm($t);

   # print non-parameter lines as they appear in target config
   print "$tp\n" and
      next if ($tv eq "noparm");

   if ($tv eq " is not set") {
      # This parameter is not set in target config...
      ($sv) = find_parm($tp,@sline);
      if ($sv eq "noparm") {
         # and doesn't exist in the source - leave it as is
         print "$t";
         print LOG "new parm: $t";
      }
      elsif ($sv ne " is not set") {
         # but set in the source - set it in the output
         print "$tp$sv\n";
         print LOG "* setting $tp=$sv\n";
      }
      else {
         # and not set in the source - leave it as is
         print "$t";
      }
   }
   else {
      # This parameter is set in target config....
      ($sv) = find_parm($tp,@sline);
      if ($sv eq " is not set") {
         # but unset in the source - unset it in the output
         print "# $tp$sv\n";
         print LOG "unsetting $tp\n";
      }
      elsif ($sv eq "noparm") {
         # but doesn't exist in the source - leave it as is
         print "$t";
      }
      else {
         # and set in source - leave it as is
         print "$t";
      }
   }
}

# find obsolete parameters and log them
print LOG "\n";
foreach $s (@sline)
{
   my ($sp,$sv) = get_parm($s);
   next if $sv eq "noparm";
   my ($tv) = find_parm($sp,@tline);
   if ($tv eq "noparm") {
      print LOG "obsolete: $s";
   }
}
close LOG;



sub find_parm
# Returns "y","m"," is not set" for parameter, or "noparm" if not found.
{
   local($parm,@line)=@_;
   foreach $s (@line)
   {
      my ($sp,$rv) = get_parm($s);
      next if $rv eq "noparm";
      next if $sp ne $parm;
      return ($rv);
   }
   return ("noparm");
}


sub get_parm
# Returns parameter name and value, or "noparm" if not a parameter
{
   local($line)=@_;
   chomp $line;

   # look for target parameters that are set
   if ($line =~ s/=.*$//) {
      local $val="$&";
      return ($line, $val);
   }
   # look for target parameters that are not set
   elsif ($line =~ s/\s*is not set\s*$//) {
      # strip the comment off the front
      $line =~ s/^[\#\s]+//;
      return ($line, " is not set");
   }
   else {
      # anything else is comments and blank lines - leave them alone
      return ($line, "noparm");
   }
}
Back to top
View user's profile Send private message
hensan
l33t
l33t


Joined: 26 Jun 2003
Posts: 868
Location: Sweden

PostPosted: Mon Jun 21, 2004 7:59 pm    Post subject: Reply with quote

It seems to me that what your script does is pretty much what make oldconfig already does, that is, take an old .config and add the new options. Or did I miss something?
Back to top
View user's profile Send private message
nesl247
Veteran
Veteran


Joined: 15 Jun 2004
Posts: 1614
Location: Florida

PostPosted: Mon Jun 21, 2004 8:05 pm    Post subject: Reply with quote

Cant ou just copy your old config and do make menuconfig? It just adds the new options when you save it..
Back to top
View user's profile Send private message
jkcunningham
l33t
l33t


Joined: 28 Apr 2003
Posts: 649
Location: 47.49N 121.79W

PostPosted: Mon Jun 21, 2004 8:13 pm    Post subject: Reply with quote

hensan: I didn't know about make oldconfig. I'll look into it. Perhaps this has just been a pointless perl exercise.

iotc247: I tried that once and it built a kernel that panicked. I am assuming it didn't get all the new parameteres correctly. Anyway, if what hensan says is correct, you're probably not, or there would be little point in having a 'make oldconfig' option.
Back to top
View user's profile Send private message
stahlsau
Guru
Guru


Joined: 09 Jan 2004
Posts: 584
Location: WildWestwoods

PostPosted: Mon Jun 21, 2004 10:42 pm    Post subject: Reply with quote

hi,
looks nice, but i think perl will be forever a book with seven seals for me...what could a line like that
Code:
$0=~/[^\/]*$/;
tell me? It looks to me like some cryptic sed-commands or s/t...which, again, is a thing i maybe won´t ever understand. :oops:
I´m always reminded of ascii-art when i see those lines ;-)
####
sry, didn´t want to bother you, i´m a lil new to linux & programming, so i try to understand most of the scripts/sources i see, but when it comes to things like this , i have no chance with my lil basic knowledge :-)
Back to top
View user's profile Send private message
stoffe
n00b
n00b


Joined: 13 Mar 2004
Posts: 45

PostPosted: Mon Jun 21, 2004 11:26 pm    Post subject: Reply with quote

stahlsau wrote:
hi,
looks nice, but i think perl will be forever a book with seven seals for me...what could a line like that
Code:
$0=~/[^\/]*$/;
tell me? It looks to me like some cryptic sed-commands or s/t...which, again, is a thing i maybe won´t ever understand. :oops:
I´m always reminded of ascii-art when i see those lines ;-)
####
sry, didn´t want to bother you, i´m a lil new to linux & programming, so i try to understand most of the scripts/sources i see, but when it comes to things like this , i have no chance with my lil basic knowledge :-)


A bit OT, but since you ask: That line noise is a regular expression, a powerful form of pattern matching which makes for advanced text search and manipulating. $0 is a variable, in this case the name of the script that was run. =~ in perl is a regex operator, meaning "run the following regular expression on the stuff before me". In its simplest form, regular expressions return true/false depending on if they find a match, but they can also manipulate the contents or return submatches etc.
Code:
/[^\/]*$/

is a typical regexp. What it does is matching from the end of the scriptname, everything that is not a forward slash, so the scriptname without the path. That is then extracted from another magic variable, $& (which I've never seen actually used before ;)) - it's kinda hackish, but gets the job done, so it's fine, Perl does not judge. ;) I would probably tell you to:
Code:
use File::Basename;
instead. :)

So, now you can spot those, and even if you don't know what they do, you know what they are at least. Actually, when you do start from the other end, with writing them, it is really easy - reading them is not until you get some experience - and then only the simpler ones. aybe one of the most powerful tools there are though, I really recommend it!

* http://perldoc.com/perl5.8.4/pod/perlre.html
* http://perldoc.com/perl5.8.4/pod/perlreref.html
* http://perldoc.com/perl5.8.4/pod/perlfaq6.html

Almost all languages, be it PHP, Ruby, Python, C# or Java uses a Perl-compatible syntax for this, so it pays of to learn. Good text editors also has this functionality. :)

Sorry for the OT.
Back to top
View user's profile Send private message
pjp
Administrator
Administrator


Joined: 16 Apr 2002
Posts: 20588

PostPosted: Mon Jun 21, 2004 11:39 pm    Post subject: Reply with quote

Moved from Installing Gentoo.
_________________
Quis separabit? Quo animo?
Back to top
View user's profile Send private message
zbindere
Guru
Guru


Joined: 27 May 2004
Posts: 356
Location: Switzerland

PostPosted: Tue Jun 22, 2004 8:36 am    Post subject: Reply with quote

i think too:
Code:
make oldconfig

will do the same.
Back to top
View user's profile Send private message
nesl247
Veteran
Veteran


Joined: 15 Jun 2004
Posts: 1614
Location: Florida

PostPosted: Tue Jun 22, 2004 1:46 pm    Post subject: Reply with quote

Well i guess i have been getting my kernel to work the wrong way then the whole time.. Ive used make menuconfig which is to modify your old config file... So i must have misunderstood that too... Lol make menuconfig is to modify your old .config file.. Just wanted to let you know.. You dont need that script..
Back to top
View user's profile Send private message
jkcunningham
l33t
l33t


Joined: 28 Apr 2003
Posts: 649
Location: 47.49N 121.79W

PostPosted: Tue Jun 22, 2004 2:41 pm    Post subject: Reply with quote

Yeah, it looks that way. The one thing my script does that make oldconfig doesn't do is give you a list of the dropped parameters.

Oh well...
Back to top
View user's profile Send private message
stahlsau
Guru
Guru


Joined: 09 Jan 2004
Posts: 584
Location: WildWestwoods

PostPosted: Wed Jun 23, 2004 12:04 pm    Post subject: Reply with quote

@stoffe:
thanks for that explanation. It really looks interesting, i´ll take a deeper look at those regular expressions in the next time. As said before, it really annoyes me if i don´t understand something, so i have to learn this ;-)
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Kernel & Hardware All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum