aboutsummaryrefslogtreecommitdiff
path: root/pbuilder-user-mode-linux
blob: 33f6a069a48685800b50b13e7667ff8ea0ca5f60 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#! /bin/bash
# common modules for pbuilder.
#   pbuilder -- personal Debian package builder
#   Copyright (C) 2001-2003 Junichi Uekawa
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

set -e

function cleanup_function () {
    rm -f ${INSIDE_PBUILDER}
    ${EXTRACLEANUP}
}


. /usr/lib/pbuilder/pbuilder-loadconfig

for CONFIGFILE in /usr/share/pbuilder/pbuilder-uml.conf /etc/pbuilder/pbuilder-uml.conf ${HOME}/.pbuilderrc; do
    if [ -f ${CONFIGFILE} ]; then
	. ${CONFIGFILE}
    fi
done

function usecow () {
    PBUILDER_COWFILENAME="${BUILDPLACE}/$$.cow"
    PBUILDER_COW="${PBUILDER_COWFILENAME},"
    if ! touch "${PBUILDER_COWFILENAME}"; then
	echo "E: Cannot write-access to the COW file, check buildplace"
	exit 1;
    fi
    rm -f "${PBUILDER_COWFILENAME}"
}

function cleancow () {
    # clean the cow file
    rm -f "${PBUILDER_COWFILENAME}"
}

function operate_uml () {
    # opeartes on UML, and runs pbuilder $1
    # use this script file to bootstrap the pbuilder inside the UML
    INSIDE_PBUILDER=$(tempfile)
    trap cleanup_function exit
    UML_CHROOT_MOUNTPOINT=/var/cache/pbuilder/pbuilder-mnt
    UML_CHROOT_BUILDRESULTMOUNTDIR=/var/cache/pbuilder/pbuilder-umlresult
#The following script is ran inside UML as soon as it is started.

    cat <<EOF > ${INSIDE_PBUILDER}
#! /bin/bash
${UML_DEBUGMODE}

mount -t proc /proc /proc
mount -t tmpfs /tmp /tmp
if ! mount -t ext2 /dev/ubd/1 ${UML_CHROOT_MOUNTPOINT}; then
  echo "E: Cannot mount /dev/ubd/1, is UML root image accessible as current user?"
  exit 1
fi
export LOGNAME="${LOGNAME}"
export HOME="${HOME}"
hostname "${UML_HOSTNAME}"
if [ "${UML_MOUNT_TMPFS}" = "yes" ]; then
  mount -t tmpfs ${UML_CHROOT_MOUNTPOINT}/tmp ${UML_CHROOT_MOUNTPOINT}/tmp
else
  # clean up tmp before playing with it.
  rm -rf ${UML_CHROOT_MOUNTPOINT}/tmp
  mkdir ${UML_CHROOT_MOUNTPOINT}/tmp
  chmod 1777 ${UML_CHROOT_MOUNTPOINT}/tmp
fi

cat <<IP > ${UML_CHROOT_MOUNTPOINT}/etc/network/interfaces
auto lo
iface lo inet loopback

# The first network card - this entry was created during the Debian installation
auto eth0
iface eth0 inet static
    address $UML_IP
    netmask $UML_NETMASK
    network $UML_NETWORK
    broadcast $UML_BROADCAST
    gateway $UML_GATEWAY

IP
cat <<SHELL > ${UML_CHROOT_MOUNTPOINT}/tmp/chrootshell
#! /bin/bash
${UML_DEBUGMODE}
# the shell executed inside chroot inside UML
    echo Starting network inside the chroot
    /etc/init.d/networking stop
    /etc/init.d/networking start
SHELL
chmod a+x ${UML_CHROOT_MOUNTPOINT}/tmp/chrootshell
chroot ${UML_CHROOT_MOUNTPOINT} /tmp/chrootshell

#some variables need to be set from outside values, possibly
export PATH=/sbin:/bin:/usr/sbin:/usr/bin
export TMPDIR=/tmp
unset EXTRAOPT
declare -a EXTRAOPT
if [ -n "${UML_DISTRIBUTION}" ]; then
  EXTRAOPT[0]="--distribution"
  EXTRAOPT[1]="${UML_DISTRIBUTION}"
fi
if [ -z "${UML_BUILDRESULT}" ]; then
  BUILDRESULT=
else
  echo " -> Mount build result dir outside UML [${UML_BUILDRESULT}] as [${UML_CHROOT_BUILDRESULTMOUNTDIR}]"
  BUILDRESULT=${UML_CHROOT_BUILDRESULTMOUNTDIR}
  mkdir -p ${UML_CHROOT_BUILDRESULTMOUNTDIR} || true
  mount -t hostfs none "${UML_CHROOT_BUILDRESULTMOUNTDIR}" -o "${UML_BUILDRESULT}"
fi
pbuilder "$1" ${UML_EXTRAOPT} \${EXTRAOPT[@]} --buildresult "\${BUILDRESULT}" --buildplace "${UML_CHROOT_MOUNTPOINT}" --internal-build-uml ${BUILDING_DSC_FILE} 
echo \$? > /proc/exitcode
EOF

    chmod a+x ${INSIDE_PBUILDER}

    if [ $( id -u ) = 0 ]; then
	echo "W: You are uid=0. Don't run UML as uid=0, it's not supported."
    fi

    echo Invoking: "linux mem=${UML_MEM} eth0=${MY_ETH0} con0=fd:0,fd:1 con=pty root=/dev/root rootflags=/ rootfstype=hostfs ubd1=${PBUILDER_COW}${PBUILDER_UML_IMAGE} devfs=mount init=${INSIDE_PBUILDER} rw"
    if linux mem=${UML_MEM} eth0=${MY_ETH0} con0=fd:0,fd:1 con=pty root=/dev/root rootflags=/ rootfstype=hostfs ubd1="${PBUILDER_COW}${PBUILDER_UML_IMAGE}" devfs=mount init=${INSIDE_PBUILDER} rw ; then
	UML_EXITCODE=$?
	echo " -> Successful exit from user-mode linux"
    else
	UML_EXITCODE=$?
	echo " -> Exit code ${UML_EXITCODE}"
    fi
}

