This adds functionality to pivot to back to the initramfs on shutdown,
thereby allowing the system to unmount the real root device. This will
be necessary for anyone with /usr as a separate partition.
---
 Makefile         |    2 +-
 hooks/shutdown   |    7 +++++++
 install/shutdown |   17 +++++++++++++++++
 shutdown         |   29 +++++++++++++++++++++++++++++
 4 files changed, 54 insertions(+), 1 deletions(-)
 create mode 100644 hooks/shutdown
 create mode 100644 install/shutdown
 create mode 100755 shutdown

diff --git a/Makefile b/Makefile
index 3e7966d..e1fe5aa 100644
--- a/Makefile
+++ b/Makefile
@@ -34,7 +34,7 @@ install: all
        chmod 755 ${DESTDIR}/usr/bin/lsinitcpio ${DESTDIR}/usr/bin/mkinitcpio
 
        install -m644 mkinitcpio.conf ${DESTDIR}/etc/mkinitcpio.conf
-       install -m755 -t ${DESTDIR}/lib/initcpio init
+       install -m755 -t ${DESTDIR}/lib/initcpio init shutdown
        install -m644 -t ${DESTDIR}/lib/initcpio init_functions functions
        install -m644 01-memdisk.rules 
${DESTDIR}/lib/initcpio/udev/01-memdisk.rules
 
diff --git a/hooks/shutdown b/hooks/shutdown
new file mode 100644
index 0000000..7c5d9e8
--- /dev/null
+++ b/hooks/shutdown
@@ -0,0 +1,7 @@
+#!/usr/bin/ash
+
+run_hook() {
+    cp -ax / /run/initramfs
+}
+
+# vim: set ft=sh ts=4 sw=4 et:
diff --git a/install/shutdown b/install/shutdown
new file mode 100644
index 0000000..2c533b6
--- /dev/null
+++ b/install/shutdown
@@ -0,0 +1,17 @@
+#!/bin/bash
+
+build() {
+    BINARIES='cp findmnt'
+    SCRIPT='shutdown'
+
+    add_file "/lib/initcpio/shutdown" "/shutdown"
+}
+
+help() {
+    cat <<HELPEOF
+This hook copies the contents of the initramfs into /run/initramfs for reuse
+on shutdown. This is needed when you have /usr mounted on a separate partition.
+HELPEOF
+}
+
+# vim: set ft=sh ts=4 sw=4 et:
diff --git a/shutdown b/shutdown
new file mode 100755
index 0000000..100f505
--- /dev/null
+++ b/shutdown
@@ -0,0 +1,29 @@
+#!/usr/bin/ash
+
+findmnt -Rruno TARGET /oldroot | awk '
+BEGIN { i = 0 }
+! /^\/(proc|dev|sys)/ {
+  i++
+  mounts[i] = $0
+}
+END {
+  for (j = i; j > 0; j--) {
+    print mounts[j]
+  }
+}
+' | while read -r mount; do
+  umount -l "$mount"
+done
+
+case $1 in
+  reboot)
+    type kexec >/dev/null && kexec -e
+    reboot -f
+    ;;
+  poweroff|shutdown|halt)
+    "$1" -f
+    ;;
+  *)
+    poweroff -f
+    ;;
+esac
-- 
1.7.8.1

Reply via email to