blob: 6688447e5b6e92cd5c6a55602f47ad6d04e7f4b3 [file] [log] [blame]
Aidan Wolterbb91a6b2024-07-01 15:42:20 +00001/*
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//! Responsible for starting an instance of the Microfuchsia VM.
18
19use android_system_virtualizationservice::aidl::android::system::virtualizationservice::{
20 CpuTopology::CpuTopology, IVirtualizationService::IVirtualizationService,
21 VirtualMachineConfig::VirtualMachineConfig, VirtualMachineRawConfig::VirtualMachineRawConfig,
22};
23use anyhow::{ensure, Context, Result};
24use binder::{LazyServiceGuard, ParcelFileDescriptor};
25use log::info;
Jiyong Park2867dd32024-08-27 16:48:30 +090026use safe_ownedfd::take_fd_ownership;
Aidan Wolterbb91a6b2024-07-01 15:42:20 +000027use std::ffi::CStr;
28use std::fs::File;
Jiyong Park2867dd32024-08-27 16:48:30 +090029use std::os::fd::AsRawFd;
Aidan Wolterbb91a6b2024-07-01 15:42:20 +000030use vmclient::VmInstance;
31
32pub struct MicrofuchsiaInstance {
33 _vm_instance: VmInstance,
34 _lazy_service_guard: LazyServiceGuard,
35 _pty: Pty,
36}
37
38pub struct InstanceStarter {
39 instance_name: String,
40 instance_id: u8,
41}
42
43impl InstanceStarter {
44 pub fn new(instance_name: &str, instance_id: u8) -> Self {
45 Self { instance_name: instance_name.to_owned(), instance_id }
46 }
47
48 pub fn start_new_instance(
49 &self,
50 virtualization_service: &dyn IVirtualizationService,
51 ) -> Result<MicrofuchsiaInstance> {
52 info!("Creating {} instance", self.instance_name);
53
54 // Always use instance id 0, because we will only ever have one instance.
55 let mut instance_id = [0u8; 64];
56 instance_id[0] = self.instance_id;
57
58 // Open the kernel and initrd files from the microfuchsia.images apex.
59 let kernel_fd =
60 File::open("/apex/com.android.microfuchsia.images/etc/linux-arm64-boot-shim.bin")
61 .context("Failed to open the boot-shim")?;
62 let initrd_fd = File::open("/apex/com.android.microfuchsia.images/etc/fuchsia.zbi")
63 .context("Failed to open the fuchsia ZBI")?;
64 let kernel = Some(ParcelFileDescriptor::new(kernel_fd));
65 let initrd = Some(ParcelFileDescriptor::new(initrd_fd));
66
67 // Prepare a pty for console input/output.
68 let pty = openpty()?;
69 let console_in = Some(pty.leader.try_clone().context("cloning pty")?);
70 let console_out = Some(pty.leader.try_clone().context("cloning pty")?);
71
72 let config = VirtualMachineConfig::RawConfig(VirtualMachineRawConfig {
73 name: "Microfuchsia".into(),
74 instanceId: instance_id,
75 kernel,
76 initrd,
77 params: None,
78 bootloader: None,
79 disks: vec![],
80 protectedVm: false,
81 memoryMib: 256,
82 cpuTopology: CpuTopology::ONE_CPU,
83 platformVersion: "1.0.0".into(),
84 // Fuchsia uses serial for console by default.
85 consoleInputDevice: Some("ttyS0".into()),
86 ..Default::default()
87 });
88 let vm_instance = VmInstance::create(
89 virtualization_service,
90 &config,
91 console_out,
92 console_in,
93 /* log= */ None,
94 None,
95 )
96 .context("Failed to create VM")?;
97 vm_instance
98 .vm
99 .setHostConsoleName(&pty.follower_name)
100 .context("Setting host console name")?;
101 vm_instance.start().context("Starting VM")?;
102
103 Ok(MicrofuchsiaInstance {
104 _vm_instance: vm_instance,
105 _lazy_service_guard: Default::default(),
106 _pty: pty,
107 })
108 }
109}
110
111struct Pty {
112 leader: File,
113 follower_name: String,
114}
115
116/// Opens a pseudoterminal (pty), configures it to be a raw terminal, and returns the file pair.
117fn openpty() -> Result<Pty> {
118 // Create a pty pair.
119 let mut leader: libc::c_int = -1;
120 let mut _follower: libc::c_int = -1;
121 let mut follower_name: Vec<libc::c_char> = vec![0; 32];
122
123 // SAFETY: calling openpty with valid+initialized variables is safe.
124 // The two null pointers are valid inputs for openpty.
125 unsafe {
126 ensure!(
127 libc::openpty(
128 &mut leader,
129 &mut _follower,
130 follower_name.as_mut_ptr(),
131 std::ptr::null_mut(),
132 std::ptr::null_mut(),
133 ) == 0,
134 "failed to openpty"
135 );
136 }
Jiyong Park2867dd32024-08-27 16:48:30 +0900137 let leader = take_fd_ownership(leader)?;
Aidan Wolterbb91a6b2024-07-01 15:42:20 +0000138
139 // SAFETY: calling these libc functions with valid+initialized variables is safe.
140 unsafe {
141 // Fetch the termios attributes.
142 let mut attr = libc::termios {
143 c_iflag: 0,
144 c_oflag: 0,
145 c_cflag: 0,
146 c_lflag: 0,
147 c_line: 0,
148 c_cc: [0u8; 19],
149 };
Jiyong Park2867dd32024-08-27 16:48:30 +0900150 ensure!(
151 libc::tcgetattr(leader.as_raw_fd(), &mut attr) == 0,
152 "failed to get termios attributes"
153 );
Aidan Wolterbb91a6b2024-07-01 15:42:20 +0000154
155 // Force it to be a raw pty and re-set it.
156 libc::cfmakeraw(&mut attr);
157 ensure!(
Jiyong Park2867dd32024-08-27 16:48:30 +0900158 libc::tcsetattr(leader.as_raw_fd(), libc::TCSANOW, &attr) == 0,
Aidan Wolterbb91a6b2024-07-01 15:42:20 +0000159 "failed to set termios attributes"
160 );
161 }
162
163 // Construct the return value.
Aidan Wolterbb91a6b2024-07-01 15:42:20 +0000164 let follower_name: Vec<u8> = follower_name.iter_mut().map(|x| *x as _).collect();
165 let follower_name = CStr::from_bytes_until_nul(&follower_name)
166 .context("pty filename missing NUL")?
167 .to_str()
168 .context("pty filename invalid utf8")?
169 .to_string();
Jiyong Park2867dd32024-08-27 16:48:30 +0900170 Ok(Pty { leader: File::from(leader), follower_name })
Aidan Wolterbb91a6b2024-07-01 15:42:20 +0000171}