diff options
| author | Ophestra <cat@gensokyo.uk> | 2025-08-12 04:38:45 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2025-08-14 04:52:49 +0900 |
| commit | 99ac96511bed17f48d908db3d00a1f48579ba011 (patch) | |
| tree | 30d404efb7256c9f053f64192068637d6b846db4 /hst/fsephemeral.go | |
| parent | e99d7affb0c128fa82722f9b3d79c99b5e5918cd (diff) | |
hst/fs: interface filesystem config
This allows mount points to be represented by different underlying structs.
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'hst/fsephemeral.go')
| -rw-r--r-- | hst/fsephemeral.go | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/hst/fsephemeral.go b/hst/fsephemeral.go new file mode 100644 index 00000000..e3f924c5 --- /dev/null +++ b/hst/fsephemeral.go @@ -0,0 +1,83 @@ +package hst + +import ( + "encoding/gob" + "os" + "strings" + + "hakurei.app/container" +) + +func init() { gob.Register(new(FSEphemeral)) } + +// FilesystemEphemeral is the [FilesystemConfig.Type] name of a mount point with ephemeral state. +const FilesystemEphemeral = "ephemeral" + +// FSEphemeral represents an ephemeral container mount point. +type FSEphemeral struct { + // mount point in container + Dst *container.Absolute `json:"dst,omitempty"` + // do not mount filesystem read-only + Write bool `json:"write,omitempty"` + // upper limit on the size of the filesystem + Size int `json:"size,omitempty"` + // initial permission bits of the new filesystem + Perm os.FileMode `json:"perm,omitempty"` +} + +func (e *FSEphemeral) Type() string { return FilesystemEphemeral } + +func (e *FSEphemeral) Target() *container.Absolute { + if e == nil { + return nil + } + return e.Dst +} + +func (e *FSEphemeral) Host() []*container.Absolute { return nil } + +const fsEphemeralDefaultPerm = os.FileMode(0755) + +func (e *FSEphemeral) Apply(ops *container.Ops) { + if e == nil || e.Dst == nil { + return + } + + size := e.Size + if size < 0 { + size = 0 + } + + perm := e.Perm + if perm == 0 { + perm = fsEphemeralDefaultPerm + } + + if e.Write { + ops.Tmpfs(e.Dst, size, perm) + } else { + ops.Readonly(e.Dst, perm) + } +} + +func (e *FSEphemeral) String() string { + if e == nil || e.Dst == nil { + return "<invalid>" + } + + expr := new(strings.Builder) + expr.Grow(15 + len(FilesystemEphemeral) + len(e.Dst.String())) + + if e.Write { + expr.WriteString("w") + } + expr.WriteString("+" + FilesystemEphemeral + "(") + if e.Perm != 0 { + expr.WriteString(e.Perm.String()) + } else { + expr.WriteString(fsEphemeralDefaultPerm.String()) + } + expr.WriteString("):" + e.Dst.String()) + + return expr.String() +} |
