diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1f0f51cf..b5a1497a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,7 +7,8 @@ # that change it and for each release. # # The parts of `cargo xtask check` run at once, each in a job of its own, and -# "cargo xtask check" passes once they all have: that check and "cargo xtask +# "cargo xtask check" passes once they all have, and fctl for macOS has +# passed Clippy on a Mac: that check and "cargo xtask # dev --smoke" are the ones main's ruleset requires (.github/rulesets). The # jobs run xtask's binary, which .github/actions/xtask takes from a cache when # nothing it is built from changed. On main, the Worker's release build runs @@ -74,11 +75,23 @@ jobs: - run: npm ci --no-audit --no-fund - run: xtask check web + # fctl for macOS, which only the Release workflow built before: code for + # Linux alone (the firecracker driver's fallocate, O_PATH and nodev) broke + # v0.30.2's release. Clippy for the host's Mac, its tests included. + macos: + name: fctl on macOS + runs-on: macos-15 + timeout-minutes: 30 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: ./.github/actions/rust + - run: cargo clippy --locked -p fctl -p fctl-host --all-targets -- -D warnings + # A skipped job would count as passed, so this one runs when a part fails # too (unless the run was cancelled), and fails unless they all passed. check: name: cargo xtask check - needs: [lint, test, web] + needs: [lint, test, web, macos] if: ${{ !cancelled() }} runs-on: ubuntu-24.04 timeout-minutes: 5 @@ -88,9 +101,11 @@ jobs: LINT: ${{ needs.lint.result }} TEST: ${{ needs.test.result }} WEB: ${{ needs.web.result }} + MACOS: ${{ needs.macos.result }} run: | - echo "lint: $LINT, test: $TEST, web: $WEB" - [ "$LINT" = success ] && [ "$TEST" = success ] && [ "$WEB" = success ] + echo "lint: $LINT, test: $TEST, web: $WEB, macos: $MACOS" + [ "$LINT" = success ] && [ "$TEST" = success ] && [ "$WEB" = success ] \ + && [ "$MACOS" = success ] # Nothing else runs the control plane before it deploys: this applies the # migrations to a fresh local D1, starts the Worker, bootstraps an owner, diff --git a/crates/fctl-host/src/docker_access.rs b/crates/fctl-host/src/docker_access.rs index 282098e8..6ab9ee8a 100644 --- a/crates/fctl-host/src/docker_access.rs +++ b/crates/fctl-host/src/docker_access.rs @@ -9,7 +9,7 @@ //! it ([`regroup`]). use std::ffi::OsString; -use std::path::{Path, PathBuf}; +use std::path::PathBuf; /// Where the docker CLI finds the daemon when `DOCKER_HOST` doesn't say. pub const DEFAULT_SOCKET: &str = "/var/run/docker.sock"; @@ -362,16 +362,9 @@ pub fn status_groups(status: &str) -> Vec { .collect() } -/// Whether the socket at `path` refuses this process. -#[cfg(unix)] -fn denied(path: &Path) -> bool { - std::os::unix::net::UnixStream::connect(path) - .is_err_and(|e| e.kind() == std::io::ErrorKind::PermissionDenied) -} - #[cfg(target_os = "linux")] mod linux { - use super::{Facts, REGROUPED, denied, exec_line}; + use super::{Facts, REGROUPED, exec_line}; use nix::sys::signal::Signal; use nix::unistd::{Gid, Group, User, getegid, getgroups, getppid, getuid}; use std::os::unix::fs::MetadataExt; @@ -379,6 +372,12 @@ mod linux { use std::path::{Path, PathBuf}; use std::process::Stdio; + /// Whether the socket at `path` refuses this process. + fn denied(path: &Path) -> bool { + std::os::unix::net::UnixStream::connect(path) + .is_err_and(|e| e.kind() == std::io::ErrorKind::PermissionDenied) + } + /// How `sg` started this process, from [`REGROUPED`]: in its own place /// (`InPlace`), or as its child, which it waits for (`Child`). #[derive(Debug, PartialEq, Eq)] diff --git a/crates/fctl-host/src/driver/firecracker/mod.rs b/crates/fctl-host/src/driver/firecracker/mod.rs index 21ea5653..0a333f21 100644 --- a/crates/fctl-host/src/driver/firecracker/mod.rs +++ b/crates/fctl-host/src/driver/firecracker/mod.rs @@ -293,7 +293,6 @@ async fn binary_version(firecracker: &Path) -> Result { /// Gives back the disk that the pages of zeros in the first `len` bytes of /// the file at `path` take, which most of a VM's saved memory is. fn punch_zeros(path: &Path, len: u64) -> Result<()> { - use nix::fcntl::{FallocateFlags, fallocate}; use std::os::unix::fs::FileExt; const PAGE: usize = 4096; let file = std::fs::OpenOptions::new() @@ -304,12 +303,7 @@ fn punch_zeros(path: &Path, len: u64) -> Result<()> { let (Ok(start), Ok(len)) = (i64::try_from(start), i64::try_from(end - start)) else { return Ok(()); }; - fallocate( - &file, - FallocateFlags::FALLOC_FL_PUNCH_HOLE | FallocateFlags::FALLOC_FL_KEEP_SIZE, - start, - len, - ) + punch_hole(&file, start, len) }; let mut buf = vec![0u8; 1 << 20]; let mut offset = 0u64; @@ -340,6 +334,26 @@ fn punch_zeros(path: &Path, len: u64) -> Result<()> { Ok(()) } +/// Gives back the disk under `len` bytes of `file` from `start`, keeping its +/// size. +#[cfg(target_os = "linux")] +fn punch_hole(file: &std::fs::File, start: i64, len: i64) -> nix::Result<()> { + use nix::fcntl::{FallocateFlags, fallocate}; + fallocate( + file, + FallocateFlags::FALLOC_FL_PUNCH_HOLE | FallocateFlags::FALLOC_FL_KEEP_SIZE, + start, + len, + ) +} + +/// Firecracker runs only on Linux, which alone has `fallocate`: elsewhere +/// (fctl for macOS still builds this driver) the zeros stay. +#[cfg(not(target_os = "linux"))] +fn punch_hole(_file: &std::fs::File, _start: i64, _len: i64) -> nix::Result<()> { + Ok(()) +} + impl FirecrackerDriver { pub fn new(host: &HostConfig) -> Self { let root = host.data_dir().join("firecracker"); diff --git a/crates/fctl-host/src/driver/firecracker/vm.rs b/crates/fctl-host/src/driver/firecracker/vm.rs index 7590792e..90357d38 100644 --- a/crates/fctl-host/src/driver/firecracker/vm.rs +++ b/crates/fctl-host/src/driver/firecracker/vm.rs @@ -479,7 +479,7 @@ impl Vm { loop { let opened = std::fs::OpenOptions::new() .read(true) - .custom_flags(nix::libc::O_PATH | nix::libc::O_NOFOLLOW) + .custom_flags(O_PATH | nix::libc::O_NOFOLLOW) .open(root.join(name)); match opened { Ok(file) => { @@ -1239,9 +1239,7 @@ pub fn jails_problem(jails: &Path) -> Option { while !at.exists() { at = at.parent()?; } - if nix::sys::statvfs::statvfs(at) - .is_ok_and(|v| v.flags().contains(nix::sys::statvfs::FsFlags::ST_NODEV)) - { + if nodev(at) { return Some(format!( "the VMs' jails need a filesystem that allows device files, and {} is on one mounted nodev: move data_dir in host.toml, or set jailer = false in its [firecracker]", jails.display() @@ -1250,6 +1248,27 @@ pub fn jails_problem(jails: &Path) -> Option { None } +/// Whether the filesystem `at` is on is mounted `nodev`. +#[cfg(target_os = "linux")] +fn nodev(at: &Path) -> bool { + nix::sys::statvfs::statvfs(at) + .is_ok_and(|v| v.flags().contains(nix::sys::statvfs::FsFlags::ST_NODEV)) +} + +/// Only Linux says (fctl for macOS still builds this driver, which never +/// runs there). +#[cfg(not(target_os = "linux"))] +fn nodev(_at: &Path) -> bool { + false +} + +/// `O_PATH`, which only Linux has: elsewhere (fctl for macOS still builds +/// this driver, which never runs there) the files are opened without it. +#[cfg(target_os = "linux")] +const O_PATH: nix::libc::c_int = nix::libc::O_PATH; +#[cfg(not(target_os = "linux"))] +const O_PATH: nix::libc::c_int = 0; + /// The jails' lock, under which a jail's uid is picked and taken; other /// processes' VMs (`fctl host image pull`'s) take it too. fn lock_jails(jails: &Path) -> Result { @@ -1367,7 +1386,7 @@ fn take_out(root: &Path, uid: u32, name: &str, to: &Path) -> Result<()> { use std::os::unix::fs::{MetadataExt, OpenOptionsExt, PermissionsExt}; let file = std::fs::OpenOptions::new() .read(true) - .custom_flags(nix::libc::O_PATH | nix::libc::O_NOFOLLOW) + .custom_flags(O_PATH | nix::libc::O_NOFOLLOW) .open(root.join(name)) .with_context(|| format!("opening the {name} Firecracker saved in the VM's jail"))?; let meta = file.metadata()?; diff --git a/docs/development.md b/docs/development.md index 8e843d84..dbbcb0ab 100644 --- a/docs/development.md +++ b/docs/development.md @@ -475,6 +475,16 @@ instead: xtask's binary (`.github/actions/xtask`), which the jobs run as which runs beside the checks for the deploy. Only `main` saves caches; pull requests use `main`'s. +CI also runs Clippy over `fctl` and `fctl-host` on a Mac (the "fctl on +macOS" job), and `cargo xtask check` waits for it: the release builds fctl +for macOS, and code that only Linux has (`O_PATH`, `fallocate`, the +firecracker driver's) needs `#[cfg(target_os = "linux")]` there. To check +from Linux, `rustup target add aarch64-apple-darwin`, then `cargo clippy +-p fctl -p fctl-host --target aarch64-apple-darwin`; `ring`'s build script +needs a C compiler for macOS, such as zig's (`python3 -m ziglang cc -target +aarch64-macos`, without the `-arch` and `-mmacosx-version-min` flags cc +passes it) in `CC_aarch64_apple_darwin`. + The Standalone image workflow (`.github/workflows/standalone-image.yml`) builds the standalone control plane's image, starts it and runs `cargo xtask dev --smoke --server` against it, so the container, its migrations