[arch-projects] [mkinitcpio][PATCH] mkinitcpio: remove --basedir option

Dave Reisner dreisner at archlinux.org
Thu May 3 14:21:58 EDT 2012


This option is just a bad idea. Initramfs creation is too important to
get wrong, and running it from outside the root FS has too many gotchas,
the worst of them being:

- where do you pull hooks from?
- how do you resolve binary dependencies within the root?

In general, dealing with the extra luggage of the base directory makes the
codebase more complicated than it needs to be (see all the '_' prefixed
functions which are called from add functions). In favor of simplifying the
code, and making it more maintainable, kill this off and force the sane
option of chroot'ing into an install if the need arises.

Signed-off-by: Dave Reisner <dreisner at archlinux.org>
---
I suspect that no one will miss this option. I certainly won't. However,
please speak up if you really do think you'll miss this option, and make
your case for keeping it around.

If no one objects, I'll be removing this with next proper release.

 functions           |   48 ++++++++++++++----------------------------------
 install/autodetect  |    4 ++--
 install/consolefont |    6 +++---
 install/fsck        |    4 ++--
 install/keymap      |    2 +-
 mkinitcpio          |   41 +++++++++++++----------------------------
 mkinitcpio.8.txt    |    4 ----
 7 files changed, 35 insertions(+), 74 deletions(-)

diff --git a/functions b/functions
index 0fec85b..2576180 100644
--- a/functions
+++ b/functions
@@ -192,26 +192,6 @@ in_array() {
     return 1 # Not Found
 }
 
-pathlookup() {
-    # a basedir aware 'type -P' (or which) for executables
-    #   $1: binary to find
-
-    local path=
-    local -a paths=
-
-    IFS=: read -r -a paths <<< "$PATH"
-
-    for path in "${paths[@]}"; do
-        [[ ${path:0:1} = [.~] ]] && continue
-        if [[ -x $BASEDIR$path/$1 ]]; then
-            printf '%s' "$BASEDIR$path/$1"
-            return 0
-        fi
-    done
-
-    return 1
-}
-
 _add_file() {
     # add a file to $BUILDROOT
     #   $1: pathname on initcpio
@@ -266,7 +246,7 @@ auto_modules() {
     IFS=$'\n' read -rd '' -a mods < \
         <(find /sys/devices -name modalias -exec sort -u {} + |
         # delimit each input by a newline, expanded in place
-        xargs -d $'\n' modprobe -qd "$BASEDIR" -aRS "$KERNELVERSION" |
+        xargs -d $'\n' modprobe -qaRS "$KERNELVERSION" |
         sort -u)
 
     printf "%s\n" "${mods[@]//-/_}"
@@ -333,12 +313,12 @@ add_module() {
                 done
                 ;;
             firmware)
-                if [[ -e "$BASEDIR/usr/lib/firmware/$value" ]]; then
-                    _add_file "/usr/lib/firmware/$value" "$BASEDIR/usr/lib/firmware/$value" 644
+                if [[ -e /usr/lib/firmware/$value ]]; then
+                    _add_file "/usr/lib/firmware/$value" "/usr/lib/firmware/$value" 644
                 fi
                 ;;
         esac
-    done < <(modinfo -b "$BASEDIR" -k "$KERNELVERSION" -0 "$module" 2>/dev/null)
+    done < <(modinfo -k "$KERNELVERSION" -0 "$module" 2>/dev/null)
 
     if [[ -z $path ]]; then
         (( ign_errors )) && return 0
@@ -410,7 +390,7 @@ add_file() {
     # determine source and destination
     local src= dest=${2:-$1} mode=
 
-    src=$BASEDIR$1
+    src=$1
 
     [[ -f "$src" ]] || { error "file not found: \`%s'" "$src"; return 1; }
 
@@ -420,7 +400,7 @@ add_file() {
         return 1
     fi
 
-    _add_file "${dest#$BASEDIR}" "$src" "$mode"
+    _add_file "$dest" "$src" "$mode"
 }
 
 add_binary() {
@@ -433,9 +413,9 @@ add_binary() {
     local line= regex= binary= dest= mode= sodep= resolved= dirname=
 
     if [[ ${1:0:1} != '/' ]]; then
-        binary=$(pathlookup "$1")
+        binary=$(type -P "$1")
     else
-        binary=$BASEDIR$1
+        binary=$1
     fi
 
     [[ -f "$binary" ]] || { error "file not found: \`%s'" "$1"; return 1; }
@@ -444,7 +424,7 @@ add_binary() {
     mode=$(stat -c %a "$binary")
 
     # always add the binary itself
-    _add_file "${dest#$BASEDIR}" "$binary" "$mode"
+    _add_file "$dest" "$binary" "$mode"
 
     lddout=$(ldd "$binary" 2>/dev/null) || return 0 # not a binary!
 
@@ -455,13 +435,13 @@ add_binary() {
 
         if [[ -f $sodep && ! -e $BUILDROOT$sodep ]]; then
             if [[ ! -L $sodep ]]; then
-                _add_file "$sodep" "$BASEDIR$sodep" "$(stat -c %a "$sodep")"
+                _add_file "$sodep" "$sodep" "$(stat -c %a "$sodep")"
             else
-                resolved=$(readlink -e "$BASEDIR$sodep")
+                resolved=$(readlink -e "$sodep")
                 dirname=${resolved%/*}
-                _add_dir "${dirname#$BASEDIR}" 755
-                _add_symlink "$sodep" "${resolved#$BASEDIR}"
-                _add_file "${resolved#$BASEDIR}" "$resolved" 755
+                _add_dir "$dirname" 755
+                _add_symlink "$sodep" "$resolved"
+                _add_file "$resolved" "$resolved" 755
             fi
         fi
     done <<< "$lddout"
diff --git a/install/autodetect b/install/autodetect
index 4ed7ccf..1daa429 100644
--- a/install/autodetect
+++ b/install/autodetect
@@ -25,7 +25,7 @@ build() {
     auto_modules >"$MODULE_FILE"
 
     # detect filesystem for root
-    if rootfstype=$(findmnt -uno fstype "${BASEDIR:-/}"); then
+    if rootfstype=$(findmnt -uno fstype '/'); then
         add_if_avail "$rootfstype"
     else
         error "failed to detect root filesystem"
@@ -33,7 +33,7 @@ build() {
     fi
 
     # detect filesystem for separate /usr
-    if usrfstype=$(findmnt -snero fstype --tab-file "$BASEDIR/etc/fstab" /usr); then
+    if usrfstype=$(findmnt -snero fstype --tab-file '/etc/fstab' /usr); then
         add_if_avail "$usrfstype"
     fi
 
diff --git a/install/consolefont b/install/consolefont
index 2d2d8ea..5be9344 100644
--- a/install/consolefont
+++ b/install/consolefont
@@ -6,19 +6,19 @@ build() {
     # subshell to avoid namespace pollution
     (
         for cfg in etc/{rc,vconsole}.conf; do
-            [[ -s $BASEDIR/$cfg ]] && . "$BASEDIR/$cfg"
+            [[ -s $cfg ]] && . "$cfg"
         done
 
         [[ $FONT ]] && CONSOLEFONT=$FONT
 
         if [[ $CONSOLEFONT ]]; then
-            for file in "$BASEDIR/usr/share/kbd/consolefonts/$CONSOLEFONT".psf?(u)?(.gz); do
+            for file in "/usr/share/kbd/consolefonts/$CONSOLEFONT".psf?(u)?(.gz); do
                 if [[ -e $file ]]; then
                     [[ $file =~ \.(psfu?)(\.gz)?$ ]] && ext=${BASH_REMATCH[1]}
                     if [[ $file = *.gz ]]; then
                         gzip -cd "$file" > "$BUILDROOT/consolefont.$ext"
                     else
-                        add_file "${file#$BASEDIR}" "/consolefont.$ext"
+                        add_file "$file" "/consolefont.$ext"
                     fi
                     exit 0
                 fi
diff --git a/install/fsck b/install/fsck
index baf1c3d..e8b5ea3 100644
--- a/install/fsck
+++ b/install/fsck
@@ -21,9 +21,9 @@ build() {
             add_fsck $usrfstype && (( ++added ))
         fi
     else
-        for fsck in "$BASEDIR"/{usr/,}{s,}bin/fsck.*; do
+        for fsck in /{usr/,}{s,}bin/fsck.*; do
             [[ -f $fsck ]] || continue
-            add_binary "${fsck#$BASEDIR}" && (( ++added ))
+            add_binary "$fsck" && (( ++added ))
         done
     fi
 
diff --git a/install/keymap b/install/keymap
index fe5e459..1cbf75e 100644
--- a/install/keymap
+++ b/install/keymap
@@ -8,7 +8,7 @@ build() {
         unset LANG
 
         for cfg in etc/{rc,vconsole,locale}.conf; do
-            [[ -s $BASEDIR/$cfg ]] && . "$BASEDIR/$cfg"
+            [[ -s $cfg ]] && . "$cfg"
         done
 
         [[ $LANG ]] && LOCALE=$LANG
diff --git a/mkinitcpio b/mkinitcpio
index 084dff7..f7fc571 100755
--- a/mkinitcpio
+++ b/mkinitcpio
@@ -22,7 +22,7 @@ INSTDIR=(install /usr/lib/initcpio/install /lib/initcpio/install)
 PRESETDIR=mkinitcpio.d
 COMPRESSION=gzip
 
-declare BASEDIR= MODULE_FILE= GENIMG= PRESET= COMPRESSION_OPTIONS= BUILDROOT=
+declare MODULE_FILE= GENIMG= PRESET= COMPRESSION_OPTIONS= BUILDROOT=
 declare NC= BOLD= BLUE= GREEN= RED= YELLOW=
 declare -i QUIET=1 SHOW_AUTOMODS=0 SAVELIST=0 COLOR=1
 declare -a SKIPHOOKS ADDED_MODULES MODPATHS
@@ -42,7 +42,6 @@ usage: ${0##*/} [options]
 
   Options:
    -A, --add <hooks>            Add specified hooks, comma separated, to image
-   -b, --basedir <dir>          Use alternate base directory. (default: /)
    -c, --config <config>        Use alternate config file. (default: /etc/mkinitcpio.conf)
    -g, --generate <path>        Generate cpio image and write to specified path
    -H, --hookhelp <hookname>    Display help for given hook and exit
@@ -87,18 +86,18 @@ get_kernver() {
         return 0
     fi
 
-    if [[ ! -e $BASEDIR$kernel ]]; then
-        error "Specified kernel image does not exist: \`%s'" "$BASEDIR$kernel"
+    if [[ ! -e $kernel ]]; then
+        error "Specified kernel image does not exist: \`%s'" "$kernel"
         return 1
     fi
 
-    read _ kernver < <(file -Lb "$BASEDIR$kernel" | grep -o 'version [^ ]\+')
-    if [[ $kernver && -e $BASEDIR/lib/modules/$kernver ]]; then
+    read _ kernver < <(file -Lb "$kernel" | grep -o 'version [^ ]\+')
+    if [[ $kernver && -e /lib/modules/$kernver ]]; then
         echo "$kernver"
         return 0
     fi
 
-    [[ ${optkver:0:1} == / ]] && optkver=$BASEDIR$optkver
+    [[ ${optkver:0:1} == / ]] && optkver=$optkver
     error "invalid kernel specifier: \`%s'" "$optkver"
 
     return 1
@@ -116,9 +115,9 @@ compute_hookset() {
 trap 'cleanup 130' INT
 trap 'cleanup 143' TERM
 
-OPT_SHORT='A:b:c:g:H:hk:mnLMp:S:st:vz:'
-OPT_LONG=('add:' 'basedir:' 'config:' 'generate:' 'hookhelp:' 'help' 'kernel:' 'listhooks'
-          'automods' 'nocolor' 'preset:' 'skiphooks:' 'save' 'builddir:' 'verbose' 'compress:')
+OPT_SHORT='A:c:g:H:hk:mnLMp:S:st:vz:'
+OPT_LONG=('add:' 'config:' 'generate:' 'hookhelp:' 'help' 'kernel:' 'listhooks' 'automods'
+          'nocolor' 'preset:' 'skiphooks:' 'save' 'builddir:' 'verbose' 'compress:')
 
 if ! parseopts "$OPT_SHORT" "${OPT_LONG[@]}" -- "$@"; then
     exit 1
@@ -141,9 +140,6 @@ while :; do
             optkver=$1 ;;
         -s|--save)
             SAVELIST=1 ;;
-        -b|--basedir)
-            shift
-            BASEDIR=$1 ;;
         -g|--generate)
             shift
             GENIMG=$1 ;;
@@ -224,16 +220,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!"
 
-if [[ $BASEDIR ]]; then
-    # resolve the path. it might be a relative path and/or contain symlinks
-    if ! pushd "$BASEDIR" &>/dev/null; then
-        die "base directory does not exist or is not a directory: \`%s'" "$BASEDIR"
-    fi
-    BASEDIR=$(pwd -P)
-    BASEDIR=${BASEDIR%/}
-    popd &>/dev/null
-fi
-
 KERNELVERSION=$(get_kernver "$optkver") || cleanup 1
 
 if [[ $TMPDIR ]]; then
@@ -261,7 +247,6 @@ if [[ $PRESET ]]; then
     if [[ -f "$PRESET" ]]; then
         # Use -b, -m and -v options specified earlier
         declare -a preset_mkopts preset_cmd
-        [[ $BASEDIR ]] && preset_mkopts+=(-b "$BASEDIR")
         (( QUIET )) || preset_mkopts+=(-v)
         # Build all images
         . "$PRESET"
@@ -279,7 +264,7 @@ if [[ $PRESET ]]; then
 
             preset_config=${p}_config
             if [[ ${!preset_config:-$ALL_config} ]]; then
-              preset_cmd+=(-c "$BASEDIR${!preset_config:-$ALL_config}")
+              preset_cmd+=(-c "${!preset_config:-$ALL_config}")
             else
                 warning "No configuration file specified. Skipping image \`%s'" "$p"
                 continue
@@ -287,7 +272,7 @@ if [[ $PRESET ]]; then
 
             preset_image=${p}_image
             if [[ ${!preset_image} ]]; then
-                preset_cmd+=(-g "$BASEDIR${!preset_image}")
+                preset_cmd+=(-g "${!preset_image}")
             else
                 warning "No image file specified. Skipping image \`%s'" "$p"
                 continue
@@ -327,7 +312,7 @@ if (( ${#hooks[*]} == 0 )); then
     die "Invalid config: No hooks found"
 fi
 
-MODULEDIR=$BASEDIR/lib/modules/$KERNELVERSION/
+MODULEDIR=/lib/modules/$KERNELVERSION/
 if [[ ! -d $MODULEDIR ]]; then
     die "'$MODULEDIR' is not a valid kernel module directory"
 fi
@@ -416,7 +401,7 @@ if (( ${#ADDED_MODULES[*]} )); then
 
     msg "Generating module dependencies"
     install -m644 -t "$BUILDROOT/usr/lib/modules/$KERNELVERSION" \
-        "$BASEDIR/lib/modules/$KERNELVERSION"/modules.{builtin,order}
+        "/lib/modules/$KERNELVERSION"/modules.{builtin,order}
     depmod -b "$BUILDROOT" "$KERNELVERSION"
 
     # remove all non-binary module.* files (except devname for on-demand module loading)
diff --git a/mkinitcpio.8.txt b/mkinitcpio.8.txt
index 4b0d770..14a349b 100644
--- a/mkinitcpio.8.txt
+++ b/mkinitcpio.8.txt
@@ -29,10 +29,6 @@ Options
 	after all other hooks from the config file. Multiple hooks should be
 	comma-separated. This option can be specified multiple times.
 
-*-b, \--basedir* 'basedir'::
-	Use 'basedir' as a starting point for gathering information about the
-	currently running system. Default: /.
-
 *-c, \--config* 'config'::
 	Use 'config' file to generate the ramdisk. Default: /etc/mkinitcpio.conf
 
-- 
1.7.10.1



More information about the arch-projects mailing list