From 9c7b8d17144ae04dcd56cd736a8f05c76ec87cc1 Mon Sep 17 00:00:00 2001 From: Michael Johnson Date: Tue, 3 Dec 2019 16:55:25 -0800 Subject: [PATCH] Set correct python version for non-chroot scripts Some phases of diskimage-builder run outside the chroot environment, such as the extra-data.d scripts, and don't have access to dib-python. This means these scripts may choose the wrong python version by using "#!/usr/bin/env python" to execute. The svc-map element is an example. This patch creates a temporary directory and symbolic link for the correct version of python, then manipulates the environment PATH to preference the symbolic link "python" command. This will allow elements with these scripts to work correctly with the version of python diskimage-builder is running under. Change-Id: I289d621e1bfbba0eb174dff977d1a5c92c04e4fa Co-Authored-By: Ian Wienand --- diskimage_builder/lib/common-functions | 8 ++++++++ diskimage_builder/lib/disk-image-create | 9 +++++++++ diskimage_builder/lib/img-functions | 1 + 3 files changed, 18 insertions(+) diff --git a/diskimage_builder/lib/common-functions b/diskimage_builder/lib/common-functions index 71c9471c..bafbf744 100644 --- a/diskimage_builder/lib/common-functions +++ b/diskimage_builder/lib/common-functions @@ -241,6 +241,14 @@ function cleanup_image_dir () { rm -rf --one-file-system $TMP_IMAGE_DIR } +function cleanup_temp_python_exec () { + if [ ! -z "$DIB_ORIGINAL_PATH" ]; then + export PATH=$DIB_ORIGINAL_PATH + fi + rm "$DIB_PYTHON_EXEC_TMP/python" || true + rmdir "$DIB_PYTHON_EXEC_TMP" || true +} + # Run a directory of hooks outside the target (that is, no chrooting). function run_d() { check_element diff --git a/diskimage_builder/lib/disk-image-create b/diskimage_builder/lib/disk-image-create index 1de310cb..e8a061a6 100644 --- a/diskimage_builder/lib/disk-image-create +++ b/diskimage_builder/lib/disk-image-create @@ -175,6 +175,14 @@ export DIB_DEBUG_TRACE export DIB_IMAGE_CACHE=${DIB_IMAGE_CACHE:-~/.cache/image-create} mkdir -p $DIB_IMAGE_CACHE +# Setup a symbolic link to the correct python exec so that element +# scripts running outside the chroot using "#!/usr/bin/env python" get +# the right python version. +export DIB_ORIGINAL_PATH=$PATH +export DIB_PYTHON_EXEC_TMP=$(mktemp -d -t --tmpdir=$HOME .DIB_PYTHON_TMP.XXXXXXXX) +ln -s "$DIB_PYTHON_EXEC" "$DIB_PYTHON_EXEC_TMP/python" +export PATH=$DIB_PYTHON_EXEC_TMP:$DIB_ORIGINAL_PATH + # We have a couple of critical sections (touching parts of the host # system or download images to common cache) that we use flock around. # Use this directory for lockfiles. @@ -591,6 +599,7 @@ fi # Remove the leftovers, i.e. the temporary image directory. cleanup_image_dir +cleanup_temp_python_exec # Restore fd 1&2 from the outfilter.py redirect back to the original # saved fd. Note small hack that we can't really wait properly for diff --git a/diskimage_builder/lib/img-functions b/diskimage_builder/lib/img-functions index fa75d7c9..5ab0212d 100644 --- a/diskimage_builder/lib/img-functions +++ b/diskimage_builder/lib/img-functions @@ -43,6 +43,7 @@ function cleanup () { dib-block-device umount cleanup_build_dir cleanup_image_dir + cleanup_temp_python_exec } # Helper function to run a command inside the chroot