Skip to content

Commit

Permalink
Merge pull request #28 from threefoldtech/main_exclude_pxe_device
Browse files Browse the repository at this point in the history
exclude devices labeled zospxe while booting
  • Loading branch information
xmonader authored Dec 4, 2024
2 parents 0a81fa1 + 5e82a32 commit 52bec59
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 40 deletions.
2 changes: 1 addition & 1 deletion pkg/storage/filesystem/btrfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (p *btrfsPool) Device() DeviceInfo {
}

func (p *btrfsPool) prepare() error {
p.name = p.device.Label
p.name = p.device.UUID
if p.device.Used() {
// device already have filesystem
return nil
Expand Down
23 changes: 16 additions & 7 deletions pkg/storage/filesystem/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ type blockDevices struct {
type DeviceInfo struct {
mgr DeviceManager

Path string `json:"path"`
Label string `json:"label"`
Size uint64 `json:"size"`
Filesystem FSType `json:"fstype"`
Rota bool `json:"rota"`
Subsystems string `json:"subsystems"`
Path string `json:"path"`
Label string `json:"label"`
Size uint64 `json:"size"`
Filesystem FSType `json:"fstype"`
Rota bool `json:"rota"`
Subsystems string `json:"subsystems"`
UUID string `json:"uuid"`
Children []DeviceInfo `json:"children,omitempty"`
}

func (i *DeviceInfo) Name() string {
Expand All @@ -77,6 +79,10 @@ func (d *DeviceInfo) Mountpoint(ctx context.Context) (string, error) {
return d.mgr.Mountpoint(ctx, d.Path)
}

func (d *DeviceInfo) IsPXEPartition() bool {
return d.Label == "ZOSPXE"
}

// lsblkDeviceManager uses the lsblk utility to scann the disk for devices, and
// caches the result.
//
Expand Down Expand Up @@ -163,7 +169,7 @@ func (l *lsblkDeviceManager) lsblk(ctx context.Context) ([]DeviceInfo, error) {
args := []string{
"--json",
"-o",
"PATH,NAME,SIZE,SUBSYSTEMS,FSTYPE,LABEL,ROTA",
"PATH,NAME,SIZE,SUBSYSTEMS,FSTYPE,LABEL,ROTA,UUID",
"--bytes",
"--exclude",
"1,2,11",
Expand All @@ -187,6 +193,9 @@ func (l *lsblkDeviceManager) lsblk(ctx context.Context) ([]DeviceInfo, error) {

for i := range devices.BlockDevices {
devices.BlockDevices[i].mgr = l
for j := range devices.BlockDevices[i].Children {
devices.BlockDevices[i].Children[j].mgr = l
}
}

return devices.BlockDevices, nil
Expand Down
84 changes: 52 additions & 32 deletions pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,53 @@ func (s *Module) poolType(pool filesystem.Pool, vm bool) (zos.DeviceType, error)
return typ, nil
}

func (s *Module) mountPool(device filesystem.DeviceInfo, vm bool) {
log.Debug().Str("path", device.Path).Msg("mounting device")

if device.IsPXEPartition() {
log.Info().Str("device", device.Path).Msg("device has 'ZOSPXE' label")
s.brokenDevices = append(s.brokenDevices, pkg.BrokenDevice{Path: device.Path, Err: fmt.Errorf("device is a PXE partition")})
return
}

pool, err := filesystem.NewBtrfsPool(device)
if err != nil {
log.Error().Err(err).Str("device", device.Path).Msg("failed to create pool on device")
s.brokenDevices = append(s.brokenDevices, pkg.BrokenDevice{Path: device.Path, Err: err})
return
}

_, err = pool.Mount()
if err != nil {
log.Error().Err(err).Str("device", device.Path).Msg("failed to mount pool")
s.brokenPools = append(s.brokenPools, pkg.BrokenPool{Label: pool.Name(), Err: err})
return
}

usage, err := pool.Usage()
if err != nil {
log.Error().Err(err).Str("pool", pool.Name()).Str("device", device.Path).Msg("failed to get usage of pool")
}

typ, err := s.poolType(pool, vm)
if err != nil {
log.Error().Str("device", device.Path).Err(err).Msg("failed to get device type")
s.brokenDevices = append(s.brokenDevices, pkg.BrokenDevice{Path: device.Path, Err: err})
return
}

switch typ {
case zos.SSDDevice:
s.totalSSD += usage.Size
s.ssds = append(s.ssds, pool)
case zos.HDDDevice:
s.totalHDD += usage.Size
s.hdds = append(s.hdds, pool)
default:
log.Error().Str("type", string(typ)).Str("device", device.Path).Msg("unknown device type")
}
}

/*
*
initialize, must be called at least onetime each boot.
Expand Down Expand Up @@ -226,41 +273,14 @@ func (s *Module) initialize(ctx context.Context) error {

for _, device := range devices {
log.Debug().Msgf("device: %+v", device)
pool, err := filesystem.NewBtrfsPool(device)
if err != nil {
log.Error().Err(err).Str("device", device.Path).Msg("failed to create pool on device")
s.brokenDevices = append(s.brokenDevices, pkg.BrokenDevice{Path: device.Path, Err: err})
continue
}

_, err = pool.Mount()
if err != nil {
log.Error().Err(err).Str("device", device.Path).Msg("failed to mount pool")
s.brokenPools = append(s.brokenPools, pkg.BrokenPool{Label: pool.Name(), Err: err})
continue
}
usage, err := pool.Usage()
if err != nil {
log.Error().Err(err).Str("pool", pool.Name()).Str("device", device.Path).Msg("failed to get usage of pool")
}

typ, err := s.poolType(pool, vm)
if err != nil {
log.Error().Str("device", device.Path).Err(err).Msg("failed to get device type")
s.brokenDevices = append(s.brokenDevices, pkg.BrokenDevice{Path: device.Path, Err: err})
if len(device.Children) != 0 {
for _, part := range device.Children {
s.mountPool(part, vm)
}
continue
}

switch typ {
case zos.SSDDevice:
s.totalSSD += usage.Size
s.ssds = append(s.ssds, pool)
case zos.HDDDevice:
s.totalHDD += usage.Size
s.hdds = append(s.hdds, pool)
default:
log.Error().Str("type", string(typ)).Str("device", device.Path).Msg("unknown device type")
}
s.mountPool(device, vm)
}

log.Info().
Expand Down

0 comments on commit 52bec59

Please sign in to comment.