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