Skip to content

Commit 612d103

Browse files
committed
feat(cfg): Hyper.Cfg.Dirs facade; define firecracker_install_dir
Creates Hyper.Cfg.Dirs with work_dir/0 (config.toml-only, default /srv/hyper) and all 8 derived directory accessors. Migrates every call-site away from Hyper.Config.*; removes now-unused aliases in img.ex, oci_loader.ex, umoci.ex. Resolves the last undefined-accessor warning: firecracker_install_dir/0.
1 parent 26b7700 commit 612d103

11 files changed

Lines changed: 107 additions & 27 deletions

File tree

lib/hyper/cfg/dirs.ex

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
defmodule Hyper.Cfg.Dirs do
2+
@moduledoc """
3+
The node's work directory and every directory derived from it.
4+
5+
`work_dir` is the single configurable root (`config.toml`-only, shared with the
6+
setuid helper); everything else is a fixed sub-path so the node and helper
7+
agree on layout without a second key to keep in sync.
8+
"""
9+
10+
import Hyper.Cfg, only: [get_cfg: 1]
11+
12+
@doc "Root work directory for this node. config.toml `work_dir`, default `/srv/hyper`."
13+
@spec work_dir :: Path.t()
14+
def work_dir, do: get_cfg(toml: "work_dir", default: "/srv/hyper")
15+
16+
@doc "Read-only image layer store (`<work_dir>/layers`)."
17+
@spec layer_dir :: Path.t()
18+
def layer_dir, do: Path.join(work_dir(), "layers")
19+
20+
@doc "Per-VM control/gRPC sockets (`<work_dir>/socks`)."
21+
@spec socket_dir :: Path.t()
22+
def socket_dir, do: Path.join(work_dir(), "socks")
23+
24+
@doc "Per-VM copy-on-write writable layers (`<work_dir>/scratch`)."
25+
@spec scratch_dir :: Path.t()
26+
def scratch_dir, do: Path.join(work_dir(), "scratch")
27+
28+
@doc "Per-VM chroot directories (`<work_dir>/jails`)."
29+
@spec chroot_base :: Path.t()
30+
def chroot_base, do: Path.join(work_dir(), "jails")
31+
32+
@doc "Node-downloaded binaries (`<work_dir>/redist`)."
33+
@spec redist_dir :: Path.t()
34+
def redist_dir, do: Path.join(work_dir(), "redist")
35+
36+
@doc "Where guest kernels install (`<work_dir>/redist/vmlinux`)."
37+
@spec vmlinux_install_dir :: Path.t()
38+
def vmlinux_install_dir, do: Path.join(redist_dir(), "vmlinux")
39+
40+
@doc "Where the default umoci installs (`<work_dir>/redist/umoci`)."
41+
@spec umoci_install_dir :: Path.t()
42+
def umoci_install_dir, do: Path.join(redist_dir(), "umoci")
43+
44+
@doc "Where a node-downloaded firecracker installs (`<work_dir>/redist/firecracker`)."
45+
@spec firecracker_install_dir :: Path.t()
46+
def firecracker_install_dir, do: Path.join(redist_dir(), "firecracker")
47+
end

