-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This first piece implements filesystem detection. Signed-off-by: Andrey Smirnov <[email protected]>
- Loading branch information
Showing
44 changed files
with
2,913 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT. | ||
# | ||
# Generated on 2024-02-05T11:19:51Z by kres 0e666ea. | ||
# Generated on 2024-02-09T14:38:27Z by kres latest. | ||
|
||
* | ||
!blkid | ||
!block | ||
!go.mod | ||
!go.sum | ||
!.golangci.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,26 @@ | ||
kind: common.Repository | ||
spec: | ||
mainBranch: v2 | ||
--- | ||
kind: common.Docker | ||
name: setup-ci | ||
spec: | ||
allowInsecure: true | ||
--- | ||
kind: golang.UnitTests | ||
name: unit-tests | ||
spec: | ||
requiresInsecure: true | ||
--- | ||
kind: golang.Toolchain | ||
spec: | ||
extraPackages: | ||
- cdrkit | ||
- cryptsetup | ||
- dosfstools | ||
- e2fsprogs | ||
- xfsprogs | ||
--- | ||
kind: service.CodeCov | ||
spec: | ||
targetThreshold: 40 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
// Package blkid provides information about blockdevice filesystem types and IDs. | ||
package blkid | ||
|
||
import ( | ||
"github.com/google/uuid" | ||
|
||
"github.com/siderolabs/go-blockdevice/v2/block" | ||
) | ||
|
||
// Info represents the result of the probe. | ||
type Info struct { //nolint:govet | ||
// Link to the block device, only if the probed file is a blockdevice. | ||
BlockDevice *block.Device | ||
|
||
// Overall size of the probed device (in bytes). | ||
Size uint64 | ||
|
||
// Optimal I/O size for the device (in bytes). | ||
IOSize uint64 | ||
|
||
// TODO: API might be different. | ||
Name string | ||
UUID *uuid.UUID | ||
Label *string | ||
|
||
BlockSize uint32 | ||
FilesystemBlockSize uint32 | ||
FilesystemSize uint64 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
//go:build linux | ||
|
||
package blkid | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"syscall" | ||
|
||
"golang.org/x/sys/unix" | ||
|
||
"github.com/siderolabs/go-blockdevice/v2/block" | ||
) | ||
|
||
// ProbePath returns the probe information for the specified path. | ||
func ProbePath(devpath string) (*Info, error) { | ||
f, err := os.OpenFile(devpath, os.O_RDONLY|unix.O_CLOEXEC|unix.O_NONBLOCK, 0) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer f.Close() //nolint:errcheck | ||
|
||
return Probe(f) | ||
} | ||
|
||
// Probe returns the probe information for the specified file. | ||
func Probe(f *os.File) (*Info, error) { | ||
unix.Fadvise(int(f.Fd()), 0, 0, unix.FADV_RANDOM) //nolint:errcheck // best-effort: we don't care if this fails | ||
|
||
st, err := f.Stat() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to stat: %w", err) | ||
} | ||
|
||
info := &Info{} | ||
|
||
sysStat := st.Sys().(*syscall.Stat_t) //nolint:errcheck,forcetypeassert // we know it's a syscall.Stat_t | ||
|
||
switch sysStat.Mode & unix.S_IFMT { | ||
case unix.S_IFBLK: | ||
// block device, initialize full support | ||
info.BlockDevice = block.NewFromFile(f) | ||
|
||
if size, err := info.BlockDevice.GetSize(); err == nil { | ||
info.Size = size | ||
} else { | ||
return nil, fmt.Errorf("failed to get block device size: %w", err) | ||
} | ||
|
||
if ioSize, err := info.BlockDevice.GetIOSize(); err == nil { | ||
info.IOSize = ioSize | ||
} else { | ||
return nil, fmt.Errorf("failed to get block device I/O size: %w", err) | ||
} | ||
case unix.S_IFREG: | ||
// regular file (an image?), so use different settings | ||
info.Size = uint64(st.Size()) | ||
info.IOSize = block.DefaultBlockSize | ||
default: | ||
return nil, fmt.Errorf("unsupported file type: %s", st.Mode().Type()) | ||
} | ||
|
||
if err := info.probe(f, 0, info.Size); err != nil { | ||
return nil, fmt.Errorf("failed to probe: %w", err) | ||
} | ||
|
||
return info, nil | ||
} |
Oops, something went wrong.