From: Andrej Shadura <andrew.shadura@collabora.co.uk>
Date: Tue, 24 Jan 2023 10:11:13 +0100
Subject: Detect and workaround fakeroot or pseudo

With fakeroot, podman and toolbox try to do root things and fail.
---
 src/cmd/root.go        | 4 ++--
 src/pkg/utils/utils.go | 5 +++++
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/cmd/root.go b/src/cmd/root.go
index 1d9adae..0b1f921 100644
--- a/src/cmd/root.go
+++ b/src/cmd/root.go
@@ -137,7 +137,7 @@ func preRun(cmd *cobra.Command, args []string) error {
 	logrus.Debugf("Running as real user ID %s", currentUser.Uid)
 	logrus.Debugf("Resolved absolute path to the executable as %s", executable)
 
-	if !utils.IsInsideContainer() {
+	if !utils.IsInsideContainer() && !utils.IsInFakeroot() {
 		logrus.Debugf("Running on a cgroups v%d host", cgroupsVersion)
 
 		if currentUser.Uid != "0" {
@@ -219,7 +219,7 @@ func rootRun(cmd *cobra.Command, args []string) error {
 func migrate() error {
 	logrus.Debug("Migrating to newer Podman")
 
-	if utils.IsInsideContainer() {
+	if utils.IsInsideContainer() || utils.IsInFakeroot() {
 		return nil
 	}
 
diff --git a/src/pkg/utils/utils.go b/src/pkg/utils/utils.go
index fafae4f..bbeaf1c 100644
--- a/src/pkg/utils/utils.go
+++ b/src/pkg/utils/utils.go
@@ -680,6 +680,11 @@ func IsContainerNameValid(containerName string) bool {
 	return matched
 }
 
+func IsInFakeroot() bool {
+	return strings.Contains(os.Getenv("LD_PRELOAD"), "libfakeroot") ||
+		strings.Contains(os.Getenv("LD_PRELOAD"), "libpseudo")
+}
+
 func IsInsideContainer() bool {
 	return PathExists("/run/.containerenv")
 }
