Bump yip and diskfs (#717)

* Bump yip and diskfs

---------

Signed-off-by: Itxaka <itxaka@kairos.io>
This commit is contained in:
Itxaka
2025-03-26 11:57:29 +01:00
committed by GitHub
parent db703db5e5
commit 4975b9b914
9 changed files with 47 additions and 87 deletions

View File

@@ -19,12 +19,11 @@ package action_test
import (
"bytes"
"fmt"
"github.com/diskfs/go-diskfs"
"os"
"path/filepath"
"regexp"
fileBackend "github.com/diskfs/go-diskfs/backend/file"
"github.com/kairos-io/kairos-agent/v2/pkg/action"
agentConfig "github.com/kairos-io/kairos-agent/v2/pkg/config"
"github.com/kairos-io/kairos-agent/v2/pkg/constants"
@@ -105,7 +104,8 @@ var _ = Describe("Install action tests", func() {
device = filepath.Join(tmpdir, "test.img")
Expect(os.RemoveAll(device)).Should(Succeed())
// at least 2Gb in size as state is set to 1G
_, err = diskfs.Create(device, 2*1024*1024*1024, diskfs.Raw, 512)
_, err = fileBackend.CreateFromPath(device, 2*1024*1024*1024)
Expect(err).ToNot(HaveOccurred())
config.Install.Device = device

View File

@@ -24,7 +24,6 @@ import (
"path/filepath"
"syscall"
diskfs "github.com/diskfs/go-diskfs/disk"
"github.com/diskfs/go-diskfs/partition/gpt"
agentConfig "github.com/kairos-io/kairos-agent/v2/pkg/config"
cnst "github.com/kairos-io/kairos-agent/v2/pkg/constants"
@@ -49,7 +48,7 @@ func NewElemental(config *agentConfig.Config) *Elemental {
// FormatPartition will format an already existing partition
func (e *Elemental) FormatPartition(part *types.Partition, opts ...string) error {
e.config.Logger.Infof("Formatting '%s' partition", part.FilesystemLabel)
return partitioner.FormatDevice(e.config.Runner, part.Path, part.FS, part.FilesystemLabel, opts...)
return partitioner.FormatDevice(e.config.Logger, e.config.Runner, part.Path, part.FS, part.FilesystemLabel, opts...)
}
// PartitionAndFormatDevice creates a new empty partition table on target disk
@@ -73,13 +72,10 @@ func (e *Elemental) PartitionAndFormatDevice(i v1.SharedInstallSpec) error {
return err
}
// Only re-read table on devices. On files there is no need and this call will fail
if disk.Type == diskfs.Device {
err = disk.ReReadPartitionTable()
if err != nil {
e.config.Logger.Errorf("Reread table: %s", err)
return err
}
err = disk.ReReadPartitionTable()
if err != nil {
e.config.Logger.Errorf("Reread table: %s", err)
return err
}
table, err := disk.GetPartitionTable()
@@ -121,7 +117,7 @@ func (e *Elemental) PartitionAndFormatDevice(i v1.SharedInstallSpec) error {
if err != nil {
e.config.Logger.Errorf("Failed finding partition %s by partition label: %s", configPart.FilesystemLabel, err)
}
err = partitioner.FormatDevice(e.config.Runner, device, configPart.FS, configPart.FilesystemLabel)
err = partitioner.FormatDevice(e.config.Logger, e.config.Runner, device, configPart.FS, configPart.FilesystemLabel)
if err != nil {
e.config.Logger.Errorf("Failed formatting partition: %s", err)
return err

View File

@@ -20,10 +20,10 @@ import (
"bytes"
"errors"
"fmt"
"github.com/diskfs/go-diskfs"
"golang.org/x/sys/unix"
"os"
"path/filepath"
"reflect"
"strings"
sc "syscall"
"testing"
@@ -38,7 +38,7 @@ import (
ghwMock "github.com/kairos-io/kairos-sdk/ghw/mocks"
sdkTypes "github.com/kairos-io/kairos-sdk/types"
"github.com/diskfs/go-diskfs"
fileBackend "github.com/diskfs/go-diskfs/backend/file"
"github.com/diskfs/go-diskfs/partition/gpt"
"github.com/gofrs/uuid"
. "github.com/onsi/ginkgo/v2"
@@ -364,7 +364,7 @@ var _ = Describe("Elemental", Label("elemental"), func() {
Expect(err).To(BeNil())
Expect(os.RemoveAll(filepath.Join(tmpDir, "/test.img"))).ToNot(HaveOccurred())
// at least 2Gb in size as state is set to 1G
_, err = diskfs.Create(filepath.Join(tmpDir, "/test.img"), 2*1024*1024*1024, diskfs.Raw, 512)
_, err = fileBackend.CreateFromPath(filepath.Join(tmpDir, "/test.img"), 2*1024*1024*1024)
Expect(err).ToNot(HaveOccurred())
config.Install.Device = filepath.Join(tmpDir, "/test.img")
install, err = agentConfig.NewInstallSpec(config)
@@ -382,17 +382,18 @@ var _ = Describe("Elemental", Label("elemental"), func() {
install.Firmware = v1.EFI
Expect(install.Partitions.SetFirmwarePartitions(v1.EFI, v1.GPT)).To(BeNil())
Expect(el.PartitionAndFormatDevice(install)).To(BeNil())
disk, err := diskfs.Open(filepath.Join(tmpDir, "/test.img"), diskfs.WithOpenMode(diskfs.ReadOnly))
disk, err := fileBackend.OpenFromPath(filepath.Join(tmpDir, "/test.img"), true)
defer disk.Close()
table, err := gpt.Read(disk, int(diskfs.SectorSize512), int(diskfs.SectorSize512))
Expect(err).ToNot(HaveOccurred())
// check that its type GPT
Expect(reflect.TypeOf(disk.Table)).To(Equal(reflect.TypeOf(&gpt.Table{})))
Expect(table.Type()).To(Equal("gpt"))
// Expect the disk UUID to be constant
Expect(strings.ToLower(disk.Table.UUID())).To(Equal(strings.ToLower(cnst.DiskUUID)))
Expect(strings.ToLower(table.UUID())).To(Equal(strings.ToLower(cnst.DiskUUID)))
// 5 partitions (boot, oem, recovery, state and persistent)
Expect(len(disk.Table.GetPartitions())).To(Equal(5))
Expect(len(table.GetPartitions())).To(Equal(5))
// Cast the boot partition into specific type to check the type and such
part := disk.Table.GetPartitions()[0]
part := table.GetPartitions()[0]
partition, ok := part.(*gpt.Partition)
Expect(ok).To(BeTrue())
// Should be efi type
@@ -402,8 +403,8 @@ var _ = Describe("Elemental", Label("elemental"), func() {
// Should have predictable UUID
Expect(strings.ToLower(partition.UUID())).To(Equal(strings.ToLower(uuid.NewV5(uuid.NamespaceURL, cnst.EfiLabel).String())))
// Check the rest have the proper types
for i := 1; i < len(disk.Table.GetPartitions()); i++ {
part := disk.Table.GetPartitions()[i]
for i := 1; i < len(table.GetPartitions()); i++ {
part := table.GetPartitions()[i]
partition, ok := part.(*gpt.Partition)
Expect(ok).To(BeTrue())
// all of them should have the Linux fs type
@@ -415,17 +416,19 @@ var _ = Describe("Elemental", Label("elemental"), func() {
install.Firmware = v1.BIOS
Expect(install.Partitions.SetFirmwarePartitions(v1.BIOS, v1.GPT)).To(BeNil())
Expect(el.PartitionAndFormatDevice(install)).To(BeNil())
disk, err := diskfs.Open(filepath.Join(tmpDir, "/test.img"), diskfs.WithOpenMode(diskfs.ReadOnly))
disk, err := fileBackend.OpenFromPath(filepath.Join(tmpDir, "/test.img"), true)
defer disk.Close()
Expect(err).ToNot(HaveOccurred())
table, err := gpt.Read(disk, int(diskfs.SectorSize512), int(diskfs.SectorSize512))
Expect(err).ToNot(HaveOccurred())
// check that its type GPT
Expect(reflect.TypeOf(disk.Table)).To(Equal(reflect.TypeOf(&gpt.Table{})))
Expect(table.Type()).To(Equal("gpt"))
// Expect the disk UUID to be constant
Expect(strings.ToLower(disk.Table.UUID())).To(Equal(strings.ToLower(cnst.DiskUUID)))
Expect(strings.ToLower(table.UUID())).To(Equal(strings.ToLower(cnst.DiskUUID)))
// 5 partitions (boot, oem, recovery, state and persistent)
Expect(len(disk.Table.GetPartitions())).To(Equal(5))
Expect(len(table.GetPartitions())).To(Equal(5))
// Cast the boot partition into specific type to check the type and such
part := disk.Table.GetPartitions()[0]
part := table.GetPartitions()[0]
partition, ok := part.(*gpt.Partition)
Expect(ok).To(BeTrue())
// Should be BIOS boot type
@@ -434,8 +437,8 @@ var _ = Describe("Elemental", Label("elemental"), func() {
Expect(partition.Name).To(Equal(cnst.BiosPartName))
// Should have predictable UUID
Expect(strings.ToLower(partition.UUID())).To(Equal(strings.ToLower(uuid.NewV5(uuid.NamespaceURL, cnst.EfiLabel).String())))
for i := 1; i < len(disk.Table.GetPartitions()); i++ {
part := disk.Table.GetPartitions()[i]
for i := 1; i < len(table.GetPartitions()); i++ {
part := table.GetPartitions()[i]
partition, ok := part.(*gpt.Partition)
Expect(ok).To(BeTrue())
// all of them should have the Linux fs type

View File

@@ -21,6 +21,7 @@ import (
"regexp"
v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1"
"github.com/kairos-io/kairos-sdk/types"
)
type MkfsCall struct {
@@ -77,11 +78,11 @@ func (mkfs MkfsCall) Apply() (string, error) {
}
// FormatDevice formats a block device with the given parameters
func FormatDevice(runner v1.Runner, device string, fileSystem string, label string, opts ...string) error {
func FormatDevice(logger types.KairosLogger, runner v1.Runner, device string, fileSystem string, label string, opts ...string) error {
mkfs := MkfsCall{fileSystem: fileSystem, label: label, customOpts: opts, dev: device, runner: runner}
out, err := mkfs.Apply()
if err != nil {
fmt.Println(out)
logger.Logger.Warn().Err(err).Str("output", out).Msg("mkfs failed")
}
return err
}

View File

@@ -162,7 +162,6 @@ func GetPartitionViaDM(fs v1.FS, label string) *types.Partition {
if len(slaves) == 1 {
// We got the partition this dm is associated to, now lets read that partition udev identifier
partNumber, err := fs.ReadFile(filepath.Join(lp.SysBlock, dev.Name(), "slaves", slaves[0].Name(), "dev"))
fmt.Println(string(partNumber))
// If no errors and partNumber not empty read the device from udev
if err == nil || string(partNumber) != "" {
// Now for some magic!
@@ -173,7 +172,6 @@ func GetPartitionViaDM(fs v1.FS, label string) *types.Partition {
// extract the udevInfo called ID_PART_ENTRY_DISK which gives us the udev ID of the parent disk
baseID := strings.Split(strings.TrimSpace(string(partNumber)), ":")
udevID = fmt.Sprintf("b%s:0", baseID[0])
fmt.Printf("Reading udevdata of device: %s\n", filepath.Join(lp.RunUdevData, udevID))
// Read udev info about this device
udevBytes, _ = fs.ReadFile(filepath.Join(lp.RunUdevData, udevID))
udevInfo = make(map[string]string)