PBUILDER_COW=""
OPERATION="$1"
UML_EXTRAOPT=
UML_DEBUGMODE=
shift;

while [ -n "$1" ] ; do
    case "$1" in
	--eth0)
	    MY_ETH0="$2";
	    shift; shift;
	    ;;
	--uml-ip)
	    UML_IP="$2";
	    shift; shift;
	    ;;
	--uml-netmask)
	    UML_NETMASK="$2";
	    shift; shift;
	    ;;
	--uml-network)
	    UML_NETWORK="$2";
	    shift; shift;
	    ;;
	--uml-broadcast)
	    UML_BROADCAST="$2";
	    shift; shift;
	    ;;
	--uml-gateway)
	    UML_GATEWAY="$2";
	    shift; shift;
	    ;;
	--uml-image)
	    PBUILDER_UML_IMAGE="$2";
	    shift; shift;
	    ;;
	--mount-tmpfs)
	    UML_MOUNT_TMPFS="$2";
	    shift; shift;
	    ;;
	--uml-mem)
	    UML_MEM="$2";
	    shift; shift;
	    ;;
	--uml-hostname)
	    UML_HOSTNAME="$2";
	    shift; shift;
	    ;;
	--uml-debugmode)
	    UML_DEBUGMODE="set -x";
	    shift;;
	--distribution)
	    UML_DISTRIBUTION="$2";
	    shift; shift;
	    ;;
	#things that can be passed through without options
	--override-config|--binary-arch)
	    UML_EXTRAOPT="${UML_EXTRAOPT} $1"
	    shift;;
	--configfile)
	    UML_EXTRAOPT="${UML_EXTRAOPT} $1 $2"
	    . "$2"
	    shift; shift;;
	#things that can be passed through with options
	--timeout|--http-proxy|--hookdir|--aptconfdir|--bindmounts)
	    UML_EXTRAOPT="${UML_EXTRAOPT} $1 $2"
	    shift; shift;;
	--buildresult)
	    # ignore buildresult
	    UML_BUILDRESULT=$(readlink -f "$2")
	    shift; shift;;
	--buildplace)
	    BUILDPLACE=$(readlink -f "$2")
	    shift; shift;; 
	--logfile)
	    exec > "$2";
	    exec 2>&1
	    PBUILDER_BUILD_LOGFILE=$(readlink -f "$2")
	    shift;shift;;
	--)
	    shift;
	    break;
	    ;;
	--*)
	    echo "Error: Unknown option [$1] was specified " >&2 
	    exit 1;
	    ;;
	*)
	    break;
	    ;;
    esac
done

BUILDING_DSC_FILE=$(readlink -f "$1") || true # ignore failure here
EXTRACLEANUP=
UML_EXITCODE=1
case "${OPERATION}" in
    build)
	usecow
	EXTRACLEANUP=cleancow
	operate_uml build 
	;;
    update)
	operate_uml update
	;;
    login)
	usecow
	EXTRACLEANUP=cleancow
	operate_uml login
	;;
    execute)
	usecow
	EXTRACLEANUP=cleancow
	operate_uml execute
	;;
    create)
	if [ -n "${UML_DISTRIBUTION}" ]; then
	    if [ ! -d ~/.pbuilder-user-mode-linux ]; then
		mkdir ~/.pbuilder-user-mode-linux
	    fi
	    sed "s/dist=.*/dist=${UML_DISTRIBUTION}/" /etc/rootstrap/rootstrap.conf >  ~/.pbuilder-user-mode-linux/rootstrap.conf
	    if [ -f rootstrap.conf ]; then
		sed "s/dist=.*/dist=${UML_DISTRIBUTION}/" rootstrap.conf >  ~/.pbuilder-user-mode-linux/rootstrap.conf
	    fi
	    cd ~/.pbuilder-user-mode-linux
	fi
	rootstrap -s 1000 ${PBUILDER_UML_IMAGE}
	operate_uml update
	;;
    *)
	echo "Error: Unknown option [${OPERATION}] was specified " >&2 
	;;
esac

exit ${UML_EXITCODE}