containerization-framework 0.5.0

Rust bindings for Apple's Containerization framework: Linux containers in lightweight VMs, in-process and without a daemon.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
//! `LinuxContainer`, and its nested `LinuxContainer.Configuration`.

use super::ContainerStatistics;
use super::FilesystemOperation;
use super::Mount;
use super::StatCategory;
use super::UnixSocketConfiguration;
use crate::containerization::GIB;
use crate::containerization::network;
use crate::containerization::process;
use crate::containerization::strings;
use crate::containerization::vm;
use crate::containerization_oci;
use crate::containerization_os::terminal;
use crate::error::Error;
use crate::platform;
use crate::platform::ffi;
use std::collections::BTreeMap;
use std::fmt;
use std::os::fd::FromRawFd;
use std::os::fd::OwnedFd;
use std::path::Path;

/// `LinuxContainer.Configuration.SeccompProfile`.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub enum SeccompProfile {
  #[default]
  Unconfined,
  Default,
  /// A profile of its own. [`containerization_oci::runtime::LinuxSeccomp::decode`]
  /// reads one from JSON.
  Profile(containerization_oci::runtime::LinuxSeccomp),
}

/// `LinuxContainer.Configuration`.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Configuration {
  pub process: process::LinuxProcessConfiguration,
  pub cpus: u32,
  pub memory_in_bytes: u64,
  pub hostname: Option<String>,
  pub sysctl: BTreeMap<String, String>,
  pub interfaces: Vec<network::Interface>,
  pub sockets: Vec<UnixSocketConfiguration>,
  pub mounts: Vec<Mount>,
  pub masked_paths: Vec<String>,
  pub readonly_paths: Vec<String>,
  pub dns: Option<network::DNS>,
  pub hosts: Option<network::Hosts>,
  pub virtualization: bool,
  pub boot_log: Option<vm::BootLog>,
  pub oci_runtime_path: Option<String>,
  pub seccomp_profile: SeccompProfile,
  pub use_init: bool,
}

/// `LinuxContainer.Configuration()`.
impl Default for Configuration {
  fn default() -> Self {
    Self {
      process: process::LinuxProcessConfiguration::default(),
      cpus: 4,
      memory_in_bytes: GIB,
      hostname: None,
      sysctl: BTreeMap::new(),
      interfaces: Vec::new(),
      sockets: Vec::new(),
      mounts: LinuxContainer::default_mounts(),
      masked_paths: LinuxContainer::default_masked_paths(),
      readonly_paths: LinuxContainer::default_readonly_paths(),
      dns: None,
      hosts: None,
      virtualization: false,
      boot_log: None,
      oci_runtime_path: None,
      seccomp_profile: SeccompProfile::Unconfined,
      use_init: false,
    }
  }
}

/// `copyIn(from:to:mode:createParents:chunkSize:)`'s defaults.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct CopyInOptions {
  pub mode: u32,
  pub create_parents: bool,
  pub chunk_size: usize,
}

/// Mode `0o644`, creating parents, in chunks of
/// [`LinuxContainer::DEFAULT_COPY_CHUNK_SIZE`].
impl Default for CopyInOptions {
  fn default() -> Self {
    Self {
      mode: 0o644,
      create_parents: true,
      chunk_size: LinuxContainer::DEFAULT_COPY_CHUNK_SIZE,
    }
  }
}

/// `copyOut(from:to:createParents:chunkSize:)`'s defaults.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct CopyOutOptions {
  pub create_parents: bool,
  pub chunk_size: usize,
}

/// Creating parents, in chunks of [`LinuxContainer::DEFAULT_COPY_CHUNK_SIZE`].
impl Default for CopyOutOptions {
  fn default() -> Self {
    Self {
      create_parents: true,
      chunk_size: LinuxContainer::DEFAULT_COPY_CHUNK_SIZE,
    }
  }
}

/// `LinuxContainer`. Made by [`super::ContainerManager::create`], or by
/// [`Self::new`] on a VM manager of the caller's.
pub struct LinuxContainer {
  pub(crate) handle: ffi::CzLinuxContainer,
}

impl fmt::Debug for LinuxContainer {
  fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
    formatter
      .debug_struct("LinuxContainer")
      .finish_non_exhaustive()
  }
}

// Swift's `LinuxContainer` is `Sendable`.
unsafe impl Send for LinuxContainer {}
unsafe impl Sync for LinuxContainer {}

