blackship 0.2.1

A FreeBSD jail manager with state machine control
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
//! Ephemeral containers: jails cloned from a release, driven through a
//! handle, and torn down without leaving anything behind.
//!
//! ```no_run
//! use blackship::{ContainerSpec, Exec, Host, Network};
//! use std::time::Duration;
//!
//! # fn main() -> blackship::Result<()> {
//! let host = Host::new("/var/db/kim/blackship").with_zfs("zroot", "kim/blackship");
//! let ci = host.create(
//!     &ContainerSpec::new("kim-42", "16.0-CURRENT")
//!         .hostname("ci")
//!         .network(Network::Inherit),
//! )?;
//! let run = ci.exec(Exec::shell("uname -a").timeout(Duration::from_secs(30)))?;
//! assert!(run.exit.success());
//! ci.destroy()?;
//! # Ok(())
//! # }
//! ```

mod copy;
mod create;
mod exec;
mod registry;
mod teardown;

pub use exec::{Exec, ExecOutput, Exit, Stream};
pub use registry::{ContainerInfo, Selector};

#[cfg(feature = "cli")]
pub use copy::{JailSide as CopyJailSide, Side as CopySide, transfer as transfer_between};
use copy::{JailSide, transfer};
#[cfg(feature = "cli")]
pub use teardown::{network_cleanup, unmount_jail_filesystems};

use crate::error::{Error, Result};
use crate::host::{Host, Layout};
use crate::manifest::validate_name;
use crate::rctl::ResourceConfig;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;

/// How a container reaches the network.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum Network {
    /// No network at all (`ip4=disable`, `ip6=disable`).
    #[default]
    None,
    /// Share the host's addresses and its `/etc/resolv.conf`.
    Inherit,
    /// A private VNET stack on a named Blackship network, with a leased address.
    Vnet(String),
}

impl Network {
    /// A VNET stack on the named network.
    pub fn vnet(network: impl Into<String>) -> Self {
        Self::Vnet(network.into())
    }
}

/// A host directory exposed inside the container through nullfs.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Mount {
    host: PathBuf,
    jail: PathBuf,
    writable: bool,
}

impl Mount {
    /// Expose `host` at absolute path `jail` inside the container, read-only.
    pub fn read_only(host: impl Into<PathBuf>, jail: impl Into<PathBuf>) -> Self {
        Self {
            host: host.into(),
            jail: jail.into(),
            writable: false,
        }
    }

    /// Expose `host` at `jail`, writable from inside the container.
    pub fn read_write(host: impl Into<PathBuf>, jail: impl Into<PathBuf>) -> Self {
        Self {
            writable: true,
            ..Self::read_only(host, jail)
        }
    }

    /// The host side of the mount.
    #[must_use]
    pub fn host_path(&self) -> &Path {
        &self.host
    }

    /// Where the mount appears inside the container.
    #[must_use]
    pub fn jail_path(&self) -> &Path {
        &self.jail
    }

    /// Whether the container may write through the mount.
    #[must_use]
    pub const fn is_writable(&self) -> bool {
        self.writable
    }
}

/// Resource limits enforced by rctl(8) and cpuset(1).
///
/// Needs `kern.racct.enable=1`; without RACCT, creation fails with [`Error::Rctl`].
#[derive(Debug, Clone, Default)]
pub struct Limits {
    resources: ResourceConfig,
}

impl Limits {
    /// No limits yet.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Resident memory cap, in rctl units: `"512m"`, `"4g"`.
    #[must_use]
    pub fn memory(mut self, amount: impl Into<String>) -> Self {
        self.resources.memory = Some(amount.into());
        self
    }

    /// Swap cap, in rctl units.
    #[must_use]
    pub fn swap(mut self, amount: impl Into<String>) -> Self {
        self.resources.swap = Some(amount.into());
        self
    }

