blob: 53b5d3b0494a547d65e509c73bfaae6c5ae7f79c [file] [log] [blame]
#!/bin/bash
# Copyright 2017 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#### CATEGORY=Run, inspect and debug
### start fuchsia in qemu with a FVM disk
## start fuchsia in qemu with a FVM disk
##
## usage: fx qemu [--no-build] [-z <zbi_image>] [RUN_ZIRCON_FLAGS]
##
## -z <zbi_image> use <zbi_image> instead of the default.
## --no-build do not attempt to build the fvm and the default zbi images.
## A custom zbi image passed by a '-z' flag is never built.
##
## This command delegates to //zircon/scripts/run-zircon. Other flags are
## documented in that script, and can be discovered by passing -h or --help.
##
set -e
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/lib/vars.sh || exit $?
fx-config-read
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/lib/fvm.sh || exit $?
build_flag=true
ZBI_IMAGE=
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
fx-command-help
"${FUCHSIA_DIR}/zircon/scripts/run-zircon" -h
exit 1
;;
--no-build)
build_flag=false
;;
-z)
shift
ZBI_IMAGE="$1"
;;
*)
break
esac
shift
done
authkeys_path="$(get-ssh-authkeys)" || {
fx-error "Cannot continue without a valid authorized keys file."
exit 1
}
qemu_dir="${PREBUILT_QEMU_DIR}/bin"
build_targets=()
KERNEL_IMAGE="$(fx-command-run list-build-artifacts --type kernel --name qemu-kernel images)"
build_targets+=( "${KERNEL_IMAGE}" )
if [[ -z "${ZBI_IMAGE}" ]]; then
ZBI_IMAGE="$(fx-command-run list-build-artifacts --type zbi --name zircon-a images)"
build_targets+=( "${ZBI_IMAGE}" )
ZBI_IMAGE="${FUCHSIA_BUILD_DIR}/${ZBI_IMAGE}"
fi
# Not all builds use an FVM so failing to find a source FVM is OK.
fvm_source="$(fx-fvm-find-raw-source)"
[[ -n "${fvm_source}" ]] && build_targets+=( "${fvm_source}" )
args=(
-a "${FUCHSIA_ARCH}"
-q "${qemu_dir}"
-t "${FUCHSIA_BUILD_DIR}/${KERNEL_IMAGE}"
)
if $build_flag; then
_targets="$(printf ", %s" "${build_targets[@]}")"
echo >&2 "Building ${_targets:2}"
fx-command-run build "${build_targets[@]}"
fi
# Construction of a qcow image prevents qemu from writing back to the
# build-produced image file, which could cause timestamp issues with that file.
# Construction of the new ZBI adds ~/.ssh/fuchsia_authorized_keys for SSH
# access.
imgdir="$(mktemp -d ${FUCHSIA_BUILD_DIR}/tmp.XXX)"
if [[ ! -d "${imgdir}" ]]; then
echo >&2 "Failed to create temporary directory"
exit 1
fi
trap 'rm -rf "$imgdir"' EXIT
kernelzbi="${imgdir}/fuchsia-ssh.zbi"
args+=(-z "${kernelzbi}")
fx-zbi -o "${kernelzbi}" "${ZBI_IMAGE}" --entry "data/ssh/authorized_keys=${authkeys_path}"
if [[ -n "${fvm_source}" ]]; then
fvm_tool="$(fx-command-run host-tool --print fvm)"
fvm_raw="${imgdir}/fvm_raw.blk"
fx-fvm-extend-image "${fvm_tool}" "${FUCHSIA_BUILD_DIR}/${fvm_source}" "${fvm_raw}" "${IMAGE_SIZE}"
args+=(-d -D "${fvm_raw}" --diskfmt=raw)
fi
"${FUCHSIA_DIR}/zircon/scripts/run-zircon" "${args[@]}" "$@"