From 414edd44c27928eb20a5e8eb758cf6d58bf8c95e Mon Sep 17 00:00:00 2001 From: loki Date: Mon, 30 Jun 2014 13:52:25 +0530 Subject: [PATCH] Provide ability to build images for other architectures Adds binaries specified by user to chroot environment allowing to build images on amd64,i386,armhf,arm64 architectures. Closes-Bug: #1332458 Change-Id: If6e63a472ee85559b93b5e6b96d9c3ddcf7bcc98 --- .../architecture-emulation-binaries/README.md | 20 ++++++ .../cleanup.d/01-cleanbinary | 9 +++ .../extra-data.d/01-copy-binary | 61 +++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 elements/architecture-emulation-binaries/README.md create mode 100755 elements/architecture-emulation-binaries/cleanup.d/01-cleanbinary create mode 100755 elements/architecture-emulation-binaries/extra-data.d/01-copy-binary diff --git a/elements/architecture-emulation-binaries/README.md b/elements/architecture-emulation-binaries/README.md new file mode 100644 index 00000000..c5661257 --- /dev/null +++ b/elements/architecture-emulation-binaries/README.md @@ -0,0 +1,20 @@ +This element enables execution for different architectures + +When building an image for an architecture that the host machine +can not execute, we need to chroot into the image to execute code, +and if the host architecture does not match, we need to emulate +the instructions. + +This element does the following: + + * copies the binary file into chroot /usr/bin environment. + Binary file is chosen based on host architecture and + image architecture the user is trying to build. + + If an image we are building for an architecture is not the host + architecture, install tools provided by qemu-user-static + (which needs to be installed) to allow us to run commands + inside the building image. + + This is tested on amd64/i386 architecture to build armhf and arm64 + ubuntu cloud images. diff --git a/elements/architecture-emulation-binaries/cleanup.d/01-cleanbinary b/elements/architecture-emulation-binaries/cleanup.d/01-cleanbinary new file mode 100755 index 00000000..72ec8d14 --- /dev/null +++ b/elements/architecture-emulation-binaries/cleanup.d/01-cleanbinary @@ -0,0 +1,9 @@ +#!/bin/bash + +set -eux +set -o pipefail + +if [ -x "$TMP_MOUNT_PATH/tmp/clean_up_qemu_binary" ] ; then + "$TMP_MOUNT_PATH/tmp/clean_up_qemu_binary" + rm -rf "$TMP_MOUNT_PATH/tmp/clean_up_qemu_binary" +fi diff --git a/elements/architecture-emulation-binaries/extra-data.d/01-copy-binary b/elements/architecture-emulation-binaries/extra-data.d/01-copy-binary new file mode 100755 index 00000000..e5f20508 --- /dev/null +++ b/elements/architecture-emulation-binaries/extra-data.d/01-copy-binary @@ -0,0 +1,61 @@ +#!/bin/bash + +set -eux +set -o pipefail + +function clean_up_arch_emulation { + cat <$TMP_MOUNT_PATH/tmp/clean_up_qemu_binary +#!/bin/bash + +set -eux +set -o pipefail + +sudo rm -rf "${TMP_MOUNT_PATH}${1}" +EOF + + sudo chmod +x "$TMP_MOUNT_PATH/tmp/clean_up_qemu_binary" +} + +function check_copy_file { + if [ -f "$1" ] ; then + if [ ! -f "${TMP_MOUNT_PATH}${1}" ]; then + sudo cp "$1" "$TMP_MOUNT_PATH/usr/bin" + clean_up_arch_emulation $1 + fi + else + echo "qemu binary file not found." + exit 1 + fi +} + +function copy_binary { + echo "checking for architecture compatibility..." + img_arch=$2 + if [[ "$host_arch" != "$img_arch" ]]; then + qemu_binary=$1 + check_copy_file $qemu_binary + else + echo "Emulation not required for this host/image architecture combination" + fi +} + +host_arch="$(uname -m)" + +case "$ARCH" in + "i386" | "amd64") + qemu_binary_file="/usr/bin/qemu-i386-static" + copy_binary $qemu_binary_file $ARCH + ;; + "armhf") + qemu_binary_file="/usr/bin/qemu-arm-static" + copy_binary $qemu_binary_file $ARCH + ;; + "arm64") + qemu_binary_file="/usr/bin/qemu-aarch64-static" + copy_binary $qemu_binary_file $ARCH + ;; + *) + echo "architecture not supported" + exit 1 + ;; +esac