blackship 0.4.2

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
//! Everything a model needs to know about this host before it plans jails:
//! what the kernel and blackship can do, how much memory, CPU and disk there
//! is and how much existing jails already claim, which networks and
//! addresses exist, and what is already running. Served as the
//! `blackship:///host` resource.
//!
//! Every section is best effort and unprivileged: a probe that fails reports
//! an error object in place, never fails the whole profile.

use std::collections::BTreeSet;
use std::net::IpAddr;
use std::path::Path;

use ipnet::IpNet;
use serde_json::{Value, json};

use crate::manifest::BlackshipConfig;

/// The full host profile, as JSON.
pub(crate) fn collect(config_path: &Path) -> Value {
    let config = crate::manifest::load_or_default(config_path);
    let config_error = config.as_ref().err().map(ToString::to_string);
    let config = config.ok();
    let global = config.as_ref().map(|config| &config.config);

    json!({
        "config": {
            "path": config_path.display().to_string(),
            "source": if config_path.exists() { "file" } else { "defaults" },
            "error": config_error,
        },
        "system": system_facts(),
        "kernel": kernel_facts(),
        "cpu": cpu_facts(config.as_ref()),
        "memory": memory_facts(config.as_ref()),
        "rctl": rctl_facts(),
        "network": network_facts(config.as_ref()),
        "releases": release_facts(global),
        "zfs": zfs_facts(global),
        "jails": jail_facts(config.as_ref()),
        "facilities": facility_facts(),
    })
}

fn system_facts() -> Value {
    json!({
        "hostname": crate::sys::sysctl_string("kern.hostname"),
        "os": std::env::consts::OS,
        "arch": std::env::consts::ARCH,
        "blackship_version": env!("CARGO_PKG_VERSION"),
    })
}

/// CPUs, and how much of them manifest jails already claim through `cpu`
/// (rctl pcpu, 100 = one core) and pinning.
fn cpu_facts(config: Option<&BlackshipConfig>) -> Value {
    let jails = config.map_or(&[][..], |c| c.jails.as_slice());
    let pcpu_claimed: u64 = jails
        .iter()
        .filter_map(|j| j.resources.cpu.as_deref()?.trim().parse::<u64>().ok())
        .sum();
    json!({
        "online": online_cpus(),
        "model": crate::sys::sysctl_string("hw.model"),
        "manifest_pcpu_limits_total": pcpu_claimed,
        "note": "pcpu: 100 = one core",
    })
}

/// Physical memory, what is free right now, and what manifest jails claim.
fn memory_facts(config: Option<&BlackshipConfig>) -> Value {
    let page = crate::sys::sysctl_u64("hw.pagesize").unwrap_or(4096);
    let pages = |name| crate::sys::sysctl_u64(name).map(|n| n * page);
    let jails = config.map_or(&[][..], |c| c.jails.as_slice());
    let limits: Vec<u64> = jails
        .iter()
        .filter_map(|j| crate::rctl::expand_number(j.resources.memory.as_deref()?))
        .collect();
    let unlimited = jails.len() - limits.len();
    let physical = crate::sys::sysctl_u64("hw.physmem");
    let committed: u64 = limits.iter().sum();

    json!({
        "physical_bytes": physical,
        "free_bytes": pages("vm.stats.vm.v_free_count"),
        "inactive_bytes": pages("vm.stats.vm.v_inactive_count"),
        "laundry_bytes": pages("vm.stats.vm.v_laundry_count"),
        "zfs_arc_bytes": crate::sys::sysctl_u64("kstat.zfs.misc.arcstats.size"),
        "swap_total_bytes": crate::sys::sysctl_u64("vm.swap_total"),
        "manifest_memory_limits_total_bytes": committed,
        "manifest_jails_without_memory_limit": unlimited,
        "uncommitted_bytes": physical.map(|p| p.saturating_sub(committed)),
        "note": "reclaimable ≈ free + inactive + laundry + most of ARC; limits are \
                 caps, not reservations",
    })
}

/// Blackship networks with their address usage, PF state, and the egress
/// interface a NAT rule would use.
fn network_facts(config: Option<&BlackshipConfig>) -> Value {
    let egress = command_lines("/sbin/route", &["-n", "get", "default"])
        .ok()
        .and_then(|lines| {
            lines.iter().find_map(|l| {
                l.trim()
                    .strip_prefix("interface:")
                    .map(|i| i.trim().to_string())
            })
        });

    let networks = match crate::network::NetworkStore::from_config(config).list() {
        Ok(records) => {
            let leases = crate::network::NetworkLeaseStore::from_config(config);
            json!(
                records
                    .iter()
                    .map(|record| network_usage(record, &leases, config))
                    .collect::<Vec<_>>()
            )
        }
        Err(e) => json!({ "error": e.to_string() }),
    };

    json!({
        "default_route_interface": egress,
        "pf_enabled": pf_state(),
        "networks": networks,
        "note": "no NAT installed: egress needs `nat on <default_route_interface>` per subnet",
    })
}

