~ogra/project-rootstock-ng/trunk

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
#!/bin/sh
#
#  Copyright (c) 2014 Canonical
#
#  Author: Oliver Grawert <ogra@canonical.com>
#
#  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

# needs 6.5GB free diskspace for building
# TODO:
#       - allow single deb to be injected

export LC_ALL=C

SUITE="vivid"
ARCH="armhf"

NOW=$(date +%Y%m%d)
OUTDIR="./out-$NOW"
TAILPID=
KEEP=
DO_PPA=

PREINSTALLED="true"
IMAGEFORMAT="plain"
PROJECT="ubuntu-touch"

[ ! $(which qemu-debootstrap) ] && echo "please install the qemu-user-static package" && exit 1

cleanup()
{
	[ -n "$TAILPID" ] && kill -9 $TAILPID
	if [ -e "$CHROOT" ]; then
		umount $CHROOT/sys >/dev/null 2>&1 || true
		umount $CHROOT/proc >/dev/null 2>&1 || true
		if [ -e "$CHROOT/build.log" ]; then
			LOG=./build-$NOW-$ARCH.log
			[ -e "$OUTDIR" ] && LOG=$OUTDIR/build-$NOW-$ARCH.log
			cp $CHROOT/build.log $LOG
		fi
		[ -z "$KEEP" ] && rm -rf $CHROOT >/dev/null 2>&1 || true
	fi
}

trap cleanup 0 1 2 3 9 15

usage()
{
	echo "usage: $(basename $0)\n
	-h|--help		this page
	-a|--arch (i386|armhf)	pick an arch, default: armhf
	-m|--mirror (url)	url to local mirror
	-o|--outdir (dir)	output dir, default: ./out-\$timestamp
	-k|--keep-chroot	keep the build environment around for inspection
	-p|--ppa (ppa url)	a ppa url in the form of ppa:user/ppaname
	-s|--series		Ubuntu distro series
	-t|--type		project type (ubuntu-touch|ubuntu-core)\n"
#	-i|--inject-package (path to deb)	inject a single .deb in the build
	exit 1
}

SUDOARGS="$@"

while [ $# -gt 0 ]; do
	case "$1" in
		-h|--help)
			usage
			;;
		-a|--arch)
			[ -n "$2" ] && ARCH=$2 shift || usage
			;;
		-m|--mirror)
			[ -n "$2" ] && MIRROR=$2 shift || usage
			;;
		-o|--outdir)
			[ -n "$2" ] && OUTDIR=$2 shift || usage
			;;
		-k|--keep-chroot)
			KEEP=1
			;;
		-p|--ppa)
			[ -n "$2" ] && PPAOPT=$2 shift || usage
			MYPPA=$(echo $PPAOPT|sed 's/^.*://')
			PPA_USER=${MYPPA%%/*}
			PPA_NAME=${MYPPA##*/}
			DO_PPA=1
			;;
		-s|--series)
			[ -n "$2" ] && SUITE=$2 shift || usage
			;;
		-t|--type)
			[ -n "$2" ] && PROJECT=$2 shift || usage
			;;
		*)
			usage
			;;
	esac
	shift
done

case $ARCH in
	i386|amd64)
		MIRROR="${MIRROR:-http://archive.ubuntu.com/ubuntu}"
		BOOTSTRAP_BIN="debootstrap --arch $ARCH"
		LB_BOOTSTRAP_QEMU_ARCHITECTURES=""
		LB_BOOTSTRAP_QEMU_STATIC=""
		;;
	armhf)
		MIRROR="${MIRROR:-http://ports.ubuntu.com/ubuntu-ports}"
		BOOTSTRAP_BIN="qemu-debootstrap --arch armhf --variant=minbase"
		LB_BOOTSTRAP_QEMU_ARCHITECTURES="armhf"
		LB_BOOTSTRAP_QEMU_STATIC="/usr/bin/qemu-arm-static"
		;;
	*)
		usage
		;;
esac

case $PROJECT in
	ubuntu-core)
		SUBPROJECT=system-image
		EXTRA_PPAS=snappy-dev/image
		;;
	ubuntu-touch)
		[ "SUITE" = "vivid" ] && EXTRA_PPAS="ci-train-ppa-service/stable-phone-overlay:1001"
		;;
