Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Commit

Permalink
rock5b: ignore lost+found when preserving partition
Browse files Browse the repository at this point in the history
As this fork uses ext4 for the `BOOT` partition for compatibility
with the vendor U-Boot, `/lost+found` exists on that (unlike the
xfs partitions, which do not).

As a result, the archiver needs to be ignored when preserving the
filesystem or the installer will fail at upgrade to to the directory
already existing on the new partition.

For safety/sanity, any files in `/lost+found` are also ignored.
Given that `/boot` is read-only, there isn't really anything we
should ever need to manually restore from this partition.

This is intended to fix upgrades without `--preserve`.
  • Loading branch information
milas committed Jan 18, 2023
1 parent ff62be7 commit fddf5a9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
20 changes: 16 additions & 4 deletions cmd/installer/pkg/install/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,9 +506,10 @@ func (m *Manifest) preserveContents(device Device, targets []*Target) (err error
}

var (
sourcePart *gpt.Partition
fileSystemType partition.FileSystemType
fnmatchFilters []string
sourcePart *gpt.Partition
fileSystemType partition.FileSystemType
fnmatchFilters []string
fnignoreFilters []string
)

sources := append(
Expand All @@ -528,6 +529,17 @@ func (m *Manifest) preserveContents(device Device, targets []*Target) (err error
fileSystemType = source.FileSystemType
fnmatchFilters = source.FnmatchFilters

// HACK: for ext filesystems, the lost+found directory will
// already exist. we'll go ahead and ignore it AND its
// contents if it has any (/boot isn't r-w, so there isn't
// anything we'd ever need to recover from it)
if source.FileSystemType == partition.FilesystemTypeExt4 {
fnignoreFilters = []string{
"lost+found",
"lost+found/*",
}
}

break
}
}
Expand All @@ -543,7 +555,7 @@ func (m *Manifest) preserveContents(device Device, targets []*Target) (err error
continue
}

if err = target.SaveContents(device, sourcePart, fileSystemType, fnmatchFilters); err != nil {
if err = target.SaveContents(device, sourcePart, fileSystemType, fnmatchFilters, fnignoreFilters); err != nil {
log.Printf("warning: failed to preserve contents of %q on %q: %s", target.Label, device.Device, err)
}
}
Expand Down
5 changes: 4 additions & 1 deletion cmd/installer/pkg/install/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ func (t *Target) SaveContents(
source *gpt.Partition,
fileSystemType partition.FileSystemType,
fnmatchFilters []string,
fnignoreFilters []string,
) error {
partPath, err := util.PartPath(device.Device, int(source.Number))
if err != nil {
Expand All @@ -341,7 +342,7 @@ func (t *Target) SaveContents(
if fileSystemType == partition.FilesystemTypeNone {
err = t.saveRawContents(partPath)
} else {
err = t.saveFilesystemContents(partPath, fileSystemType, fnmatchFilters)
err = t.saveFilesystemContents(partPath, fileSystemType, fnmatchFilters, fnignoreFilters)
}

if err != nil {
Expand Down Expand Up @@ -380,6 +381,7 @@ func (t *Target) saveFilesystemContents(
partPath string,
fileSystemType partition.FileSystemType,
fnmatchFilters []string,
fnignoreFilters []string,
) error {
t.Contents = bytes.NewBuffer(nil)

Expand All @@ -390,6 +392,7 @@ func (t *Target) saveFilesystemContents(
mountPath,
t.Contents,
archiver.WithFnmatchPatterns(fnmatchFilters...),
archiver.WithFnignorePatterns(fnignoreFilters...),
)
},
)
Expand Down
26 changes: 22 additions & 4 deletions pkg/archiver/walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ const (
)

type walkerOptions struct {
skipRoot bool
maxRecurseDepth int
fnmatchPatterns []string
types map[FileType]struct{}
skipRoot bool
maxRecurseDepth int
fnmatchPatterns []string
fnignorePatterns []string
types map[FileType]struct{}
}

// WalkerOption configures Walker.
Expand Down Expand Up @@ -65,6 +66,15 @@ func WithFnmatchPatterns(patterns ...string) WalkerOption {
}
}

// WithFnignorePatterns ignores results matching any of the patterns.
//
// Default is not to do any ignoring.
func WithFnignorePatterns(patterns ...string) WalkerOption {
return func(o *walkerOptions) {
o.fnignorePatterns = append(o.fnignorePatterns, patterns...)
}
}

// WithFileTypes filters results by file types.
//
// Default is not to do any filtering.
Expand Down Expand Up @@ -166,6 +176,14 @@ func Walker(ctx context.Context, rootPath string, options ...WalkerOption) (<-ch
}
}

if item.Error == nil && len(opts.fnignorePatterns) > 0 {
for _, pattern := range opts.fnignorePatterns {
if matches, _ := filepath.Match(pattern, item.RelPath); matches { //nolint:errcheck
return nil
}
}
}

select {
case <-ctx.Done():
return ctx.Err()
Expand Down

0 comments on commit fddf5a9

Please sign in to comment.