Skip to content

Commit 5403e07

Browse files
committed
Resolve relative disk paths and image layout
1 parent c1dc64c commit 5403e07

3 files changed

Lines changed: 36 additions & 20 deletions

File tree

apps/admin/src/lib/provisioner.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,13 @@ export const PROVISION_STEPS: ProvisionStep[] = [
124124
const kernelUrl = await presignKernel()
125125
const rootfsUrl = await presignRootfs()
126126
return [
127-
'mkdir -p /var/sandchest/images',
128-
`curl -fsSL --retry 3 --retry-delay 5 '${kernelUrl}' -o /var/sandchest/images/vmlinux-5.10`,
129-
`curl -fsSL --retry 3 --retry-delay 5 '${rootfsUrl}' -o /var/sandchest/images/rootfs.ext4`,
130-
'chmod 644 /var/sandchest/images/vmlinux-5.10 /var/sandchest/images/rootfs.ext4',
127+
'mkdir -p /var/sandchest/images/ubuntu-22.04-base',
128+
`curl -fsSL --retry 3 --retry-delay 5 '${kernelUrl}' -o /var/sandchest/images/ubuntu-22.04-base/vmlinux`,
129+
`curl -fsSL --retry 3 --retry-delay 5 '${rootfsUrl}' -o /var/sandchest/images/ubuntu-22.04-base/rootfs.ext4`,
130+
'chmod 644 /var/sandchest/images/ubuntu-22.04-base/vmlinux /var/sandchest/images/ubuntu-22.04-base/rootfs.ext4',
131131
]
132132
},
133-
validate: 'test -f /var/sandchest/images/vmlinux-5.10 && test -f /var/sandchest/images/rootfs.ext4 && echo "images ok"',
133+
validate: 'test -f /var/sandchest/images/ubuntu-22.04-base/vmlinux && test -f /var/sandchest/images/ubuntu-22.04-base/rootfs.ext4 && echo "images ok"',
134134
},
135135
{
136136
id: 'install-certs-mtls',
@@ -214,7 +214,7 @@ nft -f /etc/nftables.conf && systemctl enable nftables`,
214214
'RestartSec=5',
215215
'EnvironmentFile=-/etc/sandchest/node.env',
216216
'Environment=RUST_LOG=info',
217-
'Environment=DATA_DIR=/var/sandchest',
217+
'Environment=SANDCHEST_DATA_DIR=/var/sandchest',
218218
'',
219219
'[Install]',
220220
'WantedBy=multi-user.target',

crates/sandchest-node/src/disk.rs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,30 @@ pub async fn clone_disk(src_ext4: &str, sandbox_id: &str, data_dir: &str) -> Res
1111
let sandbox_dir = format!("{}/sandboxes/{}", data_dir, sandbox_id);
1212
let dest = format!("{}/rootfs.ext4", sandbox_dir);
1313

14+
// Resolve relative source paths against data_dir
15+
let resolved_src = if Path::new(src_ext4).is_relative() {
16+
format!("{}/{}", data_dir, src_ext4)
17+
} else {
18+
src_ext4.to_string()
19+
};
20+
1421
// Create sandbox directory
1522
tokio::fs::create_dir_all(&sandbox_dir).await.map_err(|e| {
1623
DiskError::Io(format!("failed to create sandbox directory {}: {}", sandbox_dir, e))
1724
})?;
1825

19-
if !Path::new(src_ext4).exists() {
20-
return Err(DiskError::SourceNotFound(src_ext4.to_string()));
26+
if !Path::new(&resolved_src).exists() {
27+
return Err(DiskError::SourceNotFound(resolved_src));
2128
}
2229

2330
info!(
24-
src = %src_ext4,
31+
src = %resolved_src,
2532
dest = %dest,
2633
sandbox_id = %sandbox_id,
2734
"cloning disk with reflink"
2835
);
2936

30-
let src = src_ext4.to_string();
37+
let src = resolved_src;
3138
let dst = dest.clone();
3239

3340
// Use --reflink=auto on Linux for instant CoW clones on XFS/btrfs.
@@ -62,20 +69,27 @@ pub async fn clone_disk(src_ext4: &str, sandbox_id: &str, data_dir: &str) -> Res
6269
///
6370
/// Like `clone_disk` but allows specifying the target directory directly.
6471
/// The destination directory must already exist.
65-
pub async fn clone_disk_to(src_ext4: &str, dest_dir: &str) -> Result<String, DiskError> {
72+
pub async fn clone_disk_to(src_ext4: &str, dest_dir: &str, data_dir: &str) -> Result<String, DiskError> {
6673
let dest = format!("{}/rootfs.ext4", dest_dir);
6774

68-
if !Path::new(src_ext4).exists() {
69-
return Err(DiskError::SourceNotFound(src_ext4.to_string()));
75+
// Resolve relative source paths against data_dir
76+
let resolved_src = if Path::new(src_ext4).is_relative() {
77+
format!("{}/{}", data_dir, src_ext4)
78+
} else {
79+
src_ext4.to_string()
80+
};
81+
82+
if !Path::new(&resolved_src).exists() {
83+
return Err(DiskError::SourceNotFound(resolved_src));
7084
}
7185

7286
info!(
73-
src = %src_ext4,
87+
src = %resolved_src,
7488
dest = %dest,
7589
"cloning disk with reflink to target directory"
7690
);
7791

78-
let src = src_ext4.to_string();
92+
let src = resolved_src;
7993
let dst = dest.clone();
8094

8195
let output = if cfg!(target_os = "linux") {
@@ -258,7 +272,7 @@ mod tests {
258272
let dest_dir = tmp.join("target");
259273
std::fs::create_dir_all(&dest_dir).unwrap();
260274

261-
let result = clone_disk_to(src_file.to_str().unwrap(), dest_dir.to_str().unwrap()).await;
275+
let result = clone_disk_to(src_file.to_str().unwrap(), dest_dir.to_str().unwrap(), tmp.to_str().unwrap()).await;
262276
assert!(result.is_ok());
263277

264278
let dest = result.unwrap();
@@ -274,7 +288,7 @@ mod tests {
274288
let tmp = std::env::temp_dir().join("sandchest-disk-to-missing");
275289
std::fs::create_dir_all(&tmp).unwrap();
276290

277-
let result = clone_disk_to("/nonexistent/rootfs.ext4", tmp.to_str().unwrap()).await;
291+
let result = clone_disk_to("/nonexistent/rootfs.ext4", tmp.to_str().unwrap(), "/tmp").await;
278292
assert!(matches!(result.unwrap_err(), DiskError::SourceNotFound(_)));
279293

280294
let _ = std::fs::remove_dir_all(&tmp);

crates/sandchest-node/src/sandbox.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ impl SandboxManager {
203203
return Err(SandboxError::CreateFailed(format!("chroot setup failed: {}", e)));
204204
}
205205
}
206-
match disk::clone_disk_to(rootfs_ref, &paths.dir).await {
206+
match disk::clone_disk_to(rootfs_ref, &paths.dir, &self.node_config.data_dir).await {
207207
Ok(path) => path,
208208
Err(e) => {
209209
error!(sandbox_id = %sandbox_id, error = %e, "failed to clone disk to chroot");
@@ -354,7 +354,7 @@ impl SandboxManager {
354354
}
355355
}
356356
let _rootfs_path = if self.node_config.jailer.enabled {
357-
match disk::clone_disk_to(&snapshot_rootfs, &paths.dir).await {
357+
match disk::clone_disk_to(&snapshot_rootfs, &paths.dir, &self.node_config.data_dir).await {
358358
Ok(path) => path,
359359
Err(e) => {
360360
error!(sandbox_id = %sandbox_id, error = %e, "failed to clone snapshot disk");
@@ -687,7 +687,7 @@ impl SandboxManager {
687687

688688
// --- Step 3: Clone disk (while source is paused for consistency) ---
689689
let disk_result = if self.node_config.jailer.enabled {
690-
disk::clone_disk_to(&source_rootfs, &fork_sandbox_dir).await
690+
disk::clone_disk_to(&source_rootfs, &fork_sandbox_dir, &self.node_config.data_dir).await
691691
} else {
692692
disk::clone_disk(&source_rootfs, new_sandbox_id, &self.node_config.data_dir).await
693693
};
@@ -1089,6 +1089,8 @@ impl SandboxManager {
10891089
) -> Result<FirecrackerVm, SandboxError> {
10901090
let kernel_path = if kernel_ref.is_empty() {
10911091
self.node_config.kernel_path.clone()
1092+
} else if std::path::Path::new(kernel_ref).is_relative() {
1093+
format!("{}/{}", self.node_config.data_dir, kernel_ref)
10921094
} else {
10931095
kernel_ref.to_string()
10941096
};

0 commit comments

Comments
 (0)