blob: 4873d7a6aaa4abf4df32ad4a964d53642210be1b [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};
Alan Stokesd21764c2021-10-25 15:33:40 +010027use compos_common::compos_client::{VmInstance, 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
36 vm_instance: VmInstance,
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 }
45}
46
47pub struct InstanceStarter {
48 instance_name: String,
49 instance_root: PathBuf,
50 instance_image: PathBuf,
Jooyung Hanccae66d2021-12-20 11:54:36 +090051 idsig: PathBuf,
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -080052 idsig_manifest_apk: PathBuf,
Alan Stokesd21764c2021-10-25 15:33:40 +010053 vm_parameters: VmParameters,
Alan Stokes6b2d0a82021-09-29 11:30:39 +010054}
55
56impl InstanceStarter {
Alan Stokesd21764c2021-10-25 15:33:40 +010057 pub fn new(instance_name: &str, vm_parameters: VmParameters) -> Self {
Alan Stokes6b2d0a82021-09-29 11:30:39 +010058 let instance_root = Path::new(COMPOS_DATA_ROOT).join(instance_name);
Jooyung Hanccae66d2021-12-20 11:54:36 +090059 let instance_root_path = instance_root.as_path();
60 let instance_image = instance_root_path.join(INSTANCE_IMAGE_FILE);
61 let idsig = instance_root_path.join(IDSIG_FILE);
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -080062 let idsig_manifest_apk = instance_root_path.join(IDSIG_MANIFEST_APK_FILE);
Alan Stokes6b2d0a82021-09-29 11:30:39 +010063 Self {
64 instance_name: instance_name.to_owned(),
65 instance_root,
66 instance_image,
Jooyung Hanccae66d2021-12-20 11:54:36 +090067 idsig,
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -080068 idsig_manifest_apk,
Alan Stokesd21764c2021-10-25 15:33:40 +010069 vm_parameters,
Alan Stokes6b2d0a82021-09-29 11:30:39 +010070 }
71 }
72
Alan Stokes6542fdd2022-02-17 15:21:46 +000073 pub fn start_new_instance(
Alan Stokes6b2d0a82021-09-29 11:30:39 +010074 &self,
75 virtualization_service: &dyn IVirtualizationService,
76 ) -> Result<CompOsInstance> {
77 info!("Creating {} CompOs instance", self.instance_name);
78
79 // Ignore failure here - the directory may already exist.
80 let _ = fs::create_dir(&self.instance_root);
81
Alan Stokes6542fdd2022-02-17 15:21:46 +000082 // Overwrite any existing instance - it's unlikely to be valid with the current set
83 // of APEXes, and finding out it isn't is much more expensive than creating a new one.
Alan Stokes6b2d0a82021-09-29 11:30:39 +010084 self.create_instance_image(virtualization_service)?;
Alan Stokes6542fdd2022-02-17 15:21:46 +000085
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -080086 // Delete existing idsig files. Ignore error in case idsig doesn't exist.
Jooyung Hanccae66d2021-12-20 11:54:36 +090087 let _ = fs::remove_file(&self.idsig);
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -080088 let _ = fs::remove_file(&self.idsig_manifest_apk);
Alan Stokes6b2d0a82021-09-29 11:30:39 +010089
Alan Stokes6542fdd2022-02-17 15:21:46 +000090 self.start_vm(virtualization_service)
Alan Stokes16e027f2021-10-04 17:57:31 +010091 }
92
Alan Stokesd21764c2021-10-25 15:33:40 +010093 fn start_vm(
94 &self,
95 virtualization_service: &dyn IVirtualizationService,
96 ) -> Result<CompOsInstance> {
Alan Stokes16e027f2021-10-04 17:57:31 +010097 let instance_image = fs::OpenOptions::new()
98 .read(true)
99 .write(true)
100 .open(&self.instance_image)
101 .context("Failed to open instance image")?;
Jooyung Hanccae66d2021-12-20 11:54:36 +0900102 let vm_instance = VmInstance::start(
103 virtualization_service,
104 instance_image,
105 &self.idsig,
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -0800106 &self.idsig_manifest_apk,
Jooyung Hanccae66d2021-12-20 11:54:36 +0900107 &self.vm_parameters,
108 )
109 .context("Starting VM")?;
Alan Stokes16e027f2021-10-04 17:57:31 +0100110 let service = vm_instance.get_service().context("Connecting to CompOS")?;
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100111 Ok(CompOsInstance { vm_instance, service, lazy_service_guard: Default::default() })
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100112 }
113
114 fn create_instance_image(
115 &self,
116 virtualization_service: &dyn IVirtualizationService,
117 ) -> Result<()> {
118 let instance_image = fs::OpenOptions::new()
119 .create(true)
Alan Stokes23b90ee2021-10-28 11:36:14 +0100120 .truncate(true)
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100121 .read(true)
122 .write(true)
123 .open(&self.instance_image)
124 .context("Creating instance image file")?;
125 let instance_image = ParcelFileDescriptor::new(instance_image);
126 // TODO: Where does this number come from?
127 let size = 10 * 1024 * 1024;
128 virtualization_service
129 .initializeWritablePartition(&instance_image, size, PartitionType::ANDROID_VM_INSTANCE)
130 .context("Writing instance image file")?;
131 Ok(())
132 }
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100133}