From 3ee0b9bb74b0331e0aae0bb452074ef14967393f Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 17 Mar 2008 10:40:47 +0000 Subject: * makeInitrd, makeModulesClosure: moved from NixOS. * Use sh from klibc in the initrd. svn path=/nixpkgs/trunk/; revision=11154 --- pkgs/build-support/kernel/make-initrd.nix | 31 ++++++++++++ pkgs/build-support/kernel/make-initrd.sh | 39 +++++++++++++++ pkgs/build-support/kernel/modules-closure.nix | 13 +++++ pkgs/build-support/kernel/modules-closure.sh | 36 ++++++++++++++ pkgs/build-support/kernel/paths-from-graph.pl | 68 +++++++++++++++++++++++++++ 5 files changed, 187 insertions(+) create mode 100644 pkgs/build-support/kernel/make-initrd.nix create mode 100644 pkgs/build-support/kernel/make-initrd.sh create mode 100644 pkgs/build-support/kernel/modules-closure.nix create mode 100644 pkgs/build-support/kernel/modules-closure.sh create mode 100644 pkgs/build-support/kernel/paths-from-graph.pl (limited to 'pkgs/build-support/kernel') diff --git a/pkgs/build-support/kernel/make-initrd.nix b/pkgs/build-support/kernel/make-initrd.nix new file mode 100644 index 000000000000..b62ed9840456 --- /dev/null +++ b/pkgs/build-support/kernel/make-initrd.nix @@ -0,0 +1,31 @@ +# Create an initial ramdisk containing the closure of the specified +# file system objects. An initial ramdisk is used during the initial +# stages of booting a Linux system. It is loaded by the boot loader +# along with the kernel image. It's supposed to contain everything +# (such as kernel modules) necessary to allow us to mount the root +# file system. Once the root file system is mounted, the `real' boot +# script can be called. +# +# An initrd is really just a gzipped cpio archive. +# +# Symlinks are created for each top-level file system object. E.g., +# `contents = {object = ...; symlink = /init;}' is a typical +# argument. + +{stdenv, perl, cpio, contents}: + +stdenv.mkDerivation { + name = "initrd"; + builder = ./make-initrd.sh; + buildInputs = [perl cpio]; + + # !!! should use XML. + objects = map (x: x.object) contents; + symlinks = map (x: x.symlink) contents; + suffices = map (x: if x ? suffix then x.suffix else "none") contents; + + # For obtaining the closure of `contents'. + exportReferencesGraph = + map (x: [("closure-" + baseNameOf x.symlink) x.object]) contents; + pathsFromGraph = ./paths-from-graph.pl; +} diff --git a/pkgs/build-support/kernel/make-initrd.sh b/pkgs/build-support/kernel/make-initrd.sh new file mode 100644 index 000000000000..d930e51a03f9 --- /dev/null +++ b/pkgs/build-support/kernel/make-initrd.sh @@ -0,0 +1,39 @@ +source $stdenv/setup + +set -o pipefail + +objects=($objects) +symlinks=($symlinks) +suffices=($suffices) + +mkdir root + +# Needed for splash_helper, which gets run before init. +mkdir root/dev +mkdir root/sys +mkdir root/proc + + +for ((n = 0; n < ${#objects[*]}; n++)); do + object=${objects[$n]} + symlink=${symlinks[$n]} + suffix=${suffices[$n]} + if test "$suffix" = none; then suffix=; fi + + mkdir -p $(dirname root/$symlink) + ln -s $object$suffix root/$symlink +done + + +# Get the paths in the closure of `object'. +storePaths=$(perl $pathsFromGraph closure-*) + + +# Paths in cpio archives *must* be relative, otherwise the kernel +# won't unpack 'em. +(cd root && cp -prd --parents $storePaths .) + + +# Put the closure in a gzipped cpio archive. +ensureDir $out +(cd root && find * -print0 | cpio -ov -H newc --null | gzip -9 > $out/initrd) diff --git a/pkgs/build-support/kernel/modules-closure.nix b/pkgs/build-support/kernel/modules-closure.nix new file mode 100644 index 000000000000..86015a8b182c --- /dev/null +++ b/pkgs/build-support/kernel/modules-closure.nix @@ -0,0 +1,13 @@ +# Given a kernel build (with modules in $kernel/lib/modules/VERSION), +# produce a module tree in $out/lib/modules/VERSION that contains only +# the modules identified by `rootModules', plus their dependencies. +# Also generate an appropriate modules.dep. + +{stdenv, kernel, rootModules, module_init_tools}: + +stdenv.mkDerivation { + name = kernel.name + "-shrunk"; + builder = ./modules-closure.sh; + inherit kernel rootModules module_init_tools; + allowedReferences = ["out"]; +} diff --git a/pkgs/build-support/kernel/modules-closure.sh b/pkgs/build-support/kernel/modules-closure.sh new file mode 100644 index 000000000000..d16ff46ecb99 --- /dev/null +++ b/pkgs/build-support/kernel/modules-closure.sh @@ -0,0 +1,36 @@ +source $stdenv/setup + +set -o pipefail + +PATH=$module_init_tools/sbin:$PATH + +version=$(cd $kernel/lib/modules && ls -d *) + +echo "kernel version is $version" + +export MODULE_DIR=$kernel/lib/modules/ + +# Determine the dependencies of each root module. +closure= +for module in $rootModules; do + echo "root module: $module" + deps=$(modprobe --config /dev/null --set-version "$version" --show-depends "$module" \ + | sed 's/^insmod //') + #for i in $deps; do echo $i; done + closure="$closure $deps" +done + +echo "closure:" +ensureDir $out +for module in $closure; do + target=$(echo $module | sed "s^$kernel^$out^") + if test -e "$target"; then continue; fi + echo $module + mkdir -p $(dirname $target) + cp $module $target + grep "^$module" $kernel/lib/modules/$version/modules.dep \ + | sed "s^$kernel^$out^g" \ + >> $out/lib/modules/$version/modules.dep + echo $target >> $out/insmod-list +done + diff --git a/pkgs/build-support/kernel/paths-from-graph.pl b/pkgs/build-support/kernel/paths-from-graph.pl new file mode 100644 index 000000000000..43ce4e68db44 --- /dev/null +++ b/pkgs/build-support/kernel/paths-from-graph.pl @@ -0,0 +1,68 @@ +use strict; +use File::Basename; + +my %storePaths; +my %refs; + +foreach my $graph (@ARGV) { + open GRAPH, "<$graph" or die; + + while () { + chomp; + my $storePath = "$_"; + $storePaths{$storePath} = 1; + + my $deriver = ; chomp $deriver; + my $count = ; chomp $count; + + my @refs = (); + for (my $i = 0; $i < $count; ++$i) { + my $ref = ; chomp $ref; + push @refs, $ref; + } + $refs{$storePath} = \@refs; + + } + + close GRAPH; +} + + +if ($ENV{"printManifest"} eq "1") { + print "version {\n"; + print " ManifestVersion: 3\n"; + print "}\n"; + + foreach my $storePath (sort (keys %storePaths)) { + my $base = basename $storePath; + print "localPath {\n"; + print " StorePath: $storePath\n"; + print " CopyFrom: /tmp/inst-store/$base\n"; + print " References: "; + foreach my $ref (@{$refs{$storePath}}) { + print "$ref "; + } + print "\n"; + print "}\n"; + } +} + +elsif ($ENV{"printRegistration"} eq "1") { + # This is the format used by `nix-store --register-validity + # --hash-given' / `nix-store --load-db'. + foreach my $storePath (sort (keys %storePaths)) { + print "$storePath\n"; + print "0000000000000000000000000000000000000000000000000000000000000000\n"; # !!! fix + print "\n"; # don't care about preserving the deriver + print scalar(@{$refs{$storePath}}), "\n"; + foreach my $ref (@{$refs{$storePath}}) { + print "$ref\n"; + } + } +} + +else { + foreach my $storePath (sort (keys %storePaths)) { + print "$storePath\n"; + } +} -- cgit 1.4.1