[arch-projects] [mkinitcpio][PATCH 3/4] initalize buildroot later, and out outside of hooks

Dave Reisner dreisner at archlinux.org
Sun Jul 15 10:33:51 EDT 2012


mkinitcpio needs to be able to more tightly control the layout of the
image. By moving this outside the control of the base hook, it's more
reasonable to believe that a drop-in replacement for the base hook could
be written, as it now only contains the utilities required for the /init
that it provides.

Being that initialize_buildroot is outside of a hook, we can make "raw"
calls to coreutils that create the directories and symlinks, saving some
overhead and forking.

This also symlinks /usr/local/{lib,bin} into the common /usr/lib and
/usr/bin, respectively, so that there's always a way to override libs
and bins, and they're automatically pulled into our strict early init
path of /usr/bin.

Finally, we lump all this generally a little "later" in the early setup.
This addresses an unseen "bug" wherein an extra temp directory that
never gets used is created when leveraging a preset.

Signed-off-by: Dave Reisner <dreisner at archlinux.org>
---
The lofty goal this is wanted for is possible/eventual inclusion of systemd in
early userspace.

 functions    | 26 ++++++++++++++++++++++++++
 install/base | 11 +----------
 mkinitcpio   | 35 +++++++++--------------------------
 3 files changed, 36 insertions(+), 36 deletions(-)

diff --git a/functions b/functions
index e9fb81a..e17e46e 100644
--- a/functions
+++ b/functions
@@ -606,6 +606,32 @@ write_image_config() {
     ) >"$BUILDROOT/config"
 }
 
+initialize_buildroot() {
+    # creates a temporary directory for the buildroot and initialize it with a
+    # basic set of necessary directories and symlinks
+
+    local workdir= kernver=$1
+
+    if ! workdir=$(mktemp -d --tmpdir mkinitcpio.XXXXXX); then
+        error 'Failed to create temporary working directory in %s' "${TMPDIR:-/tmp}"
+        return 1
+    fi
+
+    # base directory structure
+    install -dm755 "$workdir/root"/{new_root,proc,sys,dev,run,tmp,usr/{local,lib,bin}}
+    ln -s "usr/lib" "$workdir/root/lib"
+    ln -s "../lib"  "$workdir/root/usr/local/lib"
+    ln -s "bin"     "$workdir/root/usr/sbin"
+    ln -s "usr/bin" "$workdir/root/bin"
+    ln -s "usr/bin" "$workdir/root/sbin"
+    ln -s "../bin"  "$workdir/root/usr/local/bin"
+
+    # kernel module dir
+    install -dm755 "$workdir/root/usr/lib/modules/$kernver/kernel"
+
+    printf '%s' "$workdir"
+}
+
 run_build_hook() {
     local hook=$1 script= realscript=
     local MODULES= BINARIES= FILES= SCRIPT=
diff --git a/install/base b/install/base
index 30ad3fa..7bb23a8 100644
--- a/install/base
+++ b/install/base
@@ -1,16 +1,7 @@
 #!/bin/bash
 
 build() {
-    local dir applet
-
-    for dir in new_root proc sys dev run tmp usr/bin; do
-        add_dir "/$dir"
-    done
-
-    add_symlink /lib usr/lib
-    add_symlink /sbin usr/bin
-    add_symlink /bin usr/bin
-    add_symlink /usr/sbin bin
+    local applet
 
     add_binary /usr/lib/initcpio/busybox /bin/busybox
 
diff --git a/mkinitcpio b/mkinitcpio
index 0834dc0..5202e7f 100755
--- a/mkinitcpio
+++ b/mkinitcpio
@@ -195,7 +195,7 @@ while :; do
             SHOW_AUTOMODS=1 ;;
         -t|--builddir)
             shift
-            TMPDIR=$1 ;;
+            export TMPDIR=$1 ;;
         -z|--compress)
             shift
             optcompress=$1 ;;
@@ -232,24 +232,6 @@ readonly NC BOLD BLUE GREEN RED YELLOW
 [[ -e /proc/self/mountinfo ]] || die "/proc must be mounted!"
 [[ -e /dev/fd ]] || die "/dev must be mounted!"
 
-KERNELVERSION=$(resolve_kernver "$optkver") || cleanup 1
-
-if [[ $TMPDIR ]]; then
-    if [[ ! -d $TMPDIR ]]; then
-        error "Temporary directory does not exist or is not a directory: \`%s'" "$TMPDIR"
-        cleanup 1
-    fi
-    if [[ ! -w $TMPDIR ]]; then
-        error "Temporary directory is not writeable: \`%s'" "$TMPDIR"
-        cleanup 1
-    fi
-fi
-workdir=$(TMPDIR=$TMPDIR mktemp -d --tmpdir mkinitcpio.XXXXXX)
-BUILDROOT=$workdir/root
-
-# explicitly create the buildroot
-mkdir -p "$BUILDROOT/usr/lib/modules/$KERNELVERSION/kernel"
-
 # use preset $PRESET
 if [[ $PRESET ]]; then
     # allow absolute path to preset file, else resolve it
@@ -304,10 +286,14 @@ if [[ $PRESET ]]; then
     fi
 fi
 
-if [[ ! -f $CONFIG ]]; then
-    die "Config file does not exist: \`%s'" "$CONFIG"
-fi
-. "$CONFIG"
+KERNELVERSION=$(resolve_kernver "$optkver") || cleanup 1
+MODULEDIR=$(find_moduledir "$KERNELVERSION") || cleanup 1
+
+# initialize the working directory and buildroot
+workdir=$(initialize_buildroot "$KERNELVERSION") || cleanup 1
+BUILDROOT=$workdir/root
+
+. "$CONFIG" || die "Failed to read configuration \`%s'" "$CONFIG"
 
 # after returning, hooks are populated into the array 'hooks'
 # HOOKS should not be referenced from here on
@@ -317,8 +303,6 @@ if (( ${#hooks[*]} == 0 )); then
     die "Invalid config: No hooks found"
 fi
 
-MODULEDIR=$(find_moduledir "$KERNELVERSION") || cleanup 1
-
 if [[ ! -d $MODULEDIR ]]; then
     die "'$MODULEDIR' is not a valid kernel module directory"
 fi
@@ -332,7 +316,6 @@ if (( SHOW_AUTOMODS )); then
     cleanup 0
 fi
 
-
 if [[ -z $GENIMG ]]; then
     msg "Starting dry run: %s" "$KERNELVERSION"
 else
-- 
1.7.11.2



More information about the arch-projects mailing list