blob: f58a379c26dcb7dfa5811949d86e0ca789670cf6 [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::{
Elie Kheirallahb4b2f242025-01-23 03:38:07 +000020 CpuOptions::CpuOptions, CpuOptions::CpuTopology::CpuTopology,
21 IVirtualizationService::IVirtualizationService, VirtualMachineConfig::VirtualMachineConfig,
22 VirtualMachineRawConfig::VirtualMachineRawConfig,
Aidan Wolterbb91a6b2024-07-01 15:42:20 +000023};
24use anyhow::{ensure, Context, Result};
25use binder::{LazyServiceGuard, ParcelFileDescriptor};
26use log::info;
27use std::ffi::CStr;
28use std::fs::File;
Jiyong Parkaf63d1c2024-09-04 16:15:42 +090029use std::os::fd::FromRawFd;
Aidan Wolterbb91a6b2024-07-01 15:42:20 +000030use vmclient::VmInstance;
31
32pub struct MicrofuchsiaInstance {
33 _vm_instance: VmInstance,
34 _lazy_service_guard: LazyServiceGuard,
James Robinson6eea2b82024-11-19 17:28:00 +000035 _pty: Option<Pty>,
Aidan Wolterbb91a6b2024-07-01 15:42:20 +000036}
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.
James Robinson6eea2b82024-11-19 17:28:00 +000068 let (pty, console_in, console_out) = if cfg!(enable_console) {
69 let pty = openpty()?;
70 let console_in = Some(pty.leader.try_clone().context("cloning pty")?);
71 let console_out = Some(pty.leader.try_clone().context("cloning pty")?);
72 (Some(pty), console_in, console_out)
73 } else {
74 (None, None, None)
75 };
Aidan Wolterbb91a6b2024-07-01 15:42:20 +000076 let config = VirtualMachineConfig::RawConfig(VirtualMachineRawConfig {
77 name: "Microfuchsia".into(),
78 instanceId: instance_id,
79 kernel,
80 initrd,
81 params: None,
82 bootloader: None,
83 disks: vec![],
84 protectedVm: false,
85 memoryMib: 256,
Elie Kheirallahb4b2f242025-01-23 03:38:07 +000086 cpuOptions: CpuOptions { cpuTopology: CpuTopology::CpuCount(1) },
Aidan Wolterbb91a6b2024-07-01 15:42:20 +000087 platformVersion: "1.0.0".into(),
James Robinson6eea2b82024-11-19 17:28:00 +000088 #[cfg(enable_console)]
Aidan Wolterbb91a6b2024-07-01 15:42:20 +000089 consoleInputDevice: Some("ttyS0".into()),
Frederick Maylecc4e2d72024-12-06 16:54:40 -080090 balloon: true,
Aidan Wolterbb91a6b2024-07-01 15:42:20 +000091 ..Default::default()
92 });
93 let vm_instance = VmInstance::create(
94 virtualization_service,
95 &config,
96 console_out,
97 console_in,
98 /* log= */ None,
Elie Kheirallah5c807a22024-09-23 20:40:42 +000099 /* dump_dt= */ None,
Aidan Wolterbb91a6b2024-07-01 15:42:20 +0000100 )
101 .context("Failed to create VM")?;
James Robinson6eea2b82024-11-19 17:28:00 +0000102 if let Some(pty) = &pty {
103 vm_instance
104 .vm
105 .setHostConsoleName(&pty.follower_name)
106 .context("Setting host console name")?;
107 }
Jaewan Kimfcf98b22025-01-21 23:14:49 -0800108 vm_instance.start(None).context("Starting VM")?;
Aidan Wolterbb91a6b2024-07-01 15:42:20 +0000109
110 Ok(MicrofuchsiaInstance {
111 _vm_instance: vm_instance,
112 _lazy_service_guard: Default::default(),
113 _pty: pty,
114 })
115 }
116}
117
118struct Pty {
119 leader: File,
120 follower_name: String,
121}
122
123/// Opens a pseudoterminal (pty), configures it to be a raw terminal, and returns the file pair.
124fn openpty() -> Result<Pty> {
125 // Create a pty pair.
126 let mut leader: libc::c_int = -1;
127 let mut _follower: libc::c_int = -1;
128 let mut follower_name: Vec<libc::c_char> = vec![0; 32];
129
130 // SAFETY: calling openpty with valid+initialized variables is safe.
131 // The two null pointers are valid inputs for openpty.
132 unsafe {
133 ensure!(
134 libc::openpty(
135 &mut leader,
136 &mut _follower,
137 follower_name.as_mut_ptr(),
138 std::ptr::null_mut(),
139 std::ptr::null_mut(),
140 ) == 0,
141 "failed to openpty"
142 );
143 }
144
145 // SAFETY: calling these libc functions with valid+initialized variables is safe.
146 unsafe {
147 // Fetch the termios attributes.
148 let mut attr = libc::termios {
149 c_iflag: 0,
150 c_oflag: 0,
151 c_cflag: 0,
152 c_lflag: 0,
153 c_line: 0,
154 c_cc: [0u8; 19],
155 };
Jiyong Parkaf63d1c2024-09-04 16:15:42 +0900156 ensure!(libc::tcgetattr(leader, &mut attr) == 0, "failed to get termios attributes");
Aidan Wolterbb91a6b2024-07-01 15:42:20 +0000157
158 // Force it to be a raw pty and re-set it.
159 libc::cfmakeraw(&mut attr);
160 ensure!(
Jiyong Parkaf63d1c2024-09-04 16:15:42 +0900161 libc::tcsetattr(leader, libc::TCSANOW, &attr) == 0,
Aidan Wolterbb91a6b2024-07-01 15:42:20 +0000162 "failed to set termios attributes"
163 );
164 }
165
166 // Construct the return value.
Jiyong Parkaf63d1c2024-09-04 16:15:42 +0900167 // SAFETY: The file descriptors are valid because openpty returned without error (above).
168 let leader = unsafe { File::from_raw_fd(leader) };
Aidan Wolterbb91a6b2024-07-01 15:42:20 +0000169 let follower_name: Vec<u8> = follower_name.iter_mut().map(|x| *x as _).collect();
170 let follower_name = CStr::from_bytes_until_nul(&follower_name)
171 .context("pty filename missing NUL")?
172 .to_str()
173 .context("pty filename invalid utf8")?
174 .to_string();
Jiyong Parkaf63d1c2024-09-04 16:15:42 +0900175 Ok(Pty { leader, follower_name })
Aidan Wolterbb91a6b2024-07-01 15:42:20 +0000176}