Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use squashfuse ready notifier if available #528

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module stackerbuild.io/stacker
go 1.20

require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be
github.com/apex/log v1.9.0
github.com/apparentlymart/go-shquot v0.0.1
Expand Down Expand Up @@ -45,7 +46,6 @@ require (
github.com/DataDog/zstd v1.4.8 // indirect
github.com/MakeNowJust/heredoc v1.0.0 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.2.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
github.com/Microsoft/go-winio v0.6.0 // indirect
github.com/Microsoft/hcsshim v0.10.0-rc.7 // indirect
Expand Down
124 changes: 114 additions & 10 deletions pkg/squashfs/squashfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"syscall"
"time"

"github.com/Masterminds/semver/v3"
"github.com/pkg/errors"
"golang.org/x/sys/unix"
"stackerbuild.io/stacker/pkg/log"
Expand Down Expand Up @@ -43,6 +44,15 @@
include []string
}

type squashFuseInfoStruct struct {
Path string
Version string
SupportsNotfiy bool
}

var once sync.Once
var squashFuseInfo = squashFuseInfoStruct{"", "", false}

func NewExcludePaths() *ExcludePaths {
return &ExcludePaths{
exclude: map[string]bool{},
Expand Down Expand Up @@ -198,11 +208,45 @@
return false, nil
}

func findSquashfusePath() string {
func findSquashFuseInfo() {
var sqfsPath string

Check warning on line 212 in pkg/squashfs/squashfs.go

View check run for this annotation

Codecov / codecov/patch

pkg/squashfs/squashfs.go#L211-L212

Added lines #L211 - L212 were not covered by tests
if p := which("squashfuse_ll"); p != "" {
return p
sqfsPath = p
} else {
sqfsPath = which("squashfuse")

Check warning on line 216 in pkg/squashfs/squashfs.go

View check run for this annotation

Codecov / codecov/patch

pkg/squashfs/squashfs.go#L214-L216

Added lines #L214 - L216 were not covered by tests
}
return which("squashfuse")
if sqfsPath == "" {
return
}
version, supportsNotify := sqfuseSupportsMountNotification(sqfsPath)
log.Infof("Found squashfuse at %s (version=%s notify=%t)", sqfsPath, version, supportsNotify)
squashFuseInfo = squashFuseInfoStruct{sqfsPath, version, supportsNotify}

Check warning on line 223 in pkg/squashfs/squashfs.go

View check run for this annotation

Codecov / codecov/patch

pkg/squashfs/squashfs.go#L218-L223

Added lines #L218 - L223 were not covered by tests
}

// sqfuseSupportsMountNotification - returns true if squashfuse supports mount
// notification, false otherwise
// sqfuse is the path to the squashfuse binary
func sqfuseSupportsMountNotification(sqfuse string) (string, bool) {
cmd := exec.Command(sqfuse)

// `squashfuse` always returns an error... so we ignore it.
out, _ := cmd.CombinedOutput()

firstLine := strings.Split(string(out[:]), "\n")[0]
version := strings.Split(firstLine, " ")[1]
v, err := semver.NewVersion(version)
if err != nil {
return version, false
}

Check warning on line 240 in pkg/squashfs/squashfs.go

View check run for this annotation

Codecov / codecov/patch

pkg/squashfs/squashfs.go#L229-L240

Added lines #L229 - L240 were not covered by tests
// squashfuse notify mechanism was merged in 0.5.0
constraint, err := semver.NewConstraint(">= 0.5.0")
if err != nil {
return version, false
}
if constraint.Check(v) {
return version, true
}
return version, false

Check warning on line 249 in pkg/squashfs/squashfs.go

View check run for this annotation

Codecov / codecov/patch

pkg/squashfs/squashfs.go#L242-L249

Added lines #L242 - L249 were not covered by tests
}

var squashNotFound = errors.Errorf("squashfuse program not found")
Expand All @@ -211,12 +255,28 @@
// return a pointer to the squashfuse cmd.
// The caller of the this is responsible for the process created.
func squashFuse(squashFile, extractDir string) (*exec.Cmd, error) {
sqfuse := findSquashfusePath()
var cmd *exec.Cmd
if sqfuse == "" {

once.Do(findSquashFuseInfo)
if squashFuseInfo.Path == "" {

Check warning on line 261 in pkg/squashfs/squashfs.go

View check run for this annotation

Codecov / codecov/patch

pkg/squashfs/squashfs.go#L259-L261

Added lines #L259 - L261 were not covered by tests
return cmd, squashNotFound
}

notifyOpts := ""
notifyPath := ""
if squashFuseInfo.SupportsNotfiy {
sockdir, err := os.MkdirTemp("", "sock")
ariel-miculas marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return cmd, err
}
defer os.RemoveAll(sockdir)
notifyPath = filepath.Join(sockdir, "notifypipe")
if err := syscall.Mkfifo(notifyPath, 0640); err != nil {
return cmd, err
}
notifyOpts = "notify_pipe=" + notifyPath

Check warning on line 277 in pkg/squashfs/squashfs.go

View check run for this annotation

Codecov / codecov/patch

pkg/squashfs/squashfs.go#L265-L277

Added lines #L265 - L277 were not covered by tests
}

// given extractDir of path/to/some/dir[/], log to path/to/some/.dir-squashfs.log
extractDir = strings.TrimSuffix(extractDir, "/")
smoser marked this conversation as resolved.
Show resolved Hide resolved

Expand All @@ -240,12 +300,16 @@
// It would be nice to only enable debug (or maybe to only log to file at all)
// if 'stacker --debug', but we do not have access to that info here.
// to debug squashfuse, use "allow_other,debug"
cmd = exec.Command(sqfuse, "-f", "-o", "allow_other,debug", squashFile, extractDir)
optionArgs := "allow_other,debug"
if notifyOpts != "" {
optionArgs += "," + notifyOpts
}
cmd = exec.Command(squashFuseInfo.Path, "-f", "-o", optionArgs, squashFile, extractDir)

Check warning on line 307 in pkg/squashfs/squashfs.go

View check run for this annotation

Codecov / codecov/patch

pkg/squashfs/squashfs.go#L303-L307

Added lines #L303 - L307 were not covered by tests
cmd.Stdin = nil
cmd.Stdout = cmdOut
cmd.Stderr = cmdOut
cmdOut.Write([]byte(fmt.Sprintf("# %s\n", strings.Join(cmd.Args, " "))))
log.Debugf("Extracting %s -> %s with %s [%s]", squashFile, extractDir, sqfuse, logf)
log.Debugf("Extracting %s -> %s with %s [%s]", squashFile, extractDir, squashFuseInfo.Path, logf)

Check warning on line 312 in pkg/squashfs/squashfs.go

View check run for this annotation

Codecov / codecov/patch

pkg/squashfs/squashfs.go#L312

Added line #L312 was not covered by tests
err = cmd.Start()
if err != nil {
return cmd, err
Expand All @@ -260,18 +324,57 @@
// c. a timeout (timeLimit) was hit
startTime := time.Now()
timeLimit := 30 * time.Second
alarmCh := make(chan struct{})

Check warning on line 327 in pkg/squashfs/squashfs.go

View check run for this annotation

Codecov / codecov/patch

pkg/squashfs/squashfs.go#L327

Added line #L327 was not covered by tests
go func() {
cmd.Wait()
close(alarmCh)

Check warning on line 330 in pkg/squashfs/squashfs.go

View check run for this annotation

Codecov / codecov/patch

pkg/squashfs/squashfs.go#L330

Added line #L330 was not covered by tests
}()
if squashFuseInfo.SupportsNotfiy {
notifyCh := make(chan byte)
log.Infof("%s supports notify pipe, watching %q", squashFuseInfo.Path, notifyPath)
go func() {
f, err := os.Open(notifyPath)
if err != nil {
return
}
defer f.Close()
b1 := make([]byte, 1)
for {
n1, err := f.Read(b1)
if err != nil {
return
}
if err == nil && n1 >= 1 {
break

Check warning on line 348 in pkg/squashfs/squashfs.go

View check run for this annotation

Codecov / codecov/patch

pkg/squashfs/squashfs.go#L332-L348

Added lines #L332 - L348 were not covered by tests
}
}
notifyCh <- b1[0]

Check warning on line 351 in pkg/squashfs/squashfs.go

View check run for this annotation

Codecov / codecov/patch

pkg/squashfs/squashfs.go#L351

Added line #L351 was not covered by tests
}()
if err != nil {
return cmd, errors.Wrapf(err, "Failed reading %q", notifyPath)
}

Check warning on line 355 in pkg/squashfs/squashfs.go

View check run for this annotation

Codecov / codecov/patch

pkg/squashfs/squashfs.go#L353-L355

Added lines #L353 - L355 were not covered by tests

select {
case <-alarmCh:
cmd.Process.Kill()
return cmd, errors.Wrapf(err, "Gave up on squashFuse mount of %s with %s after %s", squashFile, squashFuseInfo.Path, timeLimit)
case ret := <-notifyCh:
if ret == 's' {
return cmd, nil
} else {
return cmd, errors.Errorf("squashfuse returned an error, check %s", logf)
}

Check warning on line 366 in pkg/squashfs/squashfs.go

View check run for this annotation

Codecov / codecov/patch

pkg/squashfs/squashfs.go#L357-L366

Added lines #L357 - L366 were not covered by tests
}
}
for count := 0; !fileChanged(fiPre, extractDir); count++ {
if cmd.ProcessState != nil {
// process exited, the Wait() call in the goroutine above
// caused ProcessState to be populated.
return cmd, errors.Errorf("squashFuse mount of %s with %s exited unexpectedly with %d", squashFile, sqfuse, cmd.ProcessState.ExitCode())
return cmd, errors.Errorf("squashFuse mount of %s with %s exited unexpectedly with %d", squashFile, squashFuseInfo.Path, cmd.ProcessState.ExitCode())

Check warning on line 373 in pkg/squashfs/squashfs.go

View check run for this annotation

Codecov / codecov/patch

pkg/squashfs/squashfs.go#L373

Added line #L373 was not covered by tests
}
if time.Since(startTime) > timeLimit {
cmd.Process.Kill()
return cmd, errors.Wrapf(err, "Gave up on squashFuse mount of %s with %s after %s", squashFile, sqfuse, timeLimit)
return cmd, errors.Wrapf(err, "Gave up on squashFuse mount of %s with %s after %s", squashFile, squashFuseInfo.Path, timeLimit)

Check warning on line 377 in pkg/squashfs/squashfs.go

View check run for this annotation

Codecov / codecov/patch

pkg/squashfs/squashfs.go#L377

Added line #L377 was not covered by tests
}
if count%10 == 1 {
log.Debugf("%s is not yet mounted...(%s)", extractDir, time.Since(startTime))
Expand Down Expand Up @@ -448,7 +551,8 @@
}

func (k *SquashFuseExtractor) IsAvailable() error {
if findSquashfusePath() == "" {
once.Do(findSquashFuseInfo)
if squashFuseInfo.Path == "" {

Check warning on line 555 in pkg/squashfs/squashfs.go

View check run for this annotation

Codecov / codecov/patch

pkg/squashfs/squashfs.go#L554-L555

Added lines #L554 - L555 were not covered by tests
return errors.Errorf("no 'squashfuse' in PATH")
}
return nil
Expand Down
Loading