]> git.ipfire.org Git - thirdparty/dracut.git/commitdiff
Add ssh client module code
authorDave Young <dyoung@redhat.com>
Mon, 26 Dec 2011 06:29:21 +0000 (14:29 +0800)
committerHarald Hoyer <harald@redhat.com>
Mon, 23 Jan 2012 08:48:35 +0000 (09:48 +0100)
Add ssh client module which support ssh key mode and interactive mode.
with --sshkey option you can provide the ssh key to be installed

>why not call it "ssh" module?
ssh-client is better, maybe future there will be ssh-server come in.
In debian these are also two different packages.

Usage:
1. sshkey mode:
transfer your public key to remote machine with ssh-copy-id or do it mannaully
example of options:
./dracut -l -H -a ssh-client --sshkey /root/.ssh/id_rsa i.img
2. interactive mode:
need use --ctty option, ie.:
./dracut -l -H -a ssh-client --ctty i.img

[v2 changes]:
per wangcong: add patch description about module name
add help line in usage()
remove useless comment

Signed-off-by: Dave Young <dyoung@redhat.com>
dracut
dracut.8.xml
modules.d/95ssh-client/module-setup.sh [new file with mode: 0644]

diff --git a/dracut b/dracut
index 3c2156118189ffb883507a880e335cde696c5caa..d0c335bd4f0b0c356544387b353dae5e96d9c1d7 100755 (executable)
--- a/dracut
+++ b/dracut
@@ -118,6 +118,7 @@ Creates initial ramdisk images for preloading modules
   -M, --show-modules    Print included module's name to standard output during
                          build.
   --keep                Keep the temporary initramfs for debugging purposes
+  --sshkey [SSHKEY]     Add ssh key to initramfs (use with ssh-client module)
 EOF
 }
 
@@ -233,6 +234,7 @@ while (($# > 0)); do
         --debug)       debug="yes";;
         --profile)     profile="yes";;
         --ctty)        cttyhack="yes";;
+        --sshkey)      read_arg sshkey   "$@" || shift;;
         -v|--verbose)  ((verbosity_mod_l++));;
         -q|--quiet)    ((verbosity_mod_l--));;
         -l|--local)    allowlocal="yes" ;;
@@ -588,7 +590,7 @@ export initdir dracutbasedir dracutmodules drivers \
     add_drivers mdadmconf lvmconf filesystems \
     use_fstab libdir usrlibdir fscks nofscks cttyhack \
     stdloglvl sysloglvl fileloglvl kmsgloglvl logfile \
-    debug host_fs_types host_devs
+    debug host_fs_types host_devs sshkey
 
 # Create some directory structure first
 [[ $prefix ]] && mkdir -m 0755 -p "${initdir}${prefix}"
index 955d4d2878b378d7bb061be8072a6cc44ff3b177..5a5df54cc99605d7660ee403994aef15cd5e4dbe 100644 (file)
@@ -368,6 +368,14 @@ Default:
 <filename>/etc/dracut.conf.d</filename></para>
           </listitem>
         </varlistentry>
+        <varlistentry>
+          <term>
+            <option>--sshkey&nbsp;<replaceable>&lt;sshkey file&gt;</replaceable></option>
+          </term>
+          <listitem>
+            <para>ssh key file used with ssh-client module.</para>
+          </listitem>
+        </varlistentry>
         <varlistentry>
           <term>
             <option>-l</option>
diff --git a/modules.d/95ssh-client/module-setup.sh b/modules.d/95ssh-client/module-setup.sh
new file mode 100644 (file)
index 0000000..0ffc298
--- /dev/null
@@ -0,0 +1,60 @@
+#!/bin/bash
+# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
+# ex: ts=8 sw=4 sts=4 et filetype=sh
+
+# fixme: assume user is root
+
+check() {
+    # If our prerequisites are not met, fail.
+    type -P ssh >/dev/null || return 1
+    type -P scp >/dev/null || return 1
+    if [[ $sshkey ]]; then
+        [ ! -f $sshkey ] && {
+            derror "sshkey is not found!"
+            return 1
+        }
+        [[ ! $cttyhack = yes ]] && {
+            dinfo "--ctty is not used, you should make sure the machine is knowhost and copy the sshkey to remote machine!"
+        }
+    else
+        [[ ! $cttyhack = yes ]] && {
+            derror "ssh interactive mode need option --ctty!"
+            return 1
+        }
+    fi
+
+    return 0
+}
+
+depends() {
+    # We depend on network modules being loaded
+    echo network
+}
+
+inst_sshenv()
+{
+    if [ -d /root/.ssh ]; then
+        inst_dir /root/.ssh
+        chmod 700 ${initdir}/root/.ssh
+    fi
+
+    # Copy over ssh key and knowhosts if needed
+    [[ $sshkey ]] && {
+        inst $sshkey
+        [[ -f /root/.ssh/known_hosts ]] && inst /root/.ssh/known_hosts
+        [[ -f /etc/ssh/ssh_known_hosts ]] && inst /etc/ssh/ssh_known_hosts
+    }
+
+    # Copy over root and system-wide ssh configs.
+    [[ -f /root/.ssh/config ]] && inst /root/.ssh/config
+    [[ -f /etc/ssh/ssh_config ]] && inst /etc/ssh/ssh_config
+
+    return 0
+}
+
+install() {
+    inst ssh
+    inst scp
+    inst_sshenv
+}
+