@@ -159,6 +159,7 @@ type driveEventHandler struct {
159159 getDeviceByFSUUID func (fsuuid string ) (string , error )
160160 setQuota func (ctx context.Context , device , path , volumeName string , quota xfs.Quota , update bool ) (err error )
161161 rmdir func (fsuuid string ) error
162+ exists func (name string ) error
162163}
163164
164165func newDriveEventHandler (nodeID directpvtypes.NodeID ) * driveEventHandler {
@@ -188,6 +189,10 @@ func newDriveEventHandler(nodeID directpvtypes.NodeID) *driveEventHandler {
188189 }
189190 return nil
190191 },
192+ exists : func (name string ) (err error ) {
193+ _ , err = os .Lstat (name )
194+ return err
195+ },
191196 }
192197}
193198
@@ -345,10 +350,92 @@ func (handler *driveEventHandler) handleUpdate(ctx context.Context, drive *types
345350 return nil
346351}
347352
353+ func (handler * driveEventHandler ) checkDrive (ctx context.Context , drive * types.Drive ) error {
354+ switch drive .Status .Status {
355+ case directpvtypes .DriveStatusReady :
356+ if err := handler .exists (types .GetVolumeRootDir (drive .Status .FSUUID )); err != nil {
357+ if errors .Unwrap (err ) == syscall .EIO {
358+ if uerr := SetIOError (ctx , drive .GetDriveID ()); uerr != nil {
359+ klog .ErrorS (uerr , "unable to set I/O error" , "drive" , drive .GetDriveID ())
360+ }
361+ } else {
362+ klog .ErrorS (err , "unable to check volume root directory existent" , "drive" , drive .GetDriveID ())
363+ }
364+
365+ return nil
366+ }
367+
368+ if _ , err := handler .getDeviceByFSUUID (drive .Status .FSUUID ); err != nil {
369+ klog .ErrorS (
370+ err ,
371+ "unable to find device by FSUUID; " +
372+ "either device is removed or run command " +
373+ "`sudo udevadm control --reload-rules && sudo udevadm trigger`" +
374+ "on the host to reload" ,
375+ "FSUUID" , drive .Status .FSUUID )
376+ client .Eventf (
377+ drive , client .EventTypeWarning , client .EventReasonDeviceNotFoundError ,
378+ "unable to find device by FSUUID %v; " +
379+ "either device is removed or run command " +
380+ "`sudo udevadm control --reload-rules && sudo udevadm trigger`" +
381+ " on the host to reload" , drive .Status .FSUUID )
382+
383+ drive .Status .Status = directpvtypes .DriveStatusLost
384+ _ , err = client .DriveClient ().Update (ctx , drive , metav1.UpdateOptions {
385+ TypeMeta : types .NewDriveTypeMeta (),
386+ })
387+ if err != nil {
388+ klog .ErrorS (err , "unable to mark lost drive" , "drive" , drive .GetDriveID ())
389+ }
390+ }
391+ case directpvtypes .DriveStatusLost :
392+ device , err := handler .getDeviceByFSUUID (drive .Status .FSUUID )
393+ if err != nil {
394+ if ! errors .Is (err , os .ErrNotExist ) {
395+ klog .ErrorS (err , "unable to get device by FSUUID" , "FSUUID" , drive .Status .FSUUID , "drive" , drive .GetDriveID ())
396+ }
397+ return nil
398+ }
399+
400+ source := utils .AddDevPrefix (device )
401+ target := types .GetDriveMountDir (drive .Status .FSUUID )
402+ if err = xfs .Mount (source , target ); err != nil {
403+ drive .Status .Status = directpvtypes .DriveStatusError
404+ drive .SetMountErrorCondition (fmt .Sprintf ("unable to mount; %v" , err ))
405+ client .Eventf (drive , client .EventTypeWarning , client .EventReasonDriveMountError , "unable to mount the drive; %v" , err )
406+ klog .ErrorS (err , "unable to mount the drive" , "Source" , source , "Target" , target )
407+ } else {
408+ drive .Status .Status = directpvtypes .DriveStatusReady
409+ client .Eventf (
410+ drive ,
411+ client .EventTypeNormal ,
412+ client .EventReasonDriveMounted ,
413+ "Drive mounted successfully to %s" , target ,
414+ )
415+ }
416+ drive .SetDriveName (directpvtypes .DriveName (utils .TrimDevPrefix (device )))
417+ _ , err = client .DriveClient ().Update (ctx , drive , metav1.UpdateOptions {
418+ TypeMeta : types .NewDriveTypeMeta (),
419+ })
420+ if err != nil {
421+ klog .ErrorS (err , "unable to mark drive ready" , "drive" , drive .GetDriveID ())
422+ }
423+ }
424+
425+ return nil
426+ }
427+
348428func (handler * driveEventHandler ) Handle (ctx context.Context , eventType controller.EventType , object runtime.Object ) error {
429+ drive := object .(* types.Drive )
349430 switch eventType {
350- case controller .AddEvent , controller .UpdateEvent :
351- return handler .handleUpdate (ctx , object .(* types.Drive ))
431+ case controller .AddEvent :
432+ return handler .handleUpdate (ctx , drive )
433+ case controller .UpdateEvent :
434+ if err := handler .handleUpdate (ctx , drive ); err != nil {
435+ return err
436+ }
437+
438+ return handler .checkDrive (ctx , drive )
352439 }
353440
354441 return nil
0 commit comments