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