Harald Hoyer 12f6cc
From d580636e97bffdd17124961e2f9db0aa2a645060 Mon Sep 17 00:00:00 2001
Harald Hoyer 12f6cc
From: Mike Gorse <mgorse@suse.com>
Harald Hoyer 12f6cc
Date: Thu, 26 Jul 2012 09:17:18 -0500
Harald Hoyer 12f6cc
Subject: [PATCH] Added cifs
Harald Hoyer 12f6cc
Harald Hoyer 12f6cc
---
Harald Hoyer 12f6cc
 dracut.cmdline.7.asc               | 15 +++++++++++
Harald Hoyer 12f6cc
 modules.d/95cifs/cifs-lib.sh       | 36 ++++++++++++++++++++++++++
Harald Hoyer 12f6cc
 modules.d/95cifs/cifsroot.sh       | 26 +++++++++++++++++++
Harald Hoyer 12f6cc
 modules.d/95cifs/module-setup.sh   | 46 +++++++++++++++++++++++++++++++++
Harald Hoyer 12f6cc
 modules.d/95cifs/parse-cifsroot.sh | 52 ++++++++++++++++++++++++++++++++++++++
Harald Hoyer 12f6cc
 5 files changed, 175 insertions(+)
Harald Hoyer 12f6cc
 create mode 100755 modules.d/95cifs/cifs-lib.sh
Harald Hoyer 12f6cc
 create mode 100755 modules.d/95cifs/cifsroot.sh
Harald Hoyer 12f6cc
 create mode 100755 modules.d/95cifs/module-setup.sh
Harald Hoyer 12f6cc
 create mode 100755 modules.d/95cifs/parse-cifsroot.sh
Harald Hoyer 12f6cc
Harald Hoyer 12f6cc
diff --git a/dracut.cmdline.7.asc b/dracut.cmdline.7.asc
Harald Hoyer 12f6cc
index 8f642f3..0b1b8a2 100644
Harald Hoyer 12f6cc
--- a/dracut.cmdline.7.asc
Harald Hoyer 12f6cc
+++ b/dracut.cmdline.7.asc
Harald Hoyer 12f6cc
@@ -398,6 +398,21 @@ NFS
Harald Hoyer 12f6cc
 **rd.nfs.domain=**_<NFSv4 domain name>_::
Harald Hoyer 12f6cc
     Set the NFSv4 domain name. Will overwrite the settings in _/etc/idmap.conf_.
Harald Hoyer 12f6cc
 
Harald Hoyer 12f6cc
+CIFS
Harald Hoyer 12f6cc
+~~~
Harald Hoyer 12f6cc
+**root=**cifs://[_<username>_[:_<password>_]@]_<server-ip>_:_<root-dir>_::
Harald Hoyer 12f6cc
+    mount cifs share from <server-ip>:/<root-dir>, if no server-ip is given, use
Harald Hoyer 12f6cc
+    dhcp next_server. if server-ip is an IPv6 address it has to be put in
Harald Hoyer 12f6cc
+    brackets, e.g. [2001:DB8::1]. If a username or password are not specified
Harald Hoyer 12f6cc
+as part of the root, then they must be passed on the command line through
Harald Hoyer 12f6cc
+cifsuser/cifspass.
Harald Hoyer 12f6cc
+
Harald Hoyer 12f6cc
+**cifsuser=_<username>_::
Harald Hoyer 12f6cc
+    Set the cifs username, if not specified as part of the root.
Harald Hoyer 12f6cc
+
Harald Hoyer 12f6cc
+**cifspass=_<password>_::
Harald Hoyer 12f6cc
+    Set the cifs password, if not specified as part of the root.
Harald Hoyer 12f6cc
+
Harald Hoyer 12f6cc
 iSCSI
Harald Hoyer 12f6cc
 ~~~~~
Harald Hoyer 12f6cc
 **root=**iscsi:[_<username>_:_<password>_[:_<reverse>_:_<password>_]@][_<servername>_]:[_<protocol>_]:[_<port>_][:[_<iscsi_iface_name>_]:[_<netdev_name>_]]:[_<LUN>_]:_<targetname>_::