fn network_usage(
    record: &crate::network::NetworkRecord,
    leases: &crate::network::NetworkLeaseStore,
    config: Option<&BlackshipConfig>,
) -> Value {
    let subnet: Option<IpNet> = record.subnet.parse().ok();
    let mut used: BTreeSet<IpAddr> = leases
        .list(&record.name)
        .unwrap_or_default()
        .iter()
        .filter_map(|lease| lease.ip.parse().ok())
        .collect();
    // Static addresses in the manifest hold their place whether or not the
    // jail is running.
    if let (Some(subnet), Some(config)) = (subnet, config) {
        used.extend(
            config
                .jails
                .iter()
                .filter_map(|j| j.network.as_ref()?.static_ip())
                .filter(|ip| subnet.contains(ip)),
        );
    }
    if let Ok(gateway) = record.gateway.parse::<IpAddr>() {
        used.remove(&gateway);
    }
    let usable = subnet.and_then(usable_hosts);

    json!({
        "name": record.name,
        "subnet": record.subnet,
        "gateway": record.gateway,
        "bridge": record.bridge,
        "backend": record.backend,
        "addresses_in_use": used.iter().map(ToString::to_string).collect::<Vec<_>>(),
        "addresses_free": usable.map(|n| n.saturating_sub(used.len() as u64)),
    })
}

/// Assignable IPv4 hosts in a subnet, minus the gateway. IPv6 subnets are
/// effectively unbounded and report `None`.
fn usable_hosts(subnet: IpNet) -> Option<u64> {
    match subnet {
        IpNet::V4(net) => {
            let bits = 32 - u32::from(net.prefix_len());
            let total = 1u64 << bits;
            Some(total.saturating_sub(3)) // network, broadcast, gateway
        }
        IpNet::V6(_) => None,
    }
}

/// Jails the manifest defines, and running jails it does not.
fn jail_facts(config: Option<&BlackshipConfig>) -> Value {
    let mut managed = BTreeSet::new();
    let manifest: Vec<Value> = config.map_or_else(Vec::new, |config| {
        config
            .jails
            .iter()
            .map(|jail| {
                let full_name = config.jail_name(&jail.name);
                let entry = json!({
                    "name": jail.name,
                    "full_name": full_name,
                    "release": jail.release,
                    "running": crate::jail::jail_getid(&full_name).is_ok(),
                    "ip": jail.network.as_ref().and_then(crate::manifest::JailNetworkConfig::static_ip).map(|ip| ip.to_string()),
                    "networks": jail.network.as_ref().map(|n| n.networks.clone()),
                    "depends_on": jail.depends_on,
                    "memory_limit": jail.resources.memory,
                    "cpu_limit": jail.resources.cpu,
                });
                managed.insert(full_name);
                entry
            })
            .collect()
    });

    json!({
        "project": config.map(|c| c.config.project_name().to_string()),
        "manifest": manifest,
        "unmanaged_running": unmanaged_jails(&managed),
    })
}

/// Host tools blackship drives, and whether each is installed.
fn facility_facts() -> Value {
    match crate::commands::capabilities::data() {
        Ok(report) => json!(report.facilities),
        Err(e) => json!({ "error": e.to_string() }),
    }
}

/// ZFS: pool capacity, and blackship's base dataset when configured.
fn zfs_facts(config: Option<&crate::manifest::GlobalConfig>) -> Value {
    let pools = match command_lines(
        "/sbin/zpool",
        &["list", "-Hp", "-o", "name,size,alloc,free,health"],
    ) {
        Ok(lines) => json!(
            lines
                .iter()
                .filter_map(|line| {
                    let f: Vec<&str> = line.split('\t').collect();
                    let num = |i: usize| f.get(i)?.parse::<u64>().ok();
                    Some(json!({
                        "name": f.first()?,
                        "size_bytes": num(1),
                        "allocated_bytes": num(2),
                        "free_bytes": num(3),
                        "health": f.get(4),
                    }))
                })
                .collect::<Vec<_>>()
        ),
        Err(e) => json!({ "error": e }),
    };

    let Some(config) = config else {
        return json!({ "pools": pools, "enabled": Value::Null });
    };
    let base = config
        .zpool
        .as_deref()
        .filter(|_| config.zfs_enabled)
        .map(|pool| format!("{pool}/{}", config.dataset));
    let available = base.as_deref().and_then(|dataset| {
        command_lines(
            "/sbin/zfs",
            &["get", "-Hp", "-o", "value", "available", dataset],
        )
        .ok()?
        .first()?
        .parse::<u64>()
        .ok()
    });

    json!({
        "pools": pools,
        "enabled": config.zfs_enabled,
        "zpool": config.zpool,
        "base_dataset": base,
        "base_dataset_exists": available.is_some(),
        "base_dataset_available_bytes": available,
        "jails_dir": config.jails_dir().display().to_string(),
        "note": match (config.zfs_enabled, config.zpool.is_some()) {
            (true, true) => "jail roots are clones of the release: near-zero cost until written",
            (true, false) => "zfs_enabled without zpool: misconfigured",
            (false, _) => "ZFS off: each jail root is a full copy of its release",
        },
    })
}

