Subject: [alpine-aports] [PATCH] main/mkinitfs: Bugfixes, speedup, data/code separation, new syntax, cleanup.

From: Linus Swälas <[email-masked]>
Date: Sun, 6 Nov 2016 00:34:21 +0100

* Many small bugfixes.
* ~100 times speedup on gathering files for the initramfs.
* Separated code/data so that directories to include in the
  initramfs is no longer hardcoded in the binary but included
  in features.d/base.files.
* Change of syntax in features.d/* - directories whose name end
  in / will have the whole tree rooted at that path included,
  directories where the name does NOT end in / will cause only
  that directory to be included and nothing below that path.
* Some cleanup, quoting fixes and similar.
---
 ..._speedup_data-code-sep_new-syntax_cleanup.patch | 666 +++++++++++++++++++++
 main/mkinitfs/APKBUILD                             |  14 +-
 main/mkinitfs/mkinitfs.pre-upgrade                 |   2 +-
 main/mkinitfs/mkinitfs.trigger                     |  18 +-
 4 files changed, 685 insertions(+), 15 deletions(-)
 create mode 100644 main/mkinitfs/0003-mkinitfs-bugfixes_speedup_data-code-sep_new-syntax_cleanup.patch

diff --git a/main/mkinitfs/0003-mkinitfs-bugfixes_speedup_data-code-sep_new-syntax_cleanup.patch b/main/mkinitfs/0003-mkinitfs-bugfixes_speedup_data-code-sep_new-syntax_cleanup.patch
new file mode 100644
index 0000000..62de46b
--- /dev/null
+++ b/main/mkinitfs/0003-mkinitfs-bugfixes_speedup_data-code-sep_new-syntax_cleanup.patch
@@ -0,0 +1,666 @@
+diff -U3 -r mkinitfs-3.0.5/features.d/base.files mkinitfs-3.0.5-new/features.d/base.files
+--- mkinitfs-3.0.5/features.d/base.files	2016-06-15 12:57:27.000000000 +0200
++++ mkinitfs-3.0.5-new/features.d/base.files	2016-10-28 13:39:54.000000000 +0200
+@@ -1,3 +1,16 @@
++/bin
++/sbin
++/dev
++/proc
++/sys
++/run
++/tmp
++/etc/apk
++/lib/modules
++/media/floppy
++/media/cdrom
++/media/usb
++/mdev/
+ /bin/busybox
+ /bin/sh
+ /lib/mdev
+diff -U3 -r mkinitfs-3.0.5/features.d/base.modules mkinitfs-3.0.5-new/features.d/base.modules
+--- mkinitfs-3.0.5/features.d/base.modules	2016-06-15 12:57:27.000000000 +0200
++++ mkinitfs-3.0.5-new/features.d/base.modules	2016-10-28 13:43:42.000000000 +0200
+@@ -1,2 +1,2 @@
+ kernel/drivers/block/loop.ko
+-kernel/fs/overlayfs
++kernel/fs/overlayfs/
+diff -U3 -r mkinitfs-3.0.5/features.d/btrfs.modules mkinitfs-3.0.5-new/features.d/btrfs.modules
+--- mkinitfs-3.0.5/features.d/btrfs.modules	2016-06-15 12:57:27.000000000 +0200
++++ mkinitfs-3.0.5-new/features.d/btrfs.modules	2016-10-28 13:43:54.000000000 +0200
+@@ -1,3 +1,3 @@
+ kernel/arch/*/crypto/crc32*
+ kernel/crypto/crc32*
+-kernel/fs/btrfs
++kernel/fs/btrfs/
+diff -U3 -r mkinitfs-3.0.5/features.d/cdrom.modules mkinitfs-3.0.5-new/features.d/cdrom.modules
+--- mkinitfs-3.0.5/features.d/cdrom.modules	2016-06-15 12:57:27.000000000 +0200
++++ mkinitfs-3.0.5-new/features.d/cdrom.modules	2016-10-28 13:44:12.000000000 +0200
+@@ -1,2 +1,2 @@
+-kernel/drivers/cdrom
+-kernel/fs/isofs
++kernel/drivers/cdrom/
++kernel/fs/isofs/
+diff -U3 -r mkinitfs-3.0.5/features.d/cramfs.modules mkinitfs-3.0.5-new/features.d/cramfs.modules
+--- mkinitfs-3.0.5/features.d/cramfs.modules	2016-06-15 12:57:27.000000000 +0200
++++ mkinitfs-3.0.5-new/features.d/cramfs.modules	2016-10-28 13:44:18.000000000 +0200
+@@ -1 +1 @@
+-kernel/fs/cramfs
++kernel/fs/cramfs/
+diff -U3 -r mkinitfs-3.0.5/features.d/ext2.modules mkinitfs-3.0.5-new/features.d/ext2.modules
+--- mkinitfs-3.0.5/features.d/ext2.modules	2016-06-15 12:57:27.000000000 +0200
++++ mkinitfs-3.0.5-new/features.d/ext2.modules	2016-10-28 13:44:42.000000000 +0200
+@@ -1 +1 @@
+-kernel/fs/ext2
++kernel/fs/ext2/
+diff -U3 -r mkinitfs-3.0.5/features.d/ext3.modules mkinitfs-3.0.5-new/features.d/ext3.modules
+--- mkinitfs-3.0.5/features.d/ext3.modules	2016-06-15 12:57:27.000000000 +0200
++++ mkinitfs-3.0.5-new/features.d/ext3.modules	2016-10-28 13:44:50.000000000 +0200
+@@ -1 +1 @@
+-kernel/fs/ext3
++kernel/fs/ext3/
+diff -U3 -r mkinitfs-3.0.5/features.d/ext4.modules mkinitfs-3.0.5-new/features.d/ext4.modules
+--- mkinitfs-3.0.5/features.d/ext4.modules	2016-06-15 12:57:27.000000000 +0200
++++ mkinitfs-3.0.5-new/features.d/ext4.modules	2016-10-28 13:44:54.000000000 +0200
+@@ -1 +1 @@
+-kernel/fs/ext4
++kernel/fs/ext4/
+diff -U3 -r mkinitfs-3.0.5/features.d/f2fs.modules mkinitfs-3.0.5-new/features.d/f2fs.modules
+--- mkinitfs-3.0.5/features.d/f2fs.modules	2016-06-15 12:57:27.000000000 +0200
++++ mkinitfs-3.0.5-new/features.d/f2fs.modules	2016-10-28 13:45:43.000000000 +0200
+@@ -1 +1 @@
+-kernel/fs/f2fs
++kernel/fs/f2fs/
+diff -U3 -r mkinitfs-3.0.5/features.d/gfs2.modules mkinitfs-3.0.5-new/features.d/gfs2.modules
+--- mkinitfs-3.0.5/features.d/gfs2.modules	2016-06-15 12:57:27.000000000 +0200
++++ mkinitfs-3.0.5-new/features.d/gfs2.modules	2016-10-28 13:45:58.000000000 +0200
+@@ -1 +1 @@
+-kernel/fs/gfs2
++kernel/fs/gfs2/
+diff -U3 -r mkinitfs-3.0.5/features.d/jfs.modules mkinitfs-3.0.5-new/features.d/jfs.modules
+--- mkinitfs-3.0.5/features.d/jfs.modules	2016-06-15 12:57:27.000000000 +0200
++++ mkinitfs-3.0.5-new/features.d/jfs.modules	2016-10-28 13:46:04.000000000 +0200
+@@ -1 +1 @@
+-kernel/fs/jfs
++kernel/fs/jfs/
+diff -U3 -r mkinitfs-3.0.5/features.d/kms.modules mkinitfs-3.0.5-new/features.d/kms.modules
+--- mkinitfs-3.0.5/features.d/kms.modules	2016-06-15 12:57:27.000000000 +0200
++++ mkinitfs-3.0.5-new/features.d/kms.modules	2016-10-28 13:46:30.000000000 +0200
+@@ -1,5 +1,5 @@
+-kernel/drivers/char/agp
+-kernel/drivers/gpu
+-kernel/drivers/i2c
+-kernel/drivers/video
++kernel/drivers/char/agp/
++kernel/drivers/gpu/
++kernel/drivers/i2c/
++kernel/drivers/video/
+ kernel/arch/x86/video/fbdev.ko
+diff -U3 -r mkinitfs-3.0.5/features.d/mmc.modules mkinitfs-3.0.5-new/features.d/mmc.modules
+--- mkinitfs-3.0.5/features.d/mmc.modules	2016-06-15 12:57:27.000000000 +0200
++++ mkinitfs-3.0.5-new/features.d/mmc.modules	2016-10-28 13:47:25.000000000 +0200
+@@ -1 +1 @@
+-kernel/drivers/mmc
++kernel/drivers/mmc/
+diff -U3 -r mkinitfs-3.0.5/features.d/network.modules mkinitfs-3.0.5-new/features.d/network.modules
+--- mkinitfs-3.0.5/features.d/network.modules	2016-06-15 12:57:27.000000000 +0200
++++ mkinitfs-3.0.5-new/features.d/network.modules	2016-10-28 13:47:34.000000000 +0200
+@@ -1,2 +1,2 @@
+-kernel/drivers/net/ethernet
++kernel/drivers/net/ethernet/
+ kernel/net/packet/af_packet.ko
+diff -U3 -r mkinitfs-3.0.5/features.d/ocfs2.modules mkinitfs-3.0.5-new/features.d/ocfs2.modules
+--- mkinitfs-3.0.5/features.d/ocfs2.modules	2016-06-15 12:57:27.000000000 +0200
++++ mkinitfs-3.0.5-new/features.d/ocfs2.modules	2016-10-28 13:47:45.000000000 +0200
+@@ -1 +1 @@
+-kernel/fs/ocfs2
++kernel/fs/ocfs2/
+diff -U3 -r mkinitfs-3.0.5/features.d/reiserfs.modules mkinitfs-3.0.5-new/features.d/reiserfs.modules
+--- mkinitfs-3.0.5/features.d/reiserfs.modules	2016-06-15 12:57:27.000000000 +0200
++++ mkinitfs-3.0.5-new/features.d/reiserfs.modules	2016-10-28 13:49:47.000000000 +0200
+@@ -1 +1 @@
+-kernel/fs/reiserfs
++kernel/fs/reiserfs/
+diff -U3 -r mkinitfs-3.0.5/features.d/scsi.modules mkinitfs-3.0.5-new/features.d/scsi.modules
+--- mkinitfs-3.0.5/features.d/scsi.modules	2016-06-15 12:57:27.000000000 +0200
++++ mkinitfs-3.0.5-new/features.d/scsi.modules	2016-10-28 13:49:57.000000000 +0200
+@@ -1,2 +1,2 @@
+ kernel/drivers/scsi/*
+-kernel/drivers/message/fusion
++kernel/drivers/message/fusion/
+diff -U3 -r mkinitfs-3.0.5/features.d/squashfs.modules mkinitfs-3.0.5-new/features.d/squashfs.modules
+--- mkinitfs-3.0.5/features.d/squashfs.modules	2016-06-15 12:57:27.000000000 +0200
++++ mkinitfs-3.0.5-new/features.d/squashfs.modules	2016-10-28 13:50:02.000000000 +0200
+@@ -1 +1 @@
+-kernel/fs/squashfs
++kernel/fs/squashfs/
+diff -U3 -r mkinitfs-3.0.5/features.d/ubifs.modules mkinitfs-3.0.5-new/features.d/ubifs.modules
+--- mkinitfs-3.0.5/features.d/ubifs.modules	2016-06-15 12:57:27.000000000 +0200
++++ mkinitfs-3.0.5-new/features.d/ubifs.modules	2016-10-28 13:50:08.000000000 +0200
+@@ -1 +1 @@
+-kernel/fs/ubifs
++kernel/fs/ubifs/
+diff -U3 -r mkinitfs-3.0.5/features.d/usb.modules mkinitfs-3.0.5-new/features.d/usb.modules
+--- mkinitfs-3.0.5/features.d/usb.modules	2016-06-15 12:57:27.000000000 +0200
++++ mkinitfs-3.0.5-new/features.d/usb.modules	2016-10-28 13:50:33.000000000 +0200
+@@ -1,7 +1,7 @@
+-kernel/drivers/usb/host
+-kernel/drivers/usb/storage
+-kernel/drivers/hid/usbhid
++kernel/drivers/usb/host/
++kernel/drivers/usb/storage/
++kernel/drivers/hid/usbhid/
+ kernel/drivers/hid/hid-generic.ko
+ kernel/drivers/hid/hid-cherry.ko
+-kernel/fs/fat
+-kernel/fs/nls
++kernel/fs/fat/
++kernel/fs/nls/
+diff -U3 -r mkinitfs-3.0.5/features.d/virtio.modules mkinitfs-3.0.5-new/features.d/virtio.modules
+--- mkinitfs-3.0.5/features.d/virtio.modules	2016-06-15 12:57:27.000000000 +0200
++++ mkinitfs-3.0.5-new/features.d/virtio.modules	2016-10-28 13:50:41.000000000 +0200
+@@ -1,3 +1,3 @@
+ kernel/drivers/block/virtio*
+-kernel/drivers/virtio
++kernel/drivers/virtio/
+ kernel/drivers/net/virtio_net*
+diff -U3 -r mkinitfs-3.0.5/features.d/xfs.modules mkinitfs-3.0.5-new/features.d/xfs.modules
+--- mkinitfs-3.0.5/features.d/xfs.modules	2016-06-15 12:57:27.000000000 +0200
++++ mkinitfs-3.0.5-new/features.d/xfs.modules	2016-10-28 13:50:50.000000000 +0200
+@@ -1,4 +1,4 @@
+ kernel/arch/*/crypto/crc32*
+ kernel/arch/*/crypto/crc32*
+ kernel/crypto/crc32*
+-kernel/fs/xfs
++kernel/fs/xfs/
+diff -U3 -r mkinitfs-3.0.5/mkinitfs.in mkinitfs-3.0.5-new/mkinitfs.in
+--- mkinitfs-3.0.5/mkinitfs.in	2016-10-30 06:52:56.000000000 +0100
++++ mkinitfs-3.0.5-new/mkinitfs.in	2016-10-30 06:51:59.000000000 +0100
+@@ -4,64 +4,262 @@
+ sysconfdir=@sysconfdir@
+ datadir=@datadir@
+ 
+-config="$sysconfdir"/mkinitfs.conf
+-init="$datadir"/initramfs-init
+-fstab="$datadir"/fstab
+-passwd="$datadir"/passwd
+-group="$datadir"/group
++config="${sysconfdir}/mkinitfs.conf"
++init="${datadir}/initramfs-init"
++fstab="${datadir}/fstab"
++passwd="${datadir}/passwd"
++group="${datadir}/group"
++rootdir=''
+ basedir=''
+ tmpdir=''
+ 
+-startdir=$PWD
++# Print files and directories while recursivly resolving links and
++# printing the names of each link visited.
++resolve_links() {
++	local name=''
++	while read -r name; do
++		# *) Check for existance.
++		# *) If it's not a link we just output it and continue, nothing more to do.
++		#
++		# *) Iteratively resolve and print all link steps.
++		# *) If the final target is NOT a directory, just print the name.
++		# *) If it is a directory, everything below that directory will be
++		#    searched and resolved.
++		if [ ! -e "$name" ]; then
++			echo "resolve_links: ${name}: No such file or directory." >&2
++			continue
++		fi
++		if [ ! -L "$name" ]; then
++			echo "${name}"
++			continue
++		fi
++		# Resolve links, all linked steps will be resolved and printed.
++		while [ -L "$name" ]; do
++			echo "$name"
++			local target="$(readlink "$name")"
++			if [ "$target" != "${target#/}" ]; then
++				# Absolute link, prefix with $basedir.
++				local name="${basedir%/}/${target#/}"
++			else
++				# Relative link, move to the directory of the link, get the
++				# resolved path for that directory and add the target name.
++				if [ -z "${target##*/*}" ]; then
++					cd -P "${name%/*}/${target%/*}"
++				else
++					cd -P "${name%/*}"
++				fi
++				local name="${PWD}/${target##*/}"
++			fi
++		done
++		if [ ! -d "$name" ]; then
++			echo "${name}"
++			continue
++		fi
++		#find "${name}" \! -type d -o \( -type d -empty \) | resolve_links
++		find "${name%/}" \! -type d -print -o \( -type d -links 2 -exec sh -c 'ls -A "{}" | grep -Eq "" || echo "{}"' \; \) | resolve_links
++	done
++}
++
++resolve_ldsos() {
++	local ld_library_path="$1"
++	shift
++	local accumulate=''
++	local last_seen_rpath=''
++	while read -r line; do
++		[ 2 -eq "${#line}" ] && continue
++		#echo "line is $line" >&2
++		case "$line" in
++		0I*)
++			echo "${line#??}"
++			continue
++			;;
++		1R*)
++			[ "$line" = "$last_seen_rpath" ] && continue
++			local last_seen_rpath="$line"
++			local ld_library_path="${line#??}
++$ld_library_path"
++			#echo "ld_library_path is now = \"$ld_library_path\"" >&2
++			continue
++			;;
++		2N*)
++			local found='N'
++			local needed="${line#??}"
++			IFS=','
++			for lib in $needed; do
++				IFS='
++'
++				local lpath=''
++				for lpath in $ld_library_path; do
++					if [ -e "${lpath}/${lib}" ]; then
++						echo "${lpath}/${lib}"
++						#echo "Found library: ${lpath}/${lib}" >&2
++						local found='Y'
++						if [ -z "$accumulate" ]; then
++							local accumulate="${lpath}/${lib}"
++						else
++							local accumulate="$accumulate
++${lpath}/${lib}"
++						fi
++						break
++					fi
++				done
++				unset IFS
++				if [ 'Y' != "$found" ]; then
++					echo "resolve_ldso_deps: Warning: Cannot find library \"$lib\". Searched in \"$ld_library_path\"." >&2
++				fi
++			done
++			unset IFS
++			continue
++			;;
++		esac
++	done
++	# No more libraries found.
++	if [ -z "$accumulate" ]; then
++		return
++	fi
++	#echo "Recursing on " >&2
++	#echo "$accumulate" | sort -u >&2
++	#echo "with ld_library_path=$ld_library_path" >&2
++	echo "$accumulate" | xargs scanelf -qF '#F0I%i
++1R%r
++2N%n' "$@" \
++		| sort -u \
++		| resolve_ldsos "$ld_library_path" "${lpath}/${lib}"
++}
++
++# Reads a list of filenames from stdin, figures out what ldso
++# dependencies there are for any ELFs found in the input and
++# outputs the list complemented by any dependencies found.
++resolve_ldso_deps() {
++	local basedir="$1"
++
++	scanelf >/dev/null 2>&1
++	local retcode=$?
++	# System doesn't have scanelf, system probably doesn't support shared libs
++	# so we just act like an empty filter and pass everything along.
++	if [ 126 -eq $retcode ] || [ 127 -eq $retcode ]; then
++		echo "No scanelf found, ldso dependencies will not be resolved." >&2
++		while read -r name; do
++			echo "$name"
++		done
++		return 0
++	fi
++
++	# Prepare, don't want to do this in a loop. This sets ld_library_path to a
++	# newline separated list of paths to search for ldso:s.
++	local ld_so_conf="${basedir%/}/etc/ld.so.conf"
++	local ld_library_path="$(echo "$LD_PRELOAD
++$LD_LIBRARY_PATH
++$([ -e "$ld_so_conf" ] && cat "$ld_so_conf")
++/lib
++/usr/lib
++/usr/local/lib" | sed -e '/^$/d' -e '/^#/d' -e 's/:/\
++/g' -e "s|^/|${basedir%/}/|" | sort -u)"
++
++	# So, we don't want to call scanelf for every filename we read as that is painfully
++	# slow. Try calling it 10k times on /bin/sh and you'll understand. Calling it once
++	# with 10k /bin/sh as arguments is blindingly fast though. First case is something
++	# like 178 secs on my i7, latter case is 0.2 secs. So, instead of calling it for
++	# every file we read, we sort out anything that is not a regular file and then let
++	# xargs accumulate as many args it can before running a scanelf on a bunch of files.
++	# We prefix the output from scanelf with 0I for interpreter, 1R for rpath and 2N
++	# for needed libraries. We then sort -u the result, echo any interpreters found,
++	# modify ld_library_path according to any rpaths found, and search ld_library_path
++	# for any needed libraries.
++	local regulars=''
++	local name=''
++	read -r regulars
++	while read -r name; do
++		echo "$name"
++		# Links we get are already resolved, we just want to check actual files.
++		if [ -L "$name" ] || [ ! -f "$name" ]; then
++			continue
++		fi
++		# Accumulate regular files.
++		local regulars="$regulars
++$name"
++	done
++
++	echo "$regulars" | xargs scanelf -qF '#F0I%i
++1R%r
++2N%n' "$@" \
++		| sort -u \
++		| resolve_ldsos "$ld_library_path" \
++		| resolve_links
++}
++
++# Expand config lines according to any globs they contain.
++# The line read should be a the name of a file to be included in the initramfs.
++# The line read should be a complete path, including any potential $basedir.
++# If the line ends in / everything below that path will be added.
++# If the line contains a globbing pattern it will be expanded and everything
++# matching the glob will be added.
++expand_globs() {
++	# This first loop expands globs in config lines.
++	local line=''
++	while read -r line; do
++		local file=''
++		IFS=''
++		for file in $line; do
++			echo "$file"
++		done
++	done | while read -r line; do
++		case "$line" in
++		*/)
++			#find "${line%/}" \! -type d -o \( -type d -empty \)
++			find "${line%/}" \! -type d -print -o \( -type d -links 2 -exec sh -c 'ls -A "{}" | grep -Eq "" || echo "{}"' \; \)
++			;;
++		*)
++			echo "$line"
++			;;
++		esac
++	done
++}
+ 
+ feature_files() {
+ 	local dir="$1"
+ 	local suffix="$2"
+-	local glob file
++	local f=''
+ 	for f in $features; do
+-		if [ ! -f "$features_dir/$f.$suffix" ]; then
++		# Skip if there is no file for this feature with the selected suffix.
++		if [ ! -f "${features_dir}/${f}.${suffix}" ]; then
+ 			continue
+ 		fi
+-		for glob in $(sed -e '/^$/d' -e '/^#/d' -e "s|^/*|$dir|" "$features_dir/$f.$suffix"); do
+-			for file in $glob; do
+-				if [ -d $file ]; then
+-					find $file -type f
+-				elif [ -e "$file" ]; then
+-					echo $file
+-				fi
+-			done
+-		done
++		# Remove comments, blank lines and translate paths to paths relative to $dir.
++		sed -e '/^$/d' -e '/^#/d' -e "s|^/*|${dir%/}/|" "${features_dir}/${f}.${suffix}" \
++			| expand_globs \
++			| resolve_links
+ 	done
+ }
+ 
+-initfs_base() {
+-	local i= dirs= glob= file=
+-	for i in dev proc sys sbin bin run .modloop lib/modules media/cdrom \
+-	    etc/apk media/floppy media/usb newroot; do
+-		dirs="$dirs $tmpdir/$i"
+-	done
+-	mkdir -p $dirs
+ 
+-	local oldpwd="$PWD"
+-	cd "${basedir}"
+-	lddtree -R "$basedir" -l --no-auto-root \
+-		$(feature_files "$basedir" files) \
+-		\
+-		| sed -e "s|^$basedir||" | sort -u \
+-		| cpio --quiet -pdm "$tmpdir" || return 1
++find_files() {
++	cd "$basedir" || return 1
++	#	| xargs lddtree -R "$basedir" -l --no-auto-root \
++	#	| resolve_ldso_deps "$basedir" \
++	feature_files "$basedir" files \
++		| resolve_ldso_deps "$basedir" \
++		| sed -e "s|^${basedir%/}/||" \
++		| sort -u
++	cd "$OLDPWD" || return 1
++}
+ 
++initfs_base() {
++	cd "$basedir" || return 1
++	find_files \
++		| cpio --quiet -pdm "$tmpdir" || return 1
+ 	# copy init
+-	cd "$startdir"
+-	install -m755 "$init" "$tmpdir"/init || return 1
++	install -m755 "$init" "${tmpdir}/init" || return 1
+ 	for i in "$fstab" "$passwd" "$group"; do
+-		install -Dm644 "$i" "$tmpdir"/etc/${i##*/} || return 1
++		install -Dm644 "$i" "${tmpdir}/etc/${i##*/}" || return 1
+ 	done
+-	cd "$oldpwd"
++	cd "$OLDPWD"
+ }
+ 
++
+ find_kmod_deps() {
+-	awk -v prepend="/lib/modules/$kernel/" -v modulesdep="${basedir}lib/modules/$kernel/modules.dep" '
+-function recursedeps(k,		j, dep) {
++	awk -v prepend="lib/modules/${kernel}/" -v modulesdep="${basedir}/lib/modules/$kernel/modules.dep" '
++function recursedeps(k, j, dep) {
+ 	if (k in visited)
+ 		return;
+ 	visited[k] = 1;
+@@ -95,55 +293,62 @@
+ }
+ 
+ find_kmods() {
+-	local oldpwd="$PWD"
+ 	cd "$kerneldir" || return 1
+-	for file in $(feature_files "${kerneldir}/" modules); do
+-		echo ${file#${kerneldir%/}/}
+-	done | find_kmod_deps
+-	cd "$oldpwd"
++	feature_files "$kerneldir" modules \
++		| sed "s|${kerneldir%/}/||" \
++		| find_kmod_deps \
++		| sort -u
++	cd "$OLDPWD" || return 1
+ }
+ 
+ initfs_kmods() {
+-	local glob= file= files= dirs=
+-	rm -rf "$tmpdir"/lib/modules
+-	# make sure we have modules.dep
+-	if ! [ -f "$kerneldir"/modules.dep ]; then
+-		depmod -b "${basedir}" $kernel
++	rm -rf "${tmpdir}/lib/modules"
++	# Make sure we have modules.dep.
++	if ! [ -f "${kerneldir}/modules.dep" ]; then
++		depmod -b "${basedir}" "$kernel"
+ 	fi
+-	local oldpwd="$PWD"
+-	cd "${basedir}"
+-	for file in $(find_kmods); do
+-		echo "${file#/}"
+-	done | sort -u | cpio --quiet -pdm "$tmpdir" || return 1
++
++	cd "${basedir}" || return 1
++	find_kmods \
++		| cpio --quiet -pdm "$tmpdir" || return 1
++
++	local file=''
+ 	for file in modules.order modules.builtin; do
+-		if [ -f "$kerneldir"/$file ]; then
+-			cp "$kerneldir"/$file "$tmpdir"/lib/modules/$kernel/
++		if [ -f "${kerneldir}/$file" ]; then
++			cp "${kerneldir}/${file}" "${tmpdir}/lib/modules/${kernel}/"
+ 		fi
+ 	done
+-	depmod $kernel -b "$tmpdir"
+-	cd "$oldpwd"
++
++	depmod "$kernel" -b "$tmpdir"
++	cd "$OLDPWD" || return 1
+ }
+ 
++
+ initfs_firmware() {
+-	rm -rf "$tmpdir"/lib/firmware
+-	mkdir -p "$tmpdir"/lib/firmware
+-	find "$tmpdir"/lib/modules -type f -name "*.ko" | xargs modinfo -F firmware | sort -u | while read FW; do
++	rm -rf "${tmpdir}/lib/firmware"
++	mkdir -p "${tmpdir}/lib/firmware"
++	find "${tmpdir}/lib/modules" -type f -name "*.ko" \
++		| xargs modinfo -F firmware \
++		| sort -u \
++		| while read FW; do
+ 		[ -e "${basedir}/lib/firmware/${FW}" ] && install -pD "${basedir}/lib/firmware/${FW}" "${tmpdir}/lib/firmware/${FW}"
+ 	done
+ 	return 0
+ }
+ 
++
+ initfs_apk_keys() {
+ 	# If the basedir has nothing to copy, and we're not asked to copy the host's keys, don't do anything.
+-	if [ ! -d "${basedir}etc/apk/keys/" ] && [ -z "$hostkeys" ]; then
++	if [ ! -d "${basedir}/etc/apk/keys/" ] && [ -z "$hostkeys" ]; then
+ 		return
+ 	fi
+ 	mkdir -p "${tmpdir}/etc/apk/keys"
+ 	[ -n "$hostkeys" ] && cp "/etc/apk/keys/"* "${tmpdir}/etc/apk/keys/"
+-	cp "${basedir}etc/apk/keys/"* "${tmpdir}/etc/apk/keys/"
++	cp "${basedir}/etc/apk/keys/"* "${tmpdir}/etc/apk/keys/"
+ }
+ 
+-initfs_cpio() {
++
++ initfs_cpio() {
+ 	if [ -n "$list_sources" ]; then
+ 		(cd "$tmpdir" && find . )
+ 		return
+@@ -203,7 +408,7 @@
+ shift $(( $OPTIND - 1 ))
+ 
+ 
+-. $(readlink -f "$config")
++. "$(cd -P "${config%/*}" ; echo "${PWD}/${config##*/}")"
+ 
+ # Override any options read from the config with what the
+ # user has specifed on the commandline.
+@@ -219,16 +424,16 @@
+ [ -n "$arg_tmpdir"   ] && tmpdir="$arg_tmpdir"
+ 
+ 
+-rootdir="${rootdir%/}/"
+-[ "${rootdir}" = "${rootdir#/}" ] && rootdir="${PWD}/${rootdir}"
++rootdir="$(cd -P "${rootdir%/}/"; echo "${PWD}")"
+ 
+-features_dir=${features_dir:-"${rootdir%/}/${sysconfdir#/}/features.d"}
++features_dir="${features_dir:-"${rootdir%/}/${sysconfdir#/}/features.d"}"
++features_dir="$(cd -P "${features_dir}"; echo "${PWD}")"
+ 
+ if [ -n "$list_features" ]; then
+-	for i in "$features_dir"/*.files "$features_dir"/*.modules; do
++	cd "$features_dir" || return 1
++	for i in *.files *.modules; do
+ 		[ -e "$i" ] || continue
+-		local file=${i##*/}
+-		echo ${file%.*}
++		echo "${i%.*}"
+ 	done | sort -u
+ 	exit 0
+ fi
+@@ -236,45 +441,47 @@
+ if [ -z "$basedir" ]; then
+ 	basedir="$rootdir"
+ else
+-	[ "${basedir}" = "${basedir#/}" ] && basedir="${PWD}/${basedir}"
++	basedir="$(cd -P "${basedir}"; echo "${PWD}")"
+ fi
+ 
++init="$(cd -P "${init%/*}"; echo "${PWD}/${init##*/}")"
+ 
+-[ -n "$1" ] && kernel="$1"
+-[ -z "$kernel" ] && kernel=$(uname -r)
+-kerneldir="${basedir}lib/modules/$kernel"
+ 
++[ -n "$1" ] && kernel="$1"
++[ -z "$kernel" ] && kernel="$(uname -r)"
++kerneldir="${basedir%/}/lib/modules/${kernel}"
+ if [ ! -d "$kerneldir" ]; then
+ 	echo "$kerneldir does not exist or is not a directory"
+ 	exit 1
+ fi
+ 
+-kflavor=${kernel##*-}
+-[ "$kflavor" = "$kernel" ] && kflavor=vanilla
+-
+ if [ -z "$outfile" ]; then
+-	outfile="${rootdir}boot/initramfs-${kernel}"
++	outfile="${rootdir%/}/boot/initramfs-${kernel}"
+ fi
+ 
++if [ -n "$DEBUG_FILES" ]; then
++	find_files
++	exit 0
++fi
+ if [ -n "$DEBUG_KMOD" ]; then
+ 	find_kmods
+ 	exit 0
+ fi
+ 
++
+ if [ -z "$tmpdir" ]; then
+-	tmpdir=$(mktemp -d "${TMPDIR:-/tmp}/mkinitfs.XXXXXX")
++	tmpdir="$(mktemp -d "${TMPDIR:-/tmp}/mkinitfs.XXXXXX")" || exit
++	tmpdir="$(cd -P "$tmpdir" ; echo "$PWD")"
+ else
+-	mkdir -p "$tmpdir"
+-fi
+-
+-tmpdir=$(readlink -f "$tmpdir")
+-if [ '/' == "$tmpdir" ] ; then
+-	echo "tmpdir must not be /"
+-	exit 1
+-fi
+-
+-if [ -z "$keeptmp" ]; then
+-	[ -d "$tmpdir" ] && rm -rf "$tmpdir"
++	tmpdir="$(cd -P "$tmpdir" ; echo "$PWD")"
++	if [ '/' == "$tmpdir" ] ; then
++		echo "tmpdir must not be /"
++		exit 1
++	fi
++	if [ -z "$keeptmp" ]; then
++		[ -d "$tmpdir" ] && rm -rf "$tmpdir"
++	fi
++	mkdir -p "$tmpdir" || exit
+ fi
+ 
+ if [ -z "$list_sources" ] && [ -z "$quiet" ]; then
diff --git a/main/mkinitfs/APKBUILD b/main/mkinitfs/APKBUILD
index c75961c..2ba5c93 100644
--- a/main/mkinitfs/APKBUILD
+++ b/main/mkinitfs/APKBUILD
@@ -2,13 +2,13 @@
 pkgname=mkinitfs
 pkgver=3.0.5
 _ver=${pkgver%_git*}
-pkgrel=5
+pkgrel=6
 pkgdesc="Tool to generate initramfs images for Alpine"
 url="http://git.alpinelinux.org/cgit/mkinitfs"
 makedepends_build=""
 makedepends_host="kmod-dev util-linux-dev cryptsetup-dev linux-headers"
 makedepends="$makedepends_build $makedepends_host"
-depends="busybox apk-tools>=2.0 lddtree>=1.25"
+depends="busybox apk-tools>=2.0 scanelf"
 install="$pkgname.pre-upgrade $pkgname.post-install $pkgname.post-upgrade"
 triggers="$pkgname.trigger=/usr/share/kernel/*"
 source="http://dev.alpinelinux.org/archive/$pkgname/$pkgname-$_ver.tar.xz
@@ -17,6 +17,7 @@ source="http://dev.alpinelinux.org/archive/$pkgname/$pkgname-$_ver.tar.xz
 	0001-group-sync-with-alpine-baselayout.patch
 	0001-mkinitfs-add-K-flag-to-copy-host-keys-to-new-initram.patch
 	0002-mkinitfs_cmdline-override_rootdir-support_honor-tmpdir.patch
+	0003-mkinitfs-bugfixes_speedup_data-code-sep_new-syntax_cleanup.patch
 	"
 arch="all"
 license="GPL2"
@@ -37,16 +38,19 @@ a4227598291fa7338ad1128bfef3720c  0001-init-add-support-for-ttyMFD-and-ttyUSB-se
 a74631b5f40001dd259a2a6ebcaec544  0001-init-dont-use-local-in-global-scope.patch
 db888a1a502fe281bbe0b1add81bf62f  0001-group-sync-with-alpine-baselayout.patch
 a46eb97de6742400ca04777a0a8478b2  0001-mkinitfs-add-K-flag-to-copy-host-keys-to-new-initram.patch
-fadbbb5e49ead2c6fd8d4e880c83bafa  0002-mkinitfs_cmdline-override_rootdir-support_honor-tmpdir.patch"
+fadbbb5e49ead2c6fd8d4e880c83bafa  0002-mkinitfs_cmdline-override_rootdir-support_honor-tmpdir.patch
+baef5ed6d50f5087135f84036a6f3ea6  0003-mkinitfs-bugfixes_speedup_data-code-sep_new-syntax_cleanup.patch"
 sha256sums="3f13619f161c506796b91f2db17644eba25c2ffc923aa0c8fff0213d1f660aa4  mkinitfs-3.0.5.tar.xz
 17547dbc77bd19d940550ea6243b44212d21a6797aee4202e4e22c31143bf9e6  0001-init-add-support-for-ttyMFD-and-ttyUSB-serial-consol.patch
 7253cd8ebc1487e4bfb98d81edd68b24a4364e9fa8e48c73fc6903f2077b056e  0001-init-dont-use-local-in-global-scope.patch
 17b8a04db505eea9e176da00f9389ca80f65ed88c0288f6785f9ab108164361a  0001-group-sync-with-alpine-baselayout.patch
 8daccc844576f0b06fc9c15a30d750bfecda019b476a0be4cf9bcd7139e219cf  0001-mkinitfs-add-K-flag-to-copy-host-keys-to-new-initram.patch
-c24fb5b60a1bdd9d0888cd9ce76996fe5c45cb384b0e30a8837e3cf3724a7727  0002-mkinitfs_cmdline-override_rootdir-support_honor-tmpdir.patch"
+c24fb5b60a1bdd9d0888cd9ce76996fe5c45cb384b0e30a8837e3cf3724a7727  0002-mkinitfs_cmdline-override_rootdir-support_honor-tmpdir.patch
+042cc227a278cb43dd23383d6756a1682c00e9b7d27fed11ba1b7c6164cd0295  0003-mkinitfs-bugfixes_speedup_data-code-sep_new-syntax_cleanup.patch"
 sha512sums="246f25ce3fb65ea19dbe611ad44a106930c28b2c7111908462d4a252433011280715b2cf87ee81cc6f0860679fd1d0a109d03040647498137e42b22ccc711662  mkinitfs-3.0.5.tar.xz
 35b4cf3f74e394d87b321d335ec4dff6973bfd9a38cdc81609e21e025ee3fb9af7d2744cf9941531603df829af1f7fba433074915ba88a688d375cfe40dad07a  0001-init-add-support-for-ttyMFD-and-ttyUSB-serial-consol.patch
 ca1bd66bc6d366dc74da389397f3e0d81b3afa21a534269230ab33f415e633b19ce54327b78713847461b67d6d23a85019c79e1fbbf1a964ee0bd26013ddf33f  0001-init-dont-use-local-in-global-scope.patch
 2736650e0c06d47f2cd0dcc4a9c3d575b2b5284ee8d1aa0cccd45b5855ff6704171f5b7761b8c88536c8dca84943810e9332db9f2e03681f6355c5068a3a3092  0001-group-sync-with-alpine-baselayout.patch
 712038089d3b58a3937549f25412803291690c992bd8e3002f02a46fc9c24903c94086704b5def3aecb422d46d7c501beeaa665000bc7da6ec20965e52eca5a1  0001-mkinitfs-add-K-flag-to-copy-host-keys-to-new-initram.patch
-81880870195b6c64d55570954be2c8e879f58f779ec165ae34504bac43b969dfcb79b0469ab96100df12fb17a7e7b362bb52bbc3402705a931cd41f7304c6d0d  0002-mkinitfs_cmdline-override_rootdir-support_honor-tmpdir.patch"
+81880870195b6c64d55570954be2c8e879f58f779ec165ae34504bac43b969dfcb79b0469ab96100df12fb17a7e7b362bb52bbc3402705a931cd41f7304c6d0d  0002-mkinitfs_cmdline-override_rootdir-support_honor-tmpdir.patch
+2b9c9ca7b28c33ccbe854449f5b879620f3bc9feca801a05f9ec8a70d46471bf835d1660a05f49b1d2d8b484175265fce53577269aff711e7a1563e8d4a0aa37  0003-mkinitfs-bugfixes_speedup_data-code-sep_new-syntax_cleanup.patch"
diff --git a/main/mkinitfs/mkinitfs.pre-upgrade b/main/mkinitfs/mkinitfs.pre-upgrade
index 0ece527..d49fb34 100644
--- a/main/mkinitfs/mkinitfs.pre-upgrade
+++ b/main/mkinitfs/mkinitfs.pre-upgrade
@@ -7,7 +7,7 @@ for i in files modules; do
 		case "$j" in
 		*.apk-new) continue;;
 		esac
-		mv $j /etc/mkinitfs/features.d/${j##*/}.$i
+		mv "$j" "/etc/mkinitfs/features.d/${j##*/}.$i"
 	done
 done
 exit 0
diff --git a/main/mkinitfs/mkinitfs.trigger b/main/mkinitfs/mkinitfs.trigger
index cedf363..4db9b4a 100644
--- a/main/mkinitfs/mkinitfs.trigger
+++ b/main/mkinitfs/mkinitfs.trigger
@@ -3,16 +3,16 @@
 for i in "$@"; do
 	# get last element in path
 	flavor=${i##*/}
-	if ! [ -f "$i"/kernel.release ]; then
+	if ! [ -f "${i}/kernel.release" ]; then
 		# kernel was uninstalled
-		rm -f $( readlink -f /boot/initramfs-$flavor ) \
-			/boot/initramfs-$flavor /boot/vmlinuz-$flavor \
-			/boot/$flavor /boot/$flavor.gz /$flavor /$flavor.gz
+		rm -f $(readlink -f "/boot/initramfs-$flavor") \
+			"/boot/initramfs-$flavor" "/boot/vmlinuz-$flavor" \
+			"/boot/$flavor" "/boot/${flavor}.gz" "/$flavor" "/${flavor}.gz"
 		continue
 	fi
-	abi_release=$(cat "$i"/kernel.release)
-	initfs=initramfs-$flavor
-	mkinitfs -o /boot/$initfs $abi_release
+	abi_release=$(cat "${i}/kernel.release")
+	initfs="initramfs-$flavor"
+	mkinitfs -o "/boot/$initfs" "$abi_release"
 done
 
 # extlinux will use path relative partition, so if /boot is on a
@@ -23,8 +23,8 @@ fi
 
 # cleanup unused initramfs
 for i in /boot/initramfs-[0-9]*; do
-	[ -f $i ] || continue
-	if ! [ -f /boot/vmlinuz-${i#/boot/initramfs-} ]; then
+	[ -f "$i" ] || continue
+	if ! [ -f "/boot/vmlinuz-${i#/boot/initramfs-}" ]; then
 		rm "$i"
 	fi
 done
-- 
2.10.0

-- 
A: Because it messes up the order in which people normally read text. 
Q: Why is top-posting such a bad thing? 

---
Unsubscribe:  [email-masked]
Help:         [email-masked]
---

---

From: Natanael Copa <[email-masked]>
Date: Mon, 14 Nov 2016 06:05:47 -0800

Hi,

Thank you very much for this patch. Do you think you could split it up
and send a patch againt the mkinitfs git repository instead of aports?

http://git.alpinelinux.org/cgit/mkinitfs
https://github.com/alpinelinux/mkinitfs

Either of those are fine.

On Sun, 6 Nov 2016 00:34:21 +0100

> * Many small bugfixes.

Do you think you could do bugifxes in a separate patch? I'd like
bugfixes in for v3.5.

> * ~100 times speedup on gathering files for the initramfs.

It would be nice to have this in a spearate incremental patch too.

>   that directory to be included and nothing below that path.

I like this idea, however, it is a bit scary change. People may have
created their custom config files for kernel modules etc. Thing may
break when they upgrade, and the consequence is non-bootable system.

I am not convinced that we want this for v3.5 release. We might need
test it carefully first.

> * Some cleanup, quoting fixes and similar.

Separate patch for this too.

That way it makes it easy to review the patches and see what is
actually going on and what is cleanup and quoting fixes.

Thanks!

---