Skip to content

Commit 9e65793

Browse files
committed
fix(vm): preserve image sandbox workdir
1 parent afda855 commit 9e65793

4 files changed

Lines changed: 30 additions & 9 deletions

File tree

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,16 +289,12 @@ mount -t tmpfs tmpfs /run 2>/dev/null &
289289
mount -t devtmpfs devtmpfs /dev 2>/dev/null &
290290
wait
291291

292-
mkdir -p /dev/pts /dev/shm /sys/fs/cgroup /sandbox
292+
mkdir -p /dev/pts /dev/shm /sys/fs/cgroup
293293
mount -t devpts devpts /dev/pts 2>/dev/null &
294294
mount -t tmpfs tmpfs /dev/shm 2>/dev/null &
295295
mount -t cgroup2 cgroup2 /sys/fs/cgroup 2>/dev/null &
296296
wait
297297

298-
mount -t tmpfs tmpfs /sandbox 2>/dev/null || true
299-
mkdir -p /sandbox
300-
chown sandbox:sandbox /sandbox 2>/dev/null || true
301-
302298
hostname openshell-sandbox-vm 2>/dev/null || true
303299
ip link set lo up 2>/dev/null || true
304300

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,6 @@ fn prepare_sandbox_rootfs(rootfs: &Path) -> Result<(), String> {
174174
fs::write(opt_dir.join(".rootfs-type"), "sandbox\n")
175175
.map_err(|e| format!("write sandbox rootfs marker: {e}"))?;
176176
ensure_sandbox_guest_user(rootfs)?;
177-
fs::create_dir_all(rootfs.join("sandbox"))
178-
.map_err(|e| format!("create sandbox workdir: {e}"))?;
179177

180178
Ok(())
181179
}
@@ -365,7 +363,7 @@ mod tests {
365363
assert!(!rootfs.join("opt/openshell/charts").exists());
366364
assert!(!rootfs.join("opt/openshell/manifests").exists());
367365
assert!(rootfs.join("srv/openshell-vm-sandbox-init.sh").is_file());
368-
assert!(rootfs.join("sandbox").is_dir());
366+
assert!(!rootfs.join("sandbox").exists());
369367
assert!(
370368
fs::read_to_string(rootfs.join("etc/passwd"))
371369
.expect("read passwd")
@@ -384,6 +382,30 @@ mod tests {
384382
let _ = fs::remove_dir_all(&dir);
385383
}
386384

385+
#[test]
386+
fn prepare_sandbox_rootfs_preserves_image_workdir_contents() {
387+
let dir = unique_temp_dir();
388+
let rootfs = dir.join("rootfs");
389+
390+
fs::create_dir_all(rootfs.join("opt/openshell/bin")).expect("create openshell bin");
391+
fs::write(
392+
rootfs.join("opt/openshell/bin/openshell-sandbox"),
393+
b"sandbox",
394+
)
395+
.expect("write openshell-sandbox");
396+
fs::create_dir_all(rootfs.join("sandbox")).expect("create sandbox workdir");
397+
fs::write(rootfs.join("sandbox/app.py"), "print('hello')\n").expect("write app");
398+
399+
prepare_sandbox_rootfs(&rootfs).expect("prepare sandbox rootfs");
400+
401+
assert_eq!(
402+
fs::read_to_string(rootfs.join("sandbox/app.py")).expect("read app"),
403+
"print('hello')\n"
404+
);
405+
406+
let _ = fs::remove_dir_all(&dir);
407+
}
408+
387409
#[cfg(unix)]
388410
#[test]
389411
fn create_rootfs_archive_preserves_broken_symlinks() {

examples/bring-your-own-container/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
2020
RUN groupadd -g 1000660000 sandbox && \
2121
useradd -m -u 1000660000 -g sandbox sandbox
2222

23+
RUN install -d -o sandbox -g sandbox /sandbox
2324
WORKDIR /sandbox
24-
COPY app.py .
25+
COPY --chown=sandbox:sandbox app.py .
2526

2627
EXPOSE 8080
2728

examples/bring-your-own-container/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ key requirements are:
6262
- **Create a `sandbox` user** (uid/gid 1000660000) for non-root execution.
6363
Use a high UID (1000000000+) to avoid conflicts with host users when running
6464
without user namespace remapping.
65+
- **Make your application workdir writable by `sandbox`**. This example creates
66+
`/sandbox` with `sandbox:sandbox` ownership before copying `app.py`.
6567
- **Install `iproute2`** for full network namespace isolation.
6668
- **Use a standard Linux base image** — distroless and `FROM scratch`
6769
images are not supported.

0 commit comments

Comments
 (0)