blob: 457520f677a15df020dd608b8c622ded312d5a34 [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};
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,
29 INSTANCE_IMAGE_FILE,
30};
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,
69 instance_image: PathBuf,
Jooyung Hanccae66d2021-12-20 11:54:36 +090070 idsig: PathBuf,
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -080071 idsig_manifest_apk: PathBuf,
Victor Hsieha61ec2e2022-09-21 16:25:27 -070072 idsig_manifest_ext_apk: PathBuf,
Alan Stokesd21764c2021-10-25 15:33:40 +010073 vm_parameters: VmParameters,
Alan Stokes6b2d0a82021-09-29 11:30:39 +010074}
75
76impl InstanceStarter {
Alan Stokesd21764c2021-10-25 15:33:40 +010077 pub fn new(instance_name: &str, vm_parameters: VmParameters) -> Self {
Alan Stokes6b2d0a82021-09-29 11:30:39 +010078 let instance_root = Path::new(COMPOS_DATA_ROOT).join(instance_name);
Jooyung Hanccae66d2021-12-20 11:54:36 +090079 let instance_root_path = instance_root.as_path();
80 let instance_image = instance_root_path.join(INSTANCE_IMAGE_FILE);
81 let idsig = instance_root_path.join(IDSIG_FILE);
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -080082 let idsig_manifest_apk = instance_root_path.join(IDSIG_MANIFEST_APK_FILE);
Victor Hsieha61ec2e2022-09-21 16:25:27 -070083 let idsig_manifest_ext_apk = instance_root_path.join(IDSIG_MANIFEST_EXT_APK_FILE);
Alan Stokes6b2d0a82021-09-29 11:30:39 +010084 Self {
85 instance_name: instance_name.to_owned(),
86 instance_root,
87 instance_image,
Jooyung Hanccae66d2021-12-20 11:54:36 +090088 idsig,
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -080089 idsig_manifest_apk,
Victor Hsieha61ec2e2022-09-21 16:25:27 -070090 idsig_manifest_ext_apk,
Alan Stokesd21764c2021-10-25 15:33:40 +010091 vm_parameters,
Alan Stokes6b2d0a82021-09-29 11:30:39 +010092 }
93 }
94
Alan Stokes6542fdd2022-02-17 15:21:46 +000095 pub fn start_new_instance(
Alan Stokes6b2d0a82021-09-29 11:30:39 +010096 &self,
97 virtualization_service: &dyn IVirtualizationService,
98 ) -> Result<CompOsInstance> {
99 info!("Creating {} CompOs instance", self.instance_name);
100
Dan Albert04e5dbd2023-05-08 22:49:40 +0000101 fs::create_dir_all(&self.instance_root)?;
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100102
Alan Stokes6542fdd2022-02-17 15:21:46 +0000103 // Overwrite any existing instance - it's unlikely to be valid with the current set
104 // of APEXes, and finding out it isn't is much more expensive than creating a new one.
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100105 self.create_instance_image(virtualization_service)?;
Alan Stokes6542fdd2022-02-17 15:21:46 +0000106
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -0800107 // Delete existing idsig files. Ignore error in case idsig doesn't exist.
Dan Albert04e5dbd2023-05-08 22:49:40 +0000108 let _ignored1 = fs::remove_file(&self.idsig);
109 let _ignored2 = fs::remove_file(&self.idsig_manifest_apk);
110 let _ignored3 = fs::remove_file(&self.idsig_manifest_ext_apk);
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100111
Alan Stokes5430eca2022-03-21 14:02:09 +0000112 let instance = self.start_vm(virtualization_service)?;
113
114 // Retrieve the VM's attestation chain as a BCC and save it in the instance directory.
115 let bcc = instance.service.getAttestationChain().context("Getting attestation chain")?;
116 fs::write(self.instance_root.join("bcc"), bcc).context("Writing BCC")?;
117
118 Ok(instance)
Alan Stokes16e027f2021-10-04 17:57:31 +0100119 }
120
Alan Stokesd21764c2021-10-25 15:33:40 +0100121 fn start_vm(
122 &self,
123 virtualization_service: &dyn IVirtualizationService,
124 ) -> Result<CompOsInstance> {
Alan Stokes16e027f2021-10-04 17:57:31 +0100125 let instance_image = fs::OpenOptions::new()
126 .read(true)
127 .write(true)
128 .open(&self.instance_image)
129 .context("Failed to open instance image")?;
Andrew Walbrand0ef4002022-05-16 16:14:10 +0000130 let vm_instance = ComposClient::start(
Jooyung Hanccae66d2021-12-20 11:54:36 +0900131 virtualization_service,
132 instance_image,
133 &self.idsig,
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -0800134 &self.idsig_manifest_apk,
Victor Hsieha61ec2e2022-09-21 16:25:27 -0700135 &self.idsig_manifest_ext_apk,
Jooyung Hanccae66d2021-12-20 11:54:36 +0900136 &self.vm_parameters,
137 )
138 .context("Starting VM")?;
Alan Stokes71403772022-06-21 14:56:28 +0100139 let service = vm_instance.connect_service().context("Connecting to CompOS")?;
Alan Stokes93863602022-08-03 17:23:25 +0100140 Ok(CompOsInstance {
141 vm_instance,
142 service,
143 lazy_service_guard: Default::default(),
144 instance_tracker: Default::default(),
145 })
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100146 }
147
148 fn create_instance_image(
149 &self,
150 virtualization_service: &dyn IVirtualizationService,
151 ) -> Result<()> {
152 let instance_image = fs::OpenOptions::new()
153 .create(true)
Alan Stokes23b90ee2021-10-28 11:36:14 +0100154 .truncate(true)
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100155 .read(true)
156 .write(true)
157 .open(&self.instance_image)
158 .context("Creating instance image file")?;
159 let instance_image = ParcelFileDescriptor::new(instance_image);
160 // TODO: Where does this number come from?
161 let size = 10 * 1024 * 1024;
162 virtualization_service
163 .initializeWritablePartition(&instance_image, size, PartitionType::ANDROID_VM_INSTANCE)
164 .context("Writing instance image file")?;
165 Ok(())
166 }
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100167}