From 319ac79c47f191e8c0aa2db4ea0eb248b8011bf3 Mon Sep 17 00:00:00 2001
From: David P <megver83@parabola.nu>
Date: Fri, 2 Jul 2021 22:51:20 -0400
Subject: [PATCH] Create mount points if mountpoint exit code is different to
 zero

This is better than checking if the exit code equals to 1, as mountpoint will
exit with code 32 if the directory exists but it's not a mount point.

Plus, in the manpage says:
EXIT STATUS
       Zero if the directory or file is a mountpoint, non-zero if not.

Signed-off-by: David P <megver83@parabola.nu>
---
 chroot-nspawn | 50 +++++++++++++++++++++++++-------------------------
 1 file changed, 25 insertions(+), 25 deletions(-)

diff --git a/chroot-nspawn b/chroot-nspawn
index 2e1fd8b..d4bc896 100755
--- a/chroot-nspawn
+++ b/chroot-nspawn
@@ -1126,7 +1126,7 @@ unset _SET_PRIVATE_NETWORK
 
 # Mount a fake "/" file system
 mountpoint		-q		$_SET_DIRECTORY
-[ $? = 1 ] && mount	-B		$_SET_DIRECTORY	$_SET_DIRECTORY
+[ $? != 0 ] && mount	-B		$_SET_DIRECTORY	$_SET_DIRECTORY
 mount			--make-slave	$_SET_DIRECTORY
 
 # Bind directories
@@ -1156,24 +1156,24 @@ unshare -Cfimpu $_UNSHARE_NETWORK --mount-proc --setgroups allow -- chroot $_SET
 
 	# Mount "/dev", "/proc", "/run", "/sys" and "/tmp" file systems
 	mountpoint	-q /dev
-	[ $? = 1 ] && mount	-t tmpfs	tmpfs	\
+	[ $? != 0 ] && mount	-t tmpfs	tmpfs	\
 		-o rw,nosuid,mode=755	\
 		/dev
 	mountpoint	-q /proc
-	[ $? = 1 ] && mount	-t proc		proc	\
+	[ $? != 0 ] && mount	-t proc		proc	\
 		-o rw,nosuid,nodev,noexec,relatime	\
 		/proc
 	mountpoint	-q /run
-	[ $? = 1 ] && mount	-t tmpfs	tmpfs	\
+	[ $? != 0 ] && mount	-t tmpfs	tmpfs	\
 		-o rw,nosuid,nodev,mode=755	\
 		/run
 	mountpoint	-q /sys
-	[ $? = 1 ] && mount	-t sysfs	sysfs	\
+	[ $? != 0 ] && mount	-t sysfs	sysfs	\
 		-o ro,nosuid,nodev,noexec,relatime	\
 		/sys
 
 	mountpoint	-q /tmp
-	[ $? = 1 ] && mount	-t tmpfs	tmpfs	\
+	[ $? != 0 ] && mount	-t tmpfs	tmpfs	\
 		-o rw	\
 		/tmp
 
@@ -1225,43 +1225,43 @@ unshare -Cfimpu $_UNSHARE_NETWORK --mount-proc --setgroups allow -- chroot $_SET
 
 	# Mount needed file systems
 #	mountpoint	-q /dev/console
-#	[ $? = 1 ] && mount	-t devpts	devpts	\
+#	[ $? != 0 ] && mount	-t devpts	devpts	\
 #		-o rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000	\
 #		/dev/console
 	mountpoint	-q /dev/pts
-	[ $? = 1 ] && mount	-t devpts	devpts	\
+	[ $? != 0 ] && mount	-t devpts	devpts	\
 		-o rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=666	\
 		/dev/pts
 	mountpoint	-q /dev/shm
-	[ $? = 1 ] && mount	-t tmpfs	tmpfs	\
+	[ $? != 0 ] && mount	-t tmpfs	tmpfs	\
 		-o rw,nosuid,nodev	\
 		/dev/shm
 #	mountpoint	-q /proc/kmsg
-#	[ $? = 1 ] && mount	-t tmpfs	tmpfs	\
+#	[ $? != 0 ] && mount	-t tmpfs	tmpfs	\
 #		-o rw,nosuid,nodev,mode=755	\
 #		/proc/kmsg
 #	mountpoint	-q /proc/sys
-#	[ $? = 1 ] && mount	-t proc		proc	\
+#	[ $? != 0 ] && mount	-t proc		proc	\
 #		-o ro,nosuid,nodev,noexec,relatime	\
 #		/proc/sys
 #	mountpoint	-q /proc/sys/kernel/random/boot_id