esac

BUILDOPTS="SUBPROJECT=${SUBPROJECT} \
	EXTRA_PPAS=${EXTRA_PPAS} \
	PREINSTALLED=${PREINSTALLED} \
	LB_BOOTSTRAP_QEMU_ARCHITECTURES=${LB_BOOTSTRAP_QEMU_ARCHITECTURES} \
	LB_BOOTSTRAP_QEMU_STATIC=${LB_BOOTSTRAP_QEMU_STATIC} \
	IMAGEFORMAT=${IMAGEFORMAT} \
	MIRROR=${MIRROR} \
	NOW=${NOW} \
	SUITE=${SUITE} \
	PROJECT=${PROJECT} \
	ARCH=${ARCH}"

[ $(id -u) -ne 0 ] && exec sudo $0 $SUDOARGS

CHROOT=$(mktemp -d /tmp/rootstock.XXXXX)

touch $CHROOT/build.log
tail -f $CHROOT/build.log &
TAILPID=$!

exec 3>&1 4>&2 >$CHROOT/build.log 2>&1

do_chroot()
{
	ROOT="$1"
	CMD="$2"
	chroot $ROOT mount -t proc proc /proc
	chroot $ROOT mount -t sysfs sys /sys
	chroot $ROOT $CMD
	chroot $ROOT umount /sys
	chroot $ROOT umount /proc
}

set_up_ppa()
{
	PPA_API="https://launchpad.net/api/1.0/~$PPA_USER/+archive/$PPA_NAME"
	PPA_FINGERPRINT="$(wget -O- -q $PPA_API|tr ',' '\n'|\
		grep signing_key_fingerprint|\
		sed s/\"//g|tail -c9)"
	KEY_URL="http://keyserver.ubuntu.com/pks/lookup?op=get&search=0x$PPA_FINGERPRINT"
	KEY=$(wget -O- -q $KEY_URL|sed -e '/-----/,$!d'|sed '/^<.*/d')
	PPA_CONFDIR="$CHROOT/usr/share/livecd-rootfs/live-build/ubuntu-touch/archives"
	mkdir -p "$PPA_CONFDIR"
	echo "$KEY" >"$PPA_CONFDIR/$PPA_USER-$PPA_NAME.key.chroot"
	PPA_SOURCESLIST="deb http://ppa.launchpad.net/$PPA_USER/$PPA_NAME/ubuntu $SUITE main"
	echo "$PPA_SOURCESLIST" >"$PPA_CONFDIR/$PPA_USER-$PPA_NAME.list.chroot"
}

build_chroot()
{
	$BOOTSTRAP_BIN $SUITE $CHROOT $MIRROR
	echo "deb $MIRROR $SUITE main universe" >$CHROOT/etc/apt/sources.list
	echo "deb $MIRROR $SUITE-updates main universe" >>$CHROOT/etc/apt/sources.list
	do_chroot $CHROOT "apt-get -y update"
	do_chroot $CHROOT "apt-get -y install livecd-rootfs"
	mkdir -p $CHROOT/build
	chroot $CHROOT sh -c "cd /build && rm -rf auto && mkdir -p auto && \
		for f in config build clean; do ln -s /usr/share/livecd-rootfs/live-build/auto/\$f auto/; done"
	# hack for click package location
	sed -i 's,archive-team.internal,people.canonical.com/~ubuntu-archive,' \
		$CHROOT/usr/share/livecd-rootfs/live-build/ubuntu-touch/hooks/60-install-click.chroot
}

build_touch()
{
	chroot $CHROOT sh -c "cd /build && lb clean --purge"
	chroot $CHROOT sh -c "cd /build && $BUILDOPTS lb config"
	chroot $CHROOT sh -c "cd /build && $BUILDOPTS lb build"
}

copy_artefacts()
{
	mkdir -p $OUTDIR
	for file in $(ls $CHROOT/build/livecd.*|grep -v bootimg); do
		cp $file $OUTDIR/$(echo $file|sed -e s/^.*livecd.// -e s/rootfs/rootfs-$ARCH/)
	done
}

build_chroot
[ -n "$DO_PPA" ] && set_up_ppa
build_touch
copy_artefacts