blob: b6b665a6b56d2036d025334e6140f2192e8222f6 [file] [log] [blame]
Bill Richardsonc09b94f2010-03-15 11:40:30 -07001#!/bin/bash
2
3# Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7# Script to build a bootable keyfob-based chromeos system image from within
8# a chromiumos setup. This assumes that all needed packages have been built into
9# the given target's root with binary packages turned on. This script will
10# build the Chrome OS image using only pre-built binary packages.
11
12# Load common constants. This should be the first executable line.
13# The path to common.sh should be relative to your script's location.
14. "$(dirname "$0")/common.sh"
15
16# Script must be run inside the chroot.
17assert_inside_chroot
18
19get_default_board
20
21# Flags.
22DEFINE_string board "$DEFAULT_BOARD" \
23 "The board to build an image for."
24DEFINE_string build_root "/build" \
25 "The root location for board sysroots."
26DEFINE_integer build_attempt 1 \
27 "The build attempt for this image build."
28DEFINE_string output_root "${DEFAULT_BUILD_ROOT}/images" \
29 "Directory in which to place image result directories (named by version)"
30DEFINE_boolean replace $FLAGS_FALSE \
31 "Overwrite existing output, if any."
32DEFINE_boolean withdev $FLAGS_TRUE \
33 "Include useful developer friendly utilities in the image."
34DEFINE_boolean installmask $FLAGS_TRUE \
35 "Use INSTALL_MASK to shrink the resulting image."
36DEFINE_integer jobs -1 \
37 "How many packages to build in parallel at maximum."
38DEFINE_boolean statefuldev $FLAGS_FALSE \
39 "Install development packages on stateful partition -- still experimental"
Chris Sosa77a29632010-03-15 14:12:15 -070040DEFINE_boolean withtest $FLAGS_FALSE \
Chris Sosa4bccd3c2010-03-15 13:23:14 -070041 "Include packages required for testing and prepare image for testing"
Bill Richardsonc09b94f2010-03-15 11:40:30 -070042
43# Parse command line.
44FLAGS "$@" || exit 1
45eval set -- "${FLAGS_ARGV}"
46
47# Only now can we die on error. shflags functions leak non-zero error codes,
48# so will die prematurely if 'set -e' is specified before now.
49set -e
50
51if [ -z "$FLAGS_board" ] ; then
52 error "--board is required."
53 exit 1
54fi
55
56# Sanity check: statefuldev cannot be true if withdev is false.
57if [ $FLAGS_statefuldev -eq $FLAGS_TRUE ] &&
58 [ $FLAGS_withdev -eq $FLAGS_FALSE ] ; then
59 echo "ERROR: statefuldev flag cannot be set to true without withdev"
60 exit 1
61fi
62
63# Determine build version.
64. "${SCRIPTS_DIR}/chromeos_version.sh"
65
66# Use canonical path since some tools (e.g. mount) do not like symlinks.
67# Append build attempt to output directory.
68IMAGE_SUBDIR="${CHROMEOS_VERSION_STRING}-a${FLAGS_build_attempt}"
69OUTPUT_DIR="${FLAGS_output_root}/${FLAGS_board}/${IMAGE_SUBDIR}"
70ROOT_FS_DIR="${OUTPUT_DIR}/rootfs"
71ROOT_FS_IMG="${OUTPUT_DIR}/rootfs.image"
Bill Richardson4364a2e2010-03-30 14:17:34 -070072OUTPUT_IMG="${OUTPUT_DIR}/chromiumos_image.bin"
Bill Richardsonc09b94f2010-03-15 11:40:30 -070073
74BOARD="${FLAGS_board}"
75BOARD_ROOT="${FLAGS_build_root}/${BOARD}"
76
77LOOP_DEV=
78
79# What cross-build are we targeting?
80. "${BOARD_ROOT}/etc/make.conf.board_setup"
81LIBC_VERSION=${LIBC_VERSION:-"2.10.1-r1"}
82
83# Figure out ARCH from the given toolchain.
84# TODO: Move to common.sh as a function after scripts are switched over.
85TC_ARCH=$(echo "$CHOST" | awk -F'-' '{ print $1 }')
86case "$TC_ARCH" in
87 arm*)
88 ARCH="arm"
89 ;;
90 *86)
91 ARCH="x86"
92 ;;
93 *)
94 error "Unable to determine ARCH from toolchain: $CHOST"
95 exit 1
96esac
97
98# Hack to fix bug where x86_64 CHOST line gets incorrectly added.
99# ToDo(msb): remove this hack.
100PACKAGES_FILE="${BOARD_ROOT}/packages/Packages"
101sudo sed -e "s/CHOST: x86_64-pc-linux-gnu//" -i "${PACKAGES_FILE}"
102
103# Handle existing directory.
104if [[ -e "$OUTPUT_DIR" ]]; then
105 if [[ $FLAGS_replace -eq $FLAGS_TRUE ]]; then
106 sudo rm -rf "$OUTPUT_DIR"
107 else
108 echo "Directory $OUTPUT_DIR already exists."
109 echo "Use --build_attempt option to specify an unused attempt."
110 echo "Or use --replace if you want to overwrite this directory."
111 exit 1
112 fi
113fi
114
115# Create the output directory.
116mkdir -p "$OUTPUT_DIR"
117
118cleanup_rootfs_loop() {
119 sudo umount "$LOOP_DEV"
120 sleep 1 # in case $LOOP_DEV is in use (TODO: Try umount -l?).
121 sudo losetup -d "$LOOP_DEV"
122}
123
124cleanup_stateful_fs_loop() {
125 sudo umount "$STATEFUL_LOOP_DEV"
126 sleep 1 # follows from cleanup_root_fs_loop.
127 sudo losetup -d "$STATEFUL_LOOP_DEV"
128}
129
130cleanup() {
131 # Disable die on error.
132 set +e
133
134 # Unmount stateful partition from usr/local if bound.
135 if [ -s "$ROOT_FS_DIR/usr/local/bin" ] ; then
136 sudo umount $ROOT_FS_DIR/usr/local
137 fi
138
139 if [[ -n "$STATEFUL_LOOP_DEV" ]]; then
140 cleanup_stateful_fs_loop
141 fi
142
143 if [[ -n "$LOOP_DEV" ]]; then
144 cleanup_rootfs_loop
145 fi
146
147 # Turn die on error back on.
148 set -e
149}
150
151trap cleanup EXIT
152
153mkdir -p "$ROOT_FS_DIR"
154
155# Create and format the root file system.
156
157# Check for loop device before creating image.
158LOOP_DEV=$(sudo losetup -f)
159if [ -z "$LOOP_DEV" ] ; then
160 echo "No free loop device. Free up a loop device or reboot. exiting. "
161 exit 1
162fi
163
164# Create root file system disk image to fit on a 1GB memory stick.
165# 1 GB in hard-drive-manufacturer-speak is 10^9, not 2^30. 950MB < 10^9 bytes.
166ROOT_SIZE_BYTES=$((1024 * 1024 * 640))
167dd if=/dev/zero of="$ROOT_FS_IMG" bs=1 count=1 seek=$((ROOT_SIZE_BYTES - 1))
168sudo losetup "$LOOP_DEV" "$ROOT_FS_IMG"
169sudo mkfs.ext3 "$LOOP_DEV"
170
171# Tune and mount rootfs.
172UUID=$(uuidgen)
173DISK_LABEL="C-KEYFOB"
174sudo tune2fs -L "$DISK_LABEL" -U "$UUID" -c 0 -i 0 "$LOOP_DEV"
175sudo mount "$LOOP_DEV" "$ROOT_FS_DIR"
176
177# Create stateful partition of the same size as the rootfs.
178STATEFUL_IMG="$OUTPUT_DIR/stateful_partition.image"
179STATEFUL_DIR="$OUTPUT_DIR/stateful_partition"
180STATEFUL_LOOP_DEV=$(sudo losetup -f)
181if [ -z "$STATEFUL_LOOP_DEV" ] ; then
182 echo "No free loop device. Free up a loop device or reboot. exiting. "
183 exit 1
184fi
185dd if=/dev/zero of="$STATEFUL_IMG" bs=1 count=1 seek=$((ROOT_SIZE_BYTES - 1))
186sudo losetup "$STATEFUL_LOOP_DEV" "$STATEFUL_IMG"
187sudo mkfs.ext3 "$STATEFUL_LOOP_DEV"
188sudo tune2fs -L "C-STATE" -U "$UUID" -c 0 -i 0 \
189 "$STATEFUL_LOOP_DEV"
190
191# Mount the stateful partition.
192mkdir -p "$STATEFUL_DIR"
193sudo mount "$STATEFUL_LOOP_DEV" "$STATEFUL_DIR"
194
195# Turn root file system into bootable image.
196if [[ "$ARCH" = "x86" ]]; then
197 # Setup extlinux configuration.
198 # TODO: For some reason the /dev/disk/by-uuid is not being generated by udev
199 # in the initramfs. When we figure that out, switch to root=UUID=$UUID.
200 sudo mkdir -p "$ROOT_FS_DIR"/boot
201 # TODO(adlr): use initramfs for booting.
202 cat <<EOF | sudo dd of="$ROOT_FS_DIR"/boot/extlinux.conf
203DEFAULT chromeos-usb
204PROMPT 0
205TIMEOUT 0
206
207label chromeos-usb
208 menu label chromeos-usb
209 kernel vmlinuz
210 append quiet console=tty2 init=/sbin/init boot=local rootwait root=/dev/sdb3 ro noresume noswap i915.modeset=1 loglevel=1
211
212label chromeos-hd
213 menu label chromeos-hd
214 kernel vmlinuz
215 append quiet console=tty2 init=/sbin/init boot=local rootwait root=HDROOT ro noresume noswap i915.modeset=1 loglevel=1
216EOF
217
218 # Make partition bootable and label it.
219 sudo extlinux -z --install "${ROOT_FS_DIR}/boot"
220fi
221
222# -- Install packages into the root file system --
223
224# We need to install libc manually from the cross toolchain.
225# TODO: Improve this? We only want libc and not the whole toolchain.
226PKGDIR="/var/lib/portage/pkgs/cross/"
227sudo tar jxvpf \
228 "${PKGDIR}/${CHOST}/cross-${CHOST}"/glibc-${LIBC_VERSION}.tbz2 \
229 -C "$ROOT_FS_DIR" --strip-components=3 \
230 --exclude=usr/include --exclude=sys-include --exclude=*.a --exclude=*.o
231
232# We need to install libstdc++ manually from the cross toolchain.
233# TODO: Figure out a better way of doing this?
234sudo cp -a "${BOARD_ROOT}"/lib/libgcc_s.so* "${ROOT_FS_DIR}/lib"
235sudo cp -a "${BOARD_ROOT}"/usr/lib/libstdc++.so* "${ROOT_FS_DIR}/usr/lib"
236
237INSTALL_MASK=""
238if [[ $FLAGS_installmask -eq $FLAGS_TRUE ]] ; then
239 INSTALL_MASK="/usr/include/ /usr/man /usr/share/man /usr/share/doc /usr/share/gtk-doc /usr/share/gtk-2.0 /usr/lib/gtk-2.0/include /usr/share/info /usr/share/aclocal /usr/lib/gcc /usr/lib/pkgconfig /usr/share/pkgconfig /usr/share/gettext /usr/share/readline /etc/runlevels /usr/share/openrc /lib/rc *.a *.la"
240fi
241
242if [[ $FLAGS_jobs -ne -1 ]]; then
243 EMERGE_JOBS="--jobs=$FLAGS_jobs"
244fi
245
246if [ $FLAGS_statefuldev -eq $FLAGS_TRUE ] ; then
247 # Creating stateful partition.
248 echo "Setting up symlinks for stateful partition install"
249 DEV_IMAGE_ROOT="$STATEFUL_DIR/dev_image"
250 sudo mkdir -p "$DEV_IMAGE_ROOT/usr"
251
252 # Setup symlinks in stateful partition.
253 for path in bin include lib libexec sbin share; do
254 sudo mkdir "$DEV_IMAGE_ROOT/$path"
255 sudo ln -s "$DEV_IMAGE_ROOT/$path" "$DEV_IMAGE_ROOT/usr/$path"
256 done
257
258 # Setup symlinks that don't conform to above model.
259 sudo ln -s "$DEV_IMAGE_ROOT/lib" "$DEV_IMAGE_ROOT/usr/lib64"
260 sudo ln -s "$DEV_IMAGE_ROOT" "$DEV_IMAGE_ROOT/usr/local"
261
262 # Bind to rootfs.
263 echo "Binding stateful partition to rootfs's /usr/local"
264 sudo mkdir -p "$ROOT_FS_DIR/usr/local"
265 sudo mount -n --bind "$DEV_IMAGE_ROOT" "$ROOT_FS_DIR/usr/local"
266fi
267
268# We "emerge --root=$ROOT_FS_DIR --root-deps=rdeps --usepkgonly" all of the
269# runtime packages for chrome os. This builds up a chrome os image from binary
270# packages with runtime dependencies only. We use INSTALL_MASK to trim the
271# image size as much as possible.
272sudo INSTALL_MASK="$INSTALL_MASK" emerge-${BOARD} \
273 --root="$ROOT_FS_DIR" --root-deps=rdeps \
274 --usepkgonly chromeos $EMERGE_JOBS
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700275
Chris Sosa4bccd3c2010-03-15 13:23:14 -0700276# Determine the root dir for development packages.
277ROOT_DEV_DIR="$ROOT_FS_DIR"
278[ $FLAGS_statefuldev -eq $FLAGS_TRUE ] && ROOT_DEV_DIR="$ROOT_FS_DIR/usr/local"
279
280# Install development packages.
281if [[ $FLAGS_withdev -eq $FLAGS_TRUE ]] ; then
282 sudo INSTALL_MASK="$INSTALL_MASK" emerge-${BOARD} \
283 --root="$ROOT_DEV_DIR" --root-deps=rdeps \
284 --usepkgonly chromeos-dev $EMERGE_JOBS
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700285
286 # The ldd tool is a useful shell script but lives in glibc; just copy it.
Chris Sosa4bccd3c2010-03-15 13:23:14 -0700287 sudo cp -a "$(which ldd)" "${ROOT_DEV_DIR}/usr/bin"
288fi
289
290# Install packages required for testing.
291if [[ $FLAGS_withtest -eq $FLAGS_TRUE ]] ; then
292 sudo INSTALL_MASK="$INSTALL_MASK" emerge-${BOARD} \
293 --root="$ROOT_DEV_DIR" --root-deps=rdeps \
294 --usepkgonly chromeos-test $EMERGE_JOBS
295fi
296
297# Clean up links setup for stateful install of extra packages.
298if [ $FLAGS_statefuldev -eq $FLAGS_TRUE ] ; then
299 # Fix symlinks so they work on live system.
300 for path in bin include lib libexec sbin share; do
301 sudo unlink $DEV_IMAGE_ROOT/usr/$path
302 sudo ln -s /usr/local/$path $DEV_IMAGE_ROOT/usr/$path
303 done
304
305 # Fix exceptions.
306 sudo unlink "$DEV_IMAGE_ROOT/usr/lib64"
307 sudo unlink "$DEV_IMAGE_ROOT/usr/local"
308
309 sudo ln -s "/usr/local/lib" "$DEV_IMAGE_ROOT/usr/lib64"
Chris Sosacbac6482010-03-15 17:52:47 -0700310 sudo ln -s "/usr/local" "$DEV_IMAGE_ROOT/usr/local"
311
312 #TODO(sosa@chromium.org) - /usr/bin/xterm symlink not created in stateful.
313 sudo ln -sf "/usr/local/bin/aterm" "/usr/bin/xterm"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700314fi
315
316# Perform any customizations on the root file system that are needed.
317WITH_DEV=""
318if [[ $FLAGS_withdev -eq $FLAGS_TRUE ]]; then
319 WITH_DEV="--withdev"
320fi
321
Bill Richardson4364a2e2010-03-30 14:17:34 -0700322# Extract the kernel from the root filesystem for use by the GPT image. Legacy
323# BIOS will use the kernel in the rootfs (via syslinux), ChromeOS BIOS will use
324# the kernel partition.
Bill Richardson6dcea162010-03-31 19:20:24 -0700325sudo cp -f "${ROOT_FS_DIR}/boot/vmlinuz" "${OUTPUT_DIR}/vmlinuz.image"
Bill Richardson4364a2e2010-03-30 14:17:34 -0700326
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700327#TODO(sosa@chromium.org) - Does it make sense to leave /usr/local bound here?
328"${SCRIPTS_DIR}/customize_rootfs" \
329 --root="$ROOT_FS_DIR" \
330 --target="$ARCH" \
331 --board="$BOARD" \
332 $WITH_DEV
333
334# Check that the image has been correctly created.
335"${SCRIPTS_DIR}/test_image" \
336 --root="$ROOT_FS_DIR" \
337 --target="$ARCH"
338
339# Set dev_mode flag and update library cache.
340if [ $FLAGS_statefuldev -eq $FLAGS_TRUE ] ; then
341 sudo touch "$ROOT_FS_DIR/root/.dev_mode"
342 sudo /sbin/ldconfig -r "$ROOT_FS_DIR"
343fi
344
345# Only bound if installing dev to stateful.
346if [ $FLAGS_statefuldev -eq $FLAGS_TRUE ] ; then
347 sudo umount "$ROOT_FS_DIR/usr/local"
348fi
349
350# Cleanup loop devices.
351cleanup_stateful_fs_loop
352cleanup_rootfs_loop
353
Bill Richardson4364a2e2010-03-30 14:17:34 -0700354# Create the GPT-formatted image
355${SCRIPTS_DIR}/build_gpt.sh \
356 --arch=${ARCH} --board=${FLAGS_board} --board_root=${BOARD_ROOT} \
Bill Richardson6dcea162010-03-31 19:20:24 -0700357 "${OUTPUT_DIR}" "${OUTPUT_IMG}"
Bill Richardson4364a2e2010-03-30 14:17:34 -0700358
Bill Richardson6dcea162010-03-31 19:20:24 -0700359# Clean up temporary files.
360rm -f "${ROOT_FS_IMG}" "${STATEFUL_IMG}" "${OUTPUT_DIR}/vmlinuz.image"
361rmdir "${ROOT_FS_DIR}" "${STATEFUL_DIR}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700362
363OUTSIDE_OUTPUT_DIR="../build/images/${FLAGS_board}/${IMAGE_SUBDIR}"
364echo "Done. Image created in ${OUTPUT_DIR}"
365echo "To copy to USB keyfob, OUTSIDE the chroot, do something like:"
366echo " ./image_to_usb.sh --from=${OUTSIDE_OUTPUT_DIR} --to=/dev/sdb"
367echo "To convert to VMWare image, OUTSIDE the chroot, do something like:"
368echo " ./image_to_vmware.sh --from=${OUTSIDE_OUTPUT_DIR}"
369echo "from the scripts directory where you entered the chroot."
370
371trap - EXIT