Skip to content

Commit

Permalink
Fix snapshot recover disks mount: related: kubernetes-sigs#566
Browse files Browse the repository at this point in the history
  • Loading branch information
mowangdk committed Dec 3, 2021
1 parent f7a31b0 commit 088bae2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
35 changes: 28 additions & 7 deletions pkg/disk/nodeserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ const (
SocketPath = "/host/etc/csi-tool/diskconnector.sock"
// MaxVolumesPerNode define max ebs one node
MaxVolumesPerNode = 15
// NOUUID is xfs fs mount opts
NOUUID = "nouuid"
)

var (
Expand Down Expand Up @@ -597,6 +599,7 @@ func (ns *nodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStageVol
if mnt.FsType != "" {
fsType = mnt.FsType
}
mountOptions := collectMountOptions(fsType, options)
if err := ns.mounter.EnsureFolder(targetPath); err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
Expand All @@ -610,18 +613,18 @@ func (ns *nodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStageVol
// do format-mount or mount
diskMounter := &k8smount.SafeFormatAndMount{Interface: ns.k8smounter, Exec: utilexec.New()}
if len(mkfsOptions) > 0 && (fsType == "ext4" || fsType == "ext3") {
if err := formatAndMount(diskMounter, device, targetPath, fsType, mkfsOptions, options); err != nil {
log.Errorf("Mountdevice: FormatAndMount fail with mkfsOptions %s, %s, %s, %s, %s with error: %s", device, targetPath, fsType, mkfsOptions, options, err.Error())
if err := formatAndMount(diskMounter, device, targetPath, fsType, mkfsOptions, mountOptions); err != nil {
log.Errorf("Mountdevice: FormatAndMount fail with mkfsOptions %s, %s, %s, %s, %s with error: %s", device, targetPath, fsType, mkfsOptions, mountOptions, err.Error())
return nil, status.Error(codes.Internal, err.Error())
}
} else {
if err := diskMounter.FormatAndMount(device, targetPath, fsType, options); err != nil {
if err := diskMounter.FormatAndMount(device, targetPath, fsType, mountOptions); err != nil {
log.Errorf("NodeStageVolume: Volume: %s, Device: %s, FormatAndMount error: %s", req.VolumeId, device, err.Error())
return nil, status.Error(codes.Internal, err.Error())
}
}

log.Infof("NodeStageVolume: Mount Successful: volumeId: %s target %v, device: %s, mkfsOptions: %v, options: %v", req.VolumeId, targetPath, device, mkfsOptions, options)
log.Infof("NodeStageVolume: Mount Successful: volumeId: %s target %v, device: %s, mkfsOptions: %v, options: %v", req.VolumeId, targetPath, device, mkfsOptions, mountOptions)
return &csi.NodeStageVolumeResponse{}, nil
}

Expand Down Expand Up @@ -864,6 +867,7 @@ func (ns *nodeServer) mountDeviceToGlobal(capability *csi.VolumeCapability, volu
if mnt.FsType != "" {
fsType = mnt.FsType
}
mountOptions := collectMountOptions(fsType, options)
if err := ns.mounter.EnsureFolder(sourcePath); err != nil {
return status.Error(codes.Internal, err.Error())
}
Expand All @@ -877,12 +881,12 @@ func (ns *nodeServer) mountDeviceToGlobal(capability *csi.VolumeCapability, volu
// do format-mount or mount
diskMounter := &k8smount.SafeFormatAndMount{Interface: ns.k8smounter, Exec: utilexec.New()}
if len(mkfsOptions) > 0 && (fsType == "ext4" || fsType == "ext3") {
if err := formatAndMount(diskMounter, device, sourcePath, fsType, mkfsOptions, options); err != nil {
log.Errorf("mountDeviceToGlobal: FormatAndMount fail with mkfsOptions %s, %s, %s, %s, %s with error: %s", device, sourcePath, fsType, mkfsOptions, options, err.Error())
if err := formatAndMount(diskMounter, device, sourcePath, fsType, mkfsOptions, mountOptions); err != nil {
log.Errorf("mountDeviceToGlobal: FormatAndMount fail with mkfsOptions %s, %s, %s, %s, %s with error: %s", device, sourcePath, fsType, mkfsOptions, mountOptions, err.Error())
return status.Error(codes.Internal, err.Error())
}
} else {
if err := diskMounter.FormatAndMount(device, sourcePath, fsType, options); err != nil {
if err := diskMounter.FormatAndMount(device, sourcePath, fsType, mountOptions); err != nil {
log.Errorf("mountDeviceToGlobal: Device: %s, FormatAndMount error: %s", device, err.Error())
return status.Error(codes.Internal, err.Error())
}
Expand Down Expand Up @@ -919,3 +923,20 @@ func (ns *nodeServer) unmountDuplicateMountPoint(targetPath string) error {
}
return nil
}

// collectMountOptions returns array of mount options
func collectMountOptions(fsType string, mntFlags []string) (options []string) {
for _, opt := range mntFlags {
if !hasMountOption(options, opt) {
options = append(options, opt)
}
}

if fsType == "xfs" {
if !hasMountOption(options, NOUUID) {
options = append(options, NOUUID)
}
}
return

}
14 changes: 12 additions & 2 deletions pkg/disk/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -996,8 +996,8 @@ func getDiskVolumeOptions(req *csi.CreateVolumeRequest) (*diskVolumeArgs, error)
diskVolArgs.FsType = "ext4"
}
}
if diskVolArgs.FsType != "ext4" && diskVolArgs.FsType != "ext3" {
return nil, fmt.Errorf("illegal required parameter fsType, only support ext3, ext4, the input is: %s", diskVolArgs.FsType)
if diskVolArgs.FsType != "ext4" && diskVolArgs.FsType != "ext3" && diskVolArgs.FsType != "xfs" {
return nil, fmt.Errorf("illegal required parameter fsType, only support ext3, ext4 and xfs, the input is: %s", diskVolArgs.FsType)
}

// disk Type
Expand Down Expand Up @@ -1514,3 +1514,13 @@ func getVolumeCount() int64 {
}
return volumeCount
}

// hasMountOption return boolean value indicating whether the slice contains a mount option
func hasMountOption(options []string, opt string) bool {
for _, o := range options {
if o == opt {
return true
}
}
return false
}

0 comments on commit 088bae2

Please sign in to comment.