blob: 76001a498fe372b20884cea8726bb692b6268a68 [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};
Shikha Panwar61a74b52024-02-16 13:17:01 +000023use anyhow::{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};
Victor Hsieha61ec2e2022-09-21 16:25:27 -070027use compos_common::{
28 COMPOS_DATA_ROOT, IDSIG_FILE, IDSIG_MANIFEST_APK_FILE, IDSIG_MANIFEST_EXT_APK_FILE,
Shikha Panwar61a74b52024-02-16 13:17:01 +000029 INSTANCE_ID_FILE, INSTANCE_IMAGE_FILE,
Victor Hsieha61ec2e2022-09-21 16:25:27 -070030};
Alan Stokes6542fdd2022-02-17 15:21:46 +000031use log::info;
Alan Stokes6b2d0a82021-09-29 11:30:39 +010032use std::fs;
33use std::path::{Path, PathBuf};
Alan Stokes93863602022-08-03 17:23:25 +010034use std::sync::Arc;
Alan Stokes6b2d0a82021-09-29 11:30:39 +010035
36pub struct CompOsInstance {
Alan Stokes9ca14ca2021-10-20 14:25:57 +010037 service: Strong<dyn ICompOsService>,
Alan Stokes6b2d0a82021-09-29 11:30:39 +010038 #[allow(dead_code)] // Keeps VirtualizationService & the VM alive
Andrew Walbrand0ef4002022-05-16 16:14:10 +000039 vm_instance: ComposClient,
Alan Stokes9ca14ca2021-10-20 14:25:57 +010040 #[allow(dead_code)] // Keeps composd process alive
41 lazy_service_guard: LazyServiceGuard,
Alan Stokes93863602022-08-03 17:23:25 +010042 // Keep this alive as long as we are
43 instance_tracker: Arc<()>,
Alan Stokes6b2d0a82021-09-29 11:30:39 +010044}
45
46impl CompOsInstance {
47 pub fn get_service(&self) -> Strong<dyn ICompOsService> {
48 self.service.clone()
49 }
Alan Stokes0fc6ce52022-08-02 17:01:48 +010050
Alan Stokes93863602022-08-03 17:23:25 +010051 /// Returns an Arc that this instance holds a strong reference to as long as it exists. This
52 /// can be used to determine when the instance has been dropped.
53 pub fn get_instance_tracker(&self) -> &Arc<()> {
54 &self.instance_tracker
55 }
56
57 /// Attempt to shut down the VM cleanly, giving time for any relevant logs to be written.
Alan Stokes0fc6ce52022-08-02 17:01:48 +010058 pub fn shutdown(self) -> LazyServiceGuard {
59 self.vm_instance.shutdown(self.service);
60 // Return the guard to the caller, since we might be terminated at any point after it is
61 // dropped, and there might still be things to do.
62 self.lazy_service_guard
63 }
Alan Stokes6b2d0a82021-09-29 11:30:39 +010064}
65
66pub struct InstanceStarter {
67 instance_name: String,
68 instance_root: PathBuf,
Shikha Panwar61a74b52024-02-16 13:17:01 +000069 instance_id_file: PathBuf,
Alan Stokes6b2d0a82021-09-29 11:30:39 +010070 instance_image: PathBuf,
Jooyung Hanccae66d2021-12-20 11:54:36 +090071 idsig: PathBuf,
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -080072 idsig_manifest_apk: PathBuf,
Victor Hsieha61ec2e2022-09-21 16:25:27 -070073 idsig_manifest_ext_apk: PathBuf,
Alan Stokesd21764c2021-10-25 15:33:40 +010074 vm_parameters: VmParameters,
Alan Stokes6b2d0a82021-09-29 11:30:39 +010075}
76
77impl InstanceStarter {
Alan Stokesd21764c2021-10-25 15:33:40 +010078 pub fn new(instance_name: &str, vm_parameters: VmParameters) -> Self {
Alan Stokes6b2d0a82021-09-29 11:30:39 +010079 let instance_root = Path::new(COMPOS_DATA_ROOT).join(instance_name);
Jooyung Hanccae66d2021-12-20 11:54:36 +090080 let instance_root_path = instance_root.as_path();
Shikha Panwar61a74b52024-02-16 13:17:01 +000081 let instance_id_file = instance_root_path.join(INSTANCE_ID_FILE);
Jooyung Hanccae66d2021-12-20 11:54:36 +090082 let instance_image = instance_root_path.join(INSTANCE_IMAGE_FILE);
83 let idsig = instance_root_path.join(IDSIG_FILE);
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -080084 let idsig_manifest_apk = instance_root_path.join(IDSIG_MANIFEST_APK_FILE);
Victor Hsieha61ec2e2022-09-21 16:25:27 -070085 let idsig_manifest_ext_apk = instance_root_path.join(IDSIG_MANIFEST_EXT_APK_FILE);
Alan Stokes6b2d0a82021-09-29 11:30:39 +010086 Self {
87 instance_name: instance_name.to_owned(),
88 instance_root,
Shikha Panwar61a74b52024-02-16 13:17:01 +000089 instance_id_file,
Alan Stokes6b2d0a82021-09-29 11:30:39 +010090 instance_image,
Jooyung Hanccae66d2021-12-20 11:54:36 +090091 idsig,
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -080092 idsig_manifest_apk,
Victor Hsieha61ec2e2022-09-21 16:25:27 -070093 idsig_manifest_ext_apk,
Alan Stokesd21764c2021-10-25 15:33:40 +010094 vm_parameters,
Alan Stokes6b2d0a82021-09-29 11:30:39 +010095 }
96 }
97
Alan Stokes6542fdd2022-02-17 15:21:46 +000098 pub fn start_new_instance(
Alan Stokes6b2d0a82021-09-29 11:30:39 +010099 &self,
100 virtualization_service: &dyn IVirtualizationService,
101 ) -> Result<CompOsInstance> {
102 info!("Creating {} CompOs instance", self.instance_name);
103
Dan Albert04e5dbd2023-05-08 22:49:40 +0000104 fs::create_dir_all(&self.instance_root)?;
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100105
Alan Stokes6542fdd2022-02-17 15:21:46 +0000106 // Overwrite any existing instance - it's unlikely to be valid with the current set
107 // of APEXes, and finding out it isn't is much more expensive than creating a new one.
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100108 self.create_instance_image(virtualization_service)?;
Shikha Panwar61a74b52024-02-16 13:17:01 +0000109 // TODO(b/294177871): Ping VS to delete the old instance's secret.
110 if cfg!(llpvm_changes) {
111 self.allocate_instance_id(virtualization_service)?;
112 }
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -0800113 // Delete existing idsig files. Ignore error in case idsig doesn't exist.
Dan Albert04e5dbd2023-05-08 22:49:40 +0000114 let _ignored1 = fs::remove_file(&self.idsig);
115 let _ignored2 = fs::remove_file(&self.idsig_manifest_apk);
116 let _ignored3 = fs::remove_file(&self.idsig_manifest_ext_apk);
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100117
Alan Stokes5430eca2022-03-21 14:02:09 +0000118 let instance = self.start_vm(virtualization_service)?;
119
120 // Retrieve the VM's attestation chain as a BCC and save it in the instance directory.
121 let bcc = instance.service.getAttestationChain().context("Getting attestation chain")?;
122 fs::write(self.instance_root.join("bcc"), bcc).context("Writing BCC")?;
123
124 Ok(instance)
Alan Stokes16e027f2021-10-04 17:57:31 +0100125 }
126
Alan Stokesd21764c2021-10-25 15:33:40 +0100127 fn start_vm(
128 &self,
129 virtualization_service: &dyn IVirtualizationService,
130 ) -> Result<CompOsInstance> {
Shikha Panwar61a74b52024-02-16 13:17:01 +0000131 let instance_id: [u8; 64] = if cfg!(llpvm_changes) {
132 fs::read(&self.instance_id_file)?
133 .try_into()
134 .map_err(|_| anyhow!("Failed to get instance_id"))?
135 } else {
136 [0u8; 64]
137 };
138
Alan Stokes16e027f2021-10-04 17:57:31 +0100139 let instance_image = fs::OpenOptions::new()
140 .read(true)
141 .write(true)
142 .open(&self.instance_image)
143 .context("Failed to open instance image")?;
Andrew Walbrand0ef4002022-05-16 16:14:10 +0000144 let vm_instance = ComposClient::start(
Jooyung Hanccae66d2021-12-20 11:54:36 +0900145 virtualization_service,
Shikha Panwar61a74b52024-02-16 13:17:01 +0000146 instance_id,
Jooyung Hanccae66d2021-12-20 11:54:36 +0900147 instance_image,
148 &self.idsig,
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -0800149 &self.idsig_manifest_apk,
Victor Hsieha61ec2e2022-09-21 16:25:27 -0700150 &self.idsig_manifest_ext_apk,
Jooyung Hanccae66d2021-12-20 11:54:36 +0900151 &self.vm_parameters,
152 )
153 .context("Starting VM")?;
Alan Stokes71403772022-06-21 14:56:28 +0100154 let service = vm_instance.connect_service().context("Connecting to CompOS")?;
Alan Stokes93863602022-08-03 17:23:25 +0100155 Ok(CompOsInstance {
156 vm_instance,
157 service,
158 lazy_service_guard: Default::default(),
159 instance_tracker: Default::default(),
160 })
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100161 }
162
163 fn create_instance_image(
164 &self,
165 virtualization_service: &dyn IVirtualizationService,
166 ) -> Result<()> {
167 let instance_image = fs::OpenOptions::new()
168 .create(true)
Alan Stokes23b90ee2021-10-28 11:36:14 +0100169 .truncate(true)
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100170 .read(true)
171 .write(true)
172 .open(&self.instance_image)
173 .context("Creating instance image file")?;
174 let instance_image = ParcelFileDescriptor::new(instance_image);
175 // TODO: Where does this number come from?
176 let size = 10 * 1024 * 1024;
177 virtualization_service
178 .initializeWritablePartition(&instance_image, size, PartitionType::ANDROID_VM_INSTANCE)
179 .context("Writing instance image file")?;
180 Ok(())
181 }
Shikha Panwar61a74b52024-02-16 13:17:01 +0000182
183 fn allocate_instance_id(
184 &self,
185 virtualization_service: &dyn IVirtualizationService,
186 ) -> Result<()> {
187 let id = virtualization_service.allocateInstanceId().context("Allocating Instance Id")?;
188 fs::write(&self.instance_id_file, id)?;
189 Ok(())
190 }
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100191}