summaryrefslogtreecommitdiff
path: root/debian/qemu-debootstrap
diff options
context:
space:
mode:
Diffstat (limited to 'debian/qemu-debootstrap')
-rw-r--r--debian/qemu-debootstrap168
1 files changed, 168 insertions, 0 deletions
diff --git a/debian/qemu-debootstrap b/debian/qemu-debootstrap
new file mode 100644
index 0000000..674d71a
--- /dev/null
+++ b/debian/qemu-debootstrap
@@ -0,0 +1,168 @@
+#!/bin/sh
+# qemu-debootstrap - setup qemu syscall emulation in a debootstrap chroot
+# Copyright (C) 2010 Loïc Minier <lool@dooz.org>
+#
+# Permission is hereby granted, free of charge, to any person obtaining a
+# copy of this software and associated documentation files (the "Software"),
+# to deal in the Software without restriction, including without limitation
+# the rights to use, copy, modify, merge, publish, distribute, sublicense,
+# and/or sell copies of the Software, and to permit persons to whom the
+# Software is furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+# SOFTWARE IN THE PUBLIC INTEREST, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR
+# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+# DEALINGS IN THE SOFTWARE.
+#
+# Except as contained in this notice, the name of the author shall not be used
+# in advertising or otherwise to promote the sale, use or other dealings in
+# this Software without prior written authorization from the author.
+#
+# requires: debootstrap
+
+set -e
+
+log() {
+ local format="$1"
+ shift
+ printf -- "$format\n" "$@" >&2
+}
+
+warn() {
+ local format="$1"
+ shift
+ log "W: $format" "$@"
+}
+
+die() {
+ local format="$1"
+ shift
+ log "E: $format" "$@"
+ exit 1
+}
+
+run() {
+ log "I: Running command: %s" "$*"
+ "$@"
+}
+
+escape() {
+ echo "$*" | sed "s/'/'\"'\"'/g; s/.*/'&'/"
+}
+unescape () {
+ eval "echo" "$*"
+}
+
+if ! which debootstrap 2>&1 >/dev/null; then
+ die "debootstrap not found; install the debootstrap package"
+fi
+
+system_arch="$(dpkg --print-architecture)"
+deb_arch="$system_arch"
+
+# backward-compatibility with "build-arm-chroot" script which defaulted to
+# armel
+if [ "`basename "$0"`" = "build-arm-chroot" ]; then
+ deb_arch="armel"
+ log "Setting Debian architecture to armel"
+ log "W:" "$0 is deprecated, please use qemu-debootstrap"
+fi
+
+opts=""
+args=""
+suite=""
+target=""
+mirror=""
+script=""
+while [ $# -gt 0 ]; do
+ case "$1" in
+ --help)
+ die "I'm just a debootstrap wrapper; please see debootstrap --help"
+ ;;
+ --arch|--arch=?*)
+ if [ "$1" = "--arch" -a $# -ge 2 -a -n "$2" ]; then
+ deb_arch="$2"
+ shift 2
+ elif [ "$1" != "${1#--arch=}" ]; then
+ deb_arch="${1#--arch=}"
+ shift
+ else
+ die "option %s requires an argument" "$1"
+ fi
+ ;;
+ --*)
+ opts="$opts $(escape "$1")"
+ shift
+ ;;
+ *)
+ if [ -z "$suite" ]; then stage="suite";
+ elif [ -z "$target" ]; then stage="target";
+ elif [ -z "$mirror" ]; then stage="mirror";
+ elif [ -z "$script" ]; then stage="script";
+ fi
+ if [ -n "$1" ]; then
+ eval $stage=\"\$1\"
+ args="$args $(escape "$1")"
+ else
+ die "option %s may not be empty" "$stage"
+ fi
+ shift
+ ;;
+ esac
+done
+
+needs_qemu="yes"
+if [ "$deb_arch" = "$system_arch" ]; then
+ warn "Target architecture is the same as host architecture; disabling QEMU support"
+ needs_qemu="no"
+fi
+# bi-arch; TODO test whether the running kernel is actually bi-arch capable
+case "$system_arch-$deb_arch" in
+ amd64-i386|amd64-lpia|arm-armel|arm-armhf|armel-arm|armhf-arm|i386-amd64|i386-lpia|lpia-i386|powerpc-ppc64|ppc64-powerpc|sparc-sparc64|sparc64-sparc)
+ warn "Host architecture might allow running target architecture; disabling QEMU support"
+ needs_qemu="no"
+ ;;
+esac
+
+if [ "$needs_qemu" = no ]; then
+ eval run debootstrap --arch "$deb_arch" $opts $args
+ exit 0
+fi
+
+qemu_arch=""
+case "$deb_arch" in
+ alpha|arm|armeb|i386|m68k|mips|mipsel|ppc64|sh4|sh4eb|sparc|sparc64)
+ qemu_arch="$deb_arch"
+ ;;
+ amd64)
+ qemu_arch="x86_64"
+ ;;
+ armel|armhf)
+ qemu_arch="arm"
+ ;;
+ lpia)
+ qemu_arch="i386"
+ ;;
+ powerpc)
+ qemu_arch="ppc"
+ ;;
+ *)
+ die "Sorry, I don't know how to support arch %s" "$arch"
+ ;;
+esac
+
+if ! which "qemu-$qemu_arch-static" >/dev/null 2>&1; then
+ die "Sorry, couldn't find binary %s" "qemu-$qemu_arch-static"
+fi
+
+eval run debootstrap --arch "$deb_arch" --foreign $opts $args
+mkdir -p "$target/usr/bin"
+cp $(which "qemu-$qemu_arch-static") "$target/usr/bin"
+run chroot "$target" /debootstrap/debootstrap --second-stage
+