Skip to content

Commit

Permalink
fix: when writing to META in the installer/imager, use fixed name
Browse files Browse the repository at this point in the history
Use fixed partition name instead of trying to auto-discover by label.

Auto-discovery by label might hit completely wrong blockdevice.

Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com>
(cherry picked from commit 6dc776b)
  • Loading branch information
smira committed Nov 8, 2023
1 parent 6be1e58 commit 0b18d74
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
25 changes: 23 additions & 2 deletions cmd/installer/pkg/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,30 @@ func (i *Installer) Install(ctx context.Context, mode Mode) (err error) {
}

if mode == ModeUpgrade || len(i.options.MetaValues.values) > 0 {
var metaState *meta.Meta
var (
metaState *meta.Meta
metaPartitionName string
)

if metaState, err = meta.New(context.Background(), nil); err != nil {
for _, targets := range i.manifest.Targets {
for _, target := range targets {
if target.Label == constants.MetaPartitionLabel {
metaPartitionName = target.PartitionName

break
}
}

if metaPartitionName != "" {
break
}
}

if metaPartitionName == "" {
return fmt.Errorf("failed to detect META partition")
}

if metaState, err = meta.New(context.Background(), nil, meta.WithPrinter(i.options.Printf), meta.WithFixedPath(metaPartitionName)); err != nil {
return err
}

Expand Down
15 changes: 13 additions & 2 deletions internal/pkg/meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Meta struct {
// Options configures the META.
type Options struct {
fixedPath string
printer func(string, ...any)
}

// Option is a functional option.
Expand All @@ -52,10 +53,20 @@ func WithFixedPath(path string) Option {
}
}

// WithPrinter sets the function to print the logs, default is log.Printf.
func WithPrinter(printer func(string, ...any)) Option {
return func(o *Options) {
o.printer = printer
}
}

// New initializes empty META, trying to probe the existing META first.
func New(ctx context.Context, st state.State, opts ...Option) (*Meta, error) {
meta := &Meta{
state: st,
opts: Options{
printer: log.Printf,
},
}

for _, opt := range opts {
Expand Down Expand Up @@ -128,7 +139,7 @@ func (meta *Meta) Reload(ctx context.Context) error {
adv.SetTagBytes(t, val)
}

log.Printf("META: loaded %d keys", len(adv.ListTags()))
meta.opts.printer("META: loaded %d keys", len(adv.ListTags()))

meta.talos = adv
meta.legacy = legacyAdv
Expand Down Expand Up @@ -221,7 +232,7 @@ func (meta *Meta) Flush() error {
return fmt.Errorf("expected to write %d bytes, wrote %d", len(serialized), n)
}

log.Printf("META: saved %d keys", len(meta.talos.ListTags()))
meta.opts.printer("META: saved %d keys", len(meta.talos.ListTags()))

return f.Sync()
}
Expand Down

0 comments on commit 0b18d74

Please sign in to comment.