Skip to content

Commit

Permalink
check if device contains filesystem before format
Browse files Browse the repository at this point in the history
  • Loading branch information
aajkl committed Aug 16, 2024
1 parent 50f2378 commit d919fa7
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 21 deletions.
2 changes: 1 addition & 1 deletion internal/virt/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Interface interface { //nolint
WriteFile(ctx context.Context, handle int, buf []byte) error
SeekFile(ctx context.Context, handle int, offset int, whence int) (position int, eof bool, err error)
AppendLine(ctx context.Context, filepath string, p []byte) error
Blkid(ctx context.Context, dev string) (string, error)
Blkid(ctx context.Context, dev string) (*types.BlkidInfo, error)
GetDiskfree(ctx context.Context, mnt string) (*types.Diskfree, error)
FSFreezeAll(ctx context.Context) (int, error)
FSThawAll(ctx context.Context) (int, error)
Expand Down
14 changes: 9 additions & 5 deletions internal/virt/agent/blk.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,27 @@ import (
"regexp"

"github.com/cockroachdb/errors"
"github.com/projecteru2/yavirt/internal/virt/agent/types"
"github.com/projecteru2/yavirt/pkg/terrors"
)

var blkidRegex = regexp.MustCompile(`(?i)uuid="([-a-f0-9]{36})"`)
var blkidRegex = regexp.MustCompile(`(?i)uuid="([-a-f0-9]{36}).*TYPE="([^"]+)"`)

// Blkid .
func (a *Agent) Blkid(ctx context.Context, dev string) (string, error) {
func (a *Agent) Blkid(ctx context.Context, dev string) (*types.BlkidInfo, error) {
var st = <-a.ExecOutput(ctx, "blkid", dev)
so, _, err := st.Stdio()
if err != nil {
return "", errors.Wrap(err, "")
return nil, errors.Wrap(err, "")
}

var mat = blkidRegex.FindSubmatch(so)
if mat == nil {
return "", errors.Wrapf(terrors.ErrInvalidValue, "invalid blkid: %s", so)
return nil, errors.Wrapf(terrors.ErrInvalidValue, "invalid blkid: %s", so)
}

return string(mat[1]), nil
return &types.BlkidInfo{
ID: string(mat[1]),
Type: string(mat[2]),
}, nil
}
42 changes: 41 additions & 1 deletion internal/virt/agent/mocks/File.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d919fa7

Please sign in to comment.