@@ -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,30 @@ 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 _ , _ , err := conn .GetStoragePool (storagePool ); err == nil {
182216 return nil
217+ } else if ! api .StatusErrorCheck (err , http .StatusNotFound ) {
218+ return fmt .Errorf ("%q storage pool unavailable: %w" , storagePool , err )
183219 }
184220
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 )
221+ // The pool is missing; make sure it can be created.
222+ _ , err := preferredDriver (supported )
223+ return err
190224}
191225
192226// checkStorageSpace puts the daemon into degraded mode when the workshop
@@ -250,7 +284,7 @@ func checkServerCapabilities() error {
250284
251285 workshop .WorkshopVMsSupportSDKs .Store (checkVMsSupportSDKs (conn ))
252286
253- return checkStorageDriver ( info .Environment .StorageSupportedDrivers )
287+ return checkStoragePool ( conn , info .Environment .StorageSupportedDrivers )
254288}
255289
256290// vmDiskShiftSupported checks whether LXD supports mounting shifted SDK
@@ -315,22 +349,33 @@ func New() (*Backend, error) {
315349// ensureBackendReady creates the LXD storage pool and network the backend
316350// relies on, it is idempotent.
317351func ensureBackendReady () error {
352+ backendReadyMu .Lock ()
353+ defer backendReadyMu .Unlock ()
354+
318355 // TODO: consider separating the storage pool or network for different users.
319356 conn , err := lxd .ConnectLXDUnix ("" , nil )
320357 if err != nil {
321358 return ErrorLxdBackend (err )
322359 }
323360 defer conn .Disconnect ()
324361
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 {
362+ // Create the LXD storage pool if it doesn't exist, and record its driver so
363+ // pool operations can reuse it.
364+ existingPool , _ , err := conn .GetStoragePool (storagePool )
365+ if api .StatusErrorCheck (err , http .StatusNotFound ) {
366+ // Pick from the drivers LXD supports now, as they may have changed
367+ // since the pool was last created.
368+ info , _ , err := conn .GetServer ()
369+ if err != nil {
370+ return err
371+ }
372+ driver , err := preferredDriver (info .Environment .StorageSupportedDrivers )
373+ if err != nil {
374+ return err
375+ }
331376 req := api.StoragePoolsPost {
332377 Name : storagePool ,
333- Driver : storagePoolDriver ,
378+ Driver : driver ,
334379 }
335380 op , err := conn .CreateStoragePool (req )
336381 if err != nil {
@@ -339,6 +384,9 @@ func ensureBackendReady() error {
339384 if err := op .Wait (); err != nil {
340385 return err
341386 }
387+ if err := adoptPoolDriver (driver ); err != nil {
388+ return err
389+ }
342390
343391 // Ensure the new pool has enough total space available.
344392 pool , etag , err := conn .GetStoragePool (storagePool )
@@ -369,8 +417,10 @@ func ensureBackendReady() error {
369417
370418 logger .Noticef ("On ensureBackendReady: set storage pool to the minimal size: %dGiB" , storagePoolMinimalGiB )
371419 }
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 )
420+ } else if err != nil {
421+ return err
422+ } else if err := adoptPoolDriver (existingPool .Driver ); err != nil {
423+ return err
374424 }
375425
376426 network , etag , err := conn .GetNetwork (networkName )
@@ -1014,7 +1064,7 @@ func (s *Backend) execCommand(conn lxd.InstanceServer, ctx context.Context, name
10141064 }
10151065
10161066 opmeta := op .Get ()
1017- var env = map [string ]string {}
1067+ env : = map [string ]string {}
10181068 for k , v := range opmeta .Metadata ["environment" ].(map [string ]any ) {
10191069 if value , ok := v .(string ); ok {
10201070 env [k ] = value
@@ -1038,7 +1088,7 @@ func (s *Backend) execCommand(conn lxd.InstanceServer, ctx context.Context, name
10381088 // unconditionally right after the operation has exited, so it will not be
10391089 // blocked if we are here
10401090 <- done
1041- var status = int (op .Get ().Metadata ["return" ].(float64 ))
1091+ status : = int (op .Get ().Metadata ["return" ].(float64 ))
10421092 if status != 0 {
10431093 return & workshop.ErrExec {Status : status }
10441094 }
0 commit comments