@@ -30,6 +30,8 @@ import (
3030 "slices"
3131 "strconv"
3232 "strings"
33+ "sync"
34+ "sync/atomic"
3335 "time"
3436
3537 lxd "github.com/canonical/lxd/client"
@@ -82,16 +84,38 @@ const (
8284)
8385
8486var (
85- storagePoolDriver = "zfs"
87+ // storageDrivers lists the supported pool drivers, most preferred first.
88+ storageDrivers = []string {"zfs" , "btrfs" }
89+
90+ storagePoolDriver atomic.Pointer [string ]
91+
92+ // backendReadyMu serializes ensureBackendReady.
93+ backendReadyMu sync.Mutex
8694
8795 workshopFormatsChecked = false
8896)
8997
90- func init () {
91- if osutil .IsWSL () {
92- storagePoolDriver = "btrfs"
98+ // poolDriver returns the storage pool driver, or "" if unknown.
99+ func poolDriver () string {
100+ if driver := storagePoolDriver .Load (); driver != nil {
101+ return * driver
102+ }
103+ return ""
104+ }
105+
106+ // adoptPoolDriver records driver as the storage pool driver if supported.
107+ func adoptPoolDriver (driver string ) error {
108+ if ! slices .Contains (storageDrivers , driver ) {
109+ return fmt .Errorf ("cannot use storage pool %q: unsupported driver %q (expected one of %s)" , storagePool , driver , strings .Join (storageDrivers , ", " ))
93110 }
111+ if old := poolDriver (); old != "" && old != driver {
112+ logger .Noticef ("Storage pool %q changed driver from %q to %q" , storagePool , old , driver )
113+ }
114+ storagePoolDriver .Store (& driver )
115+ return nil
116+ }
94117
118+ func init () {
95119 // Order matters: capabilities (version and storage) must be validated
96120 // before ensureBackendReady attempts to create the storage pool and
97121 // network. Registering it as a check also lets the daemon recover after
@@ -102,8 +126,7 @@ func init() {
102126 syscheck .RegisterCheck (checkStorageSpace )
103127}
104128
105- type Backend struct {
106- }
129+ type Backend struct {}
107130
108131var _ workshop.Backend = (* Backend )(nil )
109132
@@ -174,19 +197,35 @@ func checkVersion(version string) error {
174197 return nil
175198}
176199
177- func checkStorageDriver (drivers []api.ServerStorageDriverInfo ) error {
178- hasDriver := func (driver api.ServerStorageDriverInfo ) bool {
179- return driver .Name == storagePoolDriver
200+ // preferredDriver returns the first supported storage driver in order of
201+ // preference (ZFS, then btrfs), or an error if none is supported.
202+ func preferredDriver (supported []api.ServerStorageDriverInfo ) (string , error ) {
203+ for _ , driver := range storageDrivers {
204+ if slices .ContainsFunc (supported , func (d api.ServerStorageDriverInfo ) bool {
205+ return d .Name == driver
206+ }) {
207+ return driver , nil
208+ }
180209 }
181- if slices .ContainsFunc (drivers , hasDriver ) {
210+ return "" , fmt .Errorf ("suitable storage backend not found: none of %s is available" , strings .Join (storageDrivers , ", " ))
211+ }
212+
213+ // checkStoragePool checks that the storage pool is usable or can be created.
214+ func checkStoragePool (conn lxd.InstanceServer , supported []api.ServerStorageDriverInfo ) error {
215+ if pool , _ , err := conn .GetStoragePool (storagePool ); err == nil {
216+ // LXD still returns a pool it failed to initialize (e.g. its driver's
217+ // kernel module is gone), and retries until it recovers.
218+ if pool .Status == api .StoragePoolStatusUnvailable {
219+ return fmt .Errorf ("%q storage pool unavailable: LXD cannot initialize it with the %q driver" , storagePool , pool .Driver )
220+ }
182221 return nil
222+ } else if ! api .StatusErrorCheck (err , http .StatusNotFound ) {
223+ return fmt .Errorf ("%q storage pool unavailable: %w" , storagePool , err )
183224 }
184225
185- // The LXD error message when creating a pool is:
186- // Error: Error loading "zfs" module: Failed to run: modprobe -b zfs:
187- // exit status 1 (modprobe: FATAL: Module zfs not found ...)
188- // We keep the first part for consistency, the rest doesn't add much.
189- return fmt .Errorf (`suitable storage backend not found: error loading %q module` , storagePoolDriver )
226+ // The pool is missing; make sure it can be created.
227+ _ , err := preferredDriver (supported )
228+ return err
190229}
191230
192231// checkStorageSpace puts the daemon into degraded mode when the workshop
@@ -250,7 +289,7 @@ func checkServerCapabilities() error {
250289
251290 workshop .WorkshopVMsSupportSDKs .Store (checkVMsSupportSDKs (conn ))
252291
253- return checkStorageDriver ( info .Environment .StorageSupportedDrivers )
292+ return checkStoragePool ( conn , info .Environment .StorageSupportedDrivers )
254293}
255294
256295// vmDiskShiftSupported checks whether LXD supports mounting shifted SDK
@@ -315,22 +354,33 @@ func New() (*Backend, error) {
315354// ensureBackendReady creates the LXD storage pool and network the backend
316355// relies on, it is idempotent.
317356func ensureBackendReady () error {
357+ backendReadyMu .Lock ()
358+ defer backendReadyMu .Unlock ()
359+
318360 // TODO: consider separating the storage pool or network for different users.
319361 conn , err := lxd .ConnectLXDUnix ("" , nil )
320362 if err != nil {
321363 return ErrorLxdBackend (err )
322364 }
323365 defer conn .Disconnect ()
324366
325- // Create LXD storage pool if it doesn't exist.
326- pools , err := conn .GetStoragePools ()
327- if err != nil {
328- return err
329- }
330- if idx := slices .IndexFunc (pools , func (p api.StoragePool ) bool { return p .Name == storagePool }); idx < 0 {
367+ // Create the LXD storage pool if it doesn't exist, and record its driver so
368+ // pool operations can reuse it.
369+ existingPool , _ , err := conn .GetStoragePool (storagePool )
370+ if api .StatusErrorCheck (err , http .StatusNotFound ) {
371+ // Pick from the drivers LXD supports now, as they may have changed
372+ // since the pool was last created.
373+ info , _ , err := conn .GetServer ()
374+ if err != nil {
375+ return err
376+ }
377+ driver , err := preferredDriver (info .Environment .StorageSupportedDrivers )
378+ if err != nil {
379+ return err
380+ }
331381 req := api.StoragePoolsPost {
332382 Name : storagePool ,
333- Driver : storagePoolDriver ,
383+ Driver : driver ,
334384 }
335385 op , err := conn .CreateStoragePool (req )
336386 if err != nil {
@@ -339,6 +389,9 @@ func ensureBackendReady() error {
339389 if err := op .Wait (); err != nil {
340390 return err
341391 }
392+ if err := adoptPoolDriver (driver ); err != nil {
393+ return err
394+ }
342395
343396 // Ensure the new pool has enough total space available.
344397 pool , etag , err := conn .GetStoragePool (storagePool )
@@ -369,8 +422,10 @@ func ensureBackendReady() error {
369422
370423 logger .Noticef ("On ensureBackendReady: set storage pool to the minimal size: %dGiB" , storagePoolMinimalGiB )
371424 }
372- } else if pools [idx ].Driver != storagePoolDriver {
373- return fmt .Errorf ("storage pool %q already exists with a different driver: %q (expected %q)" , storagePool , pools [idx ].Driver , storagePoolDriver )
425+ } else if err != nil {
426+ return err
427+ } else if err := adoptPoolDriver (existingPool .Driver ); err != nil {
428+ return err
374429 }
375430
376431 network , etag , err := conn .GetNetwork (networkName )
@@ -1014,7 +1069,7 @@ func (s *Backend) execCommand(conn lxd.InstanceServer, ctx context.Context, name
10141069 }
10151070
10161071 opmeta := op .Get ()
1017- var env = map [string ]string {}
1072+ env : = map [string ]string {}
10181073 for k , v := range opmeta .Metadata ["environment" ].(map [string ]any ) {
10191074 if value , ok := v .(string ); ok {
10201075 env [k ] = value
@@ -1038,7 +1093,7 @@ func (s *Backend) execCommand(conn lxd.InstanceServer, ctx context.Context, name
10381093 // unconditionally right after the operation has exited, so it will not be
10391094 // blocked if we are here
10401095 <- done
1041- var status = int (op .Get ().Metadata ["return" ].(float64 ))
1096+ status : = int (op .Get ().Metadata ["return" ].(float64 ))
10421097 if status != 0 {
10431098 return & workshop.ErrExec {Status : status }
10441099 }
0 commit comments