    /// CPU cap in percent of one core: `200` is two cores' worth.
    #[must_use]
    pub fn cpu_percent(mut self, percent: u32) -> Self {
        self.resources.cpu = Some(percent.to_string());
        self
    }

    /// Process count cap.
    #[must_use]
    pub const fn max_processes(mut self, count: u32) -> Self {
        self.resources.maxproc = Some(count);
        self
    }

    /// Thread count cap.
    #[must_use]
    pub const fn max_threads(mut self, count: u32) -> Self {
        self.resources.maxthreads = Some(count);
        self
    }

    /// Open file descriptor cap.
    #[must_use]
    pub const fn open_files(mut self, count: u32) -> Self {
        self.resources.openfiles = Some(count);
        self
    }

    /// Pin the container to a CPU list: `"0-3"`, `"0,2"`.
    #[must_use]
    pub fn cpus(mut self, list: impl Into<String>) -> Self {
        self.resources.cpuset = Some(list.into());
        self
    }

    /// A raw rctl rule, `resource:action=amount`; the subject is added for you.
    #[must_use]
    pub fn rule(mut self, rule: impl Into<String>) -> Self {
        self.resources.rules.push(rule.into());
        self
    }

    /// Whether any limit is set.
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.resources.is_empty()
    }

    pub(crate) const fn resources(&self) -> &ResourceConfig {
        &self.resources
    }
}

/// Everything needed to create a container: name, release to clone, confinement.
///
/// ```
/// use blackship::{ContainerSpec, Limits, Mount, Network};
///
/// let spec = ContainerSpec::new("kim-42", "16.0-CURRENT")
///     .hostname("ci")
///     .network(Network::Inherit)
///     .limits(Limits::new().memory("4g").max_processes(512))
///     .mount(Mount::read_only("/var/cache/kim/distfiles", "/distfiles"))
///     .label("job", "42");
/// assert_eq!(spec.name(), "kim-42");
/// ```
#[derive(Debug, Clone)]
pub struct ContainerSpec {
    name: String,
    release: String,
    hostname: Option<String>,
    network: Network,
    limits: Limits,
    mounts: Vec<Mount>,
    labels: BTreeMap<String, String>,
    boot: bool,
    raw_sockets: bool,
    params: Vec<String>,
}

impl ContainerSpec {
    /// A container named `name` cloned from `release`, a bootstrapped
    /// release or an image from [`Container::commit`] / [`Host::commit_image`].
    pub fn new(name: impl Into<String>, release: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            release: release.into(),
            hostname: None,
            network: Network::default(),
            limits: Limits::default(),
            mounts: Vec::new(),
            labels: BTreeMap::new(),
            boot: false,
            raw_sockets: false,
            params: Vec::new(),
        }
    }

    /// The hostname seen inside; defaults to the container name.
    #[must_use]
    pub fn hostname(mut self, hostname: impl Into<String>) -> Self {
        self.hostname = Some(hostname.into());
        self
    }

    /// How the container reaches the network; [`Network::None`] by default.
    #[must_use]
    pub fn network(mut self, network: Network) -> Self {
        self.network = network;
        self
    }

    /// Resource limits; none by default.
    #[must_use]
    pub fn limits(mut self, limits: Limits) -> Self {
        self.limits = limits;
        self
    }

    /// Add a nullfs mount. The jail-side directory is created when missing.
    #[must_use]
    pub fn mount(mut self, mount: Mount) -> Self {
        self.mounts.push(mount);
        self
    }

    /// Attach a label, matched by [`Selector::label`].
    #[must_use]
    pub fn label(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.labels.insert(key.into(), value.into());
        self
    }

    /// Run `/etc/rc` when the container starts. Off by default.
    #[must_use]
    pub const fn boot(mut self) -> Self {
        self.boot = true;
        self
    }

    /// Allow raw sockets (ping, traceroute) inside the container.
    #[must_use]
    pub const fn allow_raw_sockets(mut self) -> Self {
        self.raw_sockets = true;
        self
    }

    /// Pass a raw jail(8) parameter, `name=value` or a bare flag like `allow.mlock`.
    #[must_use]
    pub fn jail_param(mut self, param: impl Into<String>) -> Self {
        self.params.push(param.into());
        self
    }

    /// The container (and jail) name.
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    /// The release the container clones.
    #[must_use]
    pub fn release(&self) -> &str {
        &self.release
    }
}

