talm

Форк
0
/
options.go 
143 строки · 4.0 Кб
1
// This Source Code Form is subject to the terms of the Mozilla Public
2
// License, v. 2.0. If a copy of the MPL was not distributed with this
3
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4

5
package mount
6

7
import (
8
	"log"
9

10
	"github.com/aenix-io/talm/internal/pkg/encryption/helpers"
11
	"github.com/siderolabs/talos/pkg/machinery/config/config"
12
)
13

14
const (
15
	// ReadOnly is a flag for setting the mount point as readonly.
16
	ReadOnly Flags = 1 << iota
17
	// Shared is a flag for setting the mount point as shared.
18
	Shared
19
	// Resize indicates that a the partition for a given mount point should be
20
	// resized to the maximum allowed.
21
	Resize
22
	// Overlay indicates that a the partition for a given mount point should be
23
	// mounted using overlayfs.
24
	Overlay
25
	// SystemOverlay indicates that overlay directory should be created under tmpfs.
26
	//
27
	// SystemOverlay should be combined with Overlay option.
28
	SystemOverlay
29
	// ReadonlyOverlay indicates that a the partition for a given mount point should be
30
	// mounted using multi-layer readonly overlay from multiple partitions given as sources.
31
	ReadonlyOverlay
32
	// SkipIfMounted is a flag for skipping mount if the mountpoint is already mounted.
33
	SkipIfMounted
34
	// SkipIfNoFilesystem is a flag for skipping formatting and mounting if the mountpoint has not filesystem.
35
	SkipIfNoFilesystem
36
	// SkipIfNoDevice is a flag for skipping errors when the device is not found.
37
	SkipIfNoDevice
38
)
39

40
// Flags is the mount flags.
41
type Flags uint
42

43
// Options is the functional options struct.
44
type Options struct {
45
	Loopback                string
46
	Prefix                  string
47
	MountFlags              Flags
48
	PreMountHooks           []Hook
49
	PostUnmountHooks        []Hook
50
	Encryption              config.Encryption
51
	SystemInformationGetter helpers.SystemInformationGetter
52
	Logger                  *log.Logger
53
	ProjectQuota            bool
54
}
55

56
// Option is the functional option func.
57
type Option func(*Options)
58

59
// Check checks if all provided flags are set.
60
func (f Flags) Check(flags Flags) bool {
61
	return (f & flags) == flags
62
}
63

64
// Intersects checks if at least one flag is set.
65
func (f Flags) Intersects(flags Flags) bool {
66
	return (f & flags) != 0
67
}
68

69
// WithPrefix is a functional option for setting the mount point prefix.
70
func WithPrefix(o string) Option {
71
	return func(args *Options) {
72
		args.Prefix = o
73
	}
74
}
75

76
// WithFlags is a functional option to set up mount flags.
77
func WithFlags(flags Flags) Option {
78
	return func(args *Options) {
79
		args.MountFlags |= flags
80
	}
81
}
82

83
// WithPreMountHooks adds functions to be called before mounting the partition.
84
func WithPreMountHooks(hooks ...Hook) Option {
85
	return func(args *Options) {
86
		args.PreMountHooks = append(args.PreMountHooks, hooks...)
87
	}
88
}
89

90
// WithPostUnmountHooks adds functions to be called after unmounting the partition.
91
func WithPostUnmountHooks(hooks ...Hook) Option {
92
	return func(args *Options) {
93
		args.PostUnmountHooks = append(args.PostUnmountHooks, hooks...)
94
	}
95
}
96

97
// WithEncryptionConfig partition encryption configuration.
98
func WithEncryptionConfig(cfg config.Encryption) Option {
99
	return func(args *Options) {
100
		args.Encryption = cfg
101
	}
102
}
103

104
// WithLogger sets the logger.
105
func WithLogger(logger *log.Logger) Option {
106
	return func(args *Options) {
107
		args.Logger = logger
108
	}
109
}
110

111
// WithProjectQuota enables project quota mount option.
112
func WithProjectQuota(enable bool) Option {
113
	return func(args *Options) {
114
		args.ProjectQuota = enable
115
	}
116
}
117

118
// WithSystemInformationGetter the function to get system information on the node.
119
func WithSystemInformationGetter(getter helpers.SystemInformationGetter) Option {
120
	return func(args *Options) {
121
		args.SystemInformationGetter = getter
122
	}
123
}
124

125
// Hook represents pre/post mount hook.
126
type Hook func(p *Point) error
127

128
// NewDefaultOptions initializes a Options struct with default values.
129
func NewDefaultOptions(setters ...Option) *Options {
130
	opts := &Options{
131
		Loopback:         "",
132
		Prefix:           "",
133
		MountFlags:       0,
134
		PreMountHooks:    []Hook{},
135
		PostUnmountHooks: []Hook{},
136
	}
137

138
	for _, setter := range setters {
139
		setter(opts)
140
	}
141

142
	return opts
143
}
144

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.