blob: 6c933356b038f967efdcb6c55aff70946ee74b1e [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 \
Chris Sosa4bffb8b2010-04-07 17:23:54 -070039 "Install development packages on stateful partition rather than the rootfs"
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
Bill Richardsonc09b94f2010-03-15 11:40:30 -070058# Determine build version.
59. "${SCRIPTS_DIR}/chromeos_version.sh"
60
61# Use canonical path since some tools (e.g. mount) do not like symlinks.
62# Append build attempt to output directory.
63IMAGE_SUBDIR="${CHROMEOS_VERSION_STRING}-a${FLAGS_build_attempt}"
64OUTPUT_DIR="${FLAGS_output_root}/${FLAGS_board}/${IMAGE_SUBDIR}"
65ROOT_FS_DIR="${OUTPUT_DIR}/rootfs"
66ROOT_FS_IMG="${OUTPUT_DIR}/rootfs.image"
Antoine Laboure9e585f2010-04-01 15:57:57 -070067OUTPUT_IMG=${FLAGS_to:-${OUTPUT_DIR}/chromiumos_image.bin}
Bill Richardsonc09b94f2010-03-15 11:40:30 -070068
69BOARD="${FLAGS_board}"
70BOARD_ROOT="${FLAGS_build_root}/${BOARD}"
71
72LOOP_DEV=
Chris Sosa4bffb8b2010-04-07 17:23:54 -070073STATEFUL_LOOP_DEV=
Bill Richardsonc09b94f2010-03-15 11:40:30 -070074
75# What cross-build are we targeting?
76. "${BOARD_ROOT}/etc/make.conf.board_setup"
77LIBC_VERSION=${LIBC_VERSION:-"2.10.1-r1"}
78
79# Figure out ARCH from the given toolchain.
80# TODO: Move to common.sh as a function after scripts are switched over.
81TC_ARCH=$(echo "$CHOST" | awk -F'-' '{ print $1 }')
82case "$TC_ARCH" in
83 arm*)
84 ARCH="arm"
85 ;;
86 *86)
87 ARCH="x86"
88 ;;
89 *)
90 error "Unable to determine ARCH from toolchain: $CHOST"
91 exit 1
92esac
93
94# Hack to fix bug where x86_64 CHOST line gets incorrectly added.
95# ToDo(msb): remove this hack.
96PACKAGES_FILE="${BOARD_ROOT}/packages/Packages"
97sudo sed -e "s/CHOST: x86_64-pc-linux-gnu//" -i "${PACKAGES_FILE}"
98
99# Handle existing directory.
100if [[ -e "$OUTPUT_DIR" ]]; then
101 if [[ $FLAGS_replace -eq $FLAGS_TRUE ]]; then
102 sudo rm -rf "$OUTPUT_DIR"
103 else
104 echo "Directory $OUTPUT_DIR already exists."
105 echo "Use --build_attempt option to specify an unused attempt."
106 echo "Or use --replace if you want to overwrite this directory."
107 exit 1
108 fi
109fi
110
111# Create the output directory.
112mkdir -p "$OUTPUT_DIR"
113
114cleanup_rootfs_loop() {
115 sudo umount "$LOOP_DEV"
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700116 sleep 1 # in case $LOOP_DEV is in use.
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700117 sudo losetup -d "$LOOP_DEV"
118}
119
120cleanup_stateful_fs_loop() {
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700121 sudo umount "${ROOT_FS_DIR}/usr/local"
122 sudo umount "${ROOT_FS_DIR}/var"
123 sudo umount "${STATEFUL_DIR}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700124 sleep 1 # follows from cleanup_root_fs_loop.
125 sudo losetup -d "$STATEFUL_LOOP_DEV"
126}
127
Bill Richardson8b3bd102010-04-06 15:00:10 -0700128cleanup_esp_loop() {
129 sudo umount "$ESP_DIR"
130}
131
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700132cleanup() {
133 # Disable die on error.
134 set +e
135
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700136 if [[ -n "$STATEFUL_LOOP_DEV" ]]; then
137 cleanup_stateful_fs_loop
138 fi
139
140 if [[ -n "$LOOP_DEV" ]]; then
141 cleanup_rootfs_loop
142 fi
143
Bill Richardson8b3bd102010-04-06 15:00:10 -0700144 if [[ -n "$ESP_DIR" ]]; then
145 cleanup_esp_loop
146 fi
147
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700148 # Turn die on error back on.
149 set -e
150}
151
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700152# ${DEV_IMAGE_ROOT} specifies the location of where developer packages will
153# be installed on the stateful dir. On a Chromium OS system, this will
154# translate to /usr/local
155DEV_IMAGE_ROOT=
156
157# Sets up symlinks for the stateful partition based on the root specified by
158# ${1} and var directory specified by ${2}.
159setup_symlinks_on_root() {
160 echo "Setting up symlinks on the stateful partition rooted at ${1} with"\
161 "var directory located at ${2}"
162
163 for path in usr local; do
164 if [ -h "${DEV_IMAGE_ROOT}/${path}" ] ; then
165 sudo unlink "${DEV_IMAGE_ROOT}/${path}"
166 elif [ -e "${DEV_IMAGE_ROOT}/${path}" ] ; then
167 echo "*** ERROR: ${DEV_IMAGE_ROOT}/${path} should be a symlink if exists"
168 return 1
169 fi
170 sudo ln -s ${1} "${DEV_IMAGE_ROOT}/${path}"
171 done
172
173 # Setup var. Var is on the stateful partition at /var for both non-developer
174 # builds and developer builds.
175 if [ -h "${DEV_IMAGE_ROOT}/var" ] ; then
176 sudo unlink "${DEV_IMAGE_ROOT}/var"
177 elif [ -e "${DEV_IMAGE_ROOT}/var" ] ; then
178 echo "*** ERROR: ${DEV_IMAGE_ROOT}/var should be a symlink if it exists"
179 return 1
180 fi
181
182 sudo ln -s "${2}" "${DEV_IMAGE_ROOT}/var"
183}
184
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700185trap cleanup EXIT
186
187mkdir -p "$ROOT_FS_DIR"
188
189# Create and format the root file system.
190
191# Check for loop device before creating image.
192LOOP_DEV=$(sudo losetup -f)
193if [ -z "$LOOP_DEV" ] ; then
194 echo "No free loop device. Free up a loop device or reboot. exiting. "
195 exit 1
196fi
197
198# Create root file system disk image to fit on a 1GB memory stick.
199# 1 GB in hard-drive-manufacturer-speak is 10^9, not 2^30. 950MB < 10^9 bytes.
Chris Masone35a83f92010-04-05 16:51:53 -0700200ROOT_SIZE_BYTES=$((1024 * 1024 * 720))
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700201dd if=/dev/zero of="$ROOT_FS_IMG" bs=1 count=1 seek=$((ROOT_SIZE_BYTES - 1))
202sudo losetup "$LOOP_DEV" "$ROOT_FS_IMG"
203sudo mkfs.ext3 "$LOOP_DEV"
204
205# Tune and mount rootfs.
206UUID=$(uuidgen)
207DISK_LABEL="C-KEYFOB"
208sudo tune2fs -L "$DISK_LABEL" -U "$UUID" -c 0 -i 0 "$LOOP_DEV"
209sudo mount "$LOOP_DEV" "$ROOT_FS_DIR"
210
211# Create stateful partition of the same size as the rootfs.
212STATEFUL_IMG="$OUTPUT_DIR/stateful_partition.image"
213STATEFUL_DIR="$OUTPUT_DIR/stateful_partition"
214STATEFUL_LOOP_DEV=$(sudo losetup -f)
215if [ -z "$STATEFUL_LOOP_DEV" ] ; then
216 echo "No free loop device. Free up a loop device or reboot. exiting. "
217 exit 1
218fi
219dd if=/dev/zero of="$STATEFUL_IMG" bs=1 count=1 seek=$((ROOT_SIZE_BYTES - 1))
220sudo losetup "$STATEFUL_LOOP_DEV" "$STATEFUL_IMG"
221sudo mkfs.ext3 "$STATEFUL_LOOP_DEV"
222sudo tune2fs -L "C-STATE" -U "$UUID" -c 0 -i 0 \
223 "$STATEFUL_LOOP_DEV"
224
225# Mount the stateful partition.
226mkdir -p "$STATEFUL_DIR"
227sudo mount "$STATEFUL_LOOP_DEV" "$STATEFUL_DIR"
228
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700229# Set dev image root now that we have mounted the stateful partition we created
230DEV_IMAGE_ROOT="$STATEFUL_DIR/dev_image"
231
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700232# Turn root file system into bootable image.
233if [[ "$ARCH" = "x86" ]]; then
234 # Setup extlinux configuration.
235 # TODO: For some reason the /dev/disk/by-uuid is not being generated by udev
236 # in the initramfs. When we figure that out, switch to root=UUID=$UUID.
237 sudo mkdir -p "$ROOT_FS_DIR"/boot
238 # TODO(adlr): use initramfs for booting.
239 cat <<EOF | sudo dd of="$ROOT_FS_DIR"/boot/extlinux.conf
240DEFAULT chromeos-usb
241PROMPT 0
242TIMEOUT 0
243
244label chromeos-usb
245 menu label chromeos-usb
246 kernel vmlinuz
247 append quiet console=tty2 init=/sbin/init boot=local rootwait root=/dev/sdb3 ro noresume noswap i915.modeset=1 loglevel=1
248
249label chromeos-hd
250 menu label chromeos-hd
251 kernel vmlinuz
252 append quiet console=tty2 init=/sbin/init boot=local rootwait root=HDROOT ro noresume noswap i915.modeset=1 loglevel=1
253EOF
254
255 # Make partition bootable and label it.
256 sudo extlinux -z --install "${ROOT_FS_DIR}/boot"
257fi
258
259# -- Install packages into the root file system --
260
261# We need to install libc manually from the cross toolchain.
262# TODO: Improve this? We only want libc and not the whole toolchain.
263PKGDIR="/var/lib/portage/pkgs/cross/"
264sudo tar jxvpf \
265 "${PKGDIR}/${CHOST}/cross-${CHOST}"/glibc-${LIBC_VERSION}.tbz2 \
266 -C "$ROOT_FS_DIR" --strip-components=3 \
267 --exclude=usr/include --exclude=sys-include --exclude=*.a --exclude=*.o
268
269# We need to install libstdc++ manually from the cross toolchain.
270# TODO: Figure out a better way of doing this?
271sudo cp -a "${BOARD_ROOT}"/lib/libgcc_s.so* "${ROOT_FS_DIR}/lib"
272sudo cp -a "${BOARD_ROOT}"/usr/lib/libstdc++.so* "${ROOT_FS_DIR}/usr/lib"
273
274INSTALL_MASK=""
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700275if [[ $FLAGS_installmask -eq $FLAGS_FALSE ]] ; then
276 INSTALL_MASK="$DEFAULT_INSTALL_MASK"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700277fi
278
279if [[ $FLAGS_jobs -ne -1 ]]; then
280 EMERGE_JOBS="--jobs=$FLAGS_jobs"
281fi
282
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700283# Prepare stateful partition with some pre-created directories
284sudo mkdir -p "${DEV_IMAGE_ROOT}"
285sudo mkdir -p "${STATEFUL_DIR}/var"
Bill Richardson8b3bd102010-04-06 15:00:10 -0700286
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700287# Create symlinks so that /usr/local/usr based directories are symlinked to
288# /usr/local/ directories e.g. /usr/local/usr/bin -> /usr/local/bin, etc.
289setup_symlinks_on_root "${DEV_IMAGE_ROOT}" "${STATEFUL_DIR}/var"
Bill Richardson8b3bd102010-04-06 15:00:10 -0700290
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700291# Perform binding rather than symlinking because directories must exist
292# on rootfs so that we can bind at run-time since rootfs is read-only
293echo "Binding directories from stateful partition onto the rootfs"
294sudo mkdir -p "${ROOT_FS_DIR}/usr/local"
295sudo mount --bind "${DEV_IMAGE_ROOT}" "${ROOT_FS_DIR}/usr/local"
296sudo mkdir -p "${ROOT_FS_DIR}/var"
297sudo mount --bind "${STATEFUL_DIR}/var" "${ROOT_FS_DIR}/var"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700298
299# We "emerge --root=$ROOT_FS_DIR --root-deps=rdeps --usepkgonly" all of the
300# runtime packages for chrome os. This builds up a chrome os image from binary
301# packages with runtime dependencies only. We use INSTALL_MASK to trim the
302# image size as much as possible.
303sudo INSTALL_MASK="$INSTALL_MASK" emerge-${BOARD} \
304 --root="$ROOT_FS_DIR" --root-deps=rdeps \
305 --usepkgonly chromeos $EMERGE_JOBS
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700306
Chris Sosa4bccd3c2010-03-15 13:23:14 -0700307# Determine the root dir for development packages.
308ROOT_DEV_DIR="$ROOT_FS_DIR"
309[ $FLAGS_statefuldev -eq $FLAGS_TRUE ] && ROOT_DEV_DIR="$ROOT_FS_DIR/usr/local"
310
311# Install development packages.
312if [[ $FLAGS_withdev -eq $FLAGS_TRUE ]] ; then
313 sudo INSTALL_MASK="$INSTALL_MASK" emerge-${BOARD} \
314 --root="$ROOT_DEV_DIR" --root-deps=rdeps \
315 --usepkgonly chromeos-dev $EMERGE_JOBS
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700316
317 # The ldd tool is a useful shell script but lives in glibc; just copy it.
Chris Sosa4bccd3c2010-03-15 13:23:14 -0700318 sudo cp -a "$(which ldd)" "${ROOT_DEV_DIR}/usr/bin"
319fi
320
321# Install packages required for testing.
322if [[ $FLAGS_withtest -eq $FLAGS_TRUE ]] ; then
323 sudo INSTALL_MASK="$INSTALL_MASK" emerge-${BOARD} \
324 --root="$ROOT_DEV_DIR" --root-deps=rdeps \
325 --usepkgonly chromeos-test $EMERGE_JOBS
326fi
327
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700328# Perform any customizations on the root file system that are needed.
329WITH_DEV=""
330if [[ $FLAGS_withdev -eq $FLAGS_TRUE ]]; then
331 WITH_DEV="--withdev"
332fi
333
Bill Richardson4364a2e2010-03-30 14:17:34 -0700334# Extract the kernel from the root filesystem for use by the GPT image. Legacy
Bill Richardson8b3bd102010-04-06 15:00:10 -0700335# BIOS will use the kernel in the rootfs (via syslinux), Chrome OS BIOS will
336# use the kernel partition.
Bill Richardson6dcea162010-03-31 19:20:24 -0700337sudo cp -f "${ROOT_FS_DIR}/boot/vmlinuz" "${OUTPUT_DIR}/vmlinuz.image"
Bill Richardson4364a2e2010-03-30 14:17:34 -0700338
Bill Richardson8b3bd102010-04-06 15:00:10 -0700339# Create EFI System Partition to boot stock EFI BIOS (but not ChromeOS EFI
340# BIOS). We only need this for x86, but it's simpler and safer to keep the disk
341# images the same for both x86 and ARM.
342ESP_IMG=${OUTPUT_DIR}/esp.image
343# NOTE: The size argument for mkfs.vfat is in 1024-byte blocks. We'll hard-code
344# it to 16M for now.
345ESP_BLOCKS=16384
346/usr/sbin/mkfs.vfat -C ${OUTPUT_DIR}/esp.image ${ESP_BLOCKS}
347ESP_DIR=${OUTPUT_DIR}/esp
348mkdir -p ${ESP_DIR}
349sudo mount -o loop ${ESP_IMG} ${ESP_DIR}
350sudo mkdir -p ${ESP_DIR}/efi/boot
351sudo grub-mkimage -p /efi/boot -o ${ESP_DIR}/efi/boot/bootx64.efi \
352 part_gpt fat ext2 normal boot sh chain configfile linux
353sudo cp ${ROOT_FS_DIR}/boot/vmlinuz ${ESP_DIR}/efi/boot/vmlinuz
354cat <<EOF | sudo dd of=${ESP_DIR}/efi/boot/grub.cfg
355set timeout=2
356set default=0
357
358menuentry "32-bit serial" {
359 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
360}
361EOF
362
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700363"${SCRIPTS_DIR}/customize_rootfs" \
364 --root="$ROOT_FS_DIR" \
365 --target="$ARCH" \
366 --board="$BOARD" \
367 $WITH_DEV
368
369# Check that the image has been correctly created.
370"${SCRIPTS_DIR}/test_image" \
371 --root="$ROOT_FS_DIR" \
372 --target="$ARCH"
373
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700374# Enable dev mode on the target system and re-run ldconfig
375# for rootfs's ld.so.cache
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700376if [ $FLAGS_statefuldev -eq $FLAGS_TRUE ] ; then
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700377 # Flag will mount /usr/local on target device
378 sudo mkdir -p "$ROOT_FS_DIR/root"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700379 sudo touch "$ROOT_FS_DIR/root/.dev_mode"
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700380
381 # Re-run ldconfig to fix /etc/ldconfig.so.cache
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700382 sudo /sbin/ldconfig -r "$ROOT_FS_DIR"
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700383
384 #TODO(sosa@chromium.org) - /usr/bin/xterm symlink not created in stateful.
385 sudo ln -sf "/usr/local/bin/aterm" "/usr/bin/xterm"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700386fi
387
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700388# Clean up symlinks so they work on a running target rooted at "/".
389# Here development packages are rooted at /usr/local. However, do not
390# create /usr/local or /var on host (already exist on target).
391setup_symlinks_on_root "/usr/local" "/var"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700392
393# Cleanup loop devices.
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700394cleanup
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700395
Bill Richardson4364a2e2010-03-30 14:17:34 -0700396# Create the GPT-formatted image
397${SCRIPTS_DIR}/build_gpt.sh \
398 --arch=${ARCH} --board=${FLAGS_board} --board_root=${BOARD_ROOT} \
Bill Richardson6dcea162010-03-31 19:20:24 -0700399 "${OUTPUT_DIR}" "${OUTPUT_IMG}"
Bill Richardson4364a2e2010-03-30 14:17:34 -0700400
Bill Richardson6dcea162010-03-31 19:20:24 -0700401# Clean up temporary files.
Bill Richardson8b3bd102010-04-06 15:00:10 -0700402rm -f "${ROOT_FS_IMG}" "${STATEFUL_IMG}" "${OUTPUT_DIR}/vmlinuz.image" \
403 "${ESP_IMG}"
404rmdir "${ROOT_FS_DIR}" "${STATEFUL_DIR}" "${ESP_DIR}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700405
406OUTSIDE_OUTPUT_DIR="../build/images/${FLAGS_board}/${IMAGE_SUBDIR}"
407echo "Done. Image created in ${OUTPUT_DIR}"
408echo "To copy to USB keyfob, OUTSIDE the chroot, do something like:"
409echo " ./image_to_usb.sh --from=${OUTSIDE_OUTPUT_DIR} --to=/dev/sdb"
410echo "To convert to VMWare image, OUTSIDE the chroot, do something like:"
411echo " ./image_to_vmware.sh --from=${OUTSIDE_OUTPUT_DIR}"
412echo "from the scripts directory where you entered the chroot."
413
414trap - EXIT