Fix tests on aosp_cf_x86_64_only_phone
Our nested virtualization check failed on this target. Move to a
prefix check to support this device and any other ones that appear.
Also extract the code to do the check to a library, rather than
changing in two places.
Bug: 236922543
Test: Presubmit
Change-Id: Id1d424f5f107e2098d89ca2ea8089ba956f212ca
diff --git a/libs/nested_virt/Android.bp b/libs/nested_virt/Android.bp
new file mode 100644
index 0000000..e364a2d
--- /dev/null
+++ b/libs/nested_virt/Android.bp
@@ -0,0 +1,18 @@
+package {
+ default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+rust_library {
+ name: "libnested_virt",
+ crate_name: "nested_virt",
+ srcs: ["src/lib.rs"],
+ edition: "2018",
+ rustlibs: [
+ "libanyhow",
+ "librustutils",
+ ],
+ apex_available: [
+ "com.android.compos",
+ "com.android.virt",
+ ],
+}
diff --git a/libs/nested_virt/src/lib.rs b/libs/nested_virt/src/lib.rs
new file mode 100644
index 0000000..ab1f06a
--- /dev/null
+++ b/libs/nested_virt/src/lib.rs
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+//! Detection for nested virtualization.
+
+use anyhow::Result;
+use rustutils::system_properties;
+
+/// Return whether we will be running our VM in a VM, which causes the nested VM to run very slowly.
+pub fn is_nested_virtualization() -> Result<bool> {
+ // Currently nested virtualization only occurs when we run KVM inside the cuttlefish VM.
+ // So we just need to check for vsoc.
+ if let Some(value) = system_properties::read("ro.build.product")? {
+ // Fuzzy matching to allow for vsoc_x86, vsoc_x86_64, vsoc_x86_64_only, ...
+ Ok(value.starts_with("vsoc_x86"))
+ } else {
+ Ok(false)
+ }
+}