summaryrefslogtreecommitdiff
path: root/debian/qemu-debootstrap
blob: f35da8b4bce64eb01ba8b0ff9d13661780b83944 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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.

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" "$*"
}

system_arch="$(dpkg --print-architecture)"
deb_arch="$system_arch"

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
      ;;
      --include|--exclude|--components|--variant|--keyring|--unpack-tarball|--make-tarball|--second-stage-target|--extractor|--private-key|--certificate)
        if [ $# -ge 2 -a -n "$2" ]; then
            opts="$opts $(escape "$1") $(escape "$2")"
            shift 2
        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|arm-armel|armel-arm|arm-armhf|armhf-arm|armel-armhf|armhf-armel|i386-amd64|powerpc-ppc64|ppc64-powerpc|sparc-sparc64|sparc64-sparc|s390-s390x|s390x-s390)
    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 $?
fi

qemu_arch=""
case "$deb_arch" in
  alpha|arm|armeb|i386|m68k|mips|mipsel|mips64el|ppc64|sh4|sh4eb|sparc|sparc64|s390x)
    qemu_arch="$deb_arch"
  ;;
  amd64)
    qemu_arch="x86_64"
  ;;
  armel|armhf)
    qemu_arch="arm"
  ;;
  arm64)
    qemu_arch="aarch64"
  ;;
  lpia)
    qemu_arch="i386"
  ;;
  powerpc|powerpcspe)
    qemu_arch="ppc"
  ;;
  ppc64el)
    qemu_arch="ppc64le"
  ;;
  *)
    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