lib/hyper/img.ex

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ defmodule Hyper.Img do
66
`create/2` ingests a prepared image file -- e.g. the ext4 rootfs produced by
77
`Hyper.Img.OciLoader` -- into the shared media store and the image database. It
88
content-addresses the file (sha256 of its bytes = the image id), publishes it
9-
into `Hyper.Config.layer_dir/0` at `layer_<id>.img`, then records it as a
9+
into `Hyper.Cfg.Dirs.layer_dir/0` at `layer_<id>.img`, then records it as a
1010
one-layer base image (`blobs` + `images` + `image_layers`). Producers of image
1111
files stay decoupled from the store and DB: they hand a path to `create/2`.
1212
"""
@@ -15,7 +15,6 @@ defmodule Hyper.Img do
1515

1616
require Logger
1717

18-
alias Hyper.Config
1918
alias Hyper.Img.Db.{Blob, Image, ImageLayer, Repo}
2019

2120
@type id :: String.t()
@@ -85,7 +84,7 @@ defmodule Hyper.Img do
8584
@spec publish(Path.t(), id()) :: {:ok, Path.t(), :created | :reused} | {:error, term()}
8685
@decorate with_span("Hyper.Img.publish", include: [:id])
8786
defp publish(src, id) do
88-
File.mkdir_p!(Config.layer_dir())
87+
File.mkdir_p!(Hyper.Cfg.Dirs.layer_dir())
8988
final = final_path(id)
9089

9190
if File.exists?(final) do
@@ -125,7 +124,7 @@ defmodule Hyper.Img do
125124
end
126125

127126
@spec final_path(id()) :: Path.t()
128-
defp final_path(id), do: Path.join(Config.layer_dir(), "layer_#{id}.img")
127+
defp final_path(id), do: Path.join(Hyper.Cfg.Dirs.layer_dir(), "layer_#{id}.img")
129128

130129
# Record the base image: one blob, one image (id == blob id), one layer at
131130
# position 0. All upserts are idempotent so a re-publish of the same bytes is a

lib/hyper/img/oci_loader.ex

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ defmodule Hyper.Img.OciLoader do
2020

2121
use Unit.Operators
2222

23-
alias Hyper.Config
2423
alias Hyper.Img.OciLoader.Umoci
2524
alias Unit.Information
2625

@@ -185,8 +184,8 @@ defmodule Hyper.Img.OciLoader do
185184
{:ok, Path.t()} | {:error, term()}
186185
defp build_ext4(rootfs, {size, inodes}) do
187186
Logger.debug("oci: building #{Information.as_mib(size)} MiB ext4 rootfs (#{inodes} inodes)")
188-
File.mkdir_p!(Config.layer_dir())
189-
staged = Path.join(Config.layer_dir(), ".incoming-#{System.unique_integer([:positive])}.img")
187+
File.mkdir_p!(Hyper.Cfg.Dirs.layer_dir())
188+
staged = Path.join(Hyper.Cfg.Dirs.layer_dir(), ".incoming-#{System.unique_integer([:positive])}.img")
190189

191190
args =
192191
["-t", "ext4", "-F", "-q", "-N", to_string(inodes), "-d", rootfs, staged] ++

lib/hyper/img/oci_loader/umoci.ex

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ defmodule Hyper.Img.OciLoader.Umoci do
66
Two sources, in priority order (mirrors `Hyper.Node.Vmlinux`):
77
88
1. An operator-configured path via `[tools] umoci` in
9-
`/etc/hyper/config.toml` (`Hyper.Config.umoci_path/0`). If set, it wins
9+
`/etc/hyper/config.toml` (`Hyper.Cfg.Tools.umoci/0`). If set, it wins
1010
and is never downloaded.
1111
2. Otherwise the pinned static binary downloaded by `ensure_installed/0`
12-
into `Hyper.Config.umoci_install_dir/0` (`<work_dir>/redist/umoci`).
12+
into `Hyper.Cfg.Dirs.umoci_install_dir/0` (`<work_dir>/redist/umoci`).
1313
"""
1414

15-
alias Hyper.Config
16-
1715
require Logger
1816

1917
# Pinned umoci release per architecture: the static binary's filename, its
@@ -69,7 +67,7 @@ defmodule Hyper.Img.OciLoader.Umoci do
6967

7068
@spec default_path(Sys.Arch.t()) :: Path.t()
7169
defp default_path(arch) do
72-
Path.join(Config.umoci_install_dir(), Map.fetch!(@downloads, arch).asset)
70+
Path.join(Hyper.Cfg.Dirs.umoci_install_dir(), Map.fetch!(@downloads, arch).asset)
7371
end
7472

7573
@spec install(Sys.Arch.t(), Path.t()) :: :ok | {:error, term()}

lib/hyper/node.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,10 @@ defmodule Hyper.Node do
160160
@spec check_helper_base(Path.t()) ::
161161
:ok | {:error, {:suid_helper_base_mismatch, Path.t(), Path.t()}}
162162
defp check_helper_base(base) do
163-
if base == Hyper.Config.work_dir() do
163+
if base == Hyper.Cfg.Dirs.work_dir() do
164164
:ok
165165
else
166-
{:error, {:suid_helper_base_mismatch, base, Hyper.Config.work_dir()}}
166+
{:error, {:suid_helper_base_mismatch, base, Hyper.Cfg.Dirs.work_dir()}}
167167
end
168168
end
169169

lib/hyper/node/fire_vmm/jailer.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ defmodule Hyper.Node.FireVMM.Jailer do
7979
end
8080

8181
defp chroot_writable do
82-
case Sys.Posix.ensure_writable_dir(Config.chroot_base()) do
82+
case Sys.Posix.ensure_writable_dir(Hyper.Cfg.Dirs.chroot_base()) do
8383
{:ok} -> :ok
8484
{:error, reason} -> {:error, {:chroot_base_unavailable, reason}}
8585
end
@@ -104,7 +104,7 @@ defmodule Hyper.Node.FireVMM.Jailer do
104104
"--gid",
105105
to_string(opts.gid),
106106
"--chroot-base-dir",
107-
Hyper.Config.chroot_base(),
107+
Hyper.Cfg.Dirs.chroot_base(),
108108
"--cgroup-version",
109109
"2",
110110
"--parent-cgroup",
@@ -129,7 +129,7 @@ defmodule Hyper.Node.FireVMM.Jailer do
129129
@doc "Host path of the VM's per-VM jail dir (`<chroot_base>/<exec>/<id>`)."
130130
@spec chroot_dir(Hyper.Vm.id()) :: Path.t()
131131
def chroot_dir(id) do
132-
Path.join([Hyper.Config.chroot_base(), exec_name(), id])
132+
Path.join([Hyper.Cfg.Dirs.chroot_base(), exec_name(), id])
133133
end
134134

