talos/internal/pkg/mount/options.go
Artem Chernyshev 02b3719df9 feat: skip filesystem for state and ephemeral partitions in the installer
Filesystem creation step is moved on the later stage: when Talos mounts
the partition for the first time.
Now it checks if the partition doesn't have any filesystem and formats
it right before mounting.

Additionally refactored mount options a bit:
- replaced separate options with a set of binary flags.
- implemented pre-mount and post-unmount hooks.

And fixed typos in couple of places and increased timeout for `apid ready`.

Signed-off-by: Artem Chernyshev <artem.0xD2@gmail.com>
2021-02-17 09:37:21 -08:00

94 lines
2.4 KiB
Go

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package mount
const (
// ReadOnly is a flag for setting the mount point as readonly.
ReadOnly Flags = 1 << iota
// Shared is a flag for setting the mount point as shared.
Shared
// Resize indicates that a the partition for a given mount point should be
// resized to the maximum allowed.
Resize
// Overlay indicates that a the partition for a given mount point should be
// mounted using overlayfs.
Overlay
// SkipIfMounted is a flag for skipping mount if the mountpoint is already mounted.
SkipIfMounted
)
// Flags is the mount flags.
type Flags uint
// Options is the functional options struct.
type Options struct {
Loopback string
Prefix string
MountFlags Flags
PreMountHooks []Hook
PostUnmountHooks []Hook
}
// Option is the functional option func.
type Option func(*Options)
// Check checks if all provided flags are set.
func (f Flags) Check(flags Flags) bool {
return (f & flags) == flags
}
// Intersects checks if at least one flag is set.
func (f Flags) Intersects(flags Flags) bool {
return (f & flags) != 0
}
// WithPrefix is a functional option for setting the mount point prefix.
func WithPrefix(o string) Option {
return func(args *Options) {
args.Prefix = o
}
}
// WithFlags is a functional option to set up mount flags.
func WithFlags(flags Flags) Option {
return func(args *Options) {
args.MountFlags = flags
}
}
// WithPreMountHooks adds functions to be called before mounting the partition.
func WithPreMountHooks(hooks ...Hook) Option {
return func(args *Options) {
args.PreMountHooks = hooks
}
}
// WithPostUnmountHooks adds functions to be called after unmounting the partition.
func WithPostUnmountHooks(hooks ...Hook) Option {
return func(args *Options) {
args.PostUnmountHooks = hooks
}
}
// Hook represents pre/post mount hook.
type Hook func(p *Point) error
// NewDefaultOptions initializes a Options struct with default values.
func NewDefaultOptions(setters ...Option) *Options {
opts := &Options{
Loopback: "",
Prefix: "",
MountFlags: 0,
PreMountHooks: []Hook{},
PostUnmountHooks: []Hook{},
}
for _, setter := range setters {
setter(opts)
}
return opts
}