Skip to content

Commit 7d0c92d

Browse files
committed
wip
1 parent 889e878 commit 7d0c92d

3 files changed

Lines changed: 74 additions & 46 deletions

File tree

‎crates/openshell-driver-vm/scripts/openshell-vm-sandbox-init.sh‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ setup_overlay_root() {
234234
exit 1
235235
fi
236236

237-
mkdir -p /overlay /lower /newroot
237+
mkdir -p /overlay /lower /newroot /image-cache
238238
mount -o remount,ro / 2>/dev/null || true
239239
mount -t ext4 -o rw /dev/vdb /overlay
240240
mkdir -p /overlay/upper /overlay/work
@@ -252,7 +252,6 @@ setup_overlay_root() {
252252

253253
local lower_root="/lower"
254254
if [ -b /dev/vdc ]; then
255-
mkdir -p /image-cache
256255
mount -t ext4 -o ro /dev/vdc /image-cache
257256
if [ -d /image-cache/image-rootfs ]; then
258257
lower_root="/image-cache/image-rootfs"

‎crates/openshell-driver-vm/src/driver.rs‎

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -580,40 +580,10 @@ impl VmDriver {
580580
let is_gpu = spec.is_some_and(|s| s.gpu);
581581
let gpu_device = spec.map_or("", |s| s.gpu_device.as_str());
582582
let gpu_bdf = if is_gpu {
583-
let inventory = self
584-
.gpu_inventory
585-
.as_ref()
586-
.ok_or_else(|| Status::internal("GPU inventory not initialized"))?;
587-
match inventory
588-
.lock()
589-
.map_err(|e| Status::internal(format!("GPU inventory lock poisoned: {e}")))
590-
.and_then(|mut inv| {
591-
inv.assign(&sandbox.id, gpu_device)
592-
.map_err(Status::failed_precondition)
593-
}) {
594-
Ok(assignment) => {
595-
tracing::info!(
596-
sandbox_id = %sandbox.id,
597-
bdf = %assignment.bdf,
598-
gpu_name = %assignment.name,
599-
iommu_group = assignment.iommu_group,
600-
"assigned GPU to sandbox"
601-
);
602-
Some(assignment.bdf)
603-
}
604-
Err(err) => {
605-
return Err(err);
606-
}
607-
}
583+
Some(self.assign_gpu_to_record(&sandbox.id, gpu_device).await?)
608584
} else {
609585
None
610586
};
611-
if let Some(bdf) = gpu_bdf.as_ref()
612-
&& !self.record_gpu_assignment(&sandbox.id, bdf.clone()).await
613-
{
614-
self.release_gpu_and_subnet(&sandbox.id);
615-
return Err(Status::cancelled("sandbox provisioning cancelled"));
616-
}
617587

618588
let console_output = state_dir.join("rootfs-console.log");
619589
let mut command = Command::new(&self.launcher_bin);
@@ -905,15 +875,39 @@ impl VmDriver {
905875
}
906876
}
907877

908-
async fn record_gpu_assignment(&self, sandbox_id: &str, bdf: String) -> bool {
878+
async fn assign_gpu_to_record(
879+
&self,
880+
sandbox_id: &str,
881+
gpu_device: &str,
882+
) -> Result<String, Status> {
909883
let mut registry = self.registry.lock().await;
910884
match registry.get_mut(sandbox_id) {
911-
Some(record) if !record.deleting => {
912-
record.gpu_bdf = Some(bdf);
913-
true
914-
}
915-
_ => false,
885+
Some(record) if !record.deleting => {}
886+
_ => return Err(Status::cancelled("sandbox provisioning cancelled")),
916887
}
888+
889+
let inventory = self
890+
.gpu_inventory
891+
.as_ref()
892+
.ok_or_else(|| Status::internal("GPU inventory not initialized"))?;
893+
let assignment = inventory
894+
.lock()
895+
.map_err(|e| Status::internal(format!("GPU inventory lock poisoned: {e}")))?
896+
.assign(sandbox_id, gpu_device)
897+
.map_err(Status::failed_precondition)?;
898+
899+
let record = registry
900+
.get_mut(sandbox_id)
901+
.expect("sandbox record exists while registry lock is held");
902+
record.gpu_bdf = Some(assignment.bdf.clone());
903+
tracing::info!(
904+
sandbox_id = %sandbox_id,
905+
bdf = %assignment.bdf,
906+
gpu_name = %assignment.name,
907+
iommu_group = assignment.iommu_group,
908+
"assigned GPU to sandbox"
909+
);
910+
Ok(assignment.bdf)
917911
}
918912

919913
async fn fail_provisioning(

‎crates/openshell-driver-vm/src/rootfs.rs‎

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,21 @@ pub fn write_rootfs_image_file(
159159

160160
let tmp_path = temporary_injection_path(image_path);
161161
fs::write(&tmp_path, contents).map_err(|e| format!("write {}: {e}", tmp_path.display()))?;
162-
let _ = run_debugfs(image_path, &format!("rm {guest_path}"));
162+
let Some(quoted_guest_path) = debugfs_quote_absolute_path(guest_path) else {
163+
let _ = fs::remove_file(&tmp_path);
164+
return Err(format!("invalid debugfs guest path '{guest_path}'"));
165+
};
166+
let Some(quoted_tmp_path) = debugfs_quote_argument(&tmp_path.to_string_lossy()) else {
167+
let _ = fs::remove_file(&tmp_path);
168+
return Err(format!(
169+
"invalid debugfs injection path '{}'",
170+
tmp_path.display()
171+
));
172+
};
173+
let _ = run_debugfs(image_path, &format!("rm {quoted_guest_path}"));
163174
let result = run_debugfs(
164175
image_path,
165-
&format!("write {} {}", tmp_path.display(), guest_path),
176+
&format!("write {quoted_tmp_path} {quoted_guest_path}"),
166177
);
167178
let _ = fs::remove_file(&tmp_path);
168179
result
@@ -174,9 +185,12 @@ pub fn set_rootfs_image_file_mode(
174185
mode: u32,
175186
) -> Result<(), String> {
176187
let regular_file_mode = 0o100_000 | (mode & 0o7777);
188+
let Some(quoted_guest_path) = debugfs_quote_absolute_path(guest_path) else {
189+
return Err(format!("invalid debugfs guest path '{guest_path}'"));
190+
};
177191
run_debugfs(
178192
image_path,
179-
&format!("set_inode_field {guest_path} mode 0{regular_file_mode:o}"),
193+
&format!("set_inode_field {quoted_guest_path} mode 0{regular_file_mode:o}"),
180194
)
181195
}
182196

@@ -539,7 +553,7 @@ fn collect_sandbox_owner_commands(
539553
return Ok(true);
540554
}
541555

542-
let Some(quoted_guest_path) = debugfs_quote_path(guest_path) else {
556+
let Some(quoted_guest_path) = debugfs_quote_absolute_path(guest_path) else {
543557
return Ok(false);
544558
};
545559
commands.push(format!("set_inode_field {quoted_guest_path} uid {uid}"));
@@ -579,14 +593,22 @@ fn collect_sandbox_owner_commands(
579593
Ok(true)
580594
}
581595

582-
fn debugfs_quote_path(path: &str) -> Option<String> {
596+
fn debugfs_quote_absolute_path(path: &str) -> Option<String> {
583597
if path.is_empty() || !path.starts_with('/') {
584598
return None;
585599
}
586600

587-
let mut quoted = String::with_capacity(path.len() + 2);
601+
debugfs_quote_argument(path)
602+
}
603+
604+
fn debugfs_quote_argument(argument: &str) -> Option<String> {
605+
if argument.is_empty() {
606+
return None;
607+
}
608+
609+
let mut quoted = String::with_capacity(argument.len() + 2);
588610
quoted.push('"');
589-
for ch in path.chars() {
611+
for ch in argument.chars() {
590612
match ch {
591613
'\0' | '\n' | '\r' => return None,
592614
'\\' => quoted.push_str("\\\\"),
@@ -1084,6 +1106,19 @@ mod tests {
10841106
let _ = fs::remove_dir_all(&dir);
10851107
}
10861108

1109+
#[test]
1110+
fn debugfs_quote_argument_quotes_source_paths_with_spaces() {
1111+
assert_eq!(
1112+
debugfs_quote_argument("/tmp/openshell state/.openshell-rootfs-inject-123-0"),
1113+
Some("\"/tmp/openshell state/.openshell-rootfs-inject-123-0\"".to_string())
1114+
);
1115+
assert_eq!(
1116+
debugfs_quote_argument("/tmp/path/with\\backslash/and\"quote"),
1117+
Some("\"/tmp/path/with\\\\backslash/and\\\"quote\"".to_string())
1118+
);
1119+
assert_eq!(debugfs_quote_argument("/tmp/bad\npath"), None);
1120+
}
1121+
10871122
fn unique_temp_dir() -> PathBuf {
10881123
static COUNTER: AtomicU64 = AtomicU64::new(0);
10891124
let nanos = SystemTime::now()

0 commit comments

Comments
 (0)