From f6c3f49fca34aed4a623fc07c335fa4fd1812966 Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Wed, 14 Jan 2026 19:16:18 +0100 Subject: [PATCH] pkg-manager.sh: Support running without sudo To support minimal environments, notably minimal Docker containers which don't have /usr/bin/sudo by the time pkg-manager.sh is invoked (possibly to install sudo), support running all commands without invoking sudo first. Of course this only works if invoked as root. Note that this is still somewhat hacky, command-line parsing needs to be cleaned up. Signed-off-by: Jan Lindemann --- scripts/pkg-manager.sh | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/scripts/pkg-manager.sh b/scripts/pkg-manager.sh index ee91b3bb..50c9cc4d 100644 --- a/scripts/pkg-manager.sh +++ b/scripts/pkg-manager.sh @@ -45,13 +45,15 @@ cmd_dup() done shift + [ "$sudo" ] && sudo="$sudo $env -S" + case $ID in opensuse|suse) [ "$non_interactive" = 1 ] && { opts="--force-resolution --auto-agree-with-licenses" global_opts="$global_opts --non-interactive --gpg-auto-import-keys --no-gpg-checks" } - run sudo $env -S zypper $global_opts dup $opts "$@" + run $sudo zypper $global_opts dup $opts "$@" ;; debian|ubuntu|raspbian) fatal "Tried to run distribution upgrade on unsupported platform \"$ID\"" @@ -84,30 +86,32 @@ cmd_install() done shift + [ "$sudo" ] && sudo="$sudo $env -S" + case $ID in opensuse|suse) [ "$non_interactive" = 1 ] && { opts="--force-resolution --auto-agree-with-licenses" global_opts="$global_opts --non-interactive --gpg-auto-import-keys --no-gpg-checks" } - run sudo $env -S zypper $global_opts install $opts "$@" + run $sudo zypper $global_opts install $opts "$@" ;; debian|ubuntu|raspbian) [ "$non_interactive" = 1 ] && { global_opts="$global_opts -yq" env="$env DEBIAN_FRONTEND=noninteractive" } - run sudo $env -S apt-get $global_opts install "$@" + run $sudo apt-get $global_opts install "$@" ;; arch) [ "$non_interactive" = 1 ] && { global_opts="$global_opts --noconfirm" env="$env DEBIAN_FRONTEND=noninteractive" } - run sudo $env -S pacman $global_opts -S --needed "$@" + run $sudo pacman $global_opts -S --needed "$@" ;; centos) - run sudo $env -S yum $global_opts install -y "$@" + run $sudo yum $global_opts install -y "$@" ;; *) fatal "Tried to install on unsupported platform \"$ID\"" @@ -131,19 +135,21 @@ cmd_refresh() done shift + [ "$sudo" ] && sudo="$sudo $env -S" + case $ID in opensuse|suse) [ "$non_interactive" = 1 ] && { global_opts="$global_opts --non-interactive --gpg-auto-import-keys --no-gpg-checks" } - run sudo $env -S zypper $global_opts refresh $opts "$@" + run $sudo zypper $global_opts refresh $opts "$@" ;; debian|ubuntu|raspbian) - run sudo $env -S apt-get $global_opts update "$@" + run $sudo apt-get $global_opts update "$@" ;; centos) - run sudo $env -S yum $global_opts clean expire-cache "$@" - run sudo $env -S yum $global_opts makecache "$@" + run $sudo yum $global_opts clean expire-cache "$@" + run $sudo yum $global_opts makecache "$@" ;; *) fatal "Tried to update unsupported platform \"$ID\"" @@ -155,6 +161,8 @@ cmd_refresh() myname=`basename $0` . /etc/os-release ID=${ID%%-*} +[ "$EUID" ] || EUID=$(id -un) +[ "$EUID" = 0 ] || sudo=/usr/bin/sudo cmd="$1" shift cmd_$cmd "$@"