From 4e4a88a387961b73a90ca28a24a26bfe4bcf93a8 Mon Sep 17 00:00:00 2001
From: Lucas Alvares Gomes <lucasagomes@gmail.com>
Date: Wed, 5 Jun 2013 14:17:59 +0100
Subject: [PATCH] Add dracut-network element.

This element will install the network modules for dracut and regenerate
the initramfs. The network support would allow us to configure the network
of the image via kernel cmdline instead of injecting files into the image.

Change-Id: I829405127e77aec80b99ac8dd2032cb0c6d81063
---
 elements/dracut-network/README.md             |  1 +
 .../dracut-network/dracut-write-ifcfg.patch   | 20 +++++++++++++++++++
 .../finalise.d/98-regenerate-initramfs        |  6 ++++++
 .../install.d/10-install-dracut-network       |  5 +++++
 .../dracut-network/install.d/11-patch-dracut  |  6 ++++++
 elements/fedora/element-deps                  |  1 +
 6 files changed, 39 insertions(+)
 create mode 100644 elements/dracut-network/README.md
 create mode 100644 elements/dracut-network/dracut-write-ifcfg.patch
 create mode 100755 elements/dracut-network/finalise.d/98-regenerate-initramfs
 create mode 100755 elements/dracut-network/install.d/10-install-dracut-network
 create mode 100755 elements/dracut-network/install.d/11-patch-dracut

diff --git a/elements/dracut-network/README.md b/elements/dracut-network/README.md
new file mode 100644
index 000000000..1618f4e77
--- /dev/null
+++ b/elements/dracut-network/README.md
@@ -0,0 +1 @@
+Extends dracut and build an initramfs with network support.
diff --git a/elements/dracut-network/dracut-write-ifcfg.patch b/elements/dracut-network/dracut-write-ifcfg.patch
new file mode 100644
index 000000000..1baf6040b
--- /dev/null
+++ b/elements/dracut-network/dracut-write-ifcfg.patch
@@ -0,0 +1,20 @@
+--- usr/lib/dracut/hooks/pre-pivot/85-write-ifcfg.sh	2013-05-30 14:01:37.492519066 +0000
++++ /85-write-ifcfg.sh	2013-05-30 14:03:09.832519065 +0000
+@@ -4,6 +4,9 @@
+
+ # NFS root might have reached here before /tmp/net.ifaces was written
+ udevadm settle --timeout=30
++
++command -v ip_to_var >/dev/null || . /lib/net-lib.sh
++
+ # Don't write anything if we don't know our bootdev
+ [ -f /tmp/net.ifaces ] || return 1
+
+@@ -98,6 +101,7 @@
+     fi
+     cat /sys/class/net/$netif/address > /tmp/net.$netif.hwaddr
+     {
++        ip_to_var $ip
+         echo "# Generated by dracut initrd"
+         echo "DEVICE=$netif"
+         echo "ONBOOT=yes"
diff --git a/elements/dracut-network/finalise.d/98-regenerate-initramfs b/elements/dracut-network/finalise.d/98-regenerate-initramfs
new file mode 100755
index 000000000..db38137f5
--- /dev/null
+++ b/elements/dracut-network/finalise.d/98-regenerate-initramfs
@@ -0,0 +1,6 @@
+#!/bin/bash
+
+LAST_VERSION=$(basename `ls -1 /boot/vmlinuz*  | tail -1 | sed 's/vmlinuz-//g'`)
+INITRAMFS=`ls /boot/initramfs-$LAST_VERSION.img`
+
+dracut --force --add "network" $INITRAMFS $LAST_VERSION
diff --git a/elements/dracut-network/install.d/10-install-dracut-network b/elements/dracut-network/install.d/10-install-dracut-network
new file mode 100755
index 000000000..97c65cdcf
--- /dev/null
+++ b/elements/dracut-network/install.d/10-install-dracut-network
@@ -0,0 +1,5 @@
+#!/bin/bash
+
+set -e
+
+install-packages dracut-network
diff --git a/elements/dracut-network/install.d/11-patch-dracut b/elements/dracut-network/install.d/11-patch-dracut
new file mode 100755
index 000000000..5674bfa21
--- /dev/null
+++ b/elements/dracut-network/install.d/11-patch-dracut
@@ -0,0 +1,6 @@
+#!/bin/sh
+
+# If the patch doesn't apply just do nothing. This patch fix a small
+# issue in the script that writes the network configuration files from
+# the cmdline to the disk on FC18.
+patch /usr/lib/dracut/modules.d/45ifcfg/write-ifcfg.sh < $(dirname $0)/../dracut-write-ifcfg.patch || true
diff --git a/elements/fedora/element-deps b/elements/fedora/element-deps
index 64b9ac34f..40937011f 100644
--- a/elements/fedora/element-deps
+++ b/elements/fedora/element-deps
@@ -1 +1,2 @@
 dib-run-parts
+dracut-network