/// A live container. Dropping a handle from [`Host::create`] destroys it;
/// [`keep`](Self::keep) opts out, [`destroy`](Self::destroy) surfaces errors.
/// Handles from [`Host::open`] are kept by default.
#[derive(Debug)]
pub struct Container {
    layout: Arc<Layout>,
    name: String,
    jid: i32,
    root: PathBuf,
    dataset: Option<String>,
    destroy_on_drop: bool,
}

impl Container {
    /// The container (and jail) name.
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    /// The kernel jail id.
    #[must_use]
    pub const fn jid(&self) -> i32 {
        self.jid
    }

    /// The container's root directory on the host.
    #[must_use]
    pub fn root(&self) -> &Path {
        &self.root
    }

    /// The ZFS dataset holding the root, when the host uses ZFS.
    #[must_use]
    pub fn dataset(&self) -> Option<&str> {
        self.dataset.as_deref()
    }

    /// Run a command inside the container and wait for it. A non-zero exit
    /// is not an error; a timeout kills every process in the container.
    ///
    /// # Errors
    ///
    /// Not root, an empty argv, or the command cannot be spawned in the jail.
    pub fn exec(&self, exec: Exec<'_>) -> Result<ExecOutput> {
        Error::require_root("running a command in a container")?;
        exec::run(self.jid, exec)
    }

    /// Copy a host file or directory to absolute `jail_path`, whose parent must exist.
    ///
    /// # Errors
    ///
    /// Not root, a destination escaping the root, or a failed copy.
    pub fn copy_in(&self, host_path: impl AsRef<Path>, jail_path: impl AsRef<str>) -> Result<()> {
        Error::require_root("copying into a container")?;
        transfer(
            &copy::Side::Host(host_path.as_ref()),
            &copy::Side::Jail(self.jail_side(jail_path.as_ref())),
            true,
        )
    }

    /// Copy a file or directory out of the container to the host.
    ///
    /// # Errors
    ///
    /// Not root, a source escaping the root, or a failed copy.
    pub fn copy_out(&self, jail_path: impl AsRef<str>, host_path: impl AsRef<Path>) -> Result<()> {
        Error::require_root("copying out of a container")?;
        transfer(
            &copy::Side::Jail(self.jail_side(jail_path.as_ref())),
            &copy::Side::Host(host_path.as_ref()),
            true,
        )
    }

    /// Freeze the container's filesystem into a reusable release (ZFS only).
    /// The snapshot is taken live; quiesce writers first.
    ///
    /// # Errors
    ///
    /// Not root, not on ZFS, an invalid or taken name, or a failed snapshot/clone.
    pub fn commit(&self, release: &str) -> Result<crate::Release> {
        Error::require_root("committing a container")?;
        let zfs = self.layout.zfs().ok_or(Error::ZfsNotEnabled)?;
        let dataset = self.dataset.as_deref().ok_or(Error::ZfsNotEnabled)?;
        crate::release::commit_dataset(&self.layout, &zfs, dataset, release)
    }

    /// Leave the container running past this handle; it is never an orphan.
    ///
    /// # Errors
    ///
    /// The ownership record could not be rewritten; the handle still destroys on drop.
    pub fn keep(&mut self) -> Result<()> {
        registry::disown(&self.layout, &self.name)?;
        self.destroy_on_drop = false;
        Ok(())
    }

