[linux-base] 01/04: Add read_kernelimg_conf() function to Perl module

debian-kernel at lists.debian.org debian-kernel at lists.debian.org
Mon May 30 17:53:33 UTC 2016


This is an automated email from the git hooks/post-receive script.

benh pushed a commit to branch benh/maint-script-helpers
in repository linux-base.

commit 9977e3b63ba7d7f5bfcd2371505de245cf3e4000
Author: Ben Hutchings <ben at decadent.org.uk>
Date:   Mon May 30 00:37:14 2016 +0100

    Add read_kernelimg_conf() function to Perl module
    
    This should support the parameters that are still useful:
    
    - do_symlinks and link_in_boot are set by the installer in at least some
      cases, and are useful
    
    - image_dest is easy to support along with link_in_boot
    
    - do_bootloader and do_initrd are also still set by the installer but are
      already ignored by linux maintainer scripts.  Quietly ignore them.
    
    - The no_symlinks (i.e. copy files) and use_hard_links options were
      broken along with do_initrd, since it's not possible to copy or hard-
      link an initrd that hasn't been built yet!  We should warn if they are
      set - except that no_symlinks is set (to the default of 0) by the
      installer, so we only warn if it's changed.
    
    - I can't work out what the effect of minimal_swap is supposed to be, so
      don't support it
    
    - {post,pre}{inst,rm}_hook have been deprecated in kernel-package and
      have not been set by the installer for some time.  linux-2.6 added
      support for hook directories in squeeze and kernel-package had them
      earlier than that.
---
 debian/changelog    |   1 +
 lib/DebianLinux.pm  |  74 +++++++++++++++++++++++++++++-
 lib/t/DebianLinux.t | 128 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 200 insertions(+), 3 deletions(-)

