Debian Codegraph @

inst/doc/parallelly-25-sandbox-workers.html

raw
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <title>Parallel Workers Running in a Sandbox</title>
6 <style>
7 body {
8 font-family: sans-serif;
9 line-height: 1.6;
10 padding-left: 3ex;
11 padding-right: 3ex;
12 background-color: white;
13 color: black;
14 }
15
16 a {
17 color: #4183C4;
18 text-decoration: none;
19 }
20
21 h1, h2, h3 {
22 margin: 2ex 0 1ex;
23 padding: 0;
24 font-weight: bold;
25 -webkit-font-smoothing: antialiased;
26 cursor: text;
27 position: relative;
28 }
29
30 h2 {
31 border-bottom: 1px solid #cccccc;
32 }
33
34 code {
35 margin: 0 2px;
36 padding: 0 5px;
37 white-space: nowrap;
38 border: 1px solid #eaeaea;
39 background-color: #f8f8f8;
40 border-radius: 3px;
41 }
42
43 pre code {
44 margin: 0;
45 padding: 0;
46 white-space: pre;
47 border: none;
48 background: transparent;
49 }
50
51 pre {
52 background-color: #f8f8f8;
53 border: 1px solid #cccccc;
54 line-height: 2.5x;
55 overflow: auto;
56 padding: 0.6ex 1ex;
57 border-radius: 3px;
58 }
59
60 pre code {
61 background-color: transparent;
62 border: none;
63 }
64 </style>
65 </head>
66 <body>
67 <h1>Parallel Workers Running in a Sandbox</h1>
68 <!--
69 %\VignetteIndexEntry{Parallel Workers Running in a Sandbox}
70 %\VignetteAuthor{Henrik Bengtsson}
71 %\VignetteKeyword{R}
72 %\VignetteKeyword{package}
73 %\VignetteKeyword{vignette}
74 %\VignetteKeyword{Docker}
75 %\VignetteKeyword{Apptainer}
76 %\VignetteEngine{parallelly::selfonly}
77 -->
78 <h1>Introduction</h1>
79 <p>This vignette shows how to set up “sandboxed” parallel workers with
80 limited access to the host system.</p>
81 <h1>Examples</h1>
82 <h2>Example: Bubblewrap on Linux</h2>
83 <p>This example sets up two parallel workers on Linux sandboxed using
84 <a href="https://github.com/containers/bubblewrap">Bubblewrap</a>.</p>
85 <pre><code class="language-r">library(parallelly)
86
87 bwrap_sandbox &lt;- function(rscript = &quot;*&quot;) {
88 ro_binds &lt;- function(dirs) {
89 dirs &lt;- unique(dirs[file_test(&quot;-d&quot;, dirs)])
90 opts &lt;- rep(dirs, each = 3L)
91 opts[seq(from = 1, to = length(opts), by = 3)] &lt;- &quot;--ro-bind&quot;
92 opts
93 }
94
95 ro_rlibs_remap &lt;- function(dirs = rev(rev(.libPaths())[-1])) {
96 dirs &lt;- unique(dirs[file_test(&quot;-d&quot;, dirs)])
97 dirs2 &lt;- sub(sprintf(&quot;^%s&quot;, Sys.getenv(&quot;HOME&quot;)), &quot;/home/sandbox-user&quot;, dirs)
98 opts &lt;- rep(dirs, each = 3L)
99 opts[seq(from = 1, to = length(opts), by = 3)] &lt;- &quot;--ro-bind&quot;
100 opts[seq(from = 3, to = length(opts), by = 3)] &lt;- dirs2
101 opts
102 }
103
104 args &lt;- c(&quot;bwrap&quot;)
105
106 ## Unshares
107 ## Note, we cannot sandbox the network (--unshare-net), because
108 ## PSOCK clusters communicate over socket connections
109 unshares &lt;- c(
110 &quot;--unshare-user&quot;, # isolate user and group ids
111 &quot;--unshare-pid&quot;, # isolate processes
112 &quot;--proc&quot;, &quot;/proc&quot;,
113 &quot;--unshare-ipc&quot; # isolate process communication, e.g. shared memory
114 )
115 args &lt;- c(args, unshares)
116
117 ## Misc options
118 opts &lt;- c(
119 &quot;--dev&quot;, &quot;/dev&quot;, # mount host's /dev
120 &quot;--tmpfs&quot;, &quot;/tmp&quot; # mount fresh, private, empty temporary directory
121 )
122 args &lt;- c(args, opts)
123
124 ## Read-only Linux mounts
125 dirs &lt;- c(&quot;/usr&quot;, &quot;/bin&quot;, &quot;/usr/bin&quot;, &quot;/lib&quot;, &quot;/lib64&quot;, &quot;/etc/alternatives&quot;)
126
127 ## Use host's R and Rscript (by read-only mounting R home folders)
128 components &lt;- c(&quot;bin&quot;, &quot;lib&quot;, &quot;doc&quot;, &quot;etc&quot;, &quot;include&quot;, &quot;modules&quot;, &quot;share&quot;)
129 r_dirs &lt;- unname(vapply(components, FUN = R.home, FUN.VALUE = NA_character_))
130 r_dirs &lt;- c(r_dirs, dirname(Sys.which(&quot;R&quot;)), dirname(Sys.which(&quot;Rscript&quot;)))
131 r_dirs &lt;- c(r_dirs, rev(.libPaths())[1])
132 dirs &lt;- c(dirs, r_dirs)
133 args &lt;- c(args, ro_binds(dirs))
134
135 ## Remap HOME to fresh, private sandboxed HOME
136 tmp_home &lt;- tempfile(pattern = &quot;sandbox-home-&quot;)
137 dir.create(tmp_home)
138 opts &lt;- c(
139 &quot;--bind&quot;, tmp_home, &quot;/home/sandbox-user&quot;,
140 &quot;--setenv&quot;, &quot;HOME&quot;, &quot;/home/sandbox-user&quot;,
141 &quot;--chdir&quot;, &quot;/home/sandbox-user&quot;
142 )
143 args &lt;- c(args, opts)
144
145 ## Read-only remapped non-system R library paths
146 args &lt;- c(args, ro_rlibs_remap())
147
148 c(args, rscript)
149 } ## bwrap_sandbox()
150
151
152 ## Launch two parallel workers inside a Bubblewrap sandbox
153 cl &lt;- makeClusterPSOCK(2L, rscript = bwrap_sandbox(&quot;*&quot;))
154 print(cl)
155 #&gt; Socket cluster with 2 nodes on host 'localhost' (R version 4.5.2
156 #&gt; (2025-10-31), platform x86_64-pc-linux-gnu)
157
158 host_user &lt;- Sys.info()[[&quot;user&quot;]]
159 host_user
160 #&gt; &quot;alice&quot;
161
162 worker_user &lt;- unlist(parallel::clusterEvalQ(cl, Sys.info()[[&quot;user&quot;]]))
163 worker_user
164 #&gt; [1] &quot;unknown&quot; &quot;unknown&quot;
165 </code></pre>
166 </body>
167 </html>