blob: a0e7e4aa02da25bd5b1d141221bea3e19a1ea8fe [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"
72MBR_IMG="${OUTPUT_DIR}/mbr.image"
73OUTPUT_IMG="${OUTPUT_DIR}/usb.img"
74
75BOARD="${FLAGS_board}"
76BOARD_ROOT="${FLAGS_build_root}/${BOARD}"
77
78LOOP_DEV=
79
80# What cross-build are we targeting?
81. "${BOARD_ROOT}/etc/make.conf.board_setup"
82LIBC_VERSION=${LIBC_VERSION:-"2.10.1-r1"}
83
84# Figure out ARCH from the given toolchain.
85# TODO: Move to common.sh as a function after scripts are switched over.
86TC_ARCH=$(echo "$CHOST" | awk -F'-' '{ print $1 }')
87case "$TC_ARCH" in
88 arm*)
89 ARCH="arm"
90 ;;
91 *86)
92 ARCH="x86"
93 ;;
94 *)
95 error "Unable to determine ARCH from toolchain: $CHOST"
96 exit 1
97esac
98
99# Hack to fix bug where x86_64 CHOST line gets incorrectly added.
100# ToDo(msb): remove this hack.
101PACKAGES_FILE="${BOARD_ROOT}/packages/Packages"
102sudo sed -e "s/CHOST: x86_64-pc-linux-gnu//" -i "${PACKAGES_FILE}"
103
104# Handle existing directory.
105if [[ -e "$OUTPUT_DIR" ]]; then
106 if [[ $FLAGS_replace -eq $FLAGS_TRUE ]]; then
107 sudo rm -rf "$OUTPUT_DIR"
108 else
109 echo "Directory $OUTPUT_DIR already exists."
110 echo "Use --build_attempt option to specify an unused attempt."
111 echo "Or use --replace if you want to overwrite this directory."
112 exit 1
113 fi
114fi
115
116# Create the output directory.
117mkdir -p "$OUTPUT_DIR"
118
119cleanup_rootfs_loop() {
120 sudo umount "$LOOP_DEV"
121 sleep 1 # in case $LOOP_DEV is in use (TODO: Try umount -l?).
122 sudo losetup -d "$LOOP_DEV"
123}
124
125cleanup_stateful_fs_loop() {
126 sudo umount "$STATEFUL_LOOP_DEV"
127 sleep 1 # follows from cleanup_root_fs_loop.
128 sudo losetup -d "$STATEFUL_LOOP_DEV"
129}
130
131cleanup() {
132 # Disable die on error.
133 set +e
134
135 # Unmount stateful partition from usr/local if bound.
136 if [ -s "$ROOT_FS_DIR/usr/local/bin" ] ; then
137 sudo umount $ROOT_FS_DIR/usr/local
138 fi
139
140 if [[ -n "$STATEFUL_LOOP_DEV" ]]; then
141 cleanup_stateful_fs_loop
142 fi
143
144 if [[ -n "$LOOP_DEV" ]]; then
145 cleanup_rootfs_loop
146 fi
147
148 # Turn die on error back on.
149 set -e
150}
151
152trap cleanup EXIT
153
154mkdir -p "$ROOT_FS_DIR"
155
156# Create and format the root file system.
157
158# Check for loop device before creating image.
159LOOP_DEV=$(sudo losetup -f)
160if [ -z "$LOOP_DEV" ] ; then
161 echo "No free loop device. Free up a loop device or reboot. exiting. "
162 exit 1
163fi
164
165# Create root file system disk image to fit on a 1GB memory stick.
166# 1 GB in hard-drive-manufacturer-speak is 10^9, not 2^30. 950MB < 10^9 bytes.
167ROOT_SIZE_BYTES=$((1024 * 1024 * 640))
168dd if=/dev/zero of="$ROOT_FS_IMG" bs=1 count=1 seek=$((ROOT_SIZE_BYTES - 1))
169sudo losetup "$LOOP_DEV" "$ROOT_FS_IMG"
170sudo mkfs.ext3 "$LOOP_DEV"
171
172# Tune and mount rootfs.
173UUID=$(uuidgen)
174DISK_LABEL="C-KEYFOB"
175sudo tune2fs -L "$DISK_LABEL" -U "$UUID" -c 0 -i 0 "$LOOP_DEV"
176sudo mount "$LOOP_DEV" "$ROOT_FS_DIR"
177
178# Create stateful partition of the same size as the rootfs.
179STATEFUL_IMG="$OUTPUT_DIR/stateful_partition.image"
180STATEFUL_DIR="$OUTPUT_DIR/stateful_partition"
181STATEFUL_LOOP_DEV=$(sudo losetup -f)
182if [ -z "$STATEFUL_LOOP_DEV" ] ; then
183 echo "No free loop device. Free up a loop device or reboot. exiting. "
184 exit 1
185fi
186dd if=/dev/zero of="$STATEFUL_IMG" bs=1 count=1 seek=$((ROOT_SIZE_BYTES - 1))
187sudo losetup "$STATEFUL_LOOP_DEV" "$STATEFUL_IMG"
188sudo mkfs.ext3 "$STATEFUL_LOOP_DEV"
189sudo tune2fs -L "C-STATE" -U "$UUID" -c 0 -i 0 \
190 "$STATEFUL_LOOP_DEV"
191
192# Mount the stateful partition.
193mkdir -p "$STATEFUL_DIR"
194sudo mount "$STATEFUL_LOOP_DEV" "$STATEFUL_DIR"
195
196# Turn root file system into bootable image.
197if [[ "$ARCH" = "x86" ]]; then
198 # Setup extlinux configuration.
199 # TODO: For some reason the /dev/disk/by-uuid is not being generated by udev
200 # in the initramfs. When we figure that out, switch to root=UUID=$UUID.
201 sudo mkdir -p "$ROOT_FS_DIR"/boot
202 # TODO(adlr): use initramfs for booting.
203 cat <<EOF | sudo dd of="$ROOT_FS_DIR"/boot/extlinux.conf
204DEFAULT chromeos-usb
205PROMPT 0
206TIMEOUT 0
207
208label chromeos-usb
209 menu label chromeos-usb
210 kernel vmlinuz
211 append quiet console=tty2 init=/sbin/init boot=local rootwait root=/dev/sdb3 ro noresume noswap i915.modeset=1 loglevel=1
212
213label chromeos-hd
214 menu label chromeos-hd
215 kernel vmlinuz
216 append quiet console=tty2 init=/sbin/init boot=local rootwait root=HDROOT ro noresume noswap i915.modeset=1 loglevel=1
217EOF
218
219 # Make partition bootable and label it.
220 sudo extlinux -z --install "${ROOT_FS_DIR}/boot"
221fi
222
223# -- Install packages into the root file system --
224
225# We need to install libc manually from the cross toolchain.
226# TODO: Improve this? We only want libc and not the whole toolchain.
227PKGDIR="/var/lib/portage/pkgs/cross/"
228sudo tar jxvpf \
229 "${PKGDIR}/${CHOST}/cross-${CHOST}"/glibc-${LIBC_VERSION}.tbz2 \
230 -C "$ROOT_FS_DIR" --strip-components=3 \
231 --exclude=usr/include --exclude=sys-include --exclude=*.a --exclude=*.o
232
233# We need to install libstdc++ manually from the cross toolchain.
234# TODO: Figure out a better way of doing this?
235sudo cp -a "${BOARD_ROOT}"/lib/libgcc_s.so* "${ROOT_FS_DIR}/lib"
236sudo cp -a "${BOARD_ROOT}"/usr/lib/libstdc++.so* "${ROOT_FS_DIR}/usr/lib"
237
238INSTALL_MASK=""
239if [[ $FLAGS_installmask -eq $FLAGS_TRUE ]] ; then
240 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"
241fi
242
243if [[ $FLAGS_jobs -ne -1 ]]; then
244 EMERGE_JOBS="--jobs=$FLAGS_jobs"
245fi
246
247if [ $FLAGS_statefuldev -eq $FLAGS_TRUE ] ; then
248 # Creating stateful partition.
249 echo "Setting up symlinks for stateful partition install"
250 DEV_IMAGE_ROOT="$STATEFUL_DIR/dev_image"
251 sudo mkdir -p "$DEV_IMAGE_ROOT/usr"
252
253 # Setup symlinks in stateful partition.
254 for path in bin include lib libexec sbin share; do
255 sudo mkdir "$DEV_IMAGE_ROOT/$path"
256 sudo ln -s "$DEV_IMAGE_ROOT/$path" "$DEV_IMAGE_ROOT/usr/$path"
257 done
258
259 # Setup symlinks that don't conform to above model.
260 sudo ln -s "$DEV_IMAGE_ROOT/lib" "$DEV_IMAGE_ROOT/usr/lib64"
261 sudo ln -s "$DEV_IMAGE_ROOT" "$DEV_IMAGE_ROOT/usr/local"
262
263 # Bind to rootfs.
264 echo "Binding stateful partition to rootfs's /usr/local"
265 sudo mkdir -p "$ROOT_FS_DIR/usr/local"
266 sudo mount -n --bind "$DEV_IMAGE_ROOT" "$ROOT_FS_DIR/usr/local"
267fi
268
269# We "emerge --root=$ROOT_FS_DIR --root-deps=rdeps --usepkgonly" all of the
270# runtime packages for chrome os. This builds up a chrome os image from binary
271# packages with runtime dependencies only. We use INSTALL_MASK to trim the
272# image size as much as possible.
273sudo INSTALL_MASK="$INSTALL_MASK" emerge-${BOARD} \
274 --root="$ROOT_FS_DIR" --root-deps=rdeps \
275 --usepkgonly chromeos $EMERGE_JOBS
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700276
Chris Sosa4bccd3c2010-03-15 13:23:14 -0700277# Determine the root dir for development packages.
278ROOT_DEV_DIR="$ROOT_FS_DIR"
279[ $FLAGS_statefuldev -eq $FLAGS_TRUE ] && ROOT_DEV_DIR="$ROOT_FS_DIR/usr/local"
280
281# Install development packages.
282if [[ $FLAGS_withdev -eq $FLAGS_TRUE ]] ; then
283 sudo INSTALL_MASK="$INSTALL_MASK" emerge-${BOARD} \
284 --root="$ROOT_DEV_DIR" --root-deps=rdeps \
285 --usepkgonly chromeos-dev $EMERGE_JOBS
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700286
287 # The ldd tool is a useful shell script but lives in glibc; just copy it.
Chris Sosa4bccd3c2010-03-15 13:23:14 -0700288 sudo cp -a "$(which ldd)" "${ROOT_DEV_DIR}/usr/bin"
289fi
290
291# Install packages required for testing.
292if [[ $FLAGS_withtest -eq $FLAGS_TRUE ]] ; then
293 sudo INSTALL_MASK="$INSTALL_MASK" emerge-${BOARD} \
294 --root="$ROOT_DEV_DIR" --root-deps=rdeps \
295 --usepkgonly chromeos-test $EMERGE_JOBS
296fi
297
298# Clean up links setup for stateful install of extra packages.
299if [ $FLAGS_statefuldev -eq $FLAGS_TRUE ] ; then
300 # Fix symlinks so they work on live system.
301 for path in bin include lib libexec sbin share; do
302 sudo unlink $DEV_IMAGE_ROOT/usr/$path
303 sudo ln -s /usr/local/$path $DEV_IMAGE_ROOT/usr/$path
304 done
305
306 # Fix exceptions.
307 sudo unlink "$DEV_IMAGE_ROOT/usr/lib64"
308 sudo unlink "$DEV_IMAGE_ROOT/usr/local"
309
310 sudo ln -s "/usr/local/lib" "$DEV_IMAGE_ROOT/usr/lib64"
311 sudo ln -s "/usr/local" "$DEV_IMAGE_ROOT/usr/local"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700312fi
313
314# Perform any customizations on the root file system that are needed.
315WITH_DEV=""
316if [[ $FLAGS_withdev -eq $FLAGS_TRUE ]]; then
317 WITH_DEV="--withdev"
318fi
319
320#TODO(sosa@chromium.org) - Does it make sense to leave /usr/local bound here?
321"${SCRIPTS_DIR}/customize_rootfs" \
322 --root="$ROOT_FS_DIR" \
323 --target="$ARCH" \
324 --board="$BOARD" \
325 $WITH_DEV
326
327# Check that the image has been correctly created.
328"${SCRIPTS_DIR}/test_image" \
329 --root="$ROOT_FS_DIR" \
330 --target="$ARCH"
331
332# Set dev_mode flag and update library cache.
333if [ $FLAGS_statefuldev -eq $FLAGS_TRUE ] ; then
334 sudo touch "$ROOT_FS_DIR/root/.dev_mode"
335 sudo /sbin/ldconfig -r "$ROOT_FS_DIR"
336fi
337
338# Only bound if installing dev to stateful.
339if [ $FLAGS_statefuldev -eq $FLAGS_TRUE ] ; then
340 sudo umount "$ROOT_FS_DIR/usr/local"
341fi
342
343# Cleanup loop devices.
344cleanup_stateful_fs_loop
345cleanup_rootfs_loop
346
347# Create a master boot record.
348# Start with the syslinux master boot record. We need to zero-pad to
349# fill out a 512-byte sector size.
350SYSLINUX_MBR="/usr/share/syslinux/mbr.bin"
351dd if="$SYSLINUX_MBR" of="$MBR_IMG" bs=512 count=1 conv=sync
352# Create a partition table in the MBR.
353NUM_SECTORS=$((`stat --format="%s" "$ROOT_FS_IMG"` / 512))
354KERNEL_SECTORS=8192
355sudo sfdisk -H64 -S32 -uS -f "$MBR_IMG" <<EOF
356,$NUM_SECTORS,L,-,
357,$NUM_SECTORS,S,-,
358,$NUM_SECTORS,L,*,
359,$KERNEL_SECTORS,L,-,
360;
361EOF
362if [[ "$ARCH" = "arm" ]]; then
363 # Write u-boot script into MBR code block that boots 4th partition kernel.
364 KERNEL_OFFSET=`printf "%08x" $(((3 * $NUM_SECTORS) + 1))`
365 KERNEL_SECS_HEX=`printf "%08x" $KERNEL_SECTORS`
366 MBR_SCRIPT="${OUTPUT_DIR}/mbr_script"
367 echo -e "echo\necho ---- ChromeOS Boot ----\necho\n" \
368 "mmc read 1 C0008000 0x$KERNEL_OFFSET 0x$KERNEL_SECS_HEX\n" \
369 "bootm C0008000" > ${MBR_SCRIPT}
370 MKIMAGE="${BOARD_ROOT}/u-boot/mkimage"
371 if [[ -f "$MKIMAGE".gz ]]; then
372 sudo gunzip "$MKIMAGE".gz
373 fi
374 if [[ -x "$MKIMAGE" ]]; then
375 MBR_SCRIPT_UIMG="${MBR_SCRIPT}.uimg"
376 "$MKIMAGE" -A "${ARCH}" -O linux -T script -a 0 -e 0 -n "COS boot" \
377 -d ${MBR_SCRIPT} ${MBR_SCRIPT_UIMG}
378 dd bs=1 count=`stat --printf="%s" ${MBR_SCRIPT_UIMG}` \
379 if="$MBR_SCRIPT_UIMG" of="$MBR_IMG"
380 hexdump -v -C "$MBR_IMG"
381 else
382 echo "Error: u-boot mkimage not found or not executable."
383 fi
384fi
385
386OUTSIDE_OUTPUT_DIR="../build/images/${FLAGS_board}/${IMAGE_SUBDIR}"
387echo "Done. Image created in ${OUTPUT_DIR}"
388echo "To copy to USB keyfob, OUTSIDE the chroot, do something like:"
389echo " ./image_to_usb.sh --from=${OUTSIDE_OUTPUT_DIR} --to=/dev/sdb"
390echo "To convert to VMWare image, OUTSIDE the chroot, do something like:"
391echo " ./image_to_vmware.sh --from=${OUTSIDE_OUTPUT_DIR}"
392echo "from the scripts directory where you entered the chroot."
393
394trap - EXIT