Greg Hartman | 71633c8 | 2018-06-21 18:49:01 -0700 | [diff] [blame] | 1 | #!/bin/bash |
Jorge E. Moreira | a8142f9 | 2018-06-13 17:33:55 -0700 | [diff] [blame] | 2 | |
| 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 Strachan | 834fbf6 | 2018-11-02 15:23:00 -0700 | [diff] [blame] | 19 | print_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 | |
| 32 | exec_run() { |
| 33 | binary=$1; shift |
| 34 | binary_args=("$@") |
| 35 | print_command "${binary}" "${binary_args[@]}" |
| 36 | exec "${binary}" "${binary_args[@]}" |
| 37 | } |
| 38 | |
| 39 | run() { |
| 40 | binary=$1; shift |
| 41 | binary_args=("$@") |
| 42 | print_command "${binary}" "${binary_args[@]}" |
| 43 | "${binary}" "${binary_args[@]}" |
| 44 | } |
| 45 | |
Jorge E. Moreira | a8142f9 | 2018-06-13 17:33:55 -0700 | [diff] [blame] | 46 | default_instance_number() { |
| 47 | if [[ "${USER::5}" == "vsoc-" ]]; then |
| 48 | echo "${USER: -2}" |
| 49 | else |
| 50 | echo "01" |
| 51 | fi |
| 52 | } |
| 53 | CUTTLEFISH_INSTANCE="${CUTTLEFISH_INSTANCE:-$(default_instance_number)}" |
| 54 | default_instance_name="cvd-${CUTTLEFISH_INSTANCE}" |
| 55 | default_uuid="699acfc4-c8c4-11e7-882b-5065f31dc1${CUTTLEFISH_INSTANCE}" |
Jorge E. Moreira | 285b48b | 2018-06-27 10:33:24 -0700 | [diff] [blame] | 56 | default_dir="${HOME}/cuttlefish_runtime" |
Jorge E. Moreira | 1e8e2f1 | 2019-10-07 18:09:49 -0700 | [diff] [blame] | 57 | default_internal_dir="${default_internal_dir}" |
Jorge E. Moreira | a8142f9 | 2018-06-13 17:33:55 -0700 | [diff] [blame] | 58 | default_mobile_tap_name="cvd-mtap-${CUTTLEFISH_INSTANCE}" |
Jorge E. Moreira | 029dc0e | 2018-06-22 19:46:19 -0700 | [diff] [blame] | 59 | default_wifi_tap_name="cvd-wtap-${CUTTLEFISH_INSTANCE}" |
Jorge E. Moreira | a8142f9 | 2018-06-13 17:33:55 -0700 | [diff] [blame] | 60 | |
Alistair Strachan | 834fbf6 | 2018-11-02 15:23:00 -0700 | [diff] [blame] | 61 | qemu_binary=${qemu_binary=/usr/bin/qemu-system-x86_64} |
| 62 | dtc_binary=${dtc_binary:-dtc} |
| 63 | |
Alistair Strachan | 834fbf6 | 2018-11-02 15:23:00 -0700 | [diff] [blame] | 64 | if [[ "${qemu_binary##*/}" = "qemu-system-aarch64" ]]; then |
| 65 | # On ARM, the early console can be PCI, and ISA is not supported |
Greg Hartman | 494eaf6 | 2019-01-30 20:31:39 -0800 | [diff] [blame] | 66 | kernel_console_serial="pci-serial" |
Alistair Strachan | 834fbf6 | 2018-11-02 15:23:00 -0700 | [diff] [blame] | 67 | machine="virt,gic_version=2" |
| 68 | cpu=cortex-a53 |
| 69 | else |
| 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 |
| 75 | fi |
| 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 Hartman | 71633c8 | 2018-06-21 18:49:01 -0700 | [diff] [blame] | 80 | args=( |
Greg Hartman | 71633c8 | 2018-06-21 18:49:01 -0700 | [diff] [blame] | 81 | -name "guest=${instance_name:-${default_instance_name}},debug-threads=on" |
Alistair Strachan | 834fbf6 | 2018-11-02 15:23:00 -0700 | [diff] [blame] | 82 | -machine "${machine},usb=off,dump-guest-core=off" |
Greg Hartman | 71633c8 | 2018-06-21 18:49:01 -0700 | [diff] [blame] | 83 | -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 Hartman | 71633c8 | 2018-06-21 18:49:01 -0700 | [diff] [blame] | 90 | -rtc "base=utc" |
| 91 | -no-shutdown |
| 92 | -boot "strict=on" |
| 93 | -kernel "${kernel_image_path:-${HOME}/kernel}" |
Alistair Strachan | 054af06 | 2018-11-27 12:41:19 -0800 | [diff] [blame] | 94 | -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 Strachan | 06e0c17 | 2018-11-05 09:52:54 -0800 | [diff] [blame] | 95 | -device "piix3-usb-uhci,id=usb,addr=0x1.0x2" |
Greg Hartman | 494eaf6 | 2019-01-30 20:31:39 -0800 | [diff] [blame] | 96 | -device "virtio-serial-pci,id=virtio-serial0" |
Alistair Strachan | 054af06 | 2018-11-27 12:41:19 -0800 | [diff] [blame] | 97 | ) |
| 98 | |
Cody Schuffelen | 3c99f5b | 2019-06-14 17:26:01 -0700 | [diff] [blame] | 99 | IFS=';' read -ra virtual_disk_array <<< "$virtual_disk_paths" |
| 100 | virtual_disk_index=0 |
| 101 | for virtual_disk in "${virtual_disk_array[@]}"; do |
| 102 | if [[ $virtual_disk_index == 0 ]]; then |
| 103 | bootindex=",bootindex=1" |
Cody Schuffelen | 9c6ae48 | 2019-06-03 12:10:49 -0700 | [diff] [blame] | 104 | else |
Cody Schuffelen | 3c99f5b | 2019-06-14 17:26:01 -0700 | [diff] [blame] | 105 | bootindex="" |
Cody Schuffelen | 9c6ae48 | 2019-06-03 12:10:49 -0700 | [diff] [blame] | 106 | fi |
Alistair Strachan | 054af06 | 2018-11-27 12:41:19 -0800 | [diff] [blame] | 107 | args+=( |
Cody Schuffelen | 3c99f5b | 2019-06-14 17:26:01 -0700 | [diff] [blame] | 108 | -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 Strachan | 054af06 | 2018-11-27 12:41:19 -0800 | [diff] [blame] | 110 | ) |
Cody Schuffelen | 3c99f5b | 2019-06-14 17:26:01 -0700 | [diff] [blame] | 111 | virtual_disk_index=$((virtual_disk_index + 1)) |
| 112 | done |
Alistair Strachan | 054af06 | 2018-11-27 12:41:19 -0800 | [diff] [blame] | 113 | |
| 114 | args+=( |
Greg Hartman | 88df7a8 | 2018-10-09 16:50:33 -0700 | [diff] [blame] | 115 | -netdev "tap,id=hostnet0,ifname=${wifi_tap_name:-${default_wifi_tap_name}},script=no,downscript=no" |
Greg Hartman | 494eaf6 | 2019-01-30 20:31:39 -0800 | [diff] [blame] | 116 | -device "virtio-net-pci,netdev=hostnet0,id=net0" |
Greg Hartman | 88df7a8 | 2018-10-09 16:50:33 -0700 | [diff] [blame] | 117 | -netdev "tap,id=hostnet1,ifname=${mobile_tap_name:-${default_mobile_tap_name}},script=no,downscript=no" |
Greg Hartman | 494eaf6 | 2019-01-30 20:31:39 -0800 | [diff] [blame] | 118 | -device "virtio-net-pci,netdev=hostnet1,id=net1" |
| 119 | -device "virtio-balloon-pci,id=balloon0" |
Alistair Strachan | 834fbf6 | 2018-11-02 15:23:00 -0700 | [diff] [blame] | 120 | -object "rng-random,id=objrng0,filename=/dev/urandom" |
Greg Hartman | 494eaf6 | 2019-01-30 20:31:39 -0800 | [diff] [blame] | 121 | -device "virtio-rng-pci,rng=objrng0,id=rng0,max-bytes=1024,period=2000" |
Alistair Strachan | 834fbf6 | 2018-11-02 15:23:00 -0700 | [diff] [blame] | 122 | -cpu "${cpu}" |
| 123 | -msg "timestamp=on" |
Alistair Strachan | 252d98b | 2019-03-04 17:58:06 -0800 | [diff] [blame] | 124 | -device "AC97" |
Alistair Strachan | 834fbf6 | 2018-11-02 15:23:00 -0700 | [diff] [blame] | 125 | ) |
| 126 | |
Alistair Strachan | 834fbf6 | 2018-11-02 15:23:00 -0700 | [diff] [blame] | 127 | # 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 Schuffelen | 1300f12 | 2019-05-28 18:24:34 -0700 | [diff] [blame] | 132 | if [[ "${use_bootloader}" = "true" ]]; then |
| 133 | args+=( |
| 134 | -bios "${bootloader}" |
| 135 | ) |
| 136 | fi |
| 137 | |
Alistair Strachan | 834fbf6 | 2018-11-02 15:23:00 -0700 | [diff] [blame] | 138 | args+=( |
Jorge E. Moreira | df2b936 | 2019-10-09 11:55:47 -0700 | [diff] [blame] | 139 | -chardev "socket,id=charmonitor,path=${monitor_path:-${default_internal_dir}/qemu_monitor.sock},server,nowait" |
Alistair Strachan | 834fbf6 | 2018-11-02 15:23:00 -0700 | [diff] [blame] | 140 | -mon "chardev=charmonitor,id=monitor,mode=control" |
Jorge E. Moreira | df2b936 | 2019-10-09 11:55:47 -0700 | [diff] [blame] | 141 | -chardev "file,id=charserial0,path=${kernel_log_pipe_name:-${default_internal_dir}/kernel-log},append=on" |
Alistair Strachan | 834fbf6 | 2018-11-02 15:23:00 -0700 | [diff] [blame] | 142 | -device "${kernel_console_serial},chardev=charserial0,id=serial0" |
Greg Hartman | 71633c8 | 2018-06-21 18:49:01 -0700 | [diff] [blame] | 143 | -chardev "socket,id=charserial1,path=${console_path:-${default_dir}/console},server,nowait" |
Cody Schuffelen | 1300f12 | 2019-05-28 18:24:34 -0700 | [diff] [blame] | 144 | -device "${kernel_console_serial},chardev=charserial1,id=serial1" |
Greg Hartman | 71633c8 | 2018-06-21 18:49:01 -0700 | [diff] [blame] | 145 | ) |
| 146 | |
Cody Schuffelen | 5de4865 | 2019-09-16 19:49:52 +0000 | [diff] [blame] | 147 | if [[ "${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 | ) |
| 152 | fi |
Jorge E. Moreira | fd10cae | 2019-02-19 15:35:42 -0800 | [diff] [blame] | 153 | |
Greg Hartman | 91f8142 | 2018-07-09 16:04:49 -0700 | [diff] [blame] | 154 | if [[ -n "${gdb_flag}" ]]; then |
| 155 | args+=(-gdb "${gdb_flag}") |
| 156 | fi |
| 157 | |
Greg Hartman | 71633c8 | 2018-06-21 18:49:01 -0700 | [diff] [blame] | 158 | if [[ -n "${ramdisk_image_path}" ]]; then |
| 159 | args+=(-initrd "${ramdisk_image_path}") |
| 160 | fi |
| 161 | |
Cody Schuffelen | d946b5f | 2018-12-12 11:54:48 -0800 | [diff] [blame] | 162 | if [[ ${vsock_guest_cid:-0} -gt 2 ]]; then |
| 163 | args+=(-device "vhost-vsock-pci,guest-cid=${vsock_guest_cid}") |
| 164 | fi |
| 165 | |
Greg Hartman | 9a18072 | 2019-03-19 22:25:37 -0700 | [diff] [blame] | 166 | export QEMU_AUDIO_DRV=none |
Alistair Strachan | 834fbf6 | 2018-11-02 15:23:00 -0700 | [diff] [blame] | 167 | exec_run "${qemu_binary}" "${args[@]}" |