git: 0bf6e572bd44 - stable/13 - rc.d/linux: Attempt to mount only if necessary

From: Edward Tomasz Napierala <trasz_at_FreeBSD.org>
Date: Tue, 22 Feb 2022 13:24:41 UTC
The branch stable/13 has been updated by trasz:

URL: https://cgit.FreeBSD.org/src/commit/?id=0bf6e572bd446c6a6cbbafc8861616080d31cdb1

commit 0bf6e572bd446c6a6cbbafc8861616080d31cdb1
Author:     Mateusz Piotrowski <0mp@FreeBSD.org>
AuthorDate: 2021-10-12 08:40:36 +0000
Commit:     Edward Tomasz Napierala <trasz@FreeBSD.org>
CommitDate: 2022-02-16 23:54:44 +0000

    rc.d/linux: Attempt to mount only if necessary
    
    Currently, if the linux service is run twice, mount(8) fails with:
    
        mount: linprocfs: Device busy
        mount: linsysfs: Device busy
        mount: devfs: Device busy
        mount: fdescfs: Device busy
        mount: tmpfs: Device busy
    
    It is a bit more user-friendly if before running mount(8) the service
    checks if there are any file systems left to be mounted. This patch
    implements this behavior.
    
    Also, while here, create mount points directories (as suggested by
    otis).
    
    Reviewed by:    trasz
    Approved by:    trasz (src)
    Differential Revision:  https://reviews.freebsd.org/D32463
    
    (cherry picked from commit 5690261858b6bd8f7d09eda2ae74f3def2d69a01)
---
 libexec/rc/rc.d/linux | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/libexec/rc/rc.d/linux b/libexec/rc/rc.d/linux
index 2cf32a1ecec3..adb9f4c64bad 100755
--- a/libexec/rc/rc.d/linux
+++ b/libexec/rc/rc.d/linux
@@ -15,6 +15,17 @@ rcvar="linux_enable"
 start_cmd="${name}_start"
 stop_cmd=":"
 
+linux_mount() {
+	local _fs _mount_point
+	_fs="$1"
+	_mount_point="$2"
+	shift 2
+	if ! mount | grep -q "^$_fs on $_mount_point ("; then
+		mkdir -p "$_mount_point"
+		mount "$@" -t "$_fs" "$_fs" "$_mount_point"
+	fi
+}
+
 linux_start()
 {
 	local _emul_path _tmpdir
@@ -61,12 +72,12 @@ linux_start()
 		sysctl kern.elf32.fallback_brand=3 > /dev/null
 	fi
 
-	if checkyesno linux_mounts_enable; then 
-		mount -o nocover -t linprocfs linprocfs "${_emul_path}/proc"
-		mount -o nocover -t linsysfs linsysfs "${_emul_path}/sys"
-		mount -o nocover -t devfs devfs "${_emul_path}/dev"
-		mount -o nocover,linrdlnk -t fdescfs fdescfs "${_emul_path}/dev/fd"
-		mount -o nocover,mode=1777 -t tmpfs tmpfs "${_emul_path}/dev/shm"
+	if checkyesno linux_mounts_enable; then
+		linux_mount linprocfs "${_emul_path}/proc" -o nocover
+		linux_mount linsysfs "${_emul_path}/sys" -o nocover
+		linux_mount devfs "${_emul_path}/dev" -o nocover
+		linux_mount fdescfs "${_emul_path}/dev/fd" -o nocover,linrdlnk
+		linux_mount tmpfs "${_emul_path}/dev/shm" -o nocover,mode=1777
 	fi
 }