Harald Hoyer 8e216f
From ae8b82e395c9530a66288f7a9e939242137d3f56 Mon Sep 17 00:00:00 2001
Harald Hoyer 8e216f
From: Harald Hoyer <harald@redhat.com>
Harald Hoyer 8e216f
Date: Thu, 26 Jan 2012 11:33:51 +0100
Harald Hoyer 8e216f
Subject: [PATCH] add usrmove module
Harald Hoyer 8e216f
Harald Hoyer 8e216f
---
Harald Hoyer 8e216f
 dracut.spec                            |    1 +
Harald Hoyer 8e216f
 modules.d/30usrmove/do-usrmove.sh      |    7 ++
Harald Hoyer 8e216f
 modules.d/30usrmove/module-setup.sh    |   19 ++++
Harald Hoyer 8e216f
 modules.d/30usrmove/usrmove-convert.sh |  153 ++++++++++++++++++++++++++++++++
Harald Hoyer 8e216f
 4 files changed, 180 insertions(+), 0 deletions(-)
Harald Hoyer 8e216f
 create mode 100755 modules.d/30usrmove/do-usrmove.sh
Harald Hoyer 8e216f
 create mode 100755 modules.d/30usrmove/module-setup.sh
Harald Hoyer 8e216f
 create mode 100755 modules.d/30usrmove/usrmove-convert.sh
Harald Hoyer 8e216f
Harald Hoyer 8e216f
diff --git a/dracut.spec b/dracut.spec
Harald Hoyer 8e216f
index 16c8aa3..06533e3 100644
Harald Hoyer 8e216f
--- a/dracut.spec
Harald Hoyer 8e216f
+++ b/dracut.spec
Harald Hoyer 8e216f
@@ -226,6 +226,7 @@ rm -rf $RPM_BUILD_ROOT
Harald Hoyer 8e216f
 %{dracutlibdir}/modules.d/05busybox
Harald Hoyer 8e216f
 %{dracutlibdir}/modules.d/10i18n
Harald Hoyer 8e216f
 %{dracutlibdir}/modules.d/10rpmversion
Harald Hoyer 8e216f
+%{dracutlibdir}/modules.d/30usrmove
Harald Hoyer 8e216f
 %{dracutlibdir}/modules.d/50plymouth
Harald Hoyer 8e216f
 %{dracutlibdir}/modules.d/90btrfs
Harald Hoyer 8e216f
 %{dracutlibdir}/modules.d/90crypt