impl LinuxContainer {
  /// `LinuxContainer.maxIDLength`.
  pub const MAX_ID_LENGTH: usize = 64;

  /// `LinuxContainer.defaultCopyChunkSize`: 1 MiB.
  pub const DEFAULT_COPY_CHUNK_SIZE: usize = 1024 * 1024;

  /// `LinuxContainer(_:rootfs:writableLayer:vmm:vm:configuration:logger:)`.
  pub fn new(
    id: &str,
    rootfs: Mount,
    writable_layer: Option<Mount>,
    vmm: &vm::VZVirtualMachineManager,
    vm: vm::VMResources,
    configuration: Configuration,
  ) -> Result<Self, Error> {
    let (has_writable_layer, writable_layer) = writable_layer_crossing(writable_layer);

    platform::outcome(
      ffi::cz_linux_container_new(
        id,
        rootfs,
        has_writable_layer,
        writable_layer,
        vmm.handle.duplicate(),
        vm.cpus,
        vm.memory_in_bytes,
        configuration,
      ),
      format!("make {id}"),
    )
    .map(|outcome| Self {
      handle: outcome.linux_container(),
    })
  }

  /// `LinuxContainer(_:rootfs:writableLayer:vmm:vm:logger:configuration:)`,
  /// whose closure changes a `LinuxContainer.Configuration()` before
  /// [`Self::new`] takes it, as Swift's convenience init does.
  pub fn new_with(
    id: &str,
    rootfs: Mount,
    writable_layer: Option<Mount>,
    vmm: &vm::VZVirtualMachineManager,
    vm: vm::VMResources,
    configuration: impl FnOnce(&mut Configuration) -> Result<(), Error>,
  ) -> Result<Self, Error> {
    let mut config = Configuration::default();
    configuration(&mut config)?;

    Self::new(id, rootfs, writable_layer, vmm, vm, config)
  }

  /// `LinuxContainer.id`.
  pub fn id(&self) -> String {
    self.handle.id()
  }

  /// `LinuxContainer.rootfs`.
  pub fn rootfs(&self) -> Mount {
    self.handle.rootfs().mount()
  }

  /// `LinuxContainer.writableLayer`.
  pub fn writable_layer(&self) -> Option<Mount> {
    self.handle.writable_layer().optional_mount()
  }

  /// `LinuxContainer.config`. Its process's `stdin`, `stdout` and `stderr`
  /// are `None`, since Swift's streams don't cross back.
  pub fn config(&self) -> Configuration {
    let ((), config) = platform::filled(|receive| self.handle.config(Configuration::default(), receive));

    config.expect("Swift hands back the configuration it fills")
  }

  /// `LinuxContainer.vm`.
  pub fn vm(&self) -> vm::VMResources {
    vm::VMResources {
      cpus: self.handle.vm_cpus(),
      memory_in_bytes: self.handle.vm_memory_in_bytes(),
    }
  }

  /// `LinuxContainer.cpus`: the configuration's.
  pub fn cpus(&self) -> u32 {
    self.config().cpus
  }

  /// `LinuxContainer.memoryInBytes`: the configuration's.
  pub fn memory_in_bytes(&self) -> u64 {
    self.config().memory_in_bytes
  }

  /// `LinuxContainer.interfaces`: the configuration's.
  pub fn interfaces(&self) -> Vec<network::Interface> {
    self.config().interfaces
  }

  /// `LinuxContainer.create()`.
  pub fn create(&self) -> Result<(), Error> {
    platform::outcome(self.handle.create(), format!("create {}", self.id())).map(drop)
  }

  /// `LinuxContainer.start()`.
  pub fn start(&self) -> Result<(), Error> {
    platform::outcome(self.handle.start(), format!("start {}", self.id())).map(drop)
  }

  /// `LinuxContainer.stop()`.
  pub fn stop(&self) -> Result<(), Error> {
    platform::outcome(self.handle.stop(), format!("stop {}", self.id())).map(drop)
  }

  /// `LinuxContainer.kill(_:)`.
  pub fn kill(&self, signal: process::Signal) -> Result<(), Error> {
    platform::outcome(self.handle.kill(signal.raw_value), format!("signal {}", self.id())).map(drop)
  }

  /// `LinuxContainer.wait(timeoutInSeconds:)`.
  pub fn wait(&self, timeout_in_seconds: Option<i64>) -> Result<process::ExitStatus, Error> {
    platform::outcome(self.handle.wait(timeout_in_seconds), format!("wait for {}", self.id()))
      .map(|outcome| platform::exit_status(&outcome))
  }