    /// Tear the container down. See [`Host::destroy_container`].
    ///
    /// # Errors
    ///
    /// As [`Host::destroy_container`].
    pub fn destroy(mut self) -> Result<()> {
        self.destroy_on_drop = false;
        teardown::destroy(&self.layout, &self.name)
    }

    fn jail_side(&self, path: &str) -> JailSide {
        JailSide {
            jid: Some(self.jid),
            root: self.root.clone(),
            path: path.to_string(),
        }
    }
}

impl Drop for Container {
    fn drop(&mut self) {
        if self.destroy_on_drop {
            // Errors stay with the orphan record for `reap_orphans`.
            let _ = teardown::destroy(&self.layout, &self.name);
        }
    }
}

impl Host {
    /// Create and start a container. A failure at any step tears down
    /// whatever was built.
    ///
    /// # Errors
    ///
    /// Not root, an invalid or taken name, a missing release, or the failing step's error.
    pub fn create(&self, spec: &ContainerSpec) -> Result<Container> {
        create::create(self.layout(), spec)
    }

    /// A kept handle on a running container, possibly from another process.
    ///
    /// # Errors
    ///
    /// Not root, an invalid name, or [`Error::JailNotRunning`].
    pub fn open(&self, name: &str) -> Result<Container> {
        Error::require_root("opening a container")?;
        validate_container_name(name)?;
        let jid = crate::jail::jail_getid(name).map_err(|_| Error::JailNotRunning(name.into()))?;
        let record = registry::load(self.layout(), name);
        let root = record
            .as_ref()
            .map_or_else(|| self.layout().container_root(name), |r| r.root.clone());
        Ok(Container {
            layout: Arc::clone(self.layout()),
            name: name.to_string(),
            jid,
            root,
            dataset: record.and_then(|r| r.dataset),
            destroy_on_drop: false,
        })
    }

    /// Destroy a container by name. Idempotent: every step tolerates its
    /// resource being gone, so half-built containers clean up too.
    ///
    /// # Errors
    ///
    /// Not root, an invalid name, or [`Error::JailOperation`] listing failed steps.
    pub fn destroy_container(&self, name: &str) -> Result<()> {
        teardown::destroy(self.layout(), name)
    }

    /// Every container this host knows about: records, datasets and roots.
    ///
    /// # Errors
    ///
    /// None today; unreadable entries are skipped.
    pub fn containers(&self) -> Result<Vec<ContainerInfo>> {
        Ok(registry::list(self.layout()))
    }

    /// Containers matching `selector` whose creating process is gone.
    ///
    /// # Errors
    ///
    /// As [`Host::containers`].
    pub fn orphans(&self, selector: &Selector) -> Result<Vec<ContainerInfo>> {
        Ok(self
            .containers()?
            .into_iter()
            .filter(|info| info.is_orphan() && selector.matches(info))
            .collect())
    }

    /// Destroy every orphan matching `selector`; returns the destroyed names.
    ///
    /// # Errors
    ///
    /// Not root, or the first teardown failure after every orphan was tried.
    pub fn reap_orphans(&self, selector: &Selector) -> Result<Vec<String>> {
        Error::require_root("reaping orphaned containers")?;
        let mut reaped = Vec::new();
        let mut first_error = None;
        for orphan in self.orphans(selector)? {
            match self.destroy_container(&orphan.name) {
                Ok(()) => reaped.push(orphan.name),
                Err(e) => {
                    first_error.get_or_insert(e);
                }
            }
        }
        match first_error {
            Some(e) => Err(e),
            None => Ok(reaped),
        }
    }
}

/// Validate a container name; dots are out since jail(8) reads `a.b` as a child jail.
pub fn validate_container_name(name: &str) -> Result<()> {
    validate_name("container", name)?;
    if name.contains('.') {
        return Err(Error::ConfigValidation(format!(
            "container name '{name}' must not contain '.' (jail(8) reads it as a child jail)"
        )));
    }
    Ok(())
}

#[cfg(test)]
mod tests;