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