@@ -36,7 +36,7 @@ func (r *rbdProvider) VolumeType() api.VolumeType {
3636
3737func (r * rbdProvider ) Mount (ctx context.Context , machineID string , volume * validatedVolume ) (string , error ) {
3838 volumeDir := r .volumeDir (machineID , volume .name )
39- if err := os .MkdirAll (volumeDir , os . ModePerm ); err != nil {
39+ if err := os .MkdirAll (volumeDir , 0o700 ); err != nil {
4040 return "" , err
4141 }
4242
@@ -55,7 +55,6 @@ func (r *rbdProvider) Mount(ctx context.Context, machineID string, volume *valid
5555
5656 if devicePath != "" {
5757 log .V (1 ).Info ("RBD image already mapped" , "device" , devicePath )
58- r .persistDevicePath (volumeDir , devicePath )
5958 return devicePath , nil
6059 }
6160
@@ -65,39 +64,27 @@ func (r *rbdProvider) Mount(ctx context.Context, machineID string, volume *valid
6564 }
6665
6766 log .V (1 ).Info ("Mapped RBD image to device" , "device" , devicePath )
68- r .persistDevicePath (volumeDir , devicePath )
6967 return devicePath , nil
7068}
7169
72- func (r * rbdProvider ) Unmount (ctx context.Context , machineID string , volumeName string ) error {
73- log := r .log .WithValues ("machineID" , machineID , "volumeName" , volumeName )
70+ func (r * rbdProvider ) Unmount (ctx context.Context , machineID string , volume * validatedVolume ) error {
71+ log := r .log .WithValues ("machineID" , machineID , "volumeName" , volume . name )
7472
75- volumeDir := r .paths .MachineVolumeDir (machineID , cephDriverName , volumeName )
76- if devicePath , err := r .readDevicePath (volumeDir ); err == nil && devicePath != "" {
77- log .V (1 ).Info ("Unmapping RBD device from persisted path" , "device" , devicePath )
78- if err := r .unmapDevice (ctx , devicePath ); err != nil {
79- return fmt .Errorf ("error unmapping rbd device %s: %w" , devicePath , err )
80- }
81- _ = os .Remove (filepath .Join (volumeDir , "device" ))
82- return nil
73+ device , err := r .findMappedDevice (volume .pool , volume .image )
74+ if err != nil {
75+ return fmt .Errorf ("error querying rbd mappings: %w" , err )
8376 }
8477
85- log .V (1 ).Info ("No persisted device path found, nothing to unmap" )
86- return nil
87- }
88-
89- const deviceFileName = "device"
90-
91- func (r * rbdProvider ) persistDevicePath (volumeDir , devicePath string ) {
92- _ = os .WriteFile (filepath .Join (volumeDir , deviceFileName ), []byte (devicePath ), 0o644 )
93- }
78+ if device == "" {
79+ log .V (1 ).Info ("RBD image not currently mapped, nothing to unmap" , "pool" , volume .pool , "image" , volume .image )
80+ return nil
81+ }
9482
95- func (r * rbdProvider ) readDevicePath (volumeDir string ) (string , error ) {
96- data , err := os .ReadFile (filepath .Join (volumeDir , deviceFileName ))
97- if err != nil {
98- return "" , err
83+ log .V (1 ).Info ("Unmapping RBD device" , "device" , device )
84+ if err := r .unmapDevice (ctx , device ); err != nil {
85+ return fmt .Errorf ("error unmapping rbd device %s: %w" , device , err )
9986 }
100- return strings . TrimSpace ( string ( data )), nil
87+ return nil
10188}
10289
10390func (r * rbdProvider ) volumeDir (machineID string , volumeHandle string ) string {
@@ -121,15 +108,21 @@ func (r *rbdProvider) createCephConf(log logr.Logger, machineID string, volume *
121108 volume .userID ,
122109 keyPath ,
123110 )
124- if err := os .WriteFile (confPath , []byte (confData ), os . ModePerm ); err != nil {
111+ if err := os .WriteFile (confPath , []byte (confData ), 0o600 ); err != nil {
125112 return "" , fmt .Errorf ("error writing conf file %s: %w" , confPath , err )
126113 }
114+ if err := os .Chmod (confPath , 0o600 ); err != nil {
115+ return "" , fmt .Errorf ("error setting permissions on conf file %s: %w" , confPath , err )
116+ }
127117
128118 log .V (1 ).Info ("Creating ceph key" , "keyPath" , keyPath )
129119 keyData := fmt .Sprintf ("[client.%s]\n key = %s\n " , volume .userID , volume .userKey )
130- if err := os .WriteFile (keyPath , []byte (keyData ), os . ModePerm ); err != nil {
120+ if err := os .WriteFile (keyPath , []byte (keyData ), 0o600 ); err != nil {
131121 return "" , fmt .Errorf ("error writing key file %s: %w" , keyPath , err )
132122 }
123+ if err := os .Chmod (keyPath , 0o600 ); err != nil {
124+ return "" , fmt .Errorf ("error setting permissions on key file %s: %w" , keyPath , err )
125+ }
133126
134127 return confPath , nil
135128}
0 commit comments