Blob Blame History Raw
From 79d298779854b6028de75804f415cb6cc5a5fd5f Mon Sep 17 00:00:00 2001
From: Cole Robinson <crobinso@redhat.com>
Date: Wed, 4 Mar 2020 17:56:03 -0500
Subject: [PATCH] rootfs: Don't overwrite /init if it already exists

The prepare_overlay() code path is called when rootfs.sh is invoked
with no passed in distro string. This is used for the dracut case
from the Makefile for example. In that particular case, the starting
root directory is empty.

It's also valid to pass a prepopulated directory to rootfs.sh, which
is essentially a request for the script to just make the necessary
kata changes. Currently though prepare_overlay() makes some changes
that could wipe out pre-arranged /sbin/init setup.

Check first to see if /sbin/init exists in the rootfs dir, and if so,
skip the symlink changes

Fixes: #419

Signed-off-by: Cole Robinson <crobinso@redhat.com>
---
 rootfs-builder/rootfs.sh | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/rootfs-builder/rootfs.sh b/rootfs-builder/rootfs.sh
index 5a5655e..f8714ab 100755
--- a/rootfs-builder/rootfs.sh
+++ b/rootfs-builder/rootfs.sh
@@ -455,10 +455,16 @@ prepare_overlay()
 {
 	pushd "${ROOTFS_DIR}" > /dev/null
 	mkdir -p ./etc ./lib/systemd ./sbin ./var
-	ln -sf  ./usr/lib/systemd/systemd ./init
-	ln -sf  ../../init ./lib/systemd/systemd
-	ln -sf  ../init ./sbin/init
-	# Kata sytemd unit file
+	if [ ! -e ./sbin/init ]; then
+		# This symlink hacking is mostly to make later rootfs
+		# validation work correctly for the dracut case.
+		# We skip this if /init exists in the rootfs, meaning
+		# we were passed a pre-populated rootfs directory
+		ln -sf  ./usr/lib/systemd/systemd ./init
+		ln -sf  ../../init ./lib/systemd/systemd
+		ln -sf  ../init ./sbin/init
+	fi
+	# Kata systemd unit file
 	mkdir -p ./etc/systemd/system/basic.target.wants/
 	ln -sf /usr/lib/systemd/system/kata-containers.target ./etc/systemd/system/basic.target.wants/kata-containers.target
 	popd  > /dev/null