Harald Hoyer 83ebbb
From cdfeb278ab4d4b6a9d848e1b9accc3554e4ac6eb Mon Sep 17 00:00:00 2001
Harald Hoyer 83ebbb
From: Dave Young <dyoung@redhat.com>
Harald Hoyer 83ebbb
Date: Mon, 26 Dec 2011 14:29:21 +0800
Harald Hoyer 83ebbb
Subject: [PATCH] Add ssh client module code
Harald Hoyer 83ebbb
Harald Hoyer 83ebbb
Add ssh client module which support ssh key mode and interactive mode.
Harald Hoyer 83ebbb
with --sshkey option you can provide the ssh key to be installed
Harald Hoyer 83ebbb
Harald Hoyer 83ebbb
>why not call it "ssh" module?
Harald Hoyer 83ebbb
ssh-client is better, maybe future there will be ssh-server come in.
Harald Hoyer 83ebbb
In debian these are also two different packages.
Harald Hoyer 83ebbb
Harald Hoyer 83ebbb
Usage:
Harald Hoyer 83ebbb
1. sshkey mode:
Harald Hoyer 83ebbb
transfer your public key to remote machine with ssh-copy-id or do it mannaully
Harald Hoyer 83ebbb
example of options:
Harald Hoyer 83ebbb
./dracut -l -H -a ssh-client --sshkey /root/.ssh/id_rsa i.img
Harald Hoyer 83ebbb
2. interactive mode:
Harald Hoyer 83ebbb
need use --ctty option, ie.:
Harald Hoyer 83ebbb
./dracut -l -H -a ssh-client --ctty i.img
Harald Hoyer 83ebbb
Harald Hoyer 83ebbb
[v2 changes]:
Harald Hoyer 83ebbb
per wangcong: add patch description about module name
Harald Hoyer 83ebbb
add help line in usage()
Harald Hoyer 83ebbb
remove useless comment
Harald Hoyer 83ebbb
Harald Hoyer 83ebbb
Signed-off-by: Dave Young <dyoung@redhat.com>
Harald Hoyer 83ebbb
---
Harald Hoyer 83ebbb
 dracut                                 |    4 ++-
Harald Hoyer 83ebbb
 dracut.8.xml                           |    8 ++++
Harald Hoyer 83ebbb
 modules.d/95ssh-client/module-setup.sh |   60 ++++++++++++++++++++++++++++++++
Harald Hoyer 83ebbb
 3 files changed, 71 insertions(+), 1 deletions(-)
Harald Hoyer 83ebbb
 create mode 100644 modules.d/95ssh-client/module-setup.sh
Harald Hoyer 83ebbb
Harald Hoyer 83ebbb
diff --git a/dracut b/dracut
Harald Hoyer 83ebbb
index 3c21561..d0c335b 100755
Harald Hoyer 83ebbb
--- a/dracut
Harald Hoyer 83ebbb
+++ b/dracut
Harald Hoyer 83ebbb
@@ -118,6 +118,7 @@ Creates initial ramdisk images for preloading modules
Harald Hoyer 83ebbb
   -M, --show-modules    Print included module's name to standard output during
Harald Hoyer 83ebbb
                          build.
Harald Hoyer 83ebbb
   --keep                Keep the temporary initramfs for debugging purposes
Harald Hoyer 83ebbb
+  --sshkey [SSHKEY]     Add ssh key to initramfs (use with ssh-client module)
Harald Hoyer 83ebbb
 EOF
Harald Hoyer 83ebbb
 }
Harald Hoyer 83ebbb
 
Harald Hoyer 83ebbb
@@ -233,6 +234,7 @@ while (($# > 0)); do
Harald Hoyer 83ebbb
         --debug)       debug="yes";;
Harald Hoyer 83ebbb
         --profile)     profile="yes";;
Harald Hoyer 83ebbb
         --ctty)        cttyhack="yes";;
Harald Hoyer 83ebbb
+        --sshkey)      read_arg sshkey   "$@" || shift;;
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
@@ -588,7 +590,7 @@ export initdir dracutbasedir dracutmodules drivers \
Harald Hoyer 83ebbb
     add_drivers mdadmconf lvmconf filesystems \
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
+    debug host_fs_types host_devs sshkey
Harald Hoyer 83ebbb
 
Harald Hoyer 83ebbb
 # Create some directory structure first
Harald Hoyer 83ebbb
 [[ $prefix ]] && mkdir -m 0755 -p "${initdir}${prefix}"
Harald Hoyer 83ebbb
diff --git a/dracut.8.xml b/dracut.8.xml
Harald Hoyer 83ebbb
index 955d4d2..5a5df54 100644
Harald Hoyer 83ebbb
--- a/dracut.8.xml
Harald Hoyer 83ebbb
+++ b/dracut.8.xml
Harald Hoyer 83ebbb
@@ -370,6 +370,14 @@ Default:
Harald Hoyer 83ebbb
         </varlistentry>
Harald Hoyer 83ebbb
         <varlistentry>
Harald Hoyer 83ebbb
           <term>