-#	[ $? = 1 ] && mount	-t tmpfs	tmpfs	\
+#	[ $? != 0 ] && mount	-t tmpfs	tmpfs	\
 #		-o ro,nosuid,nodev,mode=755	\
 #		/proc/sys/kernel/random/boot_id
 #	mountpoint	-q /proc/sys/kernel/random/boot_id
-#	[ $? = 1 ] && mount	-t tmpfs	tmpfs	\
+#	[ $? != 0 ] && mount	-t tmpfs	tmpfs	\
 #		-o rw,nosuid,nodev,mode=755	\
 #		/proc/sys/kernel/random/boot_id
 #	mountpoint	-q /proc/sysrq-trigger
-#	[ $? = 1 ] && mount	-t proc		proc	\
+#	[ $? != 0 ] && mount	-t proc		proc	\
 #		-o ro,nosuid,nodev,noexec,relatime	\
 #		/proc/sysrq-trigger
 	mountpoint	-q /run/systemd/nspawn/incoming
-	[ $? = 1 ] && mount	-t tmpfs	tmpfs	\
+	[ $? != 0 ] && mount	-t tmpfs	tmpfs	\
 		-o ro,relatime,mode=755	\
 		/run/systemd/nspawn/incoming
 	mountpoint	-q /sys/fs/cgroup
-	[ $? = 1 ] && mount	-t tmpfs	tmpfs	\
+	[ $? != 0 ] && mount	-t tmpfs	tmpfs	\
 		-o rw,nosuid,nodev,noexec,mode=755	\
 		/sys/fs/cgroup
 
@@ -1299,39 +1299,39 @@ unshare -Cfimpu $_UNSHARE_NETWORK --mount-proc --setgroups allow -- chroot $_SET
 		-o remount,ro	\
 		/sys/fs/cgroup
 	mountpoint	-q /sys/fs/cgroup/blkio
-	[ $? = 1 ] && mount	-t cgroup	cgroup	\
+	[ $? != 0 ] && mount	-t cgroup	cgroup	\
 		-o ro,nosuid,nodev,noexec,relatime,blkio	\
 		/sys/fs/cgroup/blkio
 #	mountpoint	-q /sys/fs/cgroup/cpu,cpuacct
-#	[ $? = 1 ] && mount	-t cgroup	cgroup	\
+#	[ $? != 0 ] && mount	-t cgroup	cgroup	\
 #		-o ro,nosuid,nodev,noexec,relatime,cpu,cpuacct	\
 #		/sys/fs/cgroup/cpu,cpuacct
 	mountpoint	-q /sys/fs/cgroup/cpuset
-	[ $? = 1 ] && mount	-t cgroup	cgroup	\
+	[ $? != 0 ] && mount	-t cgroup	cgroup	\
 		-o ro,nosuid,nodev,noexec,relatime,cpuset	\
 		/sys/fs/cgroup/cpuset
 	mountpoint	-q /sys/fs/cgroup/devices
-	[ $? = 1 ] && mount	-t cgroup	cgroup	\
+	[ $? != 0 ] && mount	-t cgroup	cgroup	\
 		-o ro,nosuid,nodev,noexec,relatime,devices	\
 		/sys/fs/cgroup/devices
 	mountpoint	-q /sys/fs/cgroup/freezer
-	[ $? = 1 ] && mount	-t cgroup	cgroup	\
+	[ $? != 0 ] && mount	-t cgroup	cgroup	\
 		-o ro,nosuid,nodev,noexec,relatime,freezer	\
 		/sys/fs/cgroup/freezer
 	mountpoint	-q /sys/fs/cgroup/memory
-	[ $? = 1 ] && mount	-t cgroup	cgroup	\
+	[ $? != 0 ] && mount	-t cgroup	cgroup	\
 		-o ro,nosuid,nodev,noexec,relatime,memory	\
 		/sys/fs/cgroup/memory
 #	mountpoint	-q /sys/fs/cgroup/net_cls,net_prio
-#	[ $? = 1 ] && mount	-t cgroup	cgroup	\
+#	[ $? != 0 ] && mount	-t cgroup	cgroup	\
 #		-o ro,nosuid,nodev,noexec,relatime,net_cls,net_prio	\
 #		/sys/fs/cgroup/net_cls,net_prio
 	mountpoint	-q /sys/fs/cgroup/pids
-	[ $? = 1 ] && mount	-t cgroup	cgroup	\
+	[ $? != 0 ] && mount	-t cgroup	cgroup	\
 		-o ro,nosuid,nodev,noexec,relatime,pids	\
 		/sys/fs/cgroup/pids
 #	mountpoint	-q /sys/fs/cgroup/systemd
-#	[ $? = 1 ] && mount	-t cgroup	cgroup	\
+#	[ $? != 0 ] && mount	-t cgroup	cgroup	\
 #		-o rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd	\
 #		/sys/fs/cgroup/systemd
 
-- 
2.32.0