135135
@doc "Host path of the VM's chroot root (`<chroot_base>/<exec>/<id>/root`)."
@@ -158,7 +158,7 @@ defmodule Hyper.Node.FireVMM.Jailer do
158158
@spec host_socket(Hyper.Vm.id()) :: Path.t()
159159
def host_socket(id) do
160160
Path.join([
161-
Hyper.Config.chroot_base(),
161+
Hyper.Cfg.Dirs.chroot_base(),
162162
exec_name(),
163163
id,
164164
"root",

lib/hyper/node/fire_vmm/provider.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule Hyper.Node.FireVMM.Provider do
22
@moduledoc """
33
Installs the firecracker release for the current architecture into
4-
`Hyper.Config.firecracker_install_dir/0` (`<work_dir>/redist/firecracker`).
4+
`Hyper.Cfg.Dirs.firecracker_install_dir/0` (`<work_dir>/redist/firecracker`).
55
66
`ensure_installed/0` is idempotent: if the binaries are already present and
77
executable it returns `:ok` without touching the network. Otherwise it fetches
@@ -88,5 +88,5 @@ defmodule Hyper.Node.FireVMM.Provider do
8888
Path.join(install_dir(), Map.fetch!(dl, key))
8989
end
9090

91-
defp install_dir, do: Hyper.Config.firecracker_install_dir()
91+
defp install_dir, do: Hyper.Cfg.Dirs.firecracker_install_dir()
9292
end

lib/hyper/node/fire_vmm/vm_linux/provider.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule Hyper.Node.FireVMM.VmLinux.Provider do
22
@moduledoc """
33
Installs the guest-kernel (vmlinux) images for the current architecture into
4-
`Hyper.Config.vmlinux_install_dir/0` (`<work_dir>/redist/vmlinux`).
4+
`Hyper.Cfg.Dirs.vmlinux_install_dir/0` (`<work_dir>/redist/vmlinux`).
55
66
The available kernels and their SHA-256 sums come from the statically-embedded
77
`Hyper.Node.FireVMM.VmLinux.Manifest`. `ensure_installed/0` installs *every*
@@ -89,5 +89,5 @@ defmodule Hyper.Node.FireVMM.VmLinux.Provider do
8989

9090
defp build_path(dir, build), do: Path.join(dir, build.asset)
9191

92-
defp install_dir, do: Hyper.Config.vmlinux_install_dir()
92+
defp install_dir, do: Hyper.Cfg.Dirs.vmlinux_install_dir()
9393
end

lib/hyper/node/img/thin_pool.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ defmodule Hyper.Node.Img.ThinPool do
5050
def init(_opts) do
5151
Process.flag(:trap_exit, true)
5252

53-
with :ok <- File.mkdir_p(Hyper.Config.scratch_dir()),
53+
with :ok <- File.mkdir_p(Hyper.Cfg.Dirs.scratch_dir()),
5454
{:ok, meta} <- ensure_backing(@meta_file, ImgConfig.thin_pool_meta_size()),
5555
{:ok, data} <- ensure_backing(@data_file, ImgConfig.thin_pool_data_size()),
5656
:ok <- zero_metadata(meta),
@@ -114,7 +114,7 @@ defmodule Hyper.Node.Img.ThinPool do
114114
# Create a sparse file of `size` if absent; reuse it if already present.
115115
@spec ensure_backing(String.t(), Information.t()) :: {:ok, Path.t()} | {:error, term()}
116116
defp ensure_backing(file, size) do
117-
path = Path.join(Hyper.Config.scratch_dir(), file)
117+
path = Path.join(Hyper.Cfg.Dirs.scratch_dir(), file)
118118

119119
case File.open(path, [:write, :read]) do
120120
{:ok, io} ->

lib/hyper/node/layer/repo.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ defmodule Hyper.Node.Layer.Repo do
1515
@decorate with_span("Hyper.Node.Layer.Repo.test_system")
1616
def test_system do
1717
cond do
18-
not File.exists?(Hyper.Config.layer_dir()) -> {:error, :layer_dir_not_found}
19-
not File.dir?(Hyper.Config.layer_dir()) -> {:error, :layer_dir_not_dir}
18+
not File.exists?(Hyper.Cfg.Dirs.layer_dir()) -> {:error, :layer_dir_not_found}
19+
not File.dir?(Hyper.Cfg.Dirs.layer_dir()) -> {:error, :layer_dir_not_dir}
2020
true -> :ok
2121
end
2222
end
@@ -25,7 +25,7 @@ defmodule Hyper.Node.Layer.Repo do
2525
@spec find_layer(Hyper.Layer.id()) :: {:ok, Path.t()} | {:error, File.posix()}
2626
@decorate with_span("Hyper.Node.Layer.Repo.find_layer")
2727
def find_layer(id) do
28-
path = Path.join([Hyper.Config.layer_dir(), layer_basename(id)])
28+
path = Path.join([Hyper.Cfg.Dirs.layer_dir(), layer_basename(id)])
2929

3030
case File.stat(path) do
3131
{:ok, _stat} -> {:ok, path}

0 commit comments

Comments
 (0)