diff --git a/debian/changelog b/debian/changelog
index 878f41d..88cb523 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -3,6 +3,7 @@ linux-base (4.1) UNRELEASED; urgency=medium
   * Adjust for migration to git:
     - Add .gitignore files
     - debian/control: Update Vcs-* fields (Closes: #824748)
+  * Add read_kernelimg_conf() function to Perl module
 
  -- Ben Hutchings <ben at decadent.org.uk>  Tue, 11 Aug 2015 19:12:59 +0200
 
diff --git a/lib/DebianLinux.pm b/lib/DebianLinux.pm
index 1db70e1..190461e 100644
--- a/lib/DebianLinux.pm
+++ b/lib/DebianLinux.pm
@@ -19,11 +19,12 @@ package DebianLinux;
 use strict;
 use warnings;
 use POSIX qw(uname);
+use FileHandle;
 
 BEGIN {
     use Exporter ();
     our @ISA = qw(Exporter);
-    our @EXPORT_OK = qw(version_cmp image_list);
+    our @EXPORT_OK = qw(version_cmp image_list read_kernelimg_conf);
 }
 
 sub version_split {
@@ -90,4 +91,75 @@ sub image_list {
     return @results;
 }
 
+sub read_kernelimg_conf {
+    my $conf_loc = shift || '/etc/kernel-img.conf';
+    my @bool_param = qw(do_symlinks link_in_boot no_symlinks);
+    my @path_param = qw(image_dest);
+    # These are still set in the jessie installer even though they
+    # have no effect.  Ignore them quietly.
+    my @quiet_param = qw(do_bootloader do_initrd);
+
+    # Initialise configuration to defaults
+    my $conf = {
+	do_symlinks =>		1,
+	image_dest =>		'/',
+	link_in_boot =>		0,
+	no_symlinks =>		0,
+    };
+
+    if (my $fh = new FileHandle($conf_loc, 'r')) {
+	while (<$fh>) {
+	    # Delete line endings, comments and blank lines
+	    chomp;
+	    s/\#.*$//g;
+	    next if /^\s*$/;
+
+	    # Historically this was done by matching against one
+	    # (path) or two (bool) regexps per parameter, with no
+	    # attempt to ensure that each line matched one.  We now
+	    # warn about syntax errors, but for backward compatibility
+	    # we never treat them as fatal.
+
+	    # Parse into name = value
+	    if (!/^\s*(\w+)\s*=\s*(.*)/) {
+		print STDERR "$conf_loc:$.: W: ignoring line with syntax error\n";
+		next;
+	    }
+	    my ($name, $value) = (lc($1), $2);
+
+	    # Parse value according to expected type
+	    if (grep({$_ eq $name} @bool_param)) {
+		if ($value =~ /^(?:no|false|0)\s*$/i) {
+		    $conf->{$name} = 0;
+		} elsif ($value =~ /^(?:yes|true|1)\s*$/i) {
+		    $conf->{$name} = 1;
+		} else {
+		    print STDERR "$conf_loc:$.: W: ignoring invalid value for $name\n";
+		}
+	    } elsif (grep({$_ eq $name} @path_param)) {
+		# Only one space-separated word is supported
+		$value =~ /^(\S*)(.*)/;
+		($conf->{$name}, my $excess) = ($1, $2);
+		if ($excess =~ /\S/) {
+		    print STDERR "$conf_loc:$.: W: ignoring excess values for $name\n";
+		}
+	    } elsif (grep({$_ eq $name} @quiet_param)) {
+		;
+	    } else {
+		print STDERR "$conf_loc:$.: W: ignoring unknown parameter $name\n";
+	    }
+	}
+	$fh->close();
+    }
+
+    # This is still set (to 0) by default in jessie so we should only
+    # warn if the default is changed
+    if ($conf->{no_symlinks}) {
+	print STDERR "$conf_loc: W: ignoring no_symlinks; only symlinks are supported\n";
+    }
+    delete $conf->{no_symlinks};
+
+    return $conf;
+}
+
 1;
diff --git a/lib/t/DebianLinux.t b/lib/t/DebianLinux.t
index 2c3ca8a..e11d8a9 100644
--- a/lib/t/DebianLinux.t
+++ b/lib/t/DebianLinux.t
@@ -2,12 +2,14 @@ use strict;
 use warnings;
 use Test;
 
-use DebianLinux qw(version_cmp);
+use DebianLinux qw(version_cmp read_kernelimg_conf);
 
 BEGIN {
-    plan test => 34;
+    plan test => 40;
 }
 
+## version_cmp
+
 # Simple numeric comparison
 ok(version_cmp('2', '2'), 0);
 ok(version_cmp('2', '3'), -1);
@@ -52,3 +54,125 @@ ok(version_cmp('2.6.32-local', '2.6.32-1'), 1);
 # Hyphen < dot
 ok(version_cmp('2.6.32-2', '2.6.32.1'), -1);
 ok(version_cmp('2.6.32.1', '2.6.32-2'), 1);
+
+## read_kernelimg_conf
+
+sub read_kernelimg_conf_str {
+    use File::Temp ();
+
+    my $str = shift;
+
+    my $fh = File::Temp->new() or die "$!";
+    $fh->print($str) or die "$!";
+    $fh->close();
+
+    return read_kernelimg_conf($fh->filename);
+}
+
+sub hash_equal {
+    my ($left, $right) = @_;
+
+    # 'Smart equality' only compares keys
+    return 0 unless %$left ~~ %$right;
+
+    for my $key (keys(%$left)) {
+	die "hash is too complex" unless (ref($left->{$key}) eq '' &&
+					  ref($right->{$key}) eq '');
+	return 0 unless $left->{$key} eq $right->{$key};
+    }
+
+    return 1;
+}
+
+# Empty config
+ok(hash_equal(read_kernelimg_conf_str(''),
+	      {
+		  do_symlinks =>	1,
+		  image_dest =>		'/',
+		  link_in_boot =>	0,
+	      }));
+# Sample config
+ok(hash_equal(read_kernelimg_conf_str(<< 'EOT'),
+# This is a sample /etc/kernel-img.conf file
+# See kernel-img.conf(5) for details
+
+# If you want the symbolic link (or image, if move_image is set) to be
+# stored elsewhere than / set this variable to the dir where you
+# want the symbolic link.  Please note that this is not a Boolean
+# variable.  This may be of help to loadlin users, who may set both
+# this and move_image. Defaults to /. This can be used in conjunction
+# with all above options except link_in_boot, which would not make
+# sense.  (If both image_dest and link_in_boot are set, link_in_boot
+# overrides).
+image_dest = /
+
+# This option manipulates the build link created by recent kernels. If
+# the link is a dangling link, and if a the corresponding kernel
+# headers appear to have been installed on the system, a new symlink
+# shall be created to point to them.
+#relink_build_link = YES
+
+# If set, the preinst shall silently try to move /lib/modules/version
+# out of the way if it is the same version as the image being
+# installed. Use at your own risk.
+#clobber_modules = NO
+
+# If set, does not prompt to continue after a depmod problem in the
+# postinstall script.  This facilitates automated installs, though it
+# may mask a problem with the kernel image. A diag‐ nostic is still
+# issued. This is unset be default.
+# ignore_depmod_err = NO
+
+# These setting are for legacy postinst scripts only. newer postinst
+# scripts from the kenrel-package do not use them
+do_symlinks = yes
+do_bootloader = no
+do_initrd=yes
+link_in_boot=no
+EOT
+	      {
+		  do_symlinks =>	1,
+		  image_dest =>		'/',
+		  link_in_boot =>	0,
+	      }));
+# Slightly different spacing and value syntax
+ok(hash_equal(read_kernelimg_conf_str(<< 'EOT'),
+image_dest = foo bar
+	relink_build_link = yes
+do_symlinks = 0    
+    link_in_boot= true
+no_symlinks=1
+EOT
+	      {
+		  do_symlinks =>	0,
+		  image_dest =>		'foo',
+		  link_in_boot =>	1,
+	      }));
+# Check that 'false' and 'no' also work
+ok(hash_equal(read_kernelimg_conf_str(<< 'EOT'),
+do_symlinks = false
+EOT
+	      {
+		  do_symlinks =>	0,
+		  image_dest =>		'/',
+		  link_in_boot =>	0,
+	      }));
+ok(hash_equal(read_kernelimg_conf_str(<< 'EOT'),
+do_symlinks = no
+EOT
+	      {
+		  do_symlinks =>	0,
+		  image_dest =>		'/',
+		  link_in_boot =>	0,
+	      }));
+# Check that invalid values have no effect
+ok(hash_equal(read_kernelimg_conf_str(<< 'EOT'),
+do_symlinks=
+link_in_boot yes
+link_in_boot 1
+EOT
+	      {
+		  do_symlinks =>	1,
+		  image_dest =>		'/',
+		  link_in_boot =>	0,
+	      }));

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/kernel/linux-base.git



More information about the Kernel-svn-changes mailing list