/// RACCT/RCTL: whether manifest limits can be enforced.
fn rctl_facts() -> Value {
    let enabled = crate::rctl::is_enabled();
    let mut facts = json!({
        "enabled": enabled,
        "managed_resources": crate::rctl::managed_resources(),
    });
    if !enabled {
        facts["note"] =
            json!("kern.racct.enable=0: limits unenforceable (loader.conf + reboot); cpuset works");
    }
    facts
}

/// PF state: `null` when it cannot be read (pfctl needs root), never a guess.
fn pf_state() -> Value {
    match command_lines("/sbin/pfctl", &["-s", "info"]) {
        Ok(lines) => json!(lines.iter().any(|l| l.contains("Status: Enabled"))),
        Err(_) => Value::Null,
    }
}

/// Running jails the manifest does not define, including ones blackship
/// did not create.
fn unmanaged_jails(managed: &BTreeSet<String>) -> Vec<Value> {
    crate::jail::RunningJails::new()
        .filter_map(|jid| {
            let name = crate::jail::jail_getname(jid).ok();
            if name.as_ref().is_some_and(|n| managed.contains(n)) {
                return None;
            }
            Some(json!({ "jid": jid, "name": name }))
        })
        .collect()
}

/// A command's stdout lines, or its failure as text.
fn command_lines(program: &str, args: &[&str]) -> std::result::Result<Vec<String>, String> {
    let output = std::process::Command::new(program)
        .args(args)
        .output()
        .map_err(|e| format!("cannot run {program}: {e}"))?;
    if !output.status.success() {
        return Err(format!(
            "{program} exited {}: {}",
            output.status,
            String::from_utf8_lossy(&output.stderr).trim()
        ));
    }
    Ok(String::from_utf8_lossy(&output.stdout)
        .lines()
        .map(str::to_string)
        .collect())
}

/// Bootstrapped releases under the configured (or default) releases dir.
fn release_facts(config: Option<&crate::manifest::GlobalConfig>) -> Value {
    let Some(config) = config else {
        return json!({ "status": "error", "error": "configuration is unavailable" });
    };

    let provisioner = match crate::provision::Provisioner::from_config(config) {
        Ok(provisioner) => provisioner,
        Err(e) => return json!({ "status": "error", "error": e.to_string() }),
    };

    let dir = config.releases_dir.display().to_string();
    if !config.releases_dir.exists() {
        return json!({
            "status": "absent",
            "releases_dir": dir,
            "note": "releases directory does not exist; nothing is bootstrapped yet",
        });
    }

    match provisioner.list_releases() {
        Ok(releases) => json!({
            "status": "present",
            "releases_dir": dir,
            "bootstrapped": releases
                .iter()
                .map(|release| json!({
                    "name": release.name,
                    "path": release.path.display().to_string(),
                    "arch": release.arch.freebsd_name(),
                }))
                .collect::<Vec<_>>(),
        }),
        Err(e) => json!({
            "status": "error",
            "releases_dir": dir,
            "error": e.to_string(),
        }),
    }
}

/// Kernel version and the feature gates blackship keys off it.
fn kernel_facts() -> Value {
    match crate::sys::OsVersion::detect_kernel() {
        Ok(version) => json!({
            "status": "present",
            "version": version.to_string(),
            "major": version.major,
            "minor": version.minor,
            "patch": version.patch,
            "release_type": version.release_type.to_string(),
            "features": {
                "vlan_filtering": version.supports_vlan_filtering(),
                "requires_pkgbase": version.requires_pkgbase(),
                "jail_descriptors": version.supports_jail_descriptors(),
                "jail_kqueue": version.supports_jail_kqueue(),
            },
        }),
        Err(e) => json!({ "status": "error", "error": e.to_string() }),
    }
}

/// Online logical CPUs, never less than one.
///
/// The `unsafe` call is sound unconditionally: `sysconf(3)` takes an integer
/// name and returns a `long`, no pointers are involved, and
/// `_SC_NPROCESSORS_ONLN` is always a valid name. A negative or zero return
/// means "indeterminate", which is clamped to one rather than propagated.
#[allow(unsafe_code)] // sysconf(3): nix only binds _NPROCESSORS_ONLN on Linux.
fn online_cpus() -> i64 {
    // SAFETY: integer in, `long` out; see above.
    let count = unsafe { libc::sysconf(libc::_SC_NPROCESSORS_ONLN) };
    if count > 0 { count } else { 1 }
}

#[cfg(test)]
mod tests;