Harald Hoyer 8e216f
diff --git a/modules.d/30usrmove/do-usrmove.sh b/modules.d/30usrmove/do-usrmove.sh
Harald Hoyer 8e216f
new file mode 100755
Harald Hoyer 8e216f
index 0000000..8bdf5ab
Harald Hoyer 8e216f
--- /dev/null
Harald Hoyer 8e216f
+++ b/modules.d/30usrmove/do-usrmove.sh
Harald Hoyer 8e216f
@@ -0,0 +1,7 @@
Harald Hoyer 8e216f
+#!/bin/bash
Harald Hoyer 8e216f
+# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
Harald Hoyer 8e216f
+# ex: ts=8 sw=4 sts=4 et filetype=sh
Harald Hoyer 8e216f
+
Harald Hoyer 8e216f
+if getargbool 0 rd.usrmove; then
Harald Hoyer 8e216f
+    usrmove-convert "$NEWROOT" 2>&1 | vinfo
Harald Hoyer 8e216f
+fi
Harald Hoyer 8e216f
diff --git a/modules.d/30usrmove/module-setup.sh b/modules.d/30usrmove/module-setup.sh
Harald Hoyer 8e216f
new file mode 100755
Harald Hoyer 8e216f
index 0000000..05b2366
Harald Hoyer 8e216f
--- /dev/null
Harald Hoyer 8e216f
+++ b/modules.d/30usrmove/module-setup.sh
Harald Hoyer 8e216f
@@ -0,0 +1,19 @@
Harald Hoyer 8e216f
+#!/bin/bash
Harald Hoyer 8e216f
+# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
Harald Hoyer 8e216f
+# ex: ts=8 sw=4 sts=4 et filetype=sh
Harald Hoyer 8e216f
+
Harald Hoyer 8e216f
+check() {
Harald Hoyer 8e216f
+    [[ $mount_needs ]] && return 1
Harald Hoyer 8e216f
+    return 255
Harald Hoyer 8e216f
+}
Harald Hoyer 8e216f
+
Harald Hoyer 8e216f
+depends() {
Harald Hoyer 8e216f
+    return 0
Harald Hoyer 8e216f
+}
Harald Hoyer 8e216f
+
Harald Hoyer 8e216f
+install() {
Harald Hoyer 8e216f
+    dracut_install bash
Harald Hoyer 8e216f
+    inst_hook pre-pivot 99 "$moddir/do-usrmove.sh"
Harald Hoyer 8e216f
+    inst "$moddir/usrmove-convert.sh" /usr/bin/usrmove-convert
Harald Hoyer 8e216f
+}
Harald Hoyer 8e216f
+
Harald Hoyer 8e216f
diff --git a/modules.d/30usrmove/usrmove-convert.sh b/modules.d/30usrmove/usrmove-convert.sh
Harald Hoyer 8e216f
new file mode 100755
Harald Hoyer 8e216f
index 0000000..3295074
Harald Hoyer 8e216f
--- /dev/null
Harald Hoyer 8e216f
+++ b/modules.d/30usrmove/usrmove-convert.sh
Harald Hoyer 8e216f
@@ -0,0 +1,153 @@
Harald Hoyer 8e216f
+#!/bin/bash
Harald Hoyer 8e216f
+# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
Harald Hoyer 8e216f
+# ex: ts=8 sw=4 sts=4 et filetype=sh
Harald Hoyer 8e216f
+
Harald Hoyer 8e216f
+set -x
Harald Hoyer 8e216f
+
Harald Hoyer 8e216f
+ROOT="$1"
Harald Hoyer 8e216f
+
Harald Hoyer 8e216f
+if [[ ! -d "$ROOT" ]]; then
Harald Hoyer 8e216f
+    echo "Usage: $0 <rootdir>"
Harald Hoyer 8e216f
+    exit 1
Harald Hoyer 8e216f
+fi
Harald Hoyer 8e216f
+
Harald Hoyer 8e216f
+if [[ "$ROOT" -ef / ]]; then
Harald Hoyer 8e216f
+    echo "Can't convert the running system."
Harald Hoyer 8e216f
+    echo "Please boot with 'usrmove' on the kernel command line,"
Harald Hoyer 8e216f
+    echo "to update with the help of the initramfs,"
Harald Hoyer 8e216f
+    echo "or run this script from a rescue system."
Harald Hoyer 8e216f
+    exit 1
Harald Hoyer 8e216f
+fi
Harald Hoyer 8e216f
+
Harald Hoyer 8e216f
+while [[ "$ROOT" != "${ROOT%/}" ]]; do
Harald Hoyer 8e216f
+    ROOT=${ROOT%/}
Harald Hoyer 8e216f
+done
Harald Hoyer 8e216f
+
Harald Hoyer 8e216f
+needconvert() {
Harald Hoyer 8e216f
+    for dir in "$ROOT/bin" "$ROOT/sbin" "$ROOT/lib" "$ROOT/lib64"; do
Harald Hoyer 8e216f
+        if [[ -e "$dir" ]]; then
Harald Hoyer 8e216f
+            [[ -L "$dir" ]] || return 0
Harald Hoyer 8e216f
+        fi
Harald Hoyer 8e216f
+    done
Harald Hoyer 8e216f
+    return 1
Harald Hoyer 8e216f
+}
Harald Hoyer 8e216f
+
Harald Hoyer 8e216f
+if ! needconvert; then
Harald Hoyer 8e216f
+    echo "Your system is already converted."
Harald Hoyer 8e216f
+    exit 0
Harald Hoyer 8e216f
+fi
Harald Hoyer 8e216f
+
Harald Hoyer 8e216f
+testfile="$ROOT/.usrmovecheck$$"
Harald Hoyer 8e216f
+rm -f "$testfile"
Harald Hoyer 8e216f
+> "$testfile"
Harald Hoyer 8e216f
+if [[ ! -e "$testfile" ]]; then
Harald Hoyer 8e216f
+    echo "Cannot write to $ROOT/"
Harald Hoyer 8e216f
+    exit 1
Harald Hoyer 8e216f
+fi
Harald Hoyer 8e216f
+rm -f "$testfile"
Harald Hoyer 8e216f
+
Harald Hoyer 8e216f
+testfile="$ROOT/usr/.usrmovecheck$$"
Harald Hoyer 8e216f
+rm -f "$testfile"
Harald Hoyer 8e216f
+> "$testfile"
Harald Hoyer 8e216f
+if [[ ! -e "$testfile" ]]; then
Harald Hoyer 8e216f
+    echo "Cannot write to $ROOT/usr/"
Harald Hoyer 8e216f
+    exit 1
Harald Hoyer 8e216f
+fi
Harald Hoyer 8e216f
+rm -f "$testfile"
Harald Hoyer 8e216f
+
Harald Hoyer 8e216f
+ismounted() {
Harald Hoyer 8e216f
+    while read a m a; do
Harald Hoyer 8e216f
+        [[ "$m" = "$1" ]] && return 0
Harald Hoyer 8e216f
+    done < /proc/mounts
Harald Hoyer 8e216f
+    return 1
Harald Hoyer 8e216f
+}
Harald Hoyer 8e216f
+
Harald Hoyer 8e216f
+# clean up after ourselves no matter how we die.
Harald Hoyer 8e216f
+cleanup() {
Harald Hoyer 8e216f
+    echo "Something failed. Move back to the original state"
Harald Hoyer 8e216f
+    for dir in "$ROOT/bin" "$ROOT/sbin" "$ROOT/lib" "$ROOT/lib64" \
Harald Hoyer 8e216f
+	"$ROOT/usr/bin" "$ROOT/usr/sbin" "$ROOT/usr/lib" \
Harald Hoyer 8e216f
+        "$ROOT/usr/lib64"; do
Harald Hoyer 8e216f
+        [[ -d "${dir}.usrmove-new" ]] && rm -fr "${dir}.usrmove-new"
Harald Hoyer 8e216f
+        if [[ -d "${dir}.usrmove-old" ]]; then
Harald Hoyer 8e216f
+            mv "$dir" "${dir}.del~"
Harald Hoyer 8e216f
+            mv "${dir}.usrmove-old" "$dir"
Harald Hoyer 8e216f
+            rm -fr "${dir}.del~"
Harald Hoyer 8e216f
+        fi
Harald Hoyer 8e216f
+    done
Harald Hoyer 8e216f
+}
Harald Hoyer 8e216f
+
Harald Hoyer 8e216f
+trap 'ret=$?; [[ $ret -ne 0 ]] && cleanup;exit $ret;' EXIT
Harald Hoyer 8e216f
+trap 'exit 1;' SIGINT
Harald Hoyer 8e216f
+
Harald Hoyer 8e216f
+ismounted "$ROOT/usr" || CP_HARDLINK="-l"
Harald Hoyer 8e216f
+
Harald Hoyer 8e216f
+set -e
Harald Hoyer 8e216f
+
Harald Hoyer 8e216f
+# merge / and /usr in new dir in /usr
Harald Hoyer 8e216f
+for dir in bin sbin lib lib64; do
Harald Hoyer 8e216f
+    rm -rf "$ROOT/usr/${dir}.usrmove-new"
Harald Hoyer 8e216f
+    [[ -L "$ROOT/$dir" ]] && continue
Harald Hoyer 8e216f
+    [[ -d "$ROOT/$dir" ]] || continue
Harald Hoyer 8e216f
+    echo "Make a copy of \`$ROOT/usr/$dir'."
Harald Hoyer 8e216f
+    [[ -d "$ROOT/usr/$dir" ]] \
Harald Hoyer 8e216f
+        && cp -ax $CP_HARDLINK "$ROOT/usr/$dir" "$ROOT/usr/${dir}.usrmove-new"
Harald Hoyer 8e216f
+    echo "Merge the copy with \`$ROOT/$dir'."
Harald Hoyer 8e216f
+    [[ -d "$ROOT/usr/${dir}.usrmove-new" ]] \
Harald Hoyer 8e216f
+        || mkdir -p "$ROOT/usr/${dir}.usrmove-new"
Harald Hoyer 8e216f
+    cp -axT $CP_HARDLINK --backup --suffix=.usrmove~ "$ROOT/$dir" "$ROOT/usr/${dir}.usrmove-new"
Harald Hoyer 8e216f
+    echo "Clean up duplicates in \`$ROOT/usr/$dir'."
Harald Hoyer 8e216f
+    # delete all symlinks that have been backed up
Harald Hoyer 8e216f
+    find "$ROOT/usr/${dir}.usrmove-new" -type l -name '*.usrmove~' -delete || :
Harald Hoyer 8e216f
+    # replace symlink with backed up binary
Harald Hoyer 8e216f
+    find "$ROOT/usr/${dir}.usrmove-new" \
Harald Hoyer 8e216f
+        -name '*.usrmove~' \
Harald Hoyer 8e216f
+        -type f \
Harald Hoyer 8e216f
+        -exec bash -c 'p="{}";o=${p%%%%.usrmove~};
Harald Hoyer 8e216f
+                       [[ -L "$o" ]] && mv -f "$p" "$o"' ';' || :
Harald Hoyer 8e216f
+done
Harald Hoyer 8e216f
+# switch over merged dirs in /usr
Harald Hoyer 8e216f
+for dir in bin sbin lib lib64; do
Harald Hoyer 8e216f
+    [[ -d "$ROOT/usr/${dir}.usrmove-new" ]] || continue
Harald Hoyer 8e216f
+    echo "Switch to new \`$ROOT/usr/$dir'."
Harald Hoyer 8e216f
+    rm -fr "$ROOT/usr/${dir}.usrmove-old"
Harald Hoyer 8e216f
+    mv "$ROOT/usr/$dir" "$ROOT/usr/${dir}.usrmove-old"
Harald Hoyer 8e216f
+    mv "$ROOT/usr/${dir}.usrmove-new" "$ROOT/usr/$dir"
Harald Hoyer 8e216f
+done
Harald Hoyer 8e216f
+
Harald Hoyer 8e216f
+# replace dirs in / with links to /usr
Harald Hoyer 8e216f
+for dir in bin sbin lib lib64; do
Harald Hoyer 8e216f
+    [[ -L "$ROOT/$dir" ]] && continue
Harald Hoyer 8e216f
+    [[ -d "$ROOT/$dir" ]] || continue
Harald Hoyer 8e216f
+    echo "Create \`$ROOT/$dir' symlink."
Harald Hoyer 8e216f
+    rm -rf "$ROOT/${dir}.usrmove-old" || :
Harald Hoyer 8e216f
+    mv "$ROOT/$dir" "$ROOT/${dir}.usrmove-old"
Harald Hoyer 8e216f
+    ln -sfn usr/$dir "$ROOT/$dir"
Harald Hoyer 8e216f
+done
Harald Hoyer 8e216f
+
Harald Hoyer 8e216f
+echo "Clean up backup files."
Harald Hoyer 8e216f
+# everything seems to work; cleanup
Harald Hoyer 8e216f
+for dir in bin sbin lib lib64; do
Harald Hoyer 8e216f
+     # if we get killed in the middle of "rm -rf", ensure not to leave
Harald Hoyer 8e216f
+     # an incomplete directory, which is moved back by cleanup()
Harald Hoyer 8e216f
+    [[ -d "$ROOT/usr/${dir}.usrmove-old" ]] \
Harald Hoyer 8e216f
+        && mv "$ROOT/usr/${dir}.usrmove-old" "$ROOT/usr/${dir}.usrmove-old~"
Harald Hoyer 8e216f
+    [[ -d "$ROOT/${dir}.usrmove-old" ]] \
Harald Hoyer 8e216f
+        && mv "$ROOT/${dir}.usrmove-old" "$ROOT/${dir}.usrmove-old~"
Harald Hoyer 8e216f
+done
Harald Hoyer 8e216f
+
Harald Hoyer 8e216f
+for dir in bin sbin lib lib64; do
Harald Hoyer 8e216f
+    [[ -d "$ROOT/usr/${dir}.usrmove-old~" ]] \
Harald Hoyer 8e216f
+        && rm -rf "$ROOT/usr/${dir}.usrmove-old~" || :
Harald Hoyer 8e216f
+    [[ -d "$ROOT/${dir}.usrmove-old~" ]] \
Harald Hoyer 8e216f
+        && rm -rf "$ROOT/${dir}.usrmove-old~" || :
Harald Hoyer 8e216f
+done
Harald Hoyer 8e216f
+
Harald Hoyer 8e216f
+set +e
Harald Hoyer 8e216f
+
Harald Hoyer 8e216f
+echo "Run ldconfig."
Harald Hoyer 8e216f
+ldconfig -r "$ROOT"
Harald Hoyer 8e216f
+echo "Set autorelabel flag."
Harald Hoyer 8e216f
+> "$ROOT/.autorelabel"
Harald Hoyer 8e216f
+echo "Done."
Harald Hoyer 8e216f
+exit 0