|
| 1 | +defmodule Hyper.Node.FireVMM.VmLinux.Provider do |
| 2 | + @moduledoc """ |
| 3 | + Installs the guest-kernel (vmlinux) images for the current architecture into |
| 4 | + `Hyper.Config.vmlinux_install_dir/0` (`<work_dir>/redist/vmlinux`). |
| 5 | +
|
| 6 | + The available kernels and their SHA-256 sums come from the statically-embedded |
| 7 | + `Hyper.Node.FireVMM.VmLinux.Manifest`. `ensure_installed/0` installs *every* |
| 8 | + build for this node's architecture and is idempotent: if all the expected |
| 9 | + images are already present it returns `:ok` without touching the network. |
| 10 | + Otherwise it fetches each missing image via `Hyper.Redist.File` (download, |
| 11 | + SHA-256 verify, install). |
| 12 | +
|
| 13 | + This is the download-side counterpart to operator-provided kernels; see |
| 14 | + `Hyper.Node.Vmlinux`, which prefers an operator-configured path and falls back |
| 15 | + to `default_path/1` here. |
| 16 | + """ |
| 17 | + |
| 18 | + alias Hyper.Node.FireVMM.VmLinux.Manifest |
| 19 | + alias Hyper.Redist |
| 20 | + |
| 21 | + @doc "Ensure every kernel for this node's architecture is installed." |
| 22 | + @spec ensure_installed() :: :ok | {:error, term()} |
| 23 | + def ensure_installed do |
| 24 | + with {:ok, arch} <- Sys.Arch.current() do |
| 25 | + builds = Manifest.builds_for(arch) |
| 26 | + |
| 27 | + case install_state(install_dir(), builds) do |
| 28 | + :ok -> :ok |
| 29 | + {:error, :not_installed} -> install_all(builds) |
| 30 | + {:error, :bad_install} -> reinstall(builds) |
| 31 | + end |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + @doc "Absolute path to the installed kernel for build `name` (e.g. \"x86_64-6.1\"). The returned path is only guaranteed to exist for the node's current architecture (the one `ensure_installed/0` installs)." |
| 36 | + @spec path(String.t()) :: {:ok, Path.t()} | {:error, {:unknown_build, String.t()}} |
| 37 | + def path(name) do |
| 38 | + case Manifest.fetch(name) do |
| 39 | + {:ok, build} -> {:ok, build_path(install_dir(), build)} |
| 40 | + :error -> {:error, {:unknown_build, name}} |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + @doc "Absolute path to the default (highest-version) kernel for `arch`. The returned path is only guaranteed to exist for the node's current architecture (the one `ensure_installed/0` installs)." |
| 45 | + @spec default_path(Sys.Arch.t()) :: {:ok, Path.t()} | {:error, {:no_kernel, Sys.Arch.t()}} |
| 46 | + def default_path(arch) do |
| 47 | + case Manifest.default_for(arch) do |
| 48 | + nil -> {:error, {:no_kernel, arch}} |
| 49 | + build -> {:ok, build_path(install_dir(), build)} |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + @doc """ |
| 54 | + Install state of `builds` under `dir`: `:ok` if every asset file is present; |
| 55 | + `{:error, :not_installed}` if none are; `{:error, :bad_install}` if only some |
| 56 | + are (a partial/corrupt install - `Hyper.Redist.File` keeps existing files, so |
| 57 | + the remedy is to wipe and reinstall). |
| 58 | + """ |
| 59 | + @spec install_state(Path.t(), [Manifest.Build.t()]) :: |
| 60 | + :ok | {:error, :not_installed | :bad_install} |
| 61 | + def install_state(dir, builds) do |
| 62 | + present = Enum.count(builds, &File.regular?(build_path(dir, &1))) |
| 63 | + |
| 64 | + cond do |
| 65 | + present == length(builds) -> :ok |
| 66 | + present == 0 -> {:error, :not_installed} |
| 67 | + true -> {:error, :bad_install} |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + defp install_all(builds) do |
| 72 | + Enum.reduce_while(builds, :ok, fn build, :ok -> |
| 73 | + case Redist.File.install( |
| 74 | + Manifest.asset_url(build), |
| 75 | + build.sha256, |
| 76 | + build_path(install_dir(), build) |
| 77 | + ) do |
| 78 | + :ok -> {:cont, :ok} |
| 79 | + {:error, _} = err -> {:halt, err} |
| 80 | + end |
| 81 | + end) |
| 82 | + end |
| 83 | + |
| 84 | + # Safe to wipe wholesale: install_dir/0 is Hyper's own redist cache |
| 85 | + # (<work_dir>/redist/vmlinux), never operator data. |
| 86 | + defp reinstall(builds) do |
| 87 | + _ = File.rm_rf!(install_dir()) |
| 88 | + install_all(builds) |
| 89 | + end |
| 90 | + |
| 91 | + defp build_path(dir, build), do: Path.join(dir, build.asset) |
| 92 | + |
| 93 | + defp install_dir, do: Hyper.Config.vmlinux_install_dir() |
| 94 | +end |
0 commit comments