@@ -86,12 +86,8 @@ const (
8686 DiskAttachedKey = "k8s.aliyun.com"
8787 // DiskAttachedValue attached value
8888 DiskAttachedValue = "true"
89- // VolumeDir volume dir
90- VolumeDir = "/host/etc/kubernetes/volumes/disk/"
9189 // RundSocketDir dir
9290 RundSocketDir = "/host/etc/kubernetes/volumes/rund/"
93- // VolumeDirRemove volume dir remove
94- VolumeDirRemove = "/host/etc/kubernetes/volumes/disk/remove"
9591 // InputOutputErr tag
9692 InputOutputErr = "input/output error"
9793 // CreateDiskARN ARN parameter of the CreateDisk interface
@@ -127,15 +123,6 @@ var (
127123 // BLOCKVOLUMEPREFIX block volume mount prefix
128124 BLOCKVOLUMEPREFIX = filepath .Join (utils .KubeletRootDir , "/plugins/kubernetes.io/csi/volumeDevices/publish" )
129125
130- // DiskXattrName xattr is applied on the block device file to indicate that it is managed by the CSI driver.
131- // Value is the disk ID.
132- // Linux only support trusted namespace xattr in tmpfs before kernel v6.6,
133- // but setting trusted xaattr requires CAP_SYS_ADMIN capability, we may use user namespace instead in unit tests.
134- DiskXattrName = "trusted.csi-managed-disk"
135-
136- // DiskXattrVirtioBlkName xattr is applied on the block device file to indicate that it is managed by the CSI driver in PVM ways.
137- DiskXattrVirtioBlkName = "trusted.virtio-blk"
138-
139126 // BDFTypeDevice defines the regexp of bdf number
140127 BDFTypeDevice = regexp .MustCompile (`^[0-9a-fA-F]{4}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}` )
141128 // DFBusTypeDevice defines the regexp of dfnumber
@@ -173,17 +160,9 @@ func parseVolumeCountEnv() (int, error) {
173160// NewNodeServer creates node server
174161func NewNodeServer (m metadata.MetadataProvider ) csi.NodeServer {
175162 // Create Directory
176- err := os .MkdirAll (VolumeDir , os .FileMode (0755 ))
177- if err != nil {
178- klog .Errorf ("Create Directory %s failed: %v" , VolumeDir , err )
179- }
180- err = os .MkdirAll (VolumeDirRemove , os .FileMode (0755 ))
163+ err := os .MkdirAll (RundSocketDir , os .FileMode (0755 ))
181164 if err != nil {
182- klog .Errorf ("Create Directory %s failed: %v" , VolumeDir , err )
183- }
184- err = os .MkdirAll (RundSocketDir , os .FileMode (0755 ))
185- if err != nil {
186- klog .Errorf ("Create Directory %s failed: %v" , VolumeDir , err )
165+ klog .Errorf ("Create Directory %s failed: %v" , RundSocketDir , err )
187166 }
188167
189168 if IsVFNode () {
@@ -215,6 +194,11 @@ func NewNodeServer(m metadata.MetadataProvider) csi.NodeServer {
215194 klog .Fatalf ("Failed to initialize pod cgroup: %v" , err )
216195 }
217196
197+ devMap , err := NewDevMap (utilsio.RealDevTmpFS {})
198+ if err != nil {
199+ klog .Fatalf ("failed to list devices: %v" , err )
200+ }
201+
218202 waiter , batcher := newBatcher (true )
219203 return & nodeServer {
220204 metadata : m ,
@@ -232,7 +216,8 @@ func NewNodeServer(m metadata.MetadataProvider) csi.NodeServer {
232216 attachThrottler : defaultThrottler (),
233217 detachThrottler : defaultThrottler (),
234218
235- dev : DefaultDeviceManager ,
219+ dev : DefaultDeviceManager ,
220+ devMap : devMap ,
236221 },
237222 locks : utils .NewVolumeLocks (),
238223 GenericNodeServer : common.GenericNodeServer {
@@ -278,6 +263,7 @@ func (ns *nodeServer) NodeGetCapabilities(ctx context.Context, req *csi.NodeGetC
278263
279264// csi disk driver: bind directory from global to pod.
280265func (ns * nodeServer ) NodePublishVolume (ctx context.Context , req * csi.NodePublishVolumeRequest ) (* csi.NodePublishVolumeResponse , error ) {
266+ logger := klog .FromContext (ctx )
281267 if ! ns .locks .TryAcquire (req .VolumeId ) {
282268 return nil , status .Errorf (codes .Aborted , "There is already an operation for %s" , req .VolumeId )
283269 }
@@ -388,7 +374,7 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
388374 }
389375
390376 // check device name available
391- expectName , err := GetVolumeDeviceName (req .VolumeId )
377+ expectName , err := ns . ad . GetVolumeDeviceName (logger , req .VolumeId )
392378 if err != nil {
393379 return nil , status .Errorf (codes .Internal , "NodePublishVolume: VolumeId: %s, get device name error: %s" , req .VolumeId , err .Error ())
394380 }
@@ -596,10 +582,6 @@ func (ns *nodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStageVol
596582 klog .Errorf ("NodeStageVolume: check device %s for volume %s with error: %s" , device , req .VolumeId , err .Error ())
597583 return nil , status .Error (defaultErrCode , err .Error ())
598584 }
599- if err := saveVolumeConfig (req .VolumeId , device ); err != nil {
600- klog .Errorf ("NodeStageVolume: saveVolumeConfig %s for volume %s with error: %s" , device , req .VolumeId , err .Error ())
601- return nil , status .Error (defaultErrCode , "NodeStageVolume: saveVolumeConfig for (" + req .VolumeId + device + ") error with: " + err .Error ())
602- }
603585 klog .Infof ("NodeStageVolume: Volume Successful Attached: %s, to Node: %s, Device: %s" , req .VolumeId , ns .NodeID , device )
604586
605587 err = ns .setupDisk (ctx , device , targetPath , req )
@@ -685,22 +667,23 @@ func (ns *nodeServer) setupDisk(ctx context.Context, device, targetPath string,
685667 return nil
686668}
687669
688- func addDiskXattr (diskID string ) (err error ) {
670+ func ( ns * nodeServer ) addDiskXattr (logger klog. Logger , diskID string ) (err error ) {
689671 defer func () {
690672 if errors .Is (err , os .ErrNotExist ) {
691673 klog .Infof ("addDiskXattr: disk %s not found, skip" , diskID )
692674 err = nil
693675 }
694676 }()
695- device , err := GetVolumeDeviceName (diskID )
677+ device , err := ns . ad . GetVolumeDeviceName (logger , diskID )
696678 if err != nil {
697679 return
698680 }
699- return unix . Setxattr (device , DiskXattrName , [] byte ( diskID ), 0 )
681+ return setDiskXattr (device , diskID )
700682}
701683
702684// target format: /var/lib/kubelet/plugins/kubernetes.io/csi/pv/pv-disk-1e7001e0-c54a-11e9-8f89-00163e0e78a0/globalmount
703685func (ns * nodeServer ) NodeUnstageVolume (ctx context.Context , req * csi.NodeUnstageVolumeRequest ) (* csi.NodeUnstageVolumeResponse , error ) {
686+ logger := klog .FromContext (ctx )
704687 klog .Infof ("NodeUnstageVolume:: Starting to Unmount volume, volumeId: %s, target: %v" , req .VolumeId , req .StagingTargetPath )
705688
706689 if ! ns .locks .TryAcquire (req .VolumeId ) {
@@ -788,7 +771,7 @@ func (ns *nodeServer) NodeUnstageVolume(ctx context.Context, req *csi.NodeUnstag
788771 }
789772 }
790773
791- err := addDiskXattr (req .VolumeId )
774+ err := ns . addDiskXattr (logger , req .VolumeId )
792775 if err != nil {
793776 klog .Errorf ("NodeUnstageVolume: addDiskXattr %s failed: %v" , req .VolumeId , err )
794777 }
@@ -806,8 +789,8 @@ func (ns *nodeServer) NodeUnstageVolume(ctx context.Context, req *csi.NodeUnstag
806789 klog .Errorf ("NodeUnstageVolume: VolumeId: %s, Detach failed with error %v" , req .VolumeId , err .Error ())
807790 return nil , err
808791 }
809- _ = removeVolumeConfig (req .VolumeId )
810792 }
793+ ns .ad .devMap .Delete (req .VolumeId )
811794
812795 return & csi.NodeUnstageVolumeResponse {}, nil
813796}
@@ -933,22 +916,23 @@ func (ns *nodeServer) NodeExpandVolume(ctx context.Context, req *csi.NodeExpandV
933916 return nil , status .Error (codes .InvalidArgument , err .Error ())
934917 }
935918
936- return localExpandVolume (ctx , req )
919+ return ns . localExpandVolume (ctx , req )
937920}
938921
939- func localExpandVolume (ctx context.Context , req * csi.NodeExpandVolumeRequest ) (* csi.NodeExpandVolumeResponse , error ) {
922+ func ( ns * nodeServer ) localExpandVolume (ctx context.Context , req * csi.NodeExpandVolumeRequest ) (* csi.NodeExpandVolumeResponse , error ) {
940923 requestBytes := req .GetCapacityRange ().GetRequiredBytes ()
941924 volumePath := req .GetVolumePath ()
942925 diskID := req .GetVolumeId ()
926+ logger := klog .FromContext (ctx )
943927
944- devicePath , err := GetVolumeDeviceName (diskID )
928+ devicePath , err := ns . ad . GetVolumeDeviceName (logger , diskID )
945929 if err != nil {
946930 if errors .Is (err , os .ErrNotExist ) {
947931 return nil , status .Errorf (codes .NotFound , "can't get devicePath for: %s" , diskID )
948932 }
949933 return nil , status .Errorf (codes .Internal , "NodeExpandVolume: VolumeId: %s, get device name error: %s" , req .VolumeId , err .Error ())
950934 }
951- logger := klog . FromContext ( ctx ) .WithValues ("device" , devicePath )
935+ logger = logger .WithValues ("device" , devicePath )
952936 ctx = klog .NewContext (ctx , logger )
953937
954938 rootPath , index , err := DefaultDeviceManager .GetDeviceRootAndPartitionIndex (devicePath )
@@ -1246,14 +1230,9 @@ func (ns *nodeServer) mountRunvVolumes(volumeId, sourcePath, targetPath, fsType,
12461230 klog .Errorf ("NodePublishVolume(runv): unmountStageTarget %s with error: %s" , sourcePath , err .Error ())
12471231 return status .Error (codes .InvalidArgument , "NodePublishVolume: unmountStageTarget " + sourcePath + " with error: " + err .Error ())
12481232 }
1249- deviceName , err := DefaultDeviceManager . GetDeviceByVolumeID ( volumeId )
1233+ deviceName , err := ns . ad . GetRootBlockDevice ( klog . TODO (), volumeId )
12501234 if err != nil {
1251- klog .Errorf ("NodePublishVolume(runv): failed to get device by deviceName: %s" , err .Error ())
1252- deviceName = getVolumeConfig (volumeId )
1253- }
1254- if deviceName == "" {
1255- klog .Errorf ("NodePublishVolume(runv): cannot get local deviceName for volume: %s" , volumeId )
1256- return status .Error (codes .InvalidArgument , "NodePublishVolume: cannot get local deviceName for volume: " + volumeId )
1235+ return status .Errorf (codes .InvalidArgument , "NodePublishVolume: cannot get local deviceName for volume %s: %v" , volumeId , err )
12571236 }
12581237
12591238 // save volume info to local file
@@ -1289,9 +1268,11 @@ func (ns *nodeServer) mountRunvVolumes(volumeId, sourcePath, targetPath, fsType,
12891268
12901269func (ns * nodeServer ) mountRunDVolumes (volumeId , pvName , sourcePath , targetPath , fsType , mkfsOptions string , isRawBlock , pvmMode bool , mountFlags []string ) (bool , error ) {
12911270 klog .Infof ("NodePublishVolume:: Disk Volume %s Mounted in RunD csi 3.0/2.0 protocol" , volumeId )
1292- deviceName , err := DefaultDeviceManager .GetDeviceByVolumeID (volumeId )
1271+ logger := klog .TODO ()
1272+ deviceName , err := ns .ad .GetRootBlockDevice (logger , volumeId )
12931273 if err != nil {
1294- deviceName = getVolumeConfig (volumeId )
1274+ logger .V (1 ).Info ("RunD volume device not found" , "err" , err )
1275+ // maybe OK, we can find the device by xdragon-bdf below.
12951276 }
12961277
12971278 if features .FunctionalMutableFeatureGate .Enabled (features .RundCSIProtocol3 ) {
@@ -1476,7 +1457,7 @@ func (ns *nodeServer) checkMountedOfRunvAndRund(volumeId, targetPath string) boo
14761457 }
14771458 }
14781459
1479- device , err := GetVolumeDeviceName ( volumeId )
1460+ device , err := ns . ad . GetRootBlockDevice ( klog . TODO (), volumeId )
14801461 if err != nil {
14811462 // In VFIO mode, an empty device is an expected condition, so the resulting error should be ignored.
14821463 klog .Warningf ("NodeStageVolume: GetVolumeDeviceName failed: %s" , err .Error ())
0 commit comments