Debian Codegraph @

vignettes/parallelly-25-sandbox-workers.md

raw
1 <!--
2 %\VignetteIndexEntry{Parallel Workers Running in a Sandbox}
3 %\VignetteAuthor{Henrik Bengtsson}
4 %\VignetteKeyword{R}
5 %\VignetteKeyword{package}
6 %\VignetteKeyword{vignette}
7 %\VignetteKeyword{Docker}
8 %\VignetteKeyword{Apptainer}
9 %\VignetteEngine{parallelly::selfonly}
10 -->
11
12
13 # Introduction
14
15 This vignette shows how to set up "sandboxed" parallel workers with
16 limited access to the host system.
17
18 # Examples
19
20 ## Example: Bubblewrap on Linux
21
22 This example sets up two parallel workers on Linux sandboxed using
23 [Bubblewrap].
24
25 ```r
26 library(parallelly)
27
28 bwrap_sandbox <- function(rscript = "*") {
29 ro_binds <- function(dirs) {
30 dirs <- unique(dirs[file_test("-d", dirs)])
31 opts <- rep(dirs, each = 3L)
32 opts[seq(from = 1, to = length(opts), by = 3)] <- "--ro-bind"
33 opts
34 }
35
36 ro_rlibs_remap <- function(dirs = rev(rev(.libPaths())[-1])) {
37 dirs <- unique(dirs[file_test("-d", dirs)])
38 dirs2 <- sub(sprintf("^%s", Sys.getenv("HOME")), "/home/sandbox-user", dirs)
39 opts <- rep(dirs, each = 3L)
40 opts[seq(from = 1, to = length(opts), by = 3)] <- "--ro-bind"
41 opts[seq(from = 3, to = length(opts), by = 3)] <- dirs2
42 opts
43 }
44
45 args <- c("bwrap")
46
47 ## Unshares
48 ## Note, we cannot sandbox the network (--unshare-net), because
49 ## PSOCK clusters communicate over socket connections
50 unshares <- c(
51 "--unshare-user", # isolate user and group ids
52 "--unshare-pid", # isolate processes
53 "--proc", "/proc",
54 "--unshare-ipc" # isolate process communication, e.g. shared memory
55 )
56 args <- c(args, unshares)
57
58 ## Misc options
59 opts <- c(
60 "--dev", "/dev", # mount host's /dev
61 "--tmpfs", "/tmp" # mount fresh, private, empty temporary directory
62 )
63 args <- c(args, opts)
64
65 ## Read-only Linux mounts
66 dirs <- c("/usr", "/bin", "/usr/bin", "/lib", "/lib64", "/etc/alternatives")
67
68 ## Use host's R and Rscript (by read-only mounting R home folders)
69 components <- c("bin", "lib", "doc", "etc", "include", "modules", "share")
70 r_dirs <- unname(vapply(components, FUN = R.home, FUN.VALUE = NA_character_))
71 r_dirs <- c(r_dirs, dirname(Sys.which("R")), dirname(Sys.which("Rscript")))
72 r_dirs <- c(r_dirs, rev(.libPaths())[1])
73 dirs <- c(dirs, r_dirs)
74 args <- c(args, ro_binds(dirs))
75
76 ## Remap HOME to fresh, private sandboxed HOME
77 tmp_home <- tempfile(pattern = "sandbox-home-")
78 dir.create(tmp_home)
79 opts <- c(
80 "--bind", tmp_home, "/home/sandbox-user",
81 "--setenv", "HOME", "/home/sandbox-user",
82 "--chdir", "/home/sandbox-user"
83 )
84 args <- c(args, opts)
85
86 ## Read-only remapped non-system R library paths
87 args <- c(args, ro_rlibs_remap())
88
89 c(args, rscript)
90 } ## bwrap_sandbox()
91
92
93 ## Launch two parallel workers inside a Bubblewrap sandbox
94 cl <- makeClusterPSOCK(2L, rscript = bwrap_sandbox("*"))
95 print(cl)
96 #> Socket cluster with 2 nodes on host 'localhost' (R version 4.5.2
97 #> (2025-10-31), platform x86_64-pc-linux-gnu)
98
99 host_user <- Sys.info()[["user"]]
100 host_user
101 #> "alice"
102
103 worker_user <- unlist(parallel::clusterEvalQ(cl, Sys.info()[["user"]]))
104 worker_user
105 #> [1] "unknown" "unknown"
106 ```
107
108 [Bubblewrap]: https://github.com/containers/bubblewrap