Blame 0045-Add-job-control-support-to-emergency-shell.patch

Harald Hoyer 83ebbb
From 4401925c822e2f7e927bad4c16e4a08ac6a18d9a Mon Sep 17 00:00:00 2001
Harald Hoyer 83ebbb
From: Michal Soltys <soltys@ziu.info>
Harald Hoyer 83ebbb
Date: Mon, 26 Dec 2011 14:29:15 +0800
Harald Hoyer 83ebbb
Subject: [PATCH] Add job control support to emergency shell
Harald Hoyer 83ebbb
Harald Hoyer 83ebbb
Option --ctty will optionally add setsid binary to dracut's image.
Harald Hoyer 83ebbb
Harald Hoyer 83ebbb
During runtime, if rd.ctty is set and is a character device,
Harald Hoyer 83ebbb
emergency shells will be spawned with job control.
Harald Hoyer 83ebbb
Harald Hoyer 83ebbb
in case no ctty was provided, shell was spawned without caring about
Harald Hoyer 83ebbb
/dev/console. Also, the ctty is more opportunistic. If the image was
Harald Hoyer 83ebbb
generated with --ctty, we will fallback to /dev/tty1 if rc.ctty is
Harald Hoyer 83ebbb
invalid or missing. Otherwise we spawn standard shell on /dev/console
Harald Hoyer 83ebbb
Harald Hoyer 83ebbb
[dyoung@redhat.com: Rebased to usrmove branch]
Harald Hoyer 83ebbb
Harald Hoyer 83ebbb
Signed-off-by: Michal Soltys <soltys@ziu.info>
Harald Hoyer 83ebbb
Signed-off-by: Dave Young <dyoung@redhat.com>
Harald Hoyer 83ebbb
---
Harald Hoyer 83ebbb
 dracut                           |    3 ++-
Harald Hoyer 83ebbb
 dracut.8.xml                     |    9 +++++++++
Harald Hoyer 83ebbb
 dracut.cmdline.7.xml             |   14 ++++++++++++++
Harald Hoyer 83ebbb
 modules.d/99base/init            |   12 ++++++++++--
Harald Hoyer 83ebbb
 modules.d/99base/module-setup.sh |    1 +
Harald Hoyer 83ebbb
 modules.d/99shutdown/shutdown    |   12 ++++++++++--
Harald Hoyer 83ebbb
 6 files changed, 46 insertions(+), 5 deletions(-)
Harald Hoyer 83ebbb
Harald Hoyer 83ebbb
diff --git a/dracut b/dracut
Harald Hoyer 83ebbb
index 8c24e7b..3c21561 100755
Harald Hoyer 83ebbb
--- a/dracut
Harald Hoyer 83ebbb
+++ b/dracut
Harald Hoyer 83ebbb
@@ -232,6 +232,7 @@ while (($# > 0)); do
Harald Hoyer 83ebbb
         --nolvmconf)   lvmconf_l="no";;
Harald Hoyer 83ebbb
         --debug)       debug="yes";;
Harald Hoyer 83ebbb
         --profile)     profile="yes";;
Harald Hoyer 83ebbb
+        --ctty)        cttyhack="yes";;
Harald Hoyer 83ebbb
         -v|--verbose)  ((verbosity_mod_l++));;
Harald Hoyer 83ebbb
         -q|--quiet)    ((verbosity_mod_l--));;
Harald Hoyer 83ebbb
         -l|--local)    allowlocal="yes" ;;
Harald Hoyer 83ebbb
@@ -585,7 +586,7 @@ done
Harald Hoyer 83ebbb
 export initdir dracutbasedir dracutmodules drivers \
Harald Hoyer 83ebbb
     fw_dir drivers_dir debug no_kernel kernel_only \
Harald Hoyer 83ebbb
     add_drivers mdadmconf lvmconf filesystems \
Harald Hoyer 83ebbb
-    use_fstab libdir usrlibdir fscks nofscks \
Harald Hoyer 83ebbb
+    use_fstab libdir usrlibdir fscks nofscks cttyhack \
Harald Hoyer 83ebbb
     stdloglvl sysloglvl fileloglvl kmsgloglvl logfile \
Harald Hoyer 83ebbb
     debug host_fs_types host_devs
Harald Hoyer 83ebbb
 
Harald Hoyer 83ebbb
diff --git a/dracut.8.xml b/dracut.8.xml
Harald Hoyer 83ebbb
index 221ad2e..955d4d2 100644
Harald Hoyer 83ebbb
--- a/dracut.8.xml
Harald Hoyer 83ebbb
+++ b/dracut.8.xml
Harald Hoyer 83ebbb
@@ -297,6 +297,15 @@ include in the generic initramfs. This parameter can be specified multiple times
Harald Hoyer 83ebbb
         </varlistentry>
Harald Hoyer 83ebbb
         <varlistentry>
Harald Hoyer 83ebbb
           <term>
Harald Hoyer 83ebbb
+            <option>--ctty</option>
Harald Hoyer 83ebbb
+          </term>
Harald Hoyer 83ebbb
+          <listitem>
Harald Hoyer 83ebbb
+            <para>if possible, try to spawn an emergency shell on a terminal
Harald Hoyer 83ebbb
+              with job control</para>
Harald Hoyer 83ebbb
+          </listitem>
Harald Hoyer 83ebbb
+        </varlistentry>
Harald Hoyer 83ebbb
+        <varlistentry>
Harald Hoyer 83ebbb
+          <term>
Harald Hoyer 83ebbb
             <option>-h</option>
Harald Hoyer 83ebbb
           </term>
Harald Hoyer 83ebbb
           <term>
Harald Hoyer 83ebbb
diff --git a/dracut.cmdline.7.xml b/dracut.cmdline.7.xml
Harald Hoyer 83ebbb
index 74e617c..0fa4762 100644
Harald Hoyer 83ebbb
--- a/dracut.cmdline.7.xml
Harald Hoyer 83ebbb
+++ b/dracut.cmdline.7.xml
Harald Hoyer 83ebbb
@@ -109,6 +109,20 @@ This parameter can be specified multiple times.</para>
Harald Hoyer 83ebbb
 	    <para>force loading kernel module <drivername> after all automatic loading modules have been loaded. This parameter can be specified multiple times.</para>
Harald Hoyer 83ebbb
           </listitem>
Harald Hoyer 83ebbb
         </varlistentry>
Harald Hoyer 83ebbb
+        <varlistentry>
Harald Hoyer 83ebbb
+          <term>
Harald Hoyer 83ebbb
+            <envar>rd.ctty=<replaceable><terminal></replaceable></envar>
Harald Hoyer 83ebbb
+          </term>
Harald Hoyer 83ebbb
+          <listitem>
Harald Hoyer 83ebbb
+            <para>
Harald Hoyer 83ebbb
+              if the dracut image was generated with --ctty option, try to
Harald Hoyer 83ebbb
+              spawn an emergency shell on the specified terminal; if
Harald Hoyer 83ebbb
+              <envar>rd.ctty</envar> is specified without a value or not
Harald Hoyer 83ebbb
+              provided at all, the default is /dev/tty1. The '/dev' prefix
Harald Hoyer 83ebbb
+              can be omitted.
Harald Hoyer 83ebbb
+            </para>
Harald Hoyer 83ebbb
+          </listitem>
Harald Hoyer 83ebbb
+        </varlistentry>
Harald Hoyer 83ebbb
       </variablelist>
Harald Hoyer 83ebbb
     </refsect2>
Harald Hoyer 83ebbb
     <refsect2 id="dracut-kernel-debug">
Harald Hoyer 83ebbb
diff --git a/modules.d/99base/init b/modules.d/99base/init
Harald Hoyer 83ebbb
index 1e54449..33a7379 100755
Harald Hoyer 83ebbb
--- a/modules.d/99base/init
Harald Hoyer 83ebbb
+++ b/modules.d/99base/init
Harald Hoyer 83ebbb
@@ -40,6 +40,7 @@ wait_for_loginit()
Harald Hoyer 83ebbb
 
Harald Hoyer 83ebbb
 emergency_shell()
