blob: e51db13a59a340ac8281a3b36664013ae02f40a5 [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};
Alan Stokes9ca14ca2021-10-20 14:25:57 +010024use binder_common::lazy_service::LazyServiceGuard;
Alan Stokes6b2d0a82021-09-29 11:30:39 +010025use compos_aidl_interface::aidl::com::android::compos::ICompOsService::ICompOsService;
26use compos_aidl_interface::binder::{ParcelFileDescriptor, Strong};
Andrew Walbrand0ef4002022-05-16 16:14:10 +000027use compos_common::compos_client::{ComposClient, VmParameters};
Alan Stokes6542fdd2022-02-17 15:21:46 +000028use compos_common::{COMPOS_DATA_ROOT, IDSIG_FILE, IDSIG_MANIFEST_APK_FILE, INSTANCE_IMAGE_FILE};
29use log::info;
Alan Stokes6b2d0a82021-09-29 11:30:39 +010030use std::fs;
31use std::path::{Path, PathBuf};
32
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 Stokes6b2d0a82021-09-29 11:30:39 +010039}
40
41impl CompOsInstance {
42 pub fn get_service(&self) -> Strong<dyn ICompOsService> {
43 self.service.clone()
44 }
Alan Stokes0fc6ce52022-08-02 17:01:48 +010045
46 // Attempt to shut down the VM cleanly, giving time for any relevant logs to be written.
47 pub fn shutdown(self) -> LazyServiceGuard {
48 self.vm_instance.shutdown(self.service);
49 // Return the guard to the caller, since we might be terminated at any point after it is
50 // dropped, and there might still be things to do.
51 self.lazy_service_guard
52 }
Alan Stokes6b2d0a82021-09-29 11:30:39 +010053}
54
55pub struct InstanceStarter {
56 instance_name: String,
57 instance_root: PathBuf,
58 instance_image: PathBuf,
Jooyung Hanccae66d2021-12-20 11:54:36 +090059 idsig: PathBuf,
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -080060 idsig_manifest_apk: PathBuf,
Alan Stokesd21764c2021-10-25 15:33:40 +010061 vm_parameters: VmParameters,
Alan Stokes6b2d0a82021-09-29 11:30:39 +010062}
63
64impl InstanceStarter {
Alan Stokesd21764c2021-10-25 15:33:40 +010065 pub fn new(instance_name: &str, vm_parameters: VmParameters) -> Self {
Alan Stokes6b2d0a82021-09-29 11:30:39 +010066 let instance_root = Path::new(COMPOS_DATA_ROOT).join(instance_name);
Jooyung Hanccae66d2021-12-20 11:54:36 +090067 let instance_root_path = instance_root.as_path();
68 let instance_image = instance_root_path.join(INSTANCE_IMAGE_FILE);
69 let idsig = instance_root_path.join(IDSIG_FILE);
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -080070 let idsig_manifest_apk = instance_root_path.join(IDSIG_MANIFEST_APK_FILE);
Alan Stokes6b2d0a82021-09-29 11:30:39 +010071 Self {
72 instance_name: instance_name.to_owned(),
73 instance_root,
74 instance_image,
Jooyung Hanccae66d2021-12-20 11:54:36 +090075 idsig,
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -080076 idsig_manifest_apk,
Alan Stokesd21764c2021-10-25 15:33:40 +010077 vm_parameters,
Alan Stokes6b2d0a82021-09-29 11:30:39 +010078 }
79 }
80
Alan Stokes6542fdd2022-02-17 15:21:46 +000081 pub fn start_new_instance(
Alan Stokes6b2d0a82021-09-29 11:30:39 +010082 &self,
83 virtualization_service: &dyn IVirtualizationService,
84 ) -> Result<CompOsInstance> {
85 info!("Creating {} CompOs instance", self.instance_name);
86
87 // Ignore failure here - the directory may already exist.
88 let _ = fs::create_dir(&self.instance_root);
89
Alan Stokes6542fdd2022-02-17 15:21:46 +000090 // Overwrite any existing instance - it's unlikely to be valid with the current set
91 // of APEXes, and finding out it isn't is much more expensive than creating a new one.
Alan Stokes6b2d0a82021-09-29 11:30:39 +010092 self.create_instance_image(virtualization_service)?;
Alan Stokes6542fdd2022-02-17 15:21:46 +000093
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -080094 // Delete existing idsig files. Ignore error in case idsig doesn't exist.
Jooyung Hanccae66d2021-12-20 11:54:36 +090095 let _ = fs::remove_file(&self.idsig);
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -080096 let _ = fs::remove_file(&self.idsig_manifest_apk);
Alan Stokes6b2d0a82021-09-29 11:30:39 +010097
Alan Stokes5430eca2022-03-21 14:02:09 +000098 let instance = self.start_vm(virtualization_service)?;
99
100 // Retrieve the VM's attestation chain as a BCC and save it in the instance directory.
101 let bcc = instance.service.getAttestationChain().context("Getting attestation chain")?;
102 fs::write(self.instance_root.join("bcc"), bcc).context("Writing BCC")?;
103
104 Ok(instance)
Alan Stokes16e027f2021-10-04 17:57:31 +0100105 }
106
Alan Stokesd21764c2021-10-25 15:33:40 +0100107 fn start_vm(
108 &self,
109 virtualization_service: &dyn IVirtualizationService,
110 ) -> Result<CompOsInstance> {
Alan Stokes16e027f2021-10-04 17:57:31 +0100111 let instance_image = fs::OpenOptions::new()
112 .read(true)
113 .write(true)
114 .open(&self.instance_image)
115 .context("Failed to open instance image")?;
Andrew Walbrand0ef4002022-05-16 16:14:10 +0000116 let vm_instance = ComposClient::start(
Jooyung Hanccae66d2021-12-20 11:54:36 +0900117 virtualization_service,
118 instance_image,
119 &self.idsig,
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -0800120 &self.idsig_manifest_apk,
Jooyung Hanccae66d2021-12-20 11:54:36 +0900121 &self.vm_parameters,
122 )
123 .context("Starting VM")?;
Alan Stokes71403772022-06-21 14:56:28 +0100124 let service = vm_instance.connect_service().context("Connecting to CompOS")?;
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100125 Ok(CompOsInstance { vm_instance, service, lazy_service_guard: Default::default() })
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100126 }
127
128 fn create_instance_image(
129 &self,
130 virtualization_service: &dyn IVirtualizationService,
131 ) -> Result<()> {
132 let instance_image = fs::OpenOptions::new()
133 .create(true)
Alan Stokes23b90ee2021-10-28 11:36:14 +0100134 .truncate(true)
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100135 .read(true)
136 .write(true)
137 .open(&self.instance_image)
138 .context("Creating instance image file")?;
139 let instance_image = ParcelFileDescriptor::new(instance_image);
140 // TODO: Where does this number come from?
141 let size = 10 * 1024 * 1024;
142 virtualization_service
143 .initializeWritablePartition(&instance_image, size, PartitionType::ANDROID_VM_INSTANCE)
144 .context("Writing instance image file")?;
145 Ok(())
146 }
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100147}