[v4,2/7] imager: Migrate from buildchroot to schroot

Message ID 20230511105020.1881496-3-amikan@ilbers.de
State Superseded, archived
Headers show
Series Imager schroot migration | expand

Commit Message

Anton Mikanovich May 11, 2023, 10:50 a.m. UTC
Install dependencies and perform all imager actions using schroot
instead of buildchroot. All the changes made during imager task will be
lost after the finish. This requires installing imager dependencies
during every run.

Signed-off-by: Anton Mikanovich <amikan@ilbers.de>
---
 meta/classes/image-tools-extension.bbclass | 97 +++++++++++++++-------
 1 file changed, 66 insertions(+), 31 deletions(-)

Patch

diff --git a/meta/classes/image-tools-extension.bbclass b/meta/classes/image-tools-extension.bbclass
index 7c84505f..04ed1f84 100644
--- a/meta/classes/image-tools-extension.bbclass
+++ b/meta/classes/image-tools-extension.bbclass
@@ -5,43 +5,78 @@ 
 #
 # This file extends the image.bbclass to supply tools for futher imager functions
 
-inherit buildchroot
+inherit sbuild
 
 IMAGER_INSTALL ??= ""
 IMAGER_BUILD_DEPS ??= ""
 DEPENDS += "${IMAGER_BUILD_DEPS}"
 
-do_install_imager_deps[depends] = "${BUILDCHROOT_DEP} isar-apt:do_cache_config"
-do_install_imager_deps[deptask] = "do_deploy_deb"
-do_install_imager_deps[lockfiles] += "${REPO_ISAR_DIR}/isar.lock"
-do_install_imager_deps[network] = "${TASK_USE_NETWORK_AND_SUDO}"
-do_install_imager_deps() {
-    if [ -z "${@d.getVar("IMAGER_INSTALL").strip()}" ]; then
-        exit
-    fi
+SCHROOT_MOUNTS = "${WORKDIR}:${PP_WORK} ${IMAGE_ROOTFS}:${PP_ROOTFS} ${DEPLOY_DIR_IMAGE}:${PP_DEPLOY}"
+SCHROOT_MOUNTS += "${REPO_ISAR_DIR}/${DISTRO}:/isar-apt"
+
+imager_run() {
+    schroot_create_configs
+    insert_mounts
+
+    session_id=$(schroot -q -b -c ${SBUILD_CHROOT})
+    echo "Started session: ${session_id}"
+
+    # Schroot session mountpoint for deb downloads import/export
+    schroot_dir="/var/run/schroot/mount/${session_id}"
+
+    # setting up error handler
+    imager_cleanup() {
+        set +e
+        schroot -q -f -e -c ${session_id} > /dev/null 2>&1
+        remove_mounts > /dev/null 2>&1
+        schroot_delete_configs > /dev/null 2>&1
+    }
+    trap 'exit 1' INT HUP QUIT TERM ALRM USR1
+    trap 'imager_cleanup' EXIT
+
+    if [ -n "${@d.getVar("IMAGER_INSTALL").strip()}" ]; then
+        echo "Installing deps: ${IMAGER_INSTALL}"
 
-    distro="${BASE_DISTRO}-${BASE_DISTRO_CODENAME}"
-    if [ ${ISAR_CROSS_COMPILE} -eq 1 ]; then
-        distro="${HOST_BASE_DISTRO}-${BASE_DISTRO_CODENAME}"
+        distro="${BASE_DISTRO}-${BASE_DISTRO_CODENAME}"
+        if [ ${ISAR_CROSS_COMPILE} -eq 1 ]; then
+            distro="${HOST_BASE_DISTRO}-${BASE_DISTRO_CODENAME}"
+        fi
+
+        # prepare isar-apt
+        schroot -r -c ${session_id} -d / -u root -- sh -c " \
+            mkdir -p '/etc/apt/sources.list.d'
+            echo 'deb [trusted=yes] file:///isar-apt ${DEBDISTRONAME} main' > \
+                '/etc/apt/sources.list.d/isar-apt.list'
+
+            mkdir -p '/etc/apt/preferences.d'
+            cat << EOF > '/etc/apt/preferences.d/isar-apt'
+Package: *
+Pin: release n=${DEBDISTRONAME}
+Pin-Priority: 1000
+EOF"
+
+        E="${@ isar_export_proxies(d)}"
+        deb_dl_dir_import ${schroot_dir} ${distro}
+        schroot -r -c ${session_id} -d / -u root -- sh -c " \
+            apt-get update \
+                -o Dir::Etc::SourceList='sources.list.d/isar-apt.list' \
+                -o Dir::Etc::SourceParts='-' \
+                -o APT::Get::List-Cleanup='0'
+            apt-get -o Debug::pkgProblemResolver=yes --no-install-recommends -y \
+                --allow-unauthenticated --allow-downgrades --download-only install \
+                ${IMAGER_INSTALL}"
+
+        deb_dl_dir_export ${schroot_dir} ${distro}
+        schroot -r -c ${session_id} -d / -u root -- sh -c " \
+            apt-get -o Debug::pkgProblemResolver=yes --no-install-recommends -y \
+                --allow-unauthenticated --allow-downgrades install \
+                ${IMAGER_INSTALL}"
     fi
 
-    buildchroot_do_mounts
-
-    E="${@ isar_export_proxies(d)}"
-    deb_dl_dir_import ${BUILDCHROOT_DIR} ${distro}
-    sudo -E chroot ${BUILDCHROOT_DIR} sh -c ' \
-        apt-get update \
-            -o Dir::Etc::SourceList="sources.list.d/isar-apt.list" \
-            -o Dir::Etc::SourceParts="-" \
-            -o APT::Get::List-Cleanup="0"
-        apt-get -o Debug::pkgProblemResolver=yes --no-install-recommends -y \
-            --allow-unauthenticated --allow-downgrades --download-only install \
-            ${IMAGER_INSTALL}'
-
-    deb_dl_dir_export ${BUILDCHROOT_DIR} ${distro}
-    sudo -E chroot ${BUILDCHROOT_DIR} sh -c ' \
-        apt-get -o Debug::pkgProblemResolver=yes --no-install-recommends -y \
-            --allow-unauthenticated --allow-downgrades install \
-            ${IMAGER_INSTALL}'
+    schroot -r -c ${session_id} "$@"
+
+    schroot -e -c ${session_id}
+
+    remove_mounts
+    schroot_delete_configs
 }
-addtask install_imager_deps before do_image_tools