Harald Hoyer 83ebbb
 {
Harald Hoyer 83ebbb
+    local _ctty
Harald Hoyer 83ebbb
     set +e
Harald Hoyer 83ebbb
     if [ "$1" = "-n" ]; then
Harald Hoyer 83ebbb
         _rdshell_name=$2
Harald Hoyer 83ebbb
@@ -57,8 +58,15 @@ emergency_shell()
Harald Hoyer 83ebbb
         echo "Dropping to debug shell."
Harald Hoyer 83ebbb
         echo
Harald Hoyer 83ebbb
         export PS1="$_rdshell_name:\${PWD}# "
Harald Hoyer 83ebbb
-        [ -e /.profile ] || echo "exec 0<>/dev/console 1<>/dev/console 2<>/dev/console" > /.profile
Harald Hoyer 83ebbb
-        sh -i -l
Harald Hoyer 83ebbb
+        [ -e /.profile ] || >/.profile
Harald Hoyer 83ebbb
+        _ctty=/dev/console
Harald Hoyer 83ebbb
+        if type setsid >/dev/null 2>&1; then
Harald Hoyer 83ebbb
+            _ctty="$(getarg rd.ctty=)" && _ctty="/dev/${_ctty##*/}"
Harald Hoyer 83ebbb
+            [ -c "$_ctty" ] || _ctty=/dev/tty1
Harald Hoyer 83ebbb
+            setsid sh -i -l 0<$_ctty 1>$_ctty 2>&1
Harald Hoyer 83ebbb
+        else
Harald Hoyer 83ebbb
+            sh -i -l 0<$_ctty 1>$_ctty 2>&1
Harald Hoyer 83ebbb
+        fi
Harald Hoyer 83ebbb
     else
Harald Hoyer 83ebbb
         warn "Boot has failed. To debug this issue add \"rdshell\" to the kernel command line."
Harald Hoyer 83ebbb
         # cause a kernel panic
Harald Hoyer 83ebbb
diff --git a/modules.d/99base/module-setup.sh b/modules.d/99base/module-setup.sh
Harald Hoyer 83ebbb
index f6c1209..03058b1 100755
Harald Hoyer 83ebbb
--- a/modules.d/99base/module-setup.sh
Harald Hoyer 83ebbb
+++ b/modules.d/99base/module-setup.sh
Harald Hoyer 83ebbb
@@ -16,6 +16,7 @@ install() {
Harald Hoyer 83ebbb
     dracut_install mount mknod mkdir modprobe pidof sleep chroot \
Harald Hoyer 83ebbb
         sed ls flock cp mv dmesg rm ln rmmod mkfifo umount readlink
Harald Hoyer 83ebbb
     dracut_install -o less
Harald Hoyer 83ebbb
+    [[ $cttyhack = yes ]] && dracut_install -o setsid
Harald Hoyer 83ebbb
     if [ ! -e "${initdir}/bin/sh" ]; then
Harald Hoyer 83ebbb
         dracut_install bash
Harald Hoyer 83ebbb
         (ln -s bash "${initdir}/bin/sh" || :)
Harald Hoyer 83ebbb
diff --git a/modules.d/99shutdown/shutdown b/modules.d/99shutdown/shutdown
Harald Hoyer 83ebbb
index a31a95d..21bb37f 100755
Harald Hoyer 83ebbb
--- a/modules.d/99shutdown/shutdown
Harald Hoyer 83ebbb
+++ b/modules.d/99shutdown/shutdown
Harald Hoyer 83ebbb
@@ -13,6 +13,7 @@ export TERM=linux
Harald Hoyer 83ebbb
 
Harald Hoyer 83ebbb
 emergency_shell()
Harald Hoyer 83ebbb
 {
Harald Hoyer 83ebbb
+    local _ctty
Harald Hoyer 83ebbb
     set +e
Harald Hoyer 83ebbb
     if [ "$1" = "-n" ]; then
Harald Hoyer 83ebbb
         _rdshell_name=$2
Harald Hoyer 83ebbb
@@ -29,8 +30,15 @@ emergency_shell()
Harald Hoyer 83ebbb
         echo "Dropping to debug shell."
Harald Hoyer 83ebbb
         echo
Harald Hoyer 83ebbb
         export PS1="$_rdshell_name:\${PWD}# "
Harald Hoyer 83ebbb
-        [ -e /.profile ] || echo "exec 0<>/dev/console 1<>/dev/console 2<>/dev/console" > /.profile
Harald Hoyer 83ebbb
-        sh -i -l
Harald Hoyer 83ebbb
+        [ -e /.profile ] || >/.profile
Harald Hoyer 83ebbb
+        _ctty=/dev/console
Harald Hoyer 83ebbb
+        if type setsid >/dev/null 2>&1; then
Harald Hoyer 83ebbb
+            _ctty="$(getarg rd.ctty=)" && _ctty="/dev/${_ctty##*/}"
Harald Hoyer 83ebbb
+            [ -c "$_ctty" ] || _ctty=/dev/tty1
Harald Hoyer 83ebbb
+            setsid sh -i -l 0<$_ctty 1>$_ctty 2>&1
Harald Hoyer 83ebbb
+        else
Harald Hoyer 83ebbb
+            sh -i -l 0<$_ctty 1>$_ctty 2>&1
Harald Hoyer 83ebbb
+        fi
Harald Hoyer 83ebbb
     else
Harald Hoyer 83ebbb
         exec /lib/systemd/systemd-shutdown "$@"
Harald Hoyer 83ebbb
         warn "Shutdown has failed. To debug this issue add \"rdshell\" to the kernel command line."