  /// `LinuxContainer.resize(to:)`.
  pub fn resize(&self, to: terminal::Size) -> Result<(), Error> {
    platform::outcome(self.handle.resize(to.width, to.height), format!("resize {}", self.id())).map(drop)
  }

  /// `LinuxContainer.exec(_:configuration:)`.
  pub fn exec(
    &self,
    id: &str,
    configuration: process::LinuxProcessConfiguration,
  ) -> Result<process::LinuxProcess, Error> {
    platform::outcome(
      self.handle.exec(id, configuration),
      format!("exec {id} in {}", self.id()),
    )
    .map(|outcome| process::LinuxProcess {
      handle: outcome.linux_process(),
    })
  }

  /// `LinuxContainer.exec(_:configuration:)` with a closure, which changes a
  /// `LinuxProcessConfiguration()`. Swift calls it once, on its own thread,
  /// and an error it returns is thrown.
  pub fn exec_with(
    &self,
    id: &str,
    configuration: impl FnOnce(&mut process::LinuxProcessConfiguration) -> Result<(), Error> + Send + 'static,
  ) -> Result<process::LinuxProcess, Error> {
    platform::configured(
      configuration,
      |configure| {
        self
          .handle
          .exec_with(id, process::LinuxProcessConfiguration::default(), configure)
      },
      format!("exec {id} in {}", self.id()),
    )
    .map(|outcome| process::LinuxProcess {
      handle: outcome.linux_process(),
    })
  }

  /// `LinuxContainer.dialVsock(port:)`. Swift hands over the connection's
  /// descriptor.
  pub fn dial_vsock(&self, port: u32) -> Result<OwnedFd, Error> {
    platform::outcome(
      self.handle.dial_vsock(port),
      format!("dial vsock port {port} in {}", self.id()),
    )
    // SAFETY: Swift's `FileHandle` doesn't close its descriptor, and
    // forgets it once it crosses.
    .map(|outcome| unsafe { OwnedFd::from_raw_fd(outcome.int32()) })
  }

  /// `LinuxContainer.withVirtualMachineInstance(_:)`. Swift runs its closure
  /// on the instance it hands out, once it has checked the container is
  /// created; `body` runs the same way, on this thread.
  pub fn with_virtual_machine_instance<T>(
    &self,
    body: impl FnOnce(&vm::VZVirtualMachineInstance) -> Result<T, Error>,
  ) -> Result<T, Error> {
    let instance = platform::outcome(
      self.handle.virtual_machine_instance(),
      format!("reach {}'s virtual machine", self.id()),
    )?;

    body(&vm::VZVirtualMachineInstance {
      handle: instance.virtual_machine_instance(),
    })
  }

  /// `LinuxContainer.closeStdin()`.
  pub fn close_stdin(&self) -> Result<(), Error> {
    platform::outcome(self.handle.close_stdin(), format!("close {}'s stdin", self.id())).map(drop)
  }

  /// `LinuxContainer.statistics(categories:)`.
  pub fn statistics(&self, categories: StatCategory) -> Result<ContainerStatistics, Error> {
    platform::outcome(
      self.handle.statistics(categories.raw_value),
      format!("read {}'s statistics", self.id()),
    )
    .map(|outcome| outcome.container_statistics())
  }

  /// `LinuxContainer.filesystemOperation(operation:path:)`.
  pub fn filesystem_operation(&self, operation: FilesystemOperation, path: &str) -> Result<(), Error> {
    platform::outcome(
      self.handle.filesystem_operation(operation.into(), path),
      format!("{operation:?} {path} in {}", self.id()),
    )
    .map(drop)
  }

  /// `LinuxContainer.copyIn(from:to:mode:createParents:chunkSize:)`, from a
  /// host path to a guest path.
  pub fn copy_in(&self, source: &Path, destination: &Path, options: CopyInOptions) -> Result<(), Error> {
    platform::outcome(
      self.handle.copy_in(
        platform::path(source)?,
        platform::path(destination)?,
        options.mode,
        options.create_parents,
        options.chunk_size,
      ),
      format!("copy {} into {}", source.display(), self.id()),
    )
    .map(drop)
  }

  /// `LinuxContainer.copyOut(from:to:createParents:chunkSize:)`, from a guest
  /// path to a host path.
  pub fn copy_out(&self, source: &Path, destination: &Path, options: CopyOutOptions) -> Result<(), Error> {
    platform::outcome(
      self.handle.copy_out(
        platform::path(source)?,
        platform::path(destination)?,
        options.create_parents,
        options.chunk_size,
      ),
      format!("copy {} out of {}", source.display(), self.id()),
    )
    .map(drop)
  }

