@@ -77,11 +77,11 @@ func (o *osFs) ReadlinkIfPossible(name string) (string, error) {
7777}
7878
7979type Device interface {
80- EnsureDeviceMappedOnHost (ctx context.Context , name string , secrets map [string ]string ) (bool , error )
80+ EnsureDeviceMappedOnHost (ctx context.Context , name string , secrets map [string ]string ) (bool , bool , error )
8181 MappedDevicePath () string
8282 MappedDeviceName () string
8383 RawDevicePath () string
84- EnsureFormattedAndOpen (ctx context.Context , luksPassphrase string ) (bool , error )
84+ EnsureFormattedAndOpen (ctx context.Context , luksPassphrase string ) (bool , bool , error )
8585 CheckPassphrase (ctx context.Context , luksPassphrase string ) (bool , error )
8686 RotatePassphrase (ctx context.Context , volumeId , previousLUKSPassphrase , luksPassphrase string ) error
8787 IsMappingStale (ctx context.Context ) bool
@@ -123,48 +123,48 @@ func NewDeviceFromMappingPath(
123123}
124124
125125// EnsureDeviceMappedOnHost ensures the specified device is LUKS formatted, opened, and has the current passphrase.
126- func (d * LUKSDevice ) EnsureDeviceMappedOnHost (ctx context.Context , name string , secrets map [string ]string ) (bool , error ) {
126+ func (d * LUKSDevice ) EnsureDeviceMappedOnHost (ctx context.Context , name string , secrets map [string ]string ) (bool , bool , error ) {
127127 // Try to Open with current luks passphrase
128128 luksPassphraseName , luksPassphrase , previousLUKSPassphraseName , previousLUKSPassphrase := GetLUKSPassphrasesFromSecretMap (secrets )
129129 if luksPassphrase == "" {
130- return false , errors .New ("LUKS passphrase cannot be empty" )
130+ return false , false , errors .New ("LUKS passphrase cannot be empty" )
131131 }
132132 if luksPassphraseName == "" {
133- return false , errors .New ("LUKS passphrase name cannot be empty" )
133+ return false , false , errors .New ("LUKS passphrase name cannot be empty" )
134134 }
135135
136136 Logc (ctx ).WithFields (LogFields {
137137 "volume" : name ,
138138 "luks-passphrase-name" : luksPassphraseName ,
139139 }).Info ("Opening encrypted volume." )
140- luksFormatted , err := d .EnsureFormattedAndOpen (ctx , luksPassphrase )
140+ luksFormatted , safeToFormat , err := d .EnsureFormattedAndOpen (ctx , luksPassphrase )
141141
142142 // If we fail due to a format issue there is no need to try to open the device.
143143 if err == nil || errors .IsFormatError (err ) {
144- return luksFormatted , err
144+ return luksFormatted , safeToFormat , err
145145 }
146146
147147 // If we failed to open, try previous passphrase
148148 if previousLUKSPassphrase == "" {
149149 // Return original error if there is no previous passphrase to use
150- return luksFormatted , fmt .Errorf ("could not open LUKS device; %v" , err )
150+ return luksFormatted , safeToFormat , fmt .Errorf ("could not open LUKS device; %v" , err )
151151 }
152152 if luksPassphrase == previousLUKSPassphrase {
153- return luksFormatted , errors .New ("could not open LUKS device, previous passphrase matches current" )
153+ return luksFormatted , safeToFormat , errors .New ("could not open LUKS device, previous passphrase matches current" )
154154 }
155155 if previousLUKSPassphraseName == "" {
156- return luksFormatted , errors .New ("could not open LUKS device, no previous passphrase name provided" )
156+ return luksFormatted , safeToFormat , errors .New ("could not open LUKS device, no previous passphrase name provided" )
157157 }
158158 Logc (ctx ).WithFields (LogFields {
159159 "volume" : name ,
160160 "luks-passphrase-name" : previousLUKSPassphraseName ,
161161 }).Info ("Opening encrypted volume." )
162- luksFormatted , err = d .EnsureFormattedAndOpen (ctx , previousLUKSPassphrase )
162+ luksFormatted , safeToFormat , err = d .EnsureFormattedAndOpen (ctx , previousLUKSPassphrase )
163163 if err != nil {
164- return luksFormatted , fmt .Errorf ("could not open LUKS device; %v" , err )
164+ return luksFormatted , safeToFormat , fmt .Errorf ("could not open LUKS device; %v" , err )
165165 }
166166
167- return luksFormatted , nil
167+ return luksFormatted , safeToFormat , nil
168168}
169169
170170// MappedDevicePath returns the location of the LUKS device when opened.
@@ -183,7 +183,11 @@ func (d *LUKSDevice) RawDevicePath() string {
183183}
184184
185185// EnsureFormattedAndOpen ensures the specified device is LUKS formatted and opened.
186- func (d * LUKSDevice ) EnsureFormattedAndOpen (ctx context.Context , luksPassphrase string ) (formatted bool , err error ) {
186+ // Returns two booleans: the first indicates if the device is crypt formatted,
187+ // the second indicates if the device is safe to file format, meaning the device was empty before being crypt formatted.
188+ func (d * LUKSDevice ) EnsureFormattedAndOpen (ctx context.Context , luksPassphrase string ) (
189+ formatted , safeToFileFormat bool , err error ,
190+ ) {
187191 return d .ensureLUKSDevice (ctx , luksPassphrase )
188192}
189193
@@ -199,36 +203,41 @@ func (d *LUKSDevice) IsMappingStale(ctx context.Context) bool {
199203 return d .isMappingStale (ctx )
200204}
201205
202- func (d * LUKSDevice ) ensureLUKSDevice (ctx context.Context , luksPassphrase string ) (bool , error ) {
206+ // ensureLUKSDevice ensures the device is LUKS formatted and opened.
207+ // Returns two booleans: the first indicates if the device is formatted,
208+ // the second indicates if the device is safe to file format, meaning the device was empty before being crypt formatted.
209+ func (d * LUKSDevice ) ensureLUKSDevice (ctx context.Context , luksPassphrase string ) (bool , bool , error ) {
203210 // First check if LUKS device is already opened. This is OK to check even if the device isn't LUKS formatted.
204211 if isOpen , err := d .IsOpen (ctx ); err != nil {
205212 // If the LUKS device isn't found, it means that we need to check if the device is LUKS formatted.
206213 // If it isn't, then we should format it and attempt to open it.
207214 // If any other error occurs, bail out.
208215 if ! errors .IsNotFoundError (err ) {
209216 Logc (ctx ).WithError (err ).Error ("Could not check if device is an open LUKS device." )
210- return false , err
217+ return false , false , err
211218 }
212219 } else if isOpen {
213220 Logc (ctx ).Debug ("Device is LUKS formatted and open." )
214- return true , nil
221+ return true , false , nil
215222 }
216223
217- if err := d .formatUnformattedDevice (ctx , luksPassphrase ); err != nil {
224+ var safeToFileFormat bool
225+ var err error
226+ if safeToFileFormat , err = d .formatUnformattedDevice (ctx , luksPassphrase ); err != nil {
218227 Logc (ctx ).WithError (err ).Error ("Could not LUKS format device." )
219- return false , fmt .Errorf ("could not LUKS format device; %w" , err )
228+ return false , safeToFileFormat , fmt .Errorf ("could not LUKS format device; %w" , err )
220229 }
221230
222231 // At this point, we should be able to open the device.
223232 if err := d .Open (ctx , luksPassphrase ); err != nil {
224233 // At this point, we couldn't open the LUKS device, but we do know
225234 // the device is LUKS formatted because LUKSFormat didn't fail.
226235 Logc (ctx ).WithError (err ).Error ("Could not open LUKS formatted device." )
227- return true , fmt .Errorf ("could not open LUKS device; %v" , err )
236+ return true , safeToFileFormat , fmt .Errorf ("could not open LUKS device; %v" , err )
228237 }
229238
230239 Logc (ctx ).Debug ("Device is LUKS formatted and open." )
231- return true , nil
240+ return true , safeToFileFormat , nil
232241}
233242
234243func GetLUKSPassphrasesFromSecretMap (secrets map [string ]string ) (string , string , string , string ) {
@@ -283,22 +292,23 @@ func IsLuksDevice(publishInfo *models.VolumePublishInfo) (bool, error) {
283292func EnsureCryptsetupFormattedAndMappedOnHost (
284293 ctx context.Context , name string , publishInfo * models.VolumePublishInfo , secrets map [string ]string , command execCmd.Command ,
285294 devices devices.Devices ,
286- ) (bool , error ) {
295+ ) (bool , bool , error ) {
287296 Logc (ctx ).Debug (">>>> EnsureCryptsetupFormattedAndMappedOnHost" )
288297 defer Logc (ctx ).Debug ("<<<< EnsureCryptsetupFormattedAndMappedOnHost" )
289298
290299 isLUKSDevice , err := IsLuksDevice (publishInfo )
291300 if err != nil {
292- return isLUKSDevice , err
301+ return isLUKSDevice , false , err
293302 }
294303
295304 var luksFormatted bool
305+ var safeToFsFormat bool
296306 if isLUKSDevice {
297307 luksDevice := NewDevice (publishInfo .DevicePath , name , command , devices )
298- luksFormatted , err = luksDevice .EnsureDeviceMappedOnHost (ctx , name , secrets )
308+ luksFormatted , safeToFsFormat , err = luksDevice .EnsureDeviceMappedOnHost (ctx , name , secrets )
299309 if err != nil {
300- return luksFormatted , err
310+ return luksFormatted , safeToFsFormat , err
301311 }
302312 }
303- return luksFormatted , nil
313+ return luksFormatted , safeToFsFormat , nil
304314}
0 commit comments