Harald Hoyer 12f6cc
diff --git a/modules.d/95cifs/cifs-lib.sh b/modules.d/95cifs/cifs-lib.sh
Harald Hoyer 12f6cc
new file mode 100755
Harald Hoyer 12f6cc
index 0000000..3cbe6dc
Harald Hoyer 12f6cc
--- /dev/null
Harald Hoyer 12f6cc
+++ b/modules.d/95cifs/cifs-lib.sh
Harald Hoyer 12f6cc
@@ -0,0 +1,36 @@
Harald Hoyer 12f6cc
+#!/bin/sh
Harald Hoyer 12f6cc
+# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
Harald Hoyer 12f6cc
+# ex: ts=8 sw=4 sts=4 et filetype=sh
Harald Hoyer 12f6cc
+
Harald Hoyer 12f6cc
+# cifs_to_var CIFSROOT
Harald Hoyer 12f6cc
+# use CIFSROOT to set $server, $path, and $options.
Harald Hoyer 12f6cc
+# CIFSROOT is something like: cifs://[<username>[:<password>]]@<host>/<path>
Harald Hoyer 12f6cc
+# NETIF is used to get information from DHCP options, if needed.
Harald Hoyer 12f6cc
+
Harald Hoyer 12f6cc
+type getarg >/dev/null 2>&1 || . /lib/dracut-lib.sh
Harald Hoyer 12f6cc
+
Harald Hoyer 12f6cc
+cifs_to_var() {
Harald Hoyer 12f6cc
+    local cifsuser; local cifspass
Harald Hoyer 12f6cc
+    # Check required arguments
Harald Hoyer 12f6cc
+    server=${1##cifs://}
Harald Hoyer 12f6cc
+    cifsuser=${server%@*}
Harald Hoyer 12f6cc
+    cifspass=${cifsuser#*:}
Harald Hoyer 12f6cc
+    if [ "$cifspass" != "$cifsuser" ]; then
Harald Hoyer 12f6cc
+	cifsuser=${cifsuser%:*}
Harald Hoyer 12f6cc
+    else
Harald Hoyer 12f6cc
+	cifspass=$(getarg cifspass)
Harald Hoyer 12f6cc
+    fi
Harald Hoyer 12f6cc
+    if [ "$cifsuser" != "$server" ]; then
Harald Hoyer 12f6cc
+	server="${server#*@}"
Harald Hoyer 12f6cc
+    else
Harald Hoyer 12f6cc
+	cifsuser=$(getarg cifsuser)
Harald Hoyer 12f6cc
+    fi
Harald Hoyer 12f6cc
+
Harald Hoyer 12f6cc
+    path=${server#*/}
Harald Hoyer 12f6cc
+    server=${server%/*}
Harald Hoyer 12f6cc
+
Harald Hoyer 12f6cc
+    if [ ! "$cifsuser" -o ! "$cifspass" ]; then
Harald Hoyer 12f6cc
+	die "For CIFS support you need to specify a cifsuser and cifspass either in the cifsuser and cifspass commandline parameters or in the root= CIFS URL."
Harald Hoyer 12f6cc
+    fi
Harald Hoyer 12f6cc
+    options="user=$cifsuser,pass=$cifspass"
Harald Hoyer 12f6cc
+}
Harald Hoyer 12f6cc
diff --git a/modules.d/95cifs/cifsroot.sh b/modules.d/95cifs/cifsroot.sh
Harald Hoyer 12f6cc
new file mode 100755
Harald Hoyer 12f6cc
index 0000000..7956d6f
Harald Hoyer 12f6cc
--- /dev/null
Harald Hoyer 12f6cc
+++ b/modules.d/95cifs/cifsroot.sh
Harald Hoyer 12f6cc
@@ -0,0 +1,26 @@
Harald Hoyer 12f6cc
+#!/bin/sh
Harald Hoyer 12f6cc
+# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
Harald Hoyer 12f6cc
+# ex: ts=8 sw=4 sts=4 et filetype=sh
Harald Hoyer 12f6cc
+
Harald Hoyer 12f6cc
+type getarg >/dev/null 2>&1 || . /lib/dracut-lib.sh
Harald Hoyer 12f6cc
+. /lib/cifs-lib.sh
Harald Hoyer 12f6cc
+
Harald Hoyer 12f6cc
+[ "$#" = 3 ] || exit 1
Harald Hoyer 12f6cc
+
Harald Hoyer 12f6cc
+# root is in the form root=cifs://user:pass@[server]/[folder] either from
Harald Hoyer 12f6cc
+# cmdline or dhcp root-path
Harald Hoyer 12f6cc
+netif="$1"
Harald Hoyer 12f6cc
+root="$2"
Harald Hoyer 12f6cc
+NEWROOT="$3"
Harald Hoyer 12f6cc
+
Harald Hoyer 12f6cc
+cifs_to_var $root
Harald Hoyer 12f6cc
+echo server: $server
Harald Hoyer 12f6cc
+echo path: $path
Harald Hoyer 12f6cc
+echo options: $options
Harald Hoyer 12f6cc
+
Harald Hoyer 12f6cc
+mount.cifs //$server/$path $NEWROOT -o $options && { [ -e /dev/root ] || >/dev/root ; }
Harald Hoyer 12f6cc
+
Harald Hoyer 12f6cc
+# inject new exit_if_exists
Harald Hoyer 12f6cc
+echo 'settle_exit_if_exists="--exit-if-exists=/dev/root"; rm "$job"' > $hookdir/initqueue/cifs.sh
Harald Hoyer 12f6cc
+# force udevsettle to break
Harald Hoyer 12f6cc
+> $hookdir/initqueue/work
Harald Hoyer 12f6cc
diff --git a/modules.d/95cifs/module-setup.sh b/modules.d/95cifs/module-setup.sh
Harald Hoyer 12f6cc
new file mode 100755
Harald Hoyer 12f6cc
index 0000000..2f2e78d
Harald Hoyer 12f6cc
--- /dev/null
Harald Hoyer 12f6cc
+++ b/modules.d/95cifs/module-setup.sh
Harald Hoyer 12f6cc
@@ -0,0 +1,46 @@
Harald Hoyer 12f6cc
+#!/bin/bash
Harald Hoyer 12f6cc
+# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
Harald Hoyer 12f6cc
+# ex: ts=8 sw=4 sts=4 et filetype=sh
Harald Hoyer 12f6cc
+
Harald Hoyer 12f6cc
+check() {
Harald Hoyer 12f6cc
+    # If our prerequisites are not met, fail anyways.
Harald Hoyer 12f6cc
+    type -P mount.cifs >/dev/null || return 1
Harald Hoyer 12f6cc
+
Harald Hoyer 12f6cc
+    [[ $hostonly ]] || [[ $mount_needs ]] && {
Harald Hoyer 12f6cc
+        for fs in ${host_fs_types[@]}; do
Harald Hoyer 12f6cc
+            strstr "$fs" "\|cifs"  && return 0
Harald Hoyer 12f6cc
+        done
Harald Hoyer 12f6cc
+        return 255
Harald Hoyer 12f6cc
+    }
Harald Hoyer 12f6cc
+
Harald Hoyer 12f6cc
+    return 0
Harald Hoyer 12f6cc
+}
Harald Hoyer 12f6cc
+
Harald Hoyer 12f6cc
+depends() {
Harald Hoyer 12f6cc
+    # We depend on network modules being loaded
Harald Hoyer 12f6cc
+    echo network
Harald Hoyer 12f6cc
+}
Harald Hoyer 12f6cc
+
Harald Hoyer 12f6cc
+installkernel() {
Harald Hoyer 12f6cc
+    instmods cifs ipv6
Harald Hoyer 12f6cc
+}
Harald Hoyer 12f6cc
+
Harald Hoyer 12f6cc
+install() {
Harald Hoyer 12f6cc
+    local _i
Harald Hoyer 12f6cc
+    local _nsslibs
Harald Hoyer 12f6cc
+    dracut_install -o mount.cifs
Harald Hoyer 12f6cc
+    dracut_install /etc/services /etc/nsswitch.conf /etc/protocols
Harald Hoyer 12f6cc
+
Harald Hoyer 12f6cc
+    inst_libdir_file 'libcap-ng.so*'
Harald Hoyer 12f6cc
+
Harald Hoyer 12f6cc
+    _nsslibs=$(sed -e '/^#/d' -e 's/^.*://' -e 's/\[NOTFOUND=return\]//' /etc/nsswitch.conf \
Harald Hoyer 12f6cc
+        |  tr -s '[:space:]' '\n' | sort -u | tr -s '[:space:]' '|')
Harald Hoyer 12f6cc
+    _nsslibs=${_nsslibs#|}
Harald Hoyer 12f6cc
+    _nsslibs=${_nsslibs%|}
Harald Hoyer 12f6cc
+
Harald Hoyer 12f6cc
+    inst_libdir_file -n "$_nsslibs" 'libnss*.so*'
Harald Hoyer 12f6cc
+
Harald Hoyer 12f6cc
+    inst_hook cmdline 90 "$moddir/parse-cifsroot.sh"
Harald Hoyer 12f6cc
+    inst "$moddir/cifsroot.sh" "/sbin/cifsroot"
Harald Hoyer 12f6cc
+    inst "$moddir/cifs-lib.sh" "/lib/cifs-lib.sh"
Harald Hoyer 12f6cc
+}
Harald Hoyer 12f6cc
diff --git a/modules.d/95cifs/parse-cifsroot.sh b/modules.d/95cifs/parse-cifsroot.sh
Harald Hoyer 12f6cc
new file mode 100755
Harald Hoyer 12f6cc
index 0000000..d5adb98
Harald Hoyer 12f6cc
--- /dev/null
Harald Hoyer 12f6cc
+++ b/modules.d/95cifs/parse-cifsroot.sh
Harald Hoyer 12f6cc
@@ -0,0 +1,52 @@
Harald Hoyer 12f6cc
+#!/bin/sh
Harald Hoyer 12f6cc
+# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
Harald Hoyer 12f6cc
+# ex: ts=8 sw=4 sts=4 et filetype=sh
Harald Hoyer 12f6cc
+#
Harald Hoyer 12f6cc
+# root=cifs://[user:pass@]<server>/<folder>
Harald Hoyer 12f6cc
+#
Harald Hoyer 12f6cc
+# This syntax can come from DHCP root-path as well.
Harald Hoyer 12f6cc
+#
Harald Hoyer 12f6cc
+# If a username or password are not specified as part of the root, then they
Harald Hoyer 12f6cc
+# will be pulled from cifsuser and cifspass on the kernel command line,
Harald Hoyer 12f6cc
+# respectively.
Harald Hoyer 12f6cc
+#
Harald Hoyer 12f6cc
+
Harald Hoyer 12f6cc
+type getarg >/dev/null 2>&1 || . /lib/dracut-lib.sh
Harald Hoyer 12f6cc
+. /lib/cifs-lib.sh
Harald Hoyer 12f6cc
+
Harald Hoyer 12f6cc
+#Don't continue if root is ok
Harald Hoyer 12f6cc
+[ -n "$rootok" ] && return
Harald Hoyer 12f6cc
+
Harald Hoyer 12f6cc
+# This script is sourced, so root should be set. But let's be paranoid
Harald Hoyer 12f6cc
+[ -z "$root" ] && root=$(getarg root=)
Harald Hoyer 12f6cc
+[ -z "$netroot" ] && netroot=$(getarg netroot=)
Harald Hoyer 12f6cc
+
Harald Hoyer 12f6cc
+# netroot= cmdline argument must be ignored, but must be used if
Harald Hoyer 12f6cc
+# we're inside netroot to parse dhcp root-path
Harald Hoyer 12f6cc
+if [ -n "$netroot" ] ; then
Harald Hoyer 12f6cc
+    if [ "$netroot" = "$(getarg netroot=)" ] ; then
Harald Hoyer 12f6cc
+        warn "Ignoring netroot argument for CIFS"
Harald Hoyer 12f6cc
+        netroot=$root
Harald Hoyer 12f6cc
+    fi
Harald Hoyer 12f6cc
+else
Harald Hoyer 12f6cc
+    netroot=$root;
Harald Hoyer 12f6cc
+fi
Harald Hoyer 12f6cc
+
Harald Hoyer 12f6cc
+# Continue if cifs
Harald Hoyer 12f6cc
+case "${netroot%%:*}" in
Harald Hoyer 12f6cc
+    cifs);;
Harald Hoyer 12f6cc
+    *) unset netroot; return;;
Harald Hoyer 12f6cc
+esac
Harald Hoyer 12f6cc
+
Harald Hoyer 12f6cc
+# Check required arguments
Harald Hoyer 12f6cc
+cifs_to_var $netroot
Harald Hoyer 12f6cc
+
Harald Hoyer 12f6cc
+# If we don't have a server, we need dhcp
Harald Hoyer 12f6cc
+if [ -z "$server" ] ; then
Harald Hoyer 12f6cc
+    DHCPORSERVER="1"
Harald Hoyer 12f6cc
+fi;
Harald Hoyer 12f6cc
+
Harald Hoyer 12f6cc
+# Done, all good!
Harald Hoyer 12f6cc
+rootok=1
Harald Hoyer 12f6cc
+
Harald Hoyer 12f6cc
+echo '[ -e $NEWROOT/proc ]' > $hookdir/initqueue/finished/cifsroot.sh