commit:     271c330b15d16c318cfed654d029be4b026eb770
Author:     Thomas Deutschmann <whissi <AT> gentoo <DOT> org>
AuthorDate: Fri Aug 16 19:12:04 2019 +0000
Commit:     Thomas Deutschmann <whissi <AT> gentoo <DOT> org>
CommitDate: Fri Aug 16 19:12:04 2019 +0000
URL:        https://gitweb.gentoo.org/proj/genkernel.git/commit/?id=271c330b

initrd.scripts: preserve_log(): Add support for additional mountpoints

This commit will add support for copying init.log to 
$NEWROOT/var/log/genkernel-boot.log
for example even when $NEWROOT/var/log is an own partition.

To make this work, you must tell genkernel via /etc/initramfs.mounts
to mount the additional mount point.

Signed-off-by: Thomas Deutschmann <whissi <AT> gentoo.org>

 defaults/initrd.defaults |  1 +
 defaults/initrd.scripts  | 68 ++++++++++++++++++++++++++++++++++--------------
 defaults/linuxrc         |  8 +++---
 3 files changed, 54 insertions(+), 23 deletions(-)

diff --git a/defaults/initrd.defaults b/defaults/initrd.defaults
index 06057d7..8fa2822 100644
--- a/defaults/initrd.defaults
+++ b/defaults/initrd.defaults
@@ -73,6 +73,7 @@ IP='dhcp'
 GK_DEBUGMODE_STATEFILE="/tmp/debug.enabled"
 GK_INIT_LOG='/tmp/init.log'
 GK_INIT_LOG_COPYTO=
+GK_INIT_LOG_COPYTO_DEFAULT='/genkernel-boot.log'
 GK_INIT_LOG_DISABLED='/tmp/no-init.log'
 GK_INIT_LOG_PREFIX=
 GK_NET_DHCP_PIDFILE='/var/run/udhcpc.pid'

diff --git a/defaults/initrd.scripts b/defaults/initrd.scripts
index 1d2fa20..2c57ff4 100644
--- a/defaults/initrd.scripts
+++ b/defaults/initrd.scripts
@@ -1052,37 +1052,67 @@ preserve_log() {
        [ -z "${GK_INIT_LOG_COPYTO}" ] && return
 
        local have_errors=0
+
        local logfile_target="${CHROOT}/${GK_INIT_LOG_COPYTO#/}"
-       local logfile_target_dir="$(dirname "${logfile_target}")"
+       if ! echo "${logfile_target}" | grep -qE '^.*/[A-Za-z0-9._-]+$'
+       then
+               # The test above will make sure that a filename was given
+               # so we can be sure that dirname() will always return a
+               # path.
+               warn_msg "gk.log.keep value '${GK_INIT_LOG_COPYTO}' is invalid; 
Will copy log to '${GK_INIT_LOG_COPYTO_DEFAULT}' instead ..."
+               logfile_target="${CHROOT}/${GK_INIT_LOG_COPYTO_DEFAULT#/}"
+       fi
+
        local fail_msg="Failed to copy '${GK_INIT_LOG}' to '${logfile_target}'"
+       local logfile_target_dir="$(dirname "${logfile_target}")"
 
-       if run mount -o remount,rw ${CHROOT}
+       if [ -z "${logfile_target_dir}" ]
        then
-               if [ -z "${logfile_target_dir}" ]
-               then
-                       have_errors=1
-                       bad_msg "${fail_msg}: Failed to determine dirname of 
'${logfile_target}'!"
-               elif [ ! -d "${logfile_target_dir}" ]
+               bad_msg "${fail_msg}: Failed to determine dirname of 
'${logfile_target}'!"
+               return 1
+       fi
+
+       local logfile_mountpoint="${logfile_target_dir}"
+       while [ true ]
+       do
+               if run mountpoint "${logfile_mountpoint}" 1>/dev/null 2>&1
                then
-                       if ! run mkdir -p "${logfile_target_dir}" 2>/dev/null
+                       if run mount -o remount,rw "${logfile_mountpoint}" 
1>/dev/null 2>&1
                        then
-                               have_errors=1
-                               bad_msg "${fail_msg}: Failed to create 
'${logfile_target_dir}'!"
+                               break
+                       else
+                               bad_msg "${fail_msg}: 'mount -o remount,rw 
\"${logfile_mountpoint}\"' failed!"
+                               return 1
                        fi
                fi
 
-               if [ ${have_errors} = 0 ]
+               logfile_mountpoint="$(dirname "${logfile_mountpoint}")"
+               if [ "${logfile_mountpoint}" = "${CHROOT}" ]
                then
-                       good_msg "gk.log.keep set; Copying '${GK_INIT_LOG}' to 
'${logfile_target}' ..."
-                       if ! cp "${GK_INIT_LOG}" "${logfile_target}" 2>/dev/null
-                       then
-                               bad_msg "${fail_msg}!"
-                       fi
+                       # Stop here
+                       bad_msg "${fail_msg}: Failed to determine mountpoint 
for '${logfile_target}'!"
+                       return 1
                fi
+       done
 
-               mount -o remount,ro ${CHROOT}
-       else
-               bad_msg "${fail_msg}: 'mount -o remount,rw ${CHROOT}' failed!"
+       if [ ! -d "${logfile_target_dir}" ]
+       then
+               if ! run mkdir -p "${logfile_target_dir}" 2>/dev/null
+               then
+                       bad_msg "${fail_msg}: Failed to create 
'${logfile_target_dir}'!"
+                       return 1
+               fi
+       fi
+
+       good_msg "gk.log.keep set; Copying '${GK_INIT_LOG}' to 
'${logfile_target}' ..."
+       if ! run cp -f "${GK_INIT_LOG}" "${logfile_target}" 2>/dev/null
+       then
+               bad_msg "${fail_msg}!"
+       fi
+
+       if ! run mount -o remount,ro "${logfile_mountpoint}" 1>/dev/null 2>&1
+       then
+               bad_msg "Failed to re-mount ${logfile_mountpoint} read-only!"
        fi
 }
 

diff --git a/defaults/linuxrc b/defaults/linuxrc
index 1bd6dc9..cd3a6d7 100644
--- a/defaults/linuxrc
+++ b/defaults/linuxrc
@@ -288,16 +288,16 @@ do
                gk.log.keep=*)
                        case "${x#*=}" in
                                [Tt][Rr][Uu][Ee])
-                                       GK_INIT_LOG_COPYTO=/genkernel-boot.log
+                                       
GK_INIT_LOG_COPYTO="${GK_INIT_LOG_COPYTO_DEFAULT}"
                                ;;
                                [Yy][Ee][Ss])
-                                       GK_INIT_LOG_COPYTO=/genkernel-boot.log
+                                       
GK_INIT_LOG_COPYTO="${GK_INIT_LOG_COPYTO_DEFAULT}"
                                ;;
                                [Yy])
-                                       GK_INIT_LOG_COPYTO=/genkernel-boot.log
+                                       
GK_INIT_LOG_COPYTO="${GK_INIT_LOG_COPYTO_DEFAULT}"
                                ;;
                                1)
-                                       GK_INIT_LOG_COPYTO=/genkernel-boot.log
+                                       
GK_INIT_LOG_COPYTO="${GK_INIT_LOG_COPYTO_DEFAULT}"
                                ;;
                                [Ff][Aa][Ll][Ss][Ee])
                                        GK_INIT_LOG_COPYTO=

Reply via email to