From 8f3759c04f5e987a97c5d2c73703dc8fedc308b5 Mon Sep 17 00:00:00 2001 From: Chris Frey Date: Sat, 11 Dec 2021 21:42:51 -0500 Subject: [PATCH] Meta: Use fuse2fs if available to avoid root when building image The fuse2fs tool that is part of e2fsprogs-1.46 has a 'fakeroot' mount option. This allows a non-root users to modify file ownership and permissions without actually being root. This package is available in Debian bullseye and buster-backports. If available, the script assumes the user wants to use it. Otherwise, it falls back to the usual root requirements. Now that root is not required, the root check in build-root-filesystem.sh is not necessary. Since build-root-filesystem.sh has 'set -e' enabled, removing this check will not cause a change in functionality. --- Meta/build-image-qemu.sh | 22 +++++++++++++++++----- Meta/build-root-filesystem.sh | 4 ---- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Meta/build-image-qemu.sh b/Meta/build-image-qemu.sh index 0b29b42707..92ca703f2b 100755 --- a/Meta/build-image-qemu.sh +++ b/Meta/build-image-qemu.sh @@ -7,9 +7,15 @@ die() { exit 1 } +USE_FUSE2FS=0 + if [ "$(id -u)" != 0 ]; then - sudo -E -- "$0" "$@" || die "this script needs to run as root" - exit 0 + if [ -x /usr/sbin/fuse2fs ] && /usr/sbin/fuse2fs --help 2>&1 |grep fakeroot > /dev/null; then + USE_FUSE2FS=1 + else + sudo -E -- "$0" "$@" || die "this script needs to run as root" + exit 0 + fi else : "${SUDO_UID:=0}" "${SUDO_GID:=0}" fi @@ -68,7 +74,7 @@ if [ -f _disk_image ]; then echo "checking existing image" result=0 - e2fsck -f -y _disk_image || result=$? + /usr/sbin/e2fsck -f -y _disk_image || result=$? if [ $result -ge 4 ]; then rm -f _disk_image USE_EXISTING=0 @@ -116,7 +122,9 @@ fi printf "mounting filesystem... " mkdir -p mnt use_genext2fs=0 -if [ "$(uname -s)" = "Darwin" ]; then +if [ $USE_FUSE2FS -eq 1 ]; then + mount_cmd="/usr/sbin/fuse2fs _disk_image mnt/ -o fakeroot,rw" +elif [ "$(uname -s)" = "Darwin" ]; then mount_cmd="fuse-ext2 _disk_image mnt -o rw+,allow_other,uid=501,gid=20" elif [ "$(uname -s)" = "OpenBSD" ]; then VND=$(vnconfig _disk_image) @@ -142,7 +150,11 @@ cleanup() { if [ -d mnt ]; then if [ $use_genext2fs = 0 ] ; then printf "unmounting filesystem... " - umount mnt || ( sleep 1 && sync && umount mnt ) + if [ $USE_FUSE2FS -eq 1 ]; then + fusermount -u mnt || (sleep 1 && sync && fusermount -u mnt) + else + umount mnt || ( sleep 1 && sync && umount mnt ) + fi rmdir mnt else rm -rf mnt diff --git a/Meta/build-root-filesystem.sh b/Meta/build-root-filesystem.sh index 14bd84bb8c..8cc8be1112 100755 --- a/Meta/build-root-filesystem.sh +++ b/Meta/build-root-filesystem.sh @@ -22,10 +22,6 @@ die() { exit 1 } -if [ "$(id -u)" != 0 ]; then - die "this script needs to run as root" -fi - [ -z "$SERENITY_SOURCE_DIR" ] && die "SERENITY_SOURCE_DIR is not set" [ -d "$SERENITY_SOURCE_DIR/Base" ] || die "$SERENITY_SOURCE_DIR/Base doesn't exist"