blob: 04b1ad64dd51e188a68ca5959d6d1ac90711c3be [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.
Bill Richardson8b3bd102010-04-06 15:00:10 -070059if [ $FLAGS_statefuldev -eq $FLAGS_TRUE ] &&
Bill Richardsonc09b94f2010-03-15 11:40:30 -070060 [ $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
Bill Richardson8b3bd102010-04-06 15:00:10 -0700132cleanup_esp_loop() {
133 sudo umount "$ESP_DIR"
134}
135
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700136cleanup() {
137 # Disable die on error.
138 set +e
139
140 # Unmount stateful partition from usr/local if bound.
141 if [ -s "$ROOT_FS_DIR/usr/local/bin" ] ; then
142 sudo umount $ROOT_FS_DIR/usr/local
143 fi
144
145 if [[ -n "$STATEFUL_LOOP_DEV" ]]; then
146 cleanup_stateful_fs_loop
147 fi
148
149 if [[ -n "$LOOP_DEV" ]]; then
150 cleanup_rootfs_loop
151 fi
152
Bill Richardson8b3bd102010-04-06 15:00:10 -0700153 if [[ -n "$ESP_DIR" ]]; then
154 cleanup_esp_loop
155 fi
156
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700157 # Turn die on error back on.
158 set -e
159}
160
161trap cleanup EXIT
162
163mkdir -p "$ROOT_FS_DIR"
164
165# Create and format the root file system.
166
167# Check for loop device before creating image.
168LOOP_DEV=$(sudo losetup -f)
169if [ -z "$LOOP_DEV" ] ; then
170 echo "No free loop device. Free up a loop device or reboot. exiting. "
171 exit 1
172fi
173
174# Create root file system disk image to fit on a 1GB memory stick.
175# 1 GB in hard-drive-manufacturer-speak is 10^9, not 2^30. 950MB < 10^9 bytes.
Chris Masone35a83f92010-04-05 16:51:53 -0700176ROOT_SIZE_BYTES=$((1024 * 1024 * 720))
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700177dd if=/dev/zero of="$ROOT_FS_IMG" bs=1 count=1 seek=$((ROOT_SIZE_BYTES - 1))
178sudo losetup "$LOOP_DEV" "$ROOT_FS_IMG"
179sudo mkfs.ext3 "$LOOP_DEV"
180
181# Tune and mount rootfs.
182UUID=$(uuidgen)
183DISK_LABEL="C-KEYFOB"
184sudo tune2fs -L "$DISK_LABEL" -U "$UUID" -c 0 -i 0 "$LOOP_DEV"
185sudo mount "$LOOP_DEV" "$ROOT_FS_DIR"
186
187# Create stateful partition of the same size as the rootfs.
188STATEFUL_IMG="$OUTPUT_DIR/stateful_partition.image"
189STATEFUL_DIR="$OUTPUT_DIR/stateful_partition"
190STATEFUL_LOOP_DEV=$(sudo losetup -f)
191if [ -z "$STATEFUL_LOOP_DEV" ] ; then
192 echo "No free loop device. Free up a loop device or reboot. exiting. "
193 exit 1
194fi
195dd if=/dev/zero of="$STATEFUL_IMG" bs=1 count=1 seek=$((ROOT_SIZE_BYTES - 1))
196sudo losetup "$STATEFUL_LOOP_DEV" "$STATEFUL_IMG"
197sudo mkfs.ext3 "$STATEFUL_LOOP_DEV"
198sudo tune2fs -L "C-STATE" -U "$UUID" -c 0 -i 0 \
199 "$STATEFUL_LOOP_DEV"
200
201# Mount the stateful partition.
202mkdir -p "$STATEFUL_DIR"
203sudo mount "$STATEFUL_LOOP_DEV" "$STATEFUL_DIR"
204
205# Turn root file system into bootable image.
206if [[ "$ARCH" = "x86" ]]; then
207 # Setup extlinux configuration.
208 # TODO: For some reason the /dev/disk/by-uuid is not being generated by udev
209 # in the initramfs. When we figure that out, switch to root=UUID=$UUID.
210 sudo mkdir -p "$ROOT_FS_DIR"/boot
211 # TODO(adlr): use initramfs for booting.
212 cat <<EOF | sudo dd of="$ROOT_FS_DIR"/boot/extlinux.conf
213DEFAULT chromeos-usb
214PROMPT 0
215TIMEOUT 0
216
217label chromeos-usb
218 menu label chromeos-usb
219 kernel vmlinuz
220 append quiet console=tty2 init=/sbin/init boot=local rootwait root=/dev/sdb3 ro noresume noswap i915.modeset=1 loglevel=1
221
222label chromeos-hd
223 menu label chromeos-hd
224 kernel vmlinuz
225 append quiet console=tty2 init=/sbin/init boot=local rootwait root=HDROOT ro noresume noswap i915.modeset=1 loglevel=1
226EOF
227
228 # Make partition bootable and label it.
229 sudo extlinux -z --install "${ROOT_FS_DIR}/boot"
230fi
231
232# -- Install packages into the root file system --
233
234# We need to install libc manually from the cross toolchain.
235# TODO: Improve this? We only want libc and not the whole toolchain.
236PKGDIR="/var/lib/portage/pkgs/cross/"
237sudo tar jxvpf \
238 "${PKGDIR}/${CHOST}/cross-${CHOST}"/glibc-${LIBC_VERSION}.tbz2 \
239 -C "$ROOT_FS_DIR" --strip-components=3 \
240 --exclude=usr/include --exclude=sys-include --exclude=*.a --exclude=*.o
241
242# We need to install libstdc++ manually from the cross toolchain.
243# TODO: Figure out a better way of doing this?
244sudo cp -a "${BOARD_ROOT}"/lib/libgcc_s.so* "${ROOT_FS_DIR}/lib"
245sudo cp -a "${BOARD_ROOT}"/usr/lib/libstdc++.so* "${ROOT_FS_DIR}/usr/lib"
246
247INSTALL_MASK=""
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700248if [[ $FLAGS_installmask -eq $FLAGS_FALSE ]] ; then
249 INSTALL_MASK="$DEFAULT_INSTALL_MASK"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700250fi
251
252if [[ $FLAGS_jobs -ne -1 ]]; then
253 EMERGE_JOBS="--jobs=$FLAGS_jobs"
254fi
255
256if [ $FLAGS_statefuldev -eq $FLAGS_TRUE ] ; then
257 # Creating stateful partition.
258 echo "Setting up symlinks for stateful partition install"
259 DEV_IMAGE_ROOT="$STATEFUL_DIR/dev_image"
260 sudo mkdir -p "$DEV_IMAGE_ROOT/usr"
Bill Richardson8b3bd102010-04-06 15:00:10 -0700261
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700262 # Setup symlinks in stateful partition.
263 for path in bin include lib libexec sbin share; do
264 sudo mkdir "$DEV_IMAGE_ROOT/$path"
265 sudo ln -s "$DEV_IMAGE_ROOT/$path" "$DEV_IMAGE_ROOT/usr/$path"
266 done
Bill Richardson8b3bd102010-04-06 15:00:10 -0700267
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700268 # Setup symlinks that don't conform to above model.
269 sudo ln -s "$DEV_IMAGE_ROOT/lib" "$DEV_IMAGE_ROOT/usr/lib64"
270 sudo ln -s "$DEV_IMAGE_ROOT" "$DEV_IMAGE_ROOT/usr/local"
271
272 # Bind to rootfs.
273 echo "Binding stateful partition to rootfs's /usr/local"
274 sudo mkdir -p "$ROOT_FS_DIR/usr/local"
275 sudo mount -n --bind "$DEV_IMAGE_ROOT" "$ROOT_FS_DIR/usr/local"
276fi
277
278# We "emerge --root=$ROOT_FS_DIR --root-deps=rdeps --usepkgonly" all of the
279# runtime packages for chrome os. This builds up a chrome os image from binary
280# packages with runtime dependencies only. We use INSTALL_MASK to trim the
281# image size as much as possible.
282sudo INSTALL_MASK="$INSTALL_MASK" emerge-${BOARD} \
283 --root="$ROOT_FS_DIR" --root-deps=rdeps \
284 --usepkgonly chromeos $EMERGE_JOBS
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700285
Chris Sosa4bccd3c2010-03-15 13:23:14 -0700286# Determine the root dir for development packages.
287ROOT_DEV_DIR="$ROOT_FS_DIR"
288[ $FLAGS_statefuldev -eq $FLAGS_TRUE ] && ROOT_DEV_DIR="$ROOT_FS_DIR/usr/local"
289
290# Install development packages.
291if [[ $FLAGS_withdev -eq $FLAGS_TRUE ]] ; then
292 sudo INSTALL_MASK="$INSTALL_MASK" emerge-${BOARD} \
293 --root="$ROOT_DEV_DIR" --root-deps=rdeps \
294 --usepkgonly chromeos-dev $EMERGE_JOBS
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700295
296 # The ldd tool is a useful shell script but lives in glibc; just copy it.
Chris Sosa4bccd3c2010-03-15 13:23:14 -0700297 sudo cp -a "$(which ldd)" "${ROOT_DEV_DIR}/usr/bin"
298fi
299
300# Install packages required for testing.
301if [[ $FLAGS_withtest -eq $FLAGS_TRUE ]] ; then
302 sudo INSTALL_MASK="$INSTALL_MASK" emerge-${BOARD} \
303 --root="$ROOT_DEV_DIR" --root-deps=rdeps \
304 --usepkgonly chromeos-test $EMERGE_JOBS
305fi
306
Bill Richardson8b3bd102010-04-06 15:00:10 -0700307# Clean up links setup for stateful install of extra packages.
Chris Sosa4bccd3c2010-03-15 13:23:14 -0700308if [ $FLAGS_statefuldev -eq $FLAGS_TRUE ] ; then
309 # Fix symlinks so they work on live system.
310 for path in bin include lib libexec sbin share; do
311 sudo unlink $DEV_IMAGE_ROOT/usr/$path
312 sudo ln -s /usr/local/$path $DEV_IMAGE_ROOT/usr/$path
313 done
314
315 # Fix exceptions.
316 sudo unlink "$DEV_IMAGE_ROOT/usr/lib64"
317 sudo unlink "$DEV_IMAGE_ROOT/usr/local"
318
319 sudo ln -s "/usr/local/lib" "$DEV_IMAGE_ROOT/usr/lib64"
Chris Sosacbac6482010-03-15 17:52:47 -0700320 sudo ln -s "/usr/local" "$DEV_IMAGE_ROOT/usr/local"
321
322 #TODO(sosa@chromium.org) - /usr/bin/xterm symlink not created in stateful.
Bill Richardson8b3bd102010-04-06 15:00:10 -0700323 sudo ln -sf "/usr/local/bin/aterm" "/usr/bin/xterm"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700324fi
325
326# Perform any customizations on the root file system that are needed.
327WITH_DEV=""
328if [[ $FLAGS_withdev -eq $FLAGS_TRUE ]]; then
329 WITH_DEV="--withdev"
330fi
331
Bill Richardson4364a2e2010-03-30 14:17:34 -0700332# Extract the kernel from the root filesystem for use by the GPT image. Legacy
Bill Richardson8b3bd102010-04-06 15:00:10 -0700333# BIOS will use the kernel in the rootfs (via syslinux), Chrome OS BIOS will
334# use the kernel partition.
Bill Richardson6dcea162010-03-31 19:20:24 -0700335sudo cp -f "${ROOT_FS_DIR}/boot/vmlinuz" "${OUTPUT_DIR}/vmlinuz.image"
Bill Richardson4364a2e2010-03-30 14:17:34 -0700336
Bill Richardson8b3bd102010-04-06 15:00:10 -0700337# Create EFI System Partition to boot stock EFI BIOS (but not ChromeOS EFI
338# BIOS). We only need this for x86, but it's simpler and safer to keep the disk
339# images the same for both x86 and ARM.
340ESP_IMG=${OUTPUT_DIR}/esp.image
341# NOTE: The size argument for mkfs.vfat is in 1024-byte blocks. We'll hard-code
342# it to 16M for now.
343ESP_BLOCKS=16384
344/usr/sbin/mkfs.vfat -C ${OUTPUT_DIR}/esp.image ${ESP_BLOCKS}
345ESP_DIR=${OUTPUT_DIR}/esp
346mkdir -p ${ESP_DIR}
347sudo mount -o loop ${ESP_IMG} ${ESP_DIR}
348sudo mkdir -p ${ESP_DIR}/efi/boot
349sudo grub-mkimage -p /efi/boot -o ${ESP_DIR}/efi/boot/bootx64.efi \
350 part_gpt fat ext2 normal boot sh chain configfile linux
351sudo cp ${ROOT_FS_DIR}/boot/vmlinuz ${ESP_DIR}/efi/boot/vmlinuz
352cat <<EOF | sudo dd of=${ESP_DIR}/efi/boot/grub.cfg
353set timeout=2
354set default=0
355
356menuentry "32-bit serial" {
357 linux /efi/boot/vmlinuz earlyprintk=serial,ttyS0,115200 i915.modeset=0 console=ttyS0,115200 acpi=off init=/sbin/init boot=local rootwait root=/dev/sda3 ro noresume noswap loglevel=7
358}
359EOF
360
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700361#TODO(sosa@chromium.org) - Does it make sense to leave /usr/local bound here?
362"${SCRIPTS_DIR}/customize_rootfs" \
363 --root="$ROOT_FS_DIR" \
364 --target="$ARCH" \
365 --board="$BOARD" \
366 $WITH_DEV
367
368# Check that the image has been correctly created.
369"${SCRIPTS_DIR}/test_image" \
370 --root="$ROOT_FS_DIR" \
371 --target="$ARCH"
372
373# Set dev_mode flag and update library cache.
374if [ $FLAGS_statefuldev -eq $FLAGS_TRUE ] ; then
375 sudo touch "$ROOT_FS_DIR/root/.dev_mode"
376 sudo /sbin/ldconfig -r "$ROOT_FS_DIR"
377fi
378
379# Only bound if installing dev to stateful.
380if [ $FLAGS_statefuldev -eq $FLAGS_TRUE ] ; then
381 sudo umount "$ROOT_FS_DIR/usr/local"
382fi
383
384# Cleanup loop devices.
Bill Richardson8b3bd102010-04-06 15:00:10 -0700385cleanup_esp_loop
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700386cleanup_stateful_fs_loop
387cleanup_rootfs_loop
388
Bill Richardson4364a2e2010-03-30 14:17:34 -0700389# Create the GPT-formatted image
390${SCRIPTS_DIR}/build_gpt.sh \
391 --arch=${ARCH} --board=${FLAGS_board} --board_root=${BOARD_ROOT} \
Bill Richardson6dcea162010-03-31 19:20:24 -0700392 "${OUTPUT_DIR}" "${OUTPUT_IMG}"
Bill Richardson4364a2e2010-03-30 14:17:34 -0700393
Bill Richardson6dcea162010-03-31 19:20:24 -0700394# Clean up temporary files.
Bill Richardson8b3bd102010-04-06 15:00:10 -0700395rm -f "${ROOT_FS_IMG}" "${STATEFUL_IMG}" "${OUTPUT_DIR}/vmlinuz.image" \
396 "${ESP_IMG}"
397rmdir "${ROOT_FS_DIR}" "${STATEFUL_DIR}" "${ESP_DIR}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700398
399OUTSIDE_OUTPUT_DIR="../build/images/${FLAGS_board}/${IMAGE_SUBDIR}"
400echo "Done. Image created in ${OUTPUT_DIR}"
401echo "To copy to USB keyfob, OUTSIDE the chroot, do something like:"
402echo " ./image_to_usb.sh --from=${OUTSIDE_OUTPUT_DIR} --to=/dev/sdb"
403echo "To convert to VMWare image, OUTSIDE the chroot, do something like:"
404echo " ./image_to_vmware.sh --from=${OUTSIDE_OUTPUT_DIR}"
405echo "from the scripts directory where you entered the chroot."
406
407trap - EXIT