  /// `LinuxContainer.defaultMounts()`.
  pub fn default_mounts() -> Vec<Mount> {
    let defaults = ["nosuid", "noexec", "nodev"];

    vec![
      Mount::any("proc", "proc", "/proc", &[], &[]),
      Mount::any("sysfs", "sysfs", "/sys", &defaults, &[]),
      Mount::any("devtmpfs", "none", "/dev", &["nosuid", "mode=755"], &[]),
      Mount::any("mqueue", "mqueue", "/dev/mqueue", &defaults, &[]),
      Mount::any(
        "tmpfs",
        "tmpfs",
        "/dev/shm",
        &["nosuid", "noexec", "nodev", "mode=1777", "size=65536k"],
        &[],
      ),
      Mount::any("cgroup2", "none", "/sys/fs/cgroup", &defaults, &[]),
      Mount::any(
        "devpts",
        "devpts",
        "/dev/pts",
        &["nosuid", "noexec", "newinstance", "gid=5", "mode=0620", "ptmxmode=0666"],
        &[],
      ),
    ]
  }

  /// `LinuxContainer.defaultMaskedPaths()`.
  pub fn default_masked_paths() -> Vec<String> {
    strings(&[
      "/proc/asound",
      "/proc/acpi",
      "/proc/kcore",
      "/proc/keys",
      "/proc/latency_stats",
      "/proc/timer_list",
      "/proc/timer_stats",
      "/proc/sched_debug",
      "/proc/scsi",
      "/sys/firmware",
      "/sys/devices/virtual/powercap",
    ])
  }

  /// `LinuxContainer.defaultReadonlyPaths()`.
  pub fn default_readonly_paths() -> Vec<String> {
    strings(&["/proc/bus", "/proc/fs", "/proc/irq", "/proc/sys", "/proc/sysrq-trigger"])
  }

  /// `LinuxContainer.defaultOCIMounts()`: the mounts OCI runtimes expect, with
  /// `/dev` as a tmpfs.
  pub fn default_oci_mounts() -> Vec<Mount> {
    let defaults = ["nosuid", "noexec", "nodev"];

    vec![
      Mount::any("proc", "proc", "/proc", &[], &[]),
      Mount::any("tmpfs", "tmpfs", "/dev", &["nosuid", "mode=755", "size=65536k"], &[]),
      Mount::any(
        "devpts",
        "devpts",
        "/dev/pts",
        &["nosuid", "noexec", "newinstance", "gid=5", "mode=0620", "ptmxmode=0666"],
        &[],
      ),
      Mount::any("sysfs", "sysfs", "/sys", &defaults, &[]),
      Mount::any("mqueue", "mqueue", "/dev/mqueue", &defaults, &[]),
      Mount::any(
        "tmpfs",
        "tmpfs",
        "/dev/shm",
        &["nosuid", "noexec", "nodev", "mode=1777", "size=65536k"],
        &[],
      ),
      Mount::any("cgroup2", "none", "/sys/fs/cgroup", &defaults, &[]),
    ]
  }
}

/// A `Mount?`, as it crosses to Swift: the mount stands for `nil` when the
/// flag is false.
fn writable_layer_crossing(writable_layer: Option<Mount>) -> (bool, Mount) {
  match writable_layer {
    Some(writable_layer) => (true, writable_layer),
    None => (false, Mount::any("", "", "", &[], &[])),
  }
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn defaults_a_configuration_as_containerization_does() {
    let configuration = Configuration::default();

    assert_eq!(configuration.cpus, 4);
    assert_eq!(configuration.memory_in_bytes, GIB);
    assert_eq!(configuration.mounts.len(), 7);
    assert!(
      configuration
        .masked_paths
        .contains(&"/proc/kcore".to_string())
    );
    assert!(
      configuration
        .readonly_paths
        .contains(&"/proc/sys".to_string())
    );
    assert_eq!(configuration.dns, None);
    assert_eq!(configuration.seccomp_profile, SeccompProfile::Unconfined);
    assert_eq!(configuration.process.working_directory, "/");
    assert_eq!(
      configuration.process.environment_variables,
      [format!("PATH={}", process::LinuxProcessConfiguration::DEFAULT_PATH)]
    );
  }
}