crosvm: add JSON configuration file support

Add a new `--cfg` command-line parameter, which allows a single (for
now) JSON configuration file to be specified and loaded as the base
configuration for the VM, if the `config-file` feature is enabled (the
default).

Configuration options in the file can be overriden or extended by the
regular command-line arguments. Typically, options that can be specified
only once are overriden, and options that can be specified several times
(like `--block`) are extended, allowing some devices of the same type to
be declared in a configuration file and some more on the command-line.

Due to a limitation in argh, which does not yet allow `Option<bool>`
fields to be used as switches, merging more than a single configuration
file is not reliable so we only support a single configuration file for
now. This has been addressed upstream but not yet in a released version.

Configuration file support for all the options of RunCommand increases
the crosvm binary size compiled with the `chromeos` profile by 250KB.
However, due to the currently limited set of options in the
configuration file, the present CL increases the binary size by just
78KB. Without the `config-file` feature, this CL does not incur any
binary size increase.

Supported parameters as of this commit:

* battery
* block
* executable-path
* initrd
* net
* params
* serial
* socket

BUG=b:218223240
TEST=`cargo run --features config-file -- run --cfg vm.json` boots the
VM with parameters defined in vm.json.

Change-Id: Ibb27556144ee766af81c178e5b94bd5117e102da
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/3970368
Reviewed-by: Dennis Kempin <denniskempin@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Alexandre Courbot <acourbot@chromium.org>
Reviewed-by: Keiichi Watanabe <keiichiw@chromium.org>
This commit is contained in:
Alexandre Courbot 2022-10-20 15:40:27 +09:00 committed by crosvm LUCI
parent 26aa3e3b0e
commit 4cbd1cf16e
3 changed files with 370 additions and 29 deletions

53
Cargo.lock generated
View file

@ -553,6 +553,7 @@ dependencies = [
"libc",
"libcras",
"log",
"merge",
"metrics",
"minijail",
"net_util",
@ -1372,6 +1373,28 @@ dependencies = [
"autocfg",
]
[[package]]
name = "merge"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10bbef93abb1da61525bbc45eeaff6473a41907d19f8f9aa5168d214e10693e9"
dependencies = [
"merge_derive",
"num-traits",
]
[[package]]
name = "merge_derive"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "209d075476da2e63b4b29e72a2ef627b840589588e71400a25e3565c4f849d07"
dependencies = [
"proc-macro-error",
"proc-macro2",
"quote 1.0.21",
"syn 1.0.103",
]
[[package]]
name = "metrics"
version = "0.1.0"
@ -1622,6 +1645,30 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote 1.0.21",
"syn 1.0.103",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote 1.0.21",
"version_check",
]
[[package]]
name = "proc-macro2"
version = "1.0.47"
@ -2215,6 +2262,12 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "vfio_sys"
version = "0.1.0"

View file

@ -118,7 +118,7 @@ exclude = [
[features]
## Default features of crosvm. This selection is somewhat arbitrary for historical reasons.
default = ["audio", "balloon", "gpu", "qcow", "usb"]
default = ["audio", "balloon", "config-file", "gpu", "qcow", "usb"]
## Enables cross-platform audio devices
audio = ["devices/audio"]
@ -132,6 +132,9 @@ balloon = ["devices/balloon", "vm_control/balloon"]
## concatenate large file system images into a single disk image.
composite-disk = ["protos/composite-disk", "protobuf", "disk/composite-disk"]
## Enables support for JSON configuration files that can be specified using `--cfg`.
config-file = []
## Enables using gdb to debug the guest kernel.
gdb = [
"aarch64/gdb",
@ -368,6 +371,7 @@ libc = "0.2.93"
libcras = "*"
# Compile out trace statements in release builds
log = { version = "0", features = ["release_max_level_debug"]}
merge = "0.1.0"
metrics = { path = "metrics" }
net_util = { path = "net_util" }
once_cell = "1.7"

File diff suppressed because it is too large Load diff