blob: 5d88b3f50bf4b2afacf581285d4ab09fbff4a38e [file] [log] [blame]
Greg Hartman71633c82018-06-21 18:49:01 -07001#!/bin/bash
Jorge E. Moreiraa8142f92018-06-13 17:33:55 -07002
3#
4# Copyright (C) 2018 The Android Open Source Project
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
Alistair Strachan834fbf62018-11-02 15:23:00 -070019print_command() {
20 binary=$1; shift
21 binary_args=("$@")
22 printf %s "${binary}"
23 for i in "${binary_args[@]}"; do
24 case "$i" in
25 -*) printf "\\%s %s " $'\n' "$i" ;;
26 *) printf "%s " "$i" ;;
27 esac
28 done
29 echo
30}
31
32exec_run() {
33 binary=$1; shift
34 binary_args=("$@")
35 print_command "${binary}" "${binary_args[@]}"
36 exec "${binary}" "${binary_args[@]}"
37}
38
39run() {
40 binary=$1; shift
41 binary_args=("$@")
42 print_command "${binary}" "${binary_args[@]}"
43 "${binary}" "${binary_args[@]}"
44}
45
Jorge E. Moreiraa8142f92018-06-13 17:33:55 -070046default_instance_number() {
47 if [[ "${USER::5}" == "vsoc-" ]]; then
48 echo "${USER: -2}"
49 else
50 echo "01"
51 fi
52}
53CUTTLEFISH_INSTANCE="${CUTTLEFISH_INSTANCE:-$(default_instance_number)}"
54default_instance_name="cvd-${CUTTLEFISH_INSTANCE}"
55default_uuid="699acfc4-c8c4-11e7-882b-5065f31dc1${CUTTLEFISH_INSTANCE}"
Jorge E. Moreira285b48b2018-06-27 10:33:24 -070056default_dir="${HOME}/cuttlefish_runtime"
Jorge E. Moreira1e8e2f12019-10-07 18:09:49 -070057default_internal_dir="${default_internal_dir}"
Jorge E. Moreiraa8142f92018-06-13 17:33:55 -070058default_mobile_tap_name="cvd-mtap-${CUTTLEFISH_INSTANCE}"
Jorge E. Moreira029dc0e2018-06-22 19:46:19 -070059default_wifi_tap_name="cvd-wtap-${CUTTLEFISH_INSTANCE}"
Jorge E. Moreiraa8142f92018-06-13 17:33:55 -070060
Alistair Strachan834fbf62018-11-02 15:23:00 -070061qemu_binary=${qemu_binary=/usr/bin/qemu-system-x86_64}
62dtc_binary=${dtc_binary:-dtc}
63
Alistair Strachan834fbf62018-11-02 15:23:00 -070064if [[ "${qemu_binary##*/}" = "qemu-system-aarch64" ]]; then
65 # On ARM, the early console can be PCI, and ISA is not supported
Greg Hartman494eaf62019-01-30 20:31:39 -080066 kernel_console_serial="pci-serial"
Alistair Strachan834fbf62018-11-02 15:23:00 -070067 machine="virt,gic_version=2"
68 cpu=cortex-a53
69else
70 # On x86, the early console must be ISA, not PCI, so we start to get kernel
71 # messages as soon as possible. ISA devices do not have 'addr' assignments.
72 kernel_console_serial="isa-serial"
73 machine="pc-i440fx-2.8,accel=kvm"
74 cpu=host
75fi
76
77# Put anything here that might affect the machine configuration generated by
78# QEMU. Anything which connects statefully to another service (like a socket)
79# should be added in another section below.
Greg Hartman71633c82018-06-21 18:49:01 -070080args=(
Greg Hartman71633c82018-06-21 18:49:01 -070081 -name "guest=${instance_name:-${default_instance_name}},debug-threads=on"
Alistair Strachan834fbf62018-11-02 15:23:00 -070082 -machine "${machine},usb=off,dump-guest-core=off"
Greg Hartman71633c82018-06-21 18:49:01 -070083 -m "${memory_mb:-2048}"
84 -realtime mlock=off
85 -smp "${cpus:-2},sockets=${cpus:-2},cores=1,threads=1"
86 -uuid "${uuid:-${default_uuid}}"
87 -display none
88 -no-user-config
89 -nodefaults
Greg Hartman71633c82018-06-21 18:49:01 -070090 -rtc "base=utc"
91 -no-shutdown
92 -boot "strict=on"
93 -kernel "${kernel_image_path:-${HOME}/kernel}"
Alistair Strachan054af062018-11-27 12:41:19 -080094 -append "${kernel_cmdline:-"loop.max_part=7 console=ttyS0 androidboot.console=ttyS1 androidboot.hardware=vsoc enforcing=0 audit=1 androidboot.selinux=permissive mac80211_hwsim.radios=0 security=selinux buildvariant=userdebug androidboot.serialno=CUTTLEFISHCVD01 androidboot.lcd_density=160 androidboot.boot_devices=pci0000:00/0000:00:03.0"}"
Alistair Strachan06e0c172018-11-05 09:52:54 -080095 -device "piix3-usb-uhci,id=usb,addr=0x1.0x2"
Greg Hartman494eaf62019-01-30 20:31:39 -080096 -device "virtio-serial-pci,id=virtio-serial0"
Alistair Strachan054af062018-11-27 12:41:19 -080097)
98
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -070099IFS=';' read -ra virtual_disk_array <<< "$virtual_disk_paths"
100virtual_disk_index=0
101for virtual_disk in "${virtual_disk_array[@]}"; do
102 if [[ $virtual_disk_index == 0 ]]; then
103 bootindex=",bootindex=1"
Cody Schuffelen9c6ae482019-06-03 12:10:49 -0700104 else
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700105 bootindex=""
Cody Schuffelen9c6ae482019-06-03 12:10:49 -0700106 fi
Alistair Strachan054af062018-11-27 12:41:19 -0800107 args+=(
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700108 -drive "file=${virtual_disk},format=raw,if=none,id=drive-virtio-disk${virtual_disk_index},aio=threads"
109 -device "virtio-blk-pci,scsi=off,drive=drive-virtio-disk${virtual_disk_index},id=virtio-disk${virtual_disk_index}${bootindex}"
Alistair Strachan054af062018-11-27 12:41:19 -0800110 )
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700111 virtual_disk_index=$((virtual_disk_index + 1))
112done
Alistair Strachan054af062018-11-27 12:41:19 -0800113
114args+=(
Greg Hartman88df7a82018-10-09 16:50:33 -0700115 -netdev "tap,id=hostnet0,ifname=${wifi_tap_name:-${default_wifi_tap_name}},script=no,downscript=no"
Greg Hartman494eaf62019-01-30 20:31:39 -0800116 -device "virtio-net-pci,netdev=hostnet0,id=net0"
Greg Hartman88df7a82018-10-09 16:50:33 -0700117 -netdev "tap,id=hostnet1,ifname=${mobile_tap_name:-${default_mobile_tap_name}},script=no,downscript=no"
Greg Hartman494eaf62019-01-30 20:31:39 -0800118 -device "virtio-net-pci,netdev=hostnet1,id=net1"
119 -device "virtio-balloon-pci,id=balloon0"
Alistair Strachan834fbf62018-11-02 15:23:00 -0700120 -object "rng-random,id=objrng0,filename=/dev/urandom"
Greg Hartman494eaf62019-01-30 20:31:39 -0800121 -device "virtio-rng-pci,rng=objrng0,id=rng0,max-bytes=1024,period=2000"
Alistair Strachan834fbf62018-11-02 15:23:00 -0700122 -cpu "${cpu}"
123 -msg "timestamp=on"
Alistair Strachan252d98b2019-03-04 17:58:06 -0800124 -device "AC97"
Alistair Strachan834fbf62018-11-02 15:23:00 -0700125)
126
Alistair Strachan834fbf62018-11-02 15:23:00 -0700127# The services providing these sockets don't expect multiple connections,
128# so we must not have them in 'args' when we dump the machine FDT. It's
129# OK to add them now, after the dumping and patching has completed.
130# The (maybe patched) DTB can also be provided now.
131
Cody Schuffelen1300f122019-05-28 18:24:34 -0700132if [[ "${use_bootloader}" = "true" ]]; then
133 args+=(
134 -bios "${bootloader}"
135 )
136fi
137
Alistair Strachan834fbf62018-11-02 15:23:00 -0700138args+=(
Jorge E. Moreiradf2b9362019-10-09 11:55:47 -0700139 -chardev "socket,id=charmonitor,path=${monitor_path:-${default_internal_dir}/qemu_monitor.sock},server,nowait"
Alistair Strachan834fbf62018-11-02 15:23:00 -0700140 -mon "chardev=charmonitor,id=monitor,mode=control"
Jorge E. Moreiradf2b9362019-10-09 11:55:47 -0700141 -chardev "file,id=charserial0,path=${kernel_log_pipe_name:-${default_internal_dir}/kernel-log},append=on"
Alistair Strachan834fbf62018-11-02 15:23:00 -0700142 -device "${kernel_console_serial},chardev=charserial0,id=serial0"
Greg Hartman71633c82018-06-21 18:49:01 -0700143 -chardev "socket,id=charserial1,path=${console_path:-${default_dir}/console},server,nowait"
Cody Schuffelen1300f122019-05-28 18:24:34 -0700144 -device "${kernel_console_serial},chardev=charserial1,id=serial1"
Greg Hartman71633c82018-06-21 18:49:01 -0700145)
146
Cody Schuffelen5de48652019-09-16 19:49:52 +0000147if [[ "${logcat_mode}" == "serial" ]]; then
148 args+=(
149 -chardev "file,id=charchannel0,path=${logcat_path:-${default_dir}/logcat},append=on"
150 -device "virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,name=cf-logcat"
151 )
152fi
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800153
Greg Hartman91f81422018-07-09 16:04:49 -0700154if [[ -n "${gdb_flag}" ]]; then
155 args+=(-gdb "${gdb_flag}")
156fi
157
Greg Hartman71633c82018-06-21 18:49:01 -0700158if [[ -n "${ramdisk_image_path}" ]]; then
159 args+=(-initrd "${ramdisk_image_path}")
160fi
161
Cody Schuffelend946b5f2018-12-12 11:54:48 -0800162if [[ ${vsock_guest_cid:-0} -gt 2 ]]; then
163 args+=(-device "vhost-vsock-pci,guest-cid=${vsock_guest_cid}")
164fi
165
Greg Hartman9a180722019-03-19 22:25:37 -0700166export QEMU_AUDIO_DRV=none
Alistair Strachan834fbf62018-11-02 15:23:00 -0700167exec_run "${qemu_binary}" "${args[@]}"