From b5d5402e439f5edfd642fb4f680d596f5992e874 Mon Sep 17 00:00:00 2001 From: Levente Polyak Date: Mon, 10 Oct 2022 00:37:51 +0200 Subject: src: modularize repo layout into a library This will greatly help us to structure the functionality and commands in a more sane way. We will distribute the sources as actual libraries and reuse code with imports instead of processing everything with m4 and duplicating a lot of code. --- src/lib/archroot.sh | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/lib/archroot.sh (limited to 'src/lib/archroot.sh') diff --git a/src/lib/archroot.sh b/src/lib/archroot.sh new file mode 100644 index 0000000..d7917da --- /dev/null +++ b/src/lib/archroot.sh @@ -0,0 +1,62 @@ +#!/hint/bash +# +# SPDX-License-Identifier: GPL-3.0-or-later +: + +# shellcheck disable=2034 +CHROOT_VERSION='v4' + +## +# usage : check_root $keepenv +## +orig_argv=("${BASH_SOURCE[0]}" "$@") +check_root() { + local keepenv=$1 + + (( EUID == 0 )) && return + if type -P sudo >/dev/null; then + exec sudo --preserve-env=$keepenv -- "${orig_argv[@]}" + else + exec su root -c "$(printf ' %q' "${orig_argv[@]}")" + fi +} + +## +# usage : is_btrfs( $path ) +# return : whether $path is on a btrfs +## +is_btrfs() { + [[ -e "$1" && "$(stat -f -c %T "$1")" == btrfs ]] +} + +## +# usage : is_subvolume( $path ) +# return : whether $path is a the root of a btrfs subvolume (including +# the top-level subvolume). +## +is_subvolume() { + [[ -e "$1" && "$(stat -f -c %T "$1")" == btrfs && "$(stat -c %i "$1")" == 256 ]] +} + +## +# usage : subvolume_delete_recursive( $path ) +# +# Find all btrfs subvolumes under and including $path and delete them. +## +subvolume_delete_recursive() { + local subvol + + is_subvolume "$1" || return 0 + + while IFS= read -d $'\0' -r subvol; do + if ! subvolume_delete_recursive "$subvol"; then + return 1 + fi + done < <(find "$1" -mindepth 1 -xdev -depth -inum 256 -print0) + if ! btrfs subvolume delete "$1" &>/dev/null; then + error "Unable to delete subvolume %s" "$subvol" + return 1 + fi + + return 0 +} -- cgit v1.2.3-70-g09d2 From 41d4624879d01b1269d6af9c1440592a15ad7784 Mon Sep 17 00:00:00 2001 From: Levente Polyak Date: Sat, 20 May 2023 00:20:41 +0200 Subject: fix(archroot): pass along the real command and argv by the caller Before modularizing the repo layout, we used m4_include to assemble together sources into a single file. Now, we properly use a library layout without assembling multiple files, which means we cannot anymore rely on BASH_SOURCE inside the library file. Hence, pass along the actual command and argv from the check_root caller. Fixes: src: modularize repo layout into a library Signed-off-by: Morten Linderud Signed-off-by: Levente Polyak --- src/arch-nspawn.in | 2 +- src/archbuild.in | 2 +- src/lib/archroot.sh | 5 +++-- src/makechrootpkg.in | 2 +- src/makerepropkg.in | 2 +- src/mkarchroot.in | 2 +- 6 files changed, 8 insertions(+), 7 deletions(-) (limited to 'src/lib/archroot.sh') diff --git a/src/arch-nspawn.in b/src/arch-nspawn.in index 77a27ad..b692817 100644 --- a/src/arch-nspawn.in +++ b/src/arch-nspawn.in @@ -46,7 +46,7 @@ done shift $((OPTIND - 1)) (( $# < 1 )) && die 'You must specify a directory.' -check_root +check_root "" "${BASH_SOURCE[0]}" "$@" working_dir=$(readlink -f "$1") shift 1 diff --git a/src/archbuild.in b/src/archbuild.in index a7e4231..2f3faf9 100644 --- a/src/archbuild.in +++ b/src/archbuild.in @@ -74,7 +74,7 @@ while getopts 'hcr:' arg; do esac done -check_root SOURCE_DATE_EPOCH,SRCDEST,SRCPKGDEST,PKGDEST,LOGDEST,MAKEFLAGS,PACKAGER,GNUPGHOME +check_root SOURCE_DATE_EPOCH,SRCDEST,SRCPKGDEST,PKGDEST,LOGDEST,MAKEFLAGS,PACKAGER,GNUPGHOME "${BASH_SOURCE[0]}" "$@" # Pass all arguments after -- right to makepkg makechrootpkg_args+=("${@:$OPTIND}") diff --git a/src/lib/archroot.sh b/src/lib/archroot.sh index d7917da..3f48dc1 100644 --- a/src/lib/archroot.sh +++ b/src/lib/archroot.sh @@ -9,13 +9,14 @@ CHROOT_VERSION='v4' ## # usage : check_root $keepenv ## -orig_argv=("${BASH_SOURCE[0]}" "$@") check_root() { local keepenv=$1 + shift + local orig_argv=("$@") (( EUID == 0 )) && return if type -P sudo >/dev/null; then - exec sudo --preserve-env=$keepenv -- "${orig_argv[@]}" + exec sudo --preserve-env="${keepenv}" -- "${orig_argv[@]}" else exec su root -c "$(printf ' %q' "${orig_argv[@]}")" fi diff --git a/src/makechrootpkg.in b/src/makechrootpkg.in index 8d3d093..2cfd849 100644 --- a/src/makechrootpkg.in +++ b/src/makechrootpkg.in @@ -301,7 +301,7 @@ done [[ -n $makepkg_user && -z $(id -u "$makepkg_user") ]] && die 'Invalid makepkg user.' makepkg_user=${makepkg_user:-${SUDO_USER:-$USER}} -check_root SOURCE_DATE_EPOCH,BUILDTOOL,BUILDTOOLVER,GNUPGHOME,SRCDEST,SRCPKGDEST,PKGDEST,LOGDEST,MAKEFLAGS,PACKAGER +check_root SOURCE_DATE_EPOCH,BUILDTOOL,BUILDTOOLVER,GNUPGHOME,SRCDEST,SRCPKGDEST,PKGDEST,LOGDEST,MAKEFLAGS,PACKAGER "${BASH_SOURCE[0]}" "$@" # Canonicalize chrootdir, getting rid of trailing / chrootdir=$(readlink -e "$passeddir") diff --git a/src/makerepropkg.in b/src/makerepropkg.in index f310c69..61ee9db 100644 --- a/src/makerepropkg.in +++ b/src/makerepropkg.in @@ -137,7 +137,7 @@ while getopts 'dM:c:l:h' arg; do done shift $((OPTIND - 1)) -check_root +check_root "" "${BASH_SOURCE[0]}" "$@" [[ -f PKGBUILD ]] || { error "No PKGBUILD in current directory."; exit 1; } diff --git a/src/mkarchroot.in b/src/mkarchroot.in index 6c8d8a2..610de16 100644 --- a/src/mkarchroot.in +++ b/src/mkarchroot.in @@ -52,7 +52,7 @@ shift $((OPTIND - 1)) (( $# < 2 )) && die 'You must specify a directory and one or more packages.' -check_root +check_root "" "${BASH_SOURCE[0]}" "$@" working_dir="$(readlink -f "$1")" shift 1 -- cgit v1.2.3-70-g09d2 From bf61b8472a2e50a740229f007513dd663b9dddda Mon Sep 17 00:00:00 2001 From: Levente Polyak Date: Sat, 20 May 2023 14:55:46 +0200 Subject: chore(archroot): force build chroot recreation to adapt new configs Bumping the chroot version will result in the chroots checking against the local version and force recreation in case they do not match. Signed-off-by: Levente Polyak --- src/lib/archroot.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/lib/archroot.sh') diff --git a/src/lib/archroot.sh b/src/lib/archroot.sh index 3f48dc1..8386c6c 100644 --- a/src/lib/archroot.sh +++ b/src/lib/archroot.sh @@ -4,7 +4,7 @@ : # shellcheck disable=2034 -CHROOT_VERSION='v4' +CHROOT_VERSION='v5' ## # usage : check_root $keepenv -- cgit v1.2.3-70-g09d2