9f65cc
From 3f7410e7f3c08866c84e01ccfb423d7cff4eb5a3 Mon Sep 17 00:00:00 2001
9f65cc
From: Kairui Song <kasong@redhat.com>
9f65cc
Date: Wed, 7 Apr 2021 02:11:41 +0800
9f65cc
Subject: [PATCH] fix(squash): don't mount the mount points if already mounted
9f65cc
9f65cc
It is possible that user setup some early mount in the rootfs even
9f65cc
earlier, so just be more robust and cover that case too.
9f65cc
9f65cc
Signed-off-by: Kairui Song <kasong@redhat.com>
9f65cc
(cherry picked from commit 636d6df3134dde1dac72241937724bc59deb9303)
9f65cc
9f65cc
Resolves: #1959336
9f65cc
---
9f65cc
 modules.d/99squash/init-squash.sh  | 17 +++++++++++------
9f65cc
 modules.d/99squash/module-setup.sh |  4 ++--
9f65cc
 2 files changed, 13 insertions(+), 8 deletions(-)
9f65cc
9f65cc
diff --git a/modules.d/99squash/init-squash.sh b/modules.d/99squash/init-squash.sh
9f65cc
index 3de6f819..59769f62 100755
9f65cc
--- a/modules.d/99squash/init-squash.sh
9f65cc
+++ b/modules.d/99squash/init-squash.sh
9f65cc
@@ -1,12 +1,17 @@
9f65cc
 #!/bin/sh
9f65cc
 PATH=/bin:/sbin
9f65cc
 
9f65cc
-# Basic mounts for mounting a squash image
9f65cc
-mkdir /proc /sys /dev /run
9f65cc
-mount -t proc -o nosuid,noexec,nodev proc /proc
9f65cc
-mount -t sysfs -o nosuid,noexec,nodev sysfs /sys
9f65cc
-mount -t devtmpfs -o mode=755,noexec,nosuid,strictatime devtmpfs /dev
9f65cc
-mount -t tmpfs -o mode=755,nodev,nosuid,strictatime tmpfs /run
9f65cc
+[ -e /proc/self/mounts ] \
9f65cc
+    || (mkdir -p /proc && mount -t proc -o nosuid,noexec,nodev proc /proc)
9f65cc
+
9f65cc
+grep -q '^sysfs /sys sysfs' /proc/self/mounts \
9f65cc
+    || (mkdir -p /sys && mount -t sysfs -o nosuid,noexec,nodev sysfs /sys)
9f65cc
+
9f65cc
+grep -q '^devtmpfs /dev devtmpfs' /proc/self/mounts \
9f65cc
+    || (mkdir -p /dev && mount -t devtmpfs -o mode=755,noexec,nosuid,strictatime devtmpfs /dev)
9f65cc
+
9f65cc
+grep -q '^tmpfs /run tmpfs' /proc/self/mounts \
9f65cc
+    || (mkdir -p /run && mount -t tmpfs -o mode=755,noexec,nosuid,strictatime tmpfs /run)
9f65cc
 
9f65cc
 # Load required modules
9f65cc
 modprobe loop
9f65cc
diff --git a/modules.d/99squash/module-setup.sh b/modules.d/99squash/module-setup.sh
9f65cc
index 72cc83ad..81a5b3f7 100644
9f65cc
--- a/modules.d/99squash/module-setup.sh
9f65cc
+++ b/modules.d/99squash/module-setup.sh
9f65cc
@@ -42,11 +42,11 @@ installpost() {
9f65cc
     # Install required modules and binaries for the squash image init script.
9f65cc
     if [[ $_busybox ]]; then
9f65cc
         inst "$_busybox" /usr/bin/busybox
9f65cc
-        for _i in sh echo mount modprobe mkdir switch_root; do
9f65cc
+        for _i in sh echo mount modprobe mkdir switch_root grep; do
9f65cc
             ln_r /usr/bin/busybox /usr/bin/$_i
9f65cc
         done
9f65cc
     else
9f65cc
-        DRACUT_RESOLVE_DEPS=1 inst_multiple sh mount modprobe mkdir switch_root
9f65cc
+        DRACUT_RESOLVE_DEPS=1 inst_multiple sh mount modprobe mkdir switch_root grep
9f65cc
     fi
9f65cc
 
9f65cc
     hostonly="" instmods "loop" "squashfs" "overlay"
9f65cc