blob: aaa46950894d92a34b6db9d003a974a8f83d275d [file] [log] [blame]
Alan Stokes6b2d0a82021-09-29 11:30:39 +01001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! Responsible for validating and starting an existing instance of the CompOS VM, or creating and
18//! starting a new instance if necessary.
19
20use android_system_virtualizationservice::aidl::android::system::virtualizationservice::{
21 IVirtualizationService::IVirtualizationService, PartitionType::PartitionType,
22};
Alan Stokes6542fdd2022-02-17 15:21:46 +000023use anyhow::{Context, Result};
Andrew Walbran46999c92022-08-04 17:33:46 +000024use binder::{LazyServiceGuard, ParcelFileDescriptor, Strong};
Alan Stokes6b2d0a82021-09-29 11:30:39 +010025use compos_aidl_interface::aidl::com::android::compos::ICompOsService::ICompOsService;
Andrew Walbrand0ef4002022-05-16 16:14:10 +000026use compos_common::compos_client::{ComposClient, VmParameters};
Alan Stokes6542fdd2022-02-17 15:21:46 +000027use compos_common::{COMPOS_DATA_ROOT, IDSIG_FILE, IDSIG_MANIFEST_APK_FILE, INSTANCE_IMAGE_FILE};
28use log::info;
Alan Stokes6b2d0a82021-09-29 11:30:39 +010029use std::fs;
30use std::path::{Path, PathBuf};
Alan Stokes93863602022-08-03 17:23:25 +010031use std::sync::Arc;
Alan Stokes6b2d0a82021-09-29 11:30:39 +010032
33pub struct CompOsInstance {
Alan Stokes9ca14ca2021-10-20 14:25:57 +010034 service: Strong<dyn ICompOsService>,
Alan Stokes6b2d0a82021-09-29 11:30:39 +010035 #[allow(dead_code)] // Keeps VirtualizationService & the VM alive
Andrew Walbrand0ef4002022-05-16 16:14:10 +000036 vm_instance: ComposClient,
Alan Stokes9ca14ca2021-10-20 14:25:57 +010037 #[allow(dead_code)] // Keeps composd process alive
38 lazy_service_guard: LazyServiceGuard,
Alan Stokes93863602022-08-03 17:23:25 +010039 // Keep this alive as long as we are
40 instance_tracker: Arc<()>,
Alan Stokes6b2d0a82021-09-29 11:30:39 +010041}
42
43impl CompOsInstance {
44 pub fn get_service(&self) -> Strong<dyn ICompOsService> {
45 self.service.clone()
46 }
Alan Stokes0fc6ce52022-08-02 17:01:48 +010047
Alan Stokes93863602022-08-03 17:23:25 +010048 /// Returns an Arc that this instance holds a strong reference to as long as it exists. This
49 /// can be used to determine when the instance has been dropped.
50 pub fn get_instance_tracker(&self) -> &Arc<()> {
51 &self.instance_tracker
52 }
53
54 /// Attempt to shut down the VM cleanly, giving time for any relevant logs to be written.
Alan Stokes0fc6ce52022-08-02 17:01:48 +010055 pub fn shutdown(self) -> LazyServiceGuard {
56 self.vm_instance.shutdown(self.service);
57 // Return the guard to the caller, since we might be terminated at any point after it is
58 // dropped, and there might still be things to do.
59 self.lazy_service_guard
60 }
Alan Stokes6b2d0a82021-09-29 11:30:39 +010061}
62
63pub struct InstanceStarter {
64 instance_name: String,
65 instance_root: PathBuf,
66 instance_image: PathBuf,
Jooyung Hanccae66d2021-12-20 11:54:36 +090067 idsig: PathBuf,
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -080068 idsig_manifest_apk: PathBuf,
Alan Stokesd21764c2021-10-25 15:33:40 +010069 vm_parameters: VmParameters,
Alan Stokes6b2d0a82021-09-29 11:30:39 +010070}
71
72impl InstanceStarter {
Alan Stokesd21764c2021-10-25 15:33:40 +010073 pub fn new(instance_name: &str, vm_parameters: VmParameters) -> Self {
Alan Stokes6b2d0a82021-09-29 11:30:39 +010074 let instance_root = Path::new(COMPOS_DATA_ROOT).join(instance_name);
Jooyung Hanccae66d2021-12-20 11:54:36 +090075 let instance_root_path = instance_root.as_path();
76 let instance_image = instance_root_path.join(INSTANCE_IMAGE_FILE);
77 let idsig = instance_root_path.join(IDSIG_FILE);
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -080078 let idsig_manifest_apk = instance_root_path.join(IDSIG_MANIFEST_APK_FILE);
Alan Stokes6b2d0a82021-09-29 11:30:39 +010079 Self {
80 instance_name: instance_name.to_owned(),
81 instance_root,
82 instance_image,
Jooyung Hanccae66d2021-12-20 11:54:36 +090083 idsig,
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -080084 idsig_manifest_apk,
Alan Stokesd21764c2021-10-25 15:33:40 +010085 vm_parameters,
Alan Stokes6b2d0a82021-09-29 11:30:39 +010086 }
87 }
88
Alan Stokes6542fdd2022-02-17 15:21:46 +000089 pub fn start_new_instance(
Alan Stokes6b2d0a82021-09-29 11:30:39 +010090 &self,
91 virtualization_service: &dyn IVirtualizationService,
92 ) -> Result<CompOsInstance> {
93 info!("Creating {} CompOs instance", self.instance_name);
94
95 // Ignore failure here - the directory may already exist.
96 let _ = fs::create_dir(&self.instance_root);
97
Alan Stokes6542fdd2022-02-17 15:21:46 +000098 // Overwrite any existing instance - it's unlikely to be valid with the current set
99 // of APEXes, and finding out it isn't is much more expensive than creating a new one.
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100100 self.create_instance_image(virtualization_service)?;
Alan Stokes6542fdd2022-02-17 15:21:46 +0000101
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -0800102 // Delete existing idsig files. Ignore error in case idsig doesn't exist.
Jooyung Hanccae66d2021-12-20 11:54:36 +0900103 let _ = fs::remove_file(&self.idsig);
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -0800104 let _ = fs::remove_file(&self.idsig_manifest_apk);
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100105
Alan Stokes5430eca2022-03-21 14:02:09 +0000106 let instance = self.start_vm(virtualization_service)?;
107
108 // Retrieve the VM's attestation chain as a BCC and save it in the instance directory.
109 let bcc = instance.service.getAttestationChain().context("Getting attestation chain")?;
110 fs::write(self.instance_root.join("bcc"), bcc).context("Writing BCC")?;
111
112 Ok(instance)
Alan Stokes16e027f2021-10-04 17:57:31 +0100113 }
114
Alan Stokesd21764c2021-10-25 15:33:40 +0100115 fn start_vm(
116 &self,
117 virtualization_service: &dyn IVirtualizationService,
118 ) -> Result<CompOsInstance> {
Alan Stokes16e027f2021-10-04 17:57:31 +0100119 let instance_image = fs::OpenOptions::new()
120 .read(true)
121 .write(true)
122 .open(&self.instance_image)
123 .context("Failed to open instance image")?;
Andrew Walbrand0ef4002022-05-16 16:14:10 +0000124 let vm_instance = ComposClient::start(
Jooyung Hanccae66d2021-12-20 11:54:36 +0900125 virtualization_service,
126 instance_image,
127 &self.idsig,
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -0800128 &self.idsig_manifest_apk,
Jooyung Hanccae66d2021-12-20 11:54:36 +0900129 &self.vm_parameters,
130 )
131 .context("Starting VM")?;
Alan Stokes71403772022-06-21 14:56:28 +0100132 let service = vm_instance.connect_service().context("Connecting to CompOS")?;
Alan Stokes93863602022-08-03 17:23:25 +0100133 Ok(CompOsInstance {
134 vm_instance,
135 service,
136 lazy_service_guard: Default::default(),
137 instance_tracker: Default::default(),
138 })
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100139 }
140
141 fn create_instance_image(
142 &self,
143 virtualization_service: &dyn IVirtualizationService,
144 ) -> Result<()> {
145 let instance_image = fs::OpenOptions::new()
146 .create(true)
Alan Stokes23b90ee2021-10-28 11:36:14 +0100147 .truncate(true)
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100148 .read(true)
149 .write(true)
150 .open(&self.instance_image)
151 .context("Creating instance image file")?;
152 let instance_image = ParcelFileDescriptor::new(instance_image);
153 // TODO: Where does this number come from?
154 let size = 10 * 1024 * 1024;
155 virtualization_service
156 .initializeWritablePartition(&instance_image, size, PartitionType::ANDROID_VM_INSTANCE)
157 .context("Writing instance image file")?;
158 Ok(())
159 }
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100160}