Harald Hoyer 83ebbb
+            <option>--sshkey <replaceable><sshkey file></replaceable></option>
Harald Hoyer 83ebbb
+          </term>
Harald Hoyer 83ebbb
+          <listitem>
Harald Hoyer 83ebbb
+            <para>ssh key file used with ssh-client module.</para>
Harald Hoyer 83ebbb
+          </listitem>
Harald Hoyer 83ebbb
+        </varlistentry>
Harald Hoyer 83ebbb
+        <varlistentry>
Harald Hoyer 83ebbb
+          <term>
Harald Hoyer 83ebbb
             <option>-l</option>
Harald Hoyer 83ebbb
           </term>
Harald Hoyer 83ebbb
           <term>
Harald Hoyer 83ebbb
diff --git a/modules.d/95ssh-client/module-setup.sh b/modules.d/95ssh-client/module-setup.sh
Harald Hoyer 83ebbb
new file mode 100644
Harald Hoyer 83ebbb
index 0000000..0ffc298
Harald Hoyer 83ebbb
--- /dev/null
Harald Hoyer 83ebbb
+++ b/modules.d/95ssh-client/module-setup.sh
Harald Hoyer 83ebbb
@@ -0,0 +1,60 @@
Harald Hoyer 83ebbb
+#!/bin/bash
Harald Hoyer 83ebbb
+# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
Harald Hoyer 83ebbb
+# ex: ts=8 sw=4 sts=4 et filetype=sh
Harald Hoyer 83ebbb
+
Harald Hoyer 83ebbb
+# fixme: assume user is root
Harald Hoyer 83ebbb
+
Harald Hoyer 83ebbb
+check() {
Harald Hoyer 83ebbb
+    # If our prerequisites are not met, fail.
Harald Hoyer 83ebbb
+    type -P ssh >/dev/null || return 1
Harald Hoyer 83ebbb
+    type -P scp >/dev/null || return 1
Harald Hoyer 83ebbb
+    if [[ $sshkey ]]; then
Harald Hoyer 83ebbb
+        [ ! -f $sshkey ] && {
Harald Hoyer 83ebbb
+            derror "sshkey is not found!"
Harald Hoyer 83ebbb
+            return 1
Harald Hoyer 83ebbb
+        }
Harald Hoyer 83ebbb
+        [[ ! $cttyhack = yes ]] && {
Harald Hoyer 83ebbb
+            dinfo "--ctty is not used, you should make sure the machine is knowhost and copy the sshkey to remote machine!"
Harald Hoyer 83ebbb
+        }
Harald Hoyer 83ebbb
+    else
Harald Hoyer 83ebbb
+        [[ ! $cttyhack = yes ]] && {
Harald Hoyer 83ebbb
+            derror "ssh interactive mode need option --ctty!"
Harald Hoyer 83ebbb
+            return 1
Harald Hoyer 83ebbb
+        }
Harald Hoyer 83ebbb
+    fi
Harald Hoyer 83ebbb
+
Harald Hoyer 83ebbb
+    return 0
Harald Hoyer 83ebbb
+}
Harald Hoyer 83ebbb
+
Harald Hoyer 83ebbb
+depends() {
Harald Hoyer 83ebbb
+    # We depend on network modules being loaded
Harald Hoyer 83ebbb
+    echo network
Harald Hoyer 83ebbb
+}
Harald Hoyer 83ebbb
+
Harald Hoyer 83ebbb
+inst_sshenv()
Harald Hoyer 83ebbb
+{
Harald Hoyer 83ebbb
+    if [ -d /root/.ssh ]; then
Harald Hoyer 83ebbb
+        inst_dir /root/.ssh
Harald Hoyer 83ebbb
+        chmod 700 ${initdir}/root/.ssh
Harald Hoyer 83ebbb
+    fi
Harald Hoyer 83ebbb
+
Harald Hoyer 83ebbb
+    # Copy over ssh key and knowhosts if needed
Harald Hoyer 83ebbb
+    [[ $sshkey ]] && {
Harald Hoyer 83ebbb
+        inst $sshkey
Harald Hoyer 83ebbb
+        [[ -f /root/.ssh/known_hosts ]] && inst /root/.ssh/known_hosts
Harald Hoyer 83ebbb
+        [[ -f /etc/ssh/ssh_known_hosts ]] && inst /etc/ssh/ssh_known_hosts
Harald Hoyer 83ebbb
+    }
Harald Hoyer 83ebbb
+
Harald Hoyer 83ebbb
+    # Copy over root and system-wide ssh configs.
Harald Hoyer 83ebbb
+    [[ -f /root/.ssh/config ]] && inst /root/.ssh/config
Harald Hoyer 83ebbb
+    [[ -f /etc/ssh/ssh_config ]] && inst /etc/ssh/ssh_config
Harald Hoyer 83ebbb
+
Harald Hoyer 83ebbb
+    return 0
Harald Hoyer 83ebbb
+}
Harald Hoyer 83ebbb
+
Harald Hoyer 83ebbb
+install() {
Harald Hoyer 83ebbb
+    inst ssh
Harald Hoyer 83ebbb
+    inst scp
Harald Hoyer 83ebbb
+    inst_sshenv
Harald Hoyer 83ebbb
+}
Harald Hoyer 83ebbb
+