From: "'Cedric Hombourger' via isar-users" <isar-users@googlegroups.com>
To: isar-users@googlegroups.com
Cc: "cedric.hombourger@siemens.com" <cedric.hombourger@siemens.com>
Subject: [PATCH v4 4/4] rootfs: do not get elevated privileges when downloading packages
Date: Thu, 25 Sep 2025 08:54:24 +0200 [thread overview]
Message-ID: <20250925065433.4180883-5-cedric.hombourger@siemens.com> (raw)
In-Reply-To: <20250925065433.4180883-1-cedric.hombourger@siemens.com>
From: "cedric.hombourger@siemens.com" <cedric.hombourger@siemens.com>
Use rootfs_cmd() to run "apt-get install --download-only" without sudo.
This requires /var/cache/apt/archives/ to be writable by the build
user: change ownership while populating that folder with previously
downloaded packages (those in downloads/deb/).
Signed-off-by: Cedric Hombourger <cedric.hombourger@siemens.com>
---
meta/classes/deb-dl-dir.bbclass | 21 ++++++++++++++++++---
meta/classes/rootfs.bbclass | 16 +++++++++++++---
2 files changed, 31 insertions(+), 6 deletions(-)
diff --git a/meta/classes/deb-dl-dir.bbclass b/meta/classes/deb-dl-dir.bbclass
index ea0ed3d2..16ccd426 100644
--- a/meta/classes/deb-dl-dir.bbclass
+++ b/meta/classes/deb-dl-dir.bbclass
@@ -107,9 +107,24 @@ dbg_pkgs_download() {
deb_dl_dir_import() {
export pc="${DEBDIR}/${2}"
export rootfs="${1}"
- sudo mkdir -p "${rootfs}"/var/cache/apt/archives/
+ export uid=$(id -u)
+ export gid=$(id -g)
+
+ # let our unprivileged user place downloaded packages in /var/cache/apt/archives/
+ sudo -Es << ' EOSUDO'
+ mkdir -p "${rootfs}"/var/cache/apt/archives/partial/
+ touch "${rootfs}"/var/cache/apt/archives/lock
+ chown -R ${uid}:${gid} "${rootfs}"/var/cache/apt/archives/
+ EOSUDO
+
+ # nothing to copy if download directory does not exist just yet
[ ! -d "${pc}" ] && return 0
- flock -s "${pc}".lock sudo -Es << 'EOSUDO'
+
+ # attempt to create hard-links for .deb files from downloads/ into
+ # /var/cache/apt/archives/ so apt will only download packages we
+ # have not yet downloaded. perform a regular copy whenever hard-links
+ # cannot be created
+ ( flock 9
set -e
printenv | grep -q BB_VERBOSE_LOGS && set -x
@@ -118,7 +133,7 @@ deb_dl_dir_import() {
ln -Pf -t "${rootfs}"/var/cache/apt/archives/ "$p" 2>/dev/null ||
cp -n --no-preserve=owner -t "${rootfs}"/var/cache/apt/archives/ "$p"
done
-EOSUDO
+ ) 9>"${pc}".lock
}
deb_dl_dir_export() {
diff --git a/meta/classes/rootfs.bbclass b/meta/classes/rootfs.bbclass
index f740c6e1..684d04c4 100644
--- a/meta/classes/rootfs.bbclass
+++ b/meta/classes/rootfs.bbclass
@@ -286,10 +286,20 @@ ROOTFS_INSTALL_COMMAND += "rootfs_install_pkgs_download"
rootfs_install_pkgs_download[weight] = "600"
rootfs_install_pkgs_download[progress] = "custom:rootfs_progress.PkgsDownloadProgressHandler"
rootfs_install_pkgs_download[isar-apt-lock] = "release-after"
-rootfs_install_pkgs_download[network] = "${TASK_USE_NETWORK_AND_SUDO}"
+rootfs_install_pkgs_download[network] = "${TASK_USE_NETWORK}"
rootfs_install_pkgs_download() {
- sudo -E chroot '${ROOTFSDIR}' \
- /usr/bin/apt-get ${ROOTFS_APT_ARGS} --download-only ${ROOTFS_PACKAGES}
+ mkdir -p "${WORKDIR}/dpkg"
+
+ # Use our own dpkg lock files rather than those in the rootfs since we are not root
+ # (this is safe as there are no concurrent apt/dpkg operations for that rootfs)
+ touch "${WORKDIR}/dpkg/lock" "${WORKDIR}/dpkg/lock-frontend"
+
+ # download packages using apt in a non-privileged namespace
+ rootfs_cmd --bind "${ROOTFSDIR}/var/cache/apt/archives" /var/cache/apt/archives \
+ --bind "${WORKDIR}/dpkg/lock" /var/lib/dpkg/lock \
+ --bind "${WORKDIR}/dpkg/lock-frontend" /var/lib/dpkg/lock-frontend \
+ ${ROOTFSDIR} \
+ -- /usr/bin/apt-get ${ROOTFS_APT_ARGS} --download-only ${ROOTFS_PACKAGES}
}
ROOTFS_INSTALL_COMMAND_BEFORE_EXPORT ??= ""
--
2.47.3
--
You received this message because you are subscribed to the Google Groups "isar-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to isar-users+unsubscribe@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/isar-users/20250925065433.4180883-5-cedric.hombourger%40siemens.com.
next prev parent reply other threads:[~2025-09-25 6:54 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-25 6:54 [PATCH v4 0/4] non-privileged commands in chroot 'Cedric Hombourger' via isar-users
2025-09-25 6:54 ` [PATCH v4 1/4] rootfs: introduce wrapper to run commands against a rootfs 'Cedric Hombourger' via isar-users
2025-10-01 7:21 ` Andreas Naumann
2025-09-25 6:54 ` [PATCH v4 2/4] deb-dl-dir: optimize caching of source packages using apt natively 'Cedric Hombourger' via isar-users
2025-09-25 9:07 ` 'MOESSBAUER, Felix' via isar-users
2025-09-25 6:54 ` [PATCH v4 3/4] bootstrap: create lock for downloads/deb without sudo 'Cedric Hombourger' via isar-users
2025-10-01 7:22 ` Andreas Naumann
2025-09-25 6:54 ` 'Cedric Hombourger' via isar-users [this message]
2025-09-25 9:08 ` [PATCH v4 0/4] non-privileged commands in chroot 'MOESSBAUER, Felix' via isar-users
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250925065433.4180883-5-cedric.hombourger@siemens.com \
--to=isar-users@googlegroups.com \
--cc=cedric.hombourger@siemens.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox