Aidan Wolter | bb91a6b | 2024-07-01 15:42:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2024 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 | //! Manages running instances of the Microfuchsia VM. |
| 18 | //! At most one instance should be running at a time. |
| 19 | |
| 20 | use crate::instance_starter::{InstanceStarter, MicrofuchsiaInstance}; |
| 21 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice; |
| 22 | use anyhow::{bail, Result}; |
| 23 | use binder::Strong; |
| 24 | use virtualizationservice::IVirtualizationService::IVirtualizationService; |
| 25 | |
| 26 | pub struct InstanceManager { |
| 27 | service: Strong<dyn IVirtualizationService>, |
| 28 | started: bool, |
| 29 | } |
| 30 | |
| 31 | impl InstanceManager { |
| 32 | pub fn new(service: Strong<dyn IVirtualizationService>) -> Self { |
| 33 | Self { service, started: false } |
| 34 | } |
| 35 | |
| 36 | pub fn start_instance(&mut self) -> Result<MicrofuchsiaInstance> { |
| 37 | if self.started { |
| 38 | bail!("Cannot start multiple microfuchsia instances"); |
| 39 | } |
| 40 | |
| 41 | let instance_starter = InstanceStarter::new("Microfuchsia", 0); |
| 42 | let instance = instance_starter.start_new_instance(&*self.service); |
| 43 | |
| 44 | if instance.is_ok() { |
| 45 | self.started = true; |
| 46 | } |
| 47 | instance |
| 48 | } |
| 49 | } |