1use crate::Disk;
7use crate::PetriInitrd;
8use crate::ResolvedArtifact;
9use crate::linux_bootstrap::AgentLocation;
10use crate::linux_bootstrap::GuestNetwork;
11use crate::mshv_image::MshvImage;
12use petri_artifacts_core::ArtifactResolver;
13use petri_artifacts_vmm_test::artifacts::loadable;
14use std::sync::Arc;
15
16#[derive(Debug, Clone)]
18pub enum MshvInitrd {
19 Prepared(PetriInitrd),
21 Base(ResolvedArtifact),
23}
24
25impl From<PetriInitrd> for MshvInitrd {
26 fn from(initrd: PetriInitrd) -> Self {
27 Self::Prepared(initrd)
28 }
29}
30
31#[derive(Debug, Clone)]
37pub struct MshvBoot {
38 pub host_boot: ResolvedArtifact,
40 pub kernel: ResolvedArtifact,
42 pub initrd: MshvInitrd,
44 pub boot_artifacts: ResolvedArtifact,
46 pub kernel_cmdline: String,
48 pub hypervisor_cmdline: String,
50}
51
52impl MshvBoot {
53 pub fn resolve(resolver: &ArtifactResolver<'_>) -> Self {
55 Self {
56 host_boot: resolver.require(loadable::MSHV_HOST_BOOT_X64).erase(),
57 kernel: resolver.require(loadable::MSHV_TEST_KERNEL_X64).erase(),
58 initrd: MshvInitrd::Base(
59 resolver
60 .require(loadable::LINUX_DIRECT_TEST_INITRD_X64)
61 .erase(),
62 ),
63 boot_artifacts: resolver.require(loadable::MSHV_BOOT_ARTIFACTS_X64).erase(),
64 kernel_cmdline: "console=hvc0 panic=-1 8250.nr_uarts=0".into(),
65 hypervisor_cmdline: "MSHV_ROOT=\\Windows MSHV_ENABLE=TRUE MSHV_SCHEDULER_TYPE=ROOT MSHV_X2APIC_POLICY=ENABLE MSHV_FORCE_NESTED=TRUE".into(),
66 }
67 }
68
69 pub(crate) fn prepare_initrd(&self, pipette: &ResolvedArtifact) -> anyhow::Result<PetriInitrd> {
70 let MshvInitrd::Base(base) = &self.initrd else {
71 anyhow::bail!("mshv initrd is already prepared");
72 };
73 crate::linux_bootstrap::prepare_initrd(
74 base.get(),
75 GuestNetwork::Consomme,
76 AgentLocation::Initrd(pipette.get()),
77 true,
78 None,
79 )
80 }
81
82 pub(crate) fn build(
87 &self,
88 use_virtio_vsock: bool,
89 prepared: Option<&PetriInitrd>,
90 ) -> anyhow::Result<Disk> {
91 let initrd = match &self.initrd {
92 MshvInitrd::Prepared(initrd) => initrd,
93 MshvInitrd::Base(_) => prepared.ok_or_else(|| {
94 anyhow::anyhow!("mshv base initrd requires prepared Pipette userspace")
95 })?,
96 };
97 let image = tempfile::NamedTempFile::new()?.into_temp_path();
98 fs_err::remove_file(&image)?;
99 let boot = self.boot_artifacts.get().join("boot");
100 MshvImage {
101 host_boot: self.host_boot.get(),
102 payload: &boot,
103 kernel: self.kernel.get(),
104 initrd: initrd.path.as_ref(),
105 kernel_cmdline: &format!(
106 "{} {} rdinit={}",
107 self.kernel_cmdline,
108 crate::vm::linux_vsock_blacklist(use_virtio_vsock),
109 initrd.rdinit_param
110 ),
111 hypervisor_cmdline: &self.hypervisor_cmdline,
112 }
113 .build(&image)?;
114 Ok(Disk::Temporary(Arc::new(image)))
115 }
116}
117
118#[cfg(test)]
119mod tests {
120 use super::*;
121 use std::io::Read;
122 use test_with_tracing::test;
123
124 #[test]
125 fn explicit_inputs_build_guest_media_without_a_vm() -> anyhow::Result<()> {
126 let temp = tempfile::tempdir()?;
127 let boot = temp.path().join("boot");
128 fs_err::create_dir_all(boot.join("Windows/System32"))?;
129 for name in [
130 "HvLoader.efi",
131 "lxhvloader.dll",
132 "Windows/System32/hvix64.exe",
133 "Windows/System32/hvax64.exe",
134 "mshv_host_boot.efi",
135 ] {
136 fs_err::write(boot.join(name), name)?;
137 }
138 let kernel = temp.path().join("root-kernel");
139 let initrd_path = temp.path().join("prepared-initrd");
140 fs_err::write(&kernel, b"explicit root kernel")?;
141 let prepared = b"prepared initrd contents";
142 fs_err::write(&initrd_path, prepared)?;
143 let initrd = PetriInitrd {
144 path: crate::TempOrPersistentPath::Persistent(initrd_path.clone()),
145 rdinit_param: "/caller-init".into(),
146 };
147 let disk = MshvBoot {
148 host_boot: ResolvedArtifact::from_local_path(boot.join("mshv_host_boot.efi")),
149 kernel: ResolvedArtifact::from_local_path(kernel),
150 initrd: initrd.into(),
151 boot_artifacts: ResolvedArtifact::from_local_path(temp.path().to_path_buf()),
152 kernel_cmdline: "test_option=caller".into(),
153 hypervisor_cmdline: "MSHV_SCHEDULER_TYPE=ROOT".into(),
154 }
155 .build(true, None)?;
156 let Disk::Temporary(path) = &disk else {
157 panic!("expected owned temporary disk")
158 };
159 let output = path.to_path_buf();
160 let mut image = fs_err::File::open(&output)?;
161 let gpt = gptman::GPT::read_from(&mut image, 512)?;
162 let partition = fscommon::StreamSlice::new(
163 image,
164 gpt[1].starting_lba * 512,
165 (gpt[1].ending_lba + 1) * 512,
166 )?;
167 let fs = fatfs::FileSystem::new(partition, fatfs::FsOptions::new())?;
168 let mut bytes = Vec::new();
169 fs.root_dir()
170 .open_file("bzImage")?
171 .read_to_end(&mut bytes)?;
172 assert_eq!(bytes, b"explicit root kernel");
173 bytes.clear();
174 fs.root_dir()
175 .open_file("initramfs.cpio")?
176 .read_to_end(&mut bytes)?;
177 assert_eq!(bytes, prepared);
178 let mut config = String::new();
179 fs.root_dir()
180 .open_file(mshv_host_boot::KERNEL_CMDLINE_FILE)?
181 .read_to_string(&mut config)?;
182 assert_eq!(
183 config,
184 "initrd=\\initramfs.cpio test_option=caller initcall_blacklist=hv_sock_init rdinit=/caller-init\n"
185 );
186 config.clear();
187 fs.root_dir()
188 .open_file(mshv_host_boot::HYPERVISOR_CMDLINE_FILE)?
189 .read_to_string(&mut config)?;
190 assert_eq!(config, "\\lxhvloader.dll MSHV_SCHEDULER_TYPE=ROOT\n");
191 fs.unmount()?;
192 assert_eq!(fs_err::read(&initrd_path)?, prepared);
193 drop(disk);
194 assert!(!output.exists());
195 Ok(())
196 }
197
198 #[test]
199 fn base_initrd_must_be_prepared_before_media_composition() {
200 let unused = || ResolvedArtifact::from_local_path("unused".into());
201 let boot = MshvBoot {
202 host_boot: unused(),
203 kernel: unused(),
204 initrd: MshvInitrd::Base(unused()),
205 boot_artifacts: unused(),
206 kernel_cmdline: "test_option=caller".into(),
207 hypervisor_cmdline: "MSHV_SCHEDULER_TYPE=ROOT".into(),
208 };
209 let error = boot.build(true, None).unwrap_err();
210 assert!(
211 error
212 .to_string()
213 .contains("requires prepared Pipette userspace")
214 );
215 }
216}