nixos/initrd: Do a lazy library copy in hopes to save some space for replaced binaries

+21 -17
+21 -17
nixos/modules/system/boot/stage-1.nix
··· 39 39 mkdir -p $out/bin $out/lib 40 40 ln -s $out/bin $out/sbin 41 41 42 - # Copy ld manually since it isn't detected correctly 43 - cp -pv ${pkgs.glibc}/lib/ld*.so.? $out/lib 44 - 45 42 copy_bin_and_libs () { 46 43 [ -f "$out/bin/$(basename $1)" ] && rm "$out/bin/$(basename $1)" 47 44 cp -pdv $1 $out/bin 48 - LDD="$(ldd $1)" || return 0 49 - LIBS="$(echo "$LDD" | awk '{print $3}' | sed '/^$/d')" 50 - for LIB in $LIBS; do 51 - [ ! -f "$out/lib/$(basename $LIB)" ] && cp -pdv $LIB $out/lib 52 - while [ "$(readlink $LIB)" != "" ]; do 53 - LINK="$(readlink $LIB)" 54 - if [ "${LINK:0:1}" != "/" ]; then 55 - LINK="$(dirname $LIB)/$LINK" 56 - fi 57 - LIB="$LINK" 58 - [ ! -f "$out/lib/$(basename $LIB)" ] && cp -pdv $LIB $out/lib 59 - done 60 - done 61 - return 0 62 45 } 63 46 64 47 # Copy BusyBox. ··· 88 71 ln -sf kmod $out/bin/modprobe 89 72 90 73 ${config.boot.initrd.extraUtilsCommands} 74 + 75 + # Copy ld manually since it isn't detected correctly 76 + cp -pv ${pkgs.glibc}/lib/ld*.so.? $out/lib 77 + 78 + # Copy all of the needed libraries for the binaries 79 + for BIN in $(find $out/{bin,sbin} -type f); do 80 + echo "Copying libs for bin $BIN" 81 + LDD="$(ldd $BIN)" || continue 82 + LIBS="$(echo "$LDD" | awk '{print $3}' | sed '/^$/d')" 83 + for LIB in $LIBS; do 84 + [ ! -f "$out/lib/$(basename $LIB)" ] && cp -pdv $LIB $out/lib 85 + while [ "$(readlink $LIB)" != "" ]; do 86 + LINK="$(readlink $LIB)" 87 + if [ "${LINK:0:1}" != "/" ]; then 88 + LINK="$(dirname $LIB)/$LINK" 89 + fi 90 + LIB="$LINK" 91 + [ ! -f "$out/lib/$(basename $LIB)" ] && cp -pdv $LIB $out/lib 92 + done 93 + done 94 + done 91 95 92 96 # Strip binaries further than normal. 93 97 chmod -R u+w $out