@@ -2,11 +2,13 @@ package storage
22
33import (
44 "fmt"
5+ "slices"
56
67 "github.com/pkg/errors"
78 "github.com/rs/zerolog/log"
89 "github.com/threefoldtech/zos/pkg"
910 "github.com/threefoldtech/zos/pkg/gridtypes"
11+ "github.com/threefoldtech/zos/pkg/storage/filesystem"
1012)
1113
1214const (
@@ -17,7 +19,7 @@ const (
1719func (m * Module ) Devices () ([]pkg.Device , error ) {
1820 var devices []pkg.Device
1921 log .Debug ().Int ("disks" , len (m .hdds )).Msg ("listing zdb devices" )
20- for _ , hdd := range m .hdds {
22+ for _ , hdd := range m .pools ( PolicyHDDOnly ) {
2123 log .Debug ().Str ("device" , hdd .Path ()).Msg ("checking device" )
2224 if _ , err := hdd .Mounted (); err != nil {
2325 log .Debug ().Str ("device" , hdd .Path ()).Msg ("not mounted" )
@@ -29,10 +31,12 @@ func (m *Module) Devices() ([]pkg.Device, error) {
2931 log .Error ().Err (err ).Str ("pool" , hdd .Name ()).Msg ("failed to get pool volumes" )
3032 continue
3133 }
34+
3235 usage , err := hdd .Usage ()
3336 if err != nil {
3437 return nil , err
3538 }
39+
3640 for _ , vol := range volumes {
3741 log .Debug ().Str ("volume" , vol .Path ()).Str ("name" , vol .Name ()).Msg ("checking volume" )
3842 if vol .Name () != zdbVolume {
@@ -56,7 +60,7 @@ func (m *Module) Devices() ([]pkg.Device, error) {
5660
5761// DeviceLookup looks up device by name
5862func (m * Module ) DeviceLookup (name string ) (pkg.Device , error ) {
59- for _ , hdd := range m .hdds {
63+ for _ , hdd := range m .pools ( PolicyHDDOnly ) {
6064 if hdd .Name () != name {
6165 continue
6266 }
@@ -98,12 +102,7 @@ func (m *Module) DeviceLookup(name string) (pkg.Device, error) {
98102// DeviceAllocate allocates a new free device, allocation is done
99103// by creation a zdb subvolume
100104func (m * Module ) DeviceAllocate (min gridtypes.Unit ) (pkg.Device , error ) {
101- for _ , hdd := range m .hdds {
102- if _ , err := hdd .Mounted (); err == nil {
103- // mounted pool. skip
104- continue
105- }
106-
105+ for _ , hdd := range m .pools (PolicyHDDOnly ) {
107106 if hdd .Device ().Size < uint64 (min ) {
108107 continue
109108 }
@@ -119,20 +118,29 @@ func (m *Module) DeviceAllocate(min gridtypes.Unit) (pkg.Device, error) {
119118 continue
120119 }
121120
122- if len (volumes ) != 0 {
123- log .Info ().Str ("pool" , hdd .Name ()).Msg ("pool is already used" )
121+ exists := slices .ContainsFunc (volumes , func (vol filesystem.Volume ) bool {
122+ return vol .Name () == zdbVolume
123+ })
124+
125+ if exists {
126+ // a zdb volume already existws
124127 continue
125128 }
126129
127- volume , err := hdd .AddVolume ( zdbVolume )
130+ usage , err := hdd .Usage ( )
128131 if err != nil {
129- log .Error ().Err (err ).Msg ("failed to create zdb volume" )
132+ return pkg.Device {}, err
133+ }
134+
135+ if usage .Used + uint64 (min ) > usage .Size {
136+ // not enough space
130137 continue
131138 }
132139
133- usage , err := hdd .Usage ( )
140+ volume , err := hdd .AddVolume ( zdbVolume )
134141 if err != nil {
135- return pkg.Device {}, err
142+ log .Error ().Err (err ).Msg ("failed to create zdb volume" )
143+ continue
136144 }
137145
138146 return pkg.Device {
0 commit comments