
This patch takes all the HostUtil functionality currently found in mount*.go files and copies it into hostutil*.go files. Care was taken to preserve git history to the fullest extent. As part of doing this, some common functionality was moved into mount_helper files in preperation for HostUtils to stay in k/k and Mount to move out. THe tests for each relevant function were moved to test files to match the appropriate location.
134 lines
3.8 KiB
Go
134 lines
3.8 KiB
Go
// +build !windows
|
|
|
|
/*
|
|
Copyright 2019 The Kubernetes Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package mount
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"syscall"
|
|
|
|
utilio "k8s.io/utils/io"
|
|
)
|
|
|
|
const (
|
|
// At least number of fields per line in /proc/<pid>/mountinfo.
|
|
expectedAtLeastNumFieldsPerMountInfo = 10
|
|
// How many times to retry for a consistent read of /proc/mounts.
|
|
maxListTries = 3
|
|
)
|
|
|
|
// IsCorruptedMnt return true if err is about corrupted mount point
|
|
func IsCorruptedMnt(err error) bool {
|
|
if err == nil {
|
|
return false
|
|
}
|
|
var underlyingError error
|
|
switch pe := err.(type) {
|
|
case nil:
|
|
return false
|
|
case *os.PathError:
|
|
underlyingError = pe.Err
|
|
case *os.LinkError:
|
|
underlyingError = pe.Err
|
|
case *os.SyscallError:
|
|
underlyingError = pe.Err
|
|
}
|
|
|
|
return underlyingError == syscall.ENOTCONN || underlyingError == syscall.ESTALE || underlyingError == syscall.EIO || underlyingError == syscall.EACCES
|
|
}
|
|
|
|
// This represents a single line in /proc/<pid>/mountinfo.
|
|
type mountInfo struct {
|
|
// Unique ID for the mount (maybe reused after umount).
|
|
id int
|
|
// The ID of the parent mount (or of self for the root of this mount namespace's mount tree).
|
|
parentID int
|
|
// The value of `st_dev` for files on this filesystem.
|
|
majorMinor string
|
|
// The pathname of the directory in the filesystem which forms the root of this mount.
|
|
root string
|
|
// Mount source, filesystem-specific information. e.g. device, tmpfs name.
|
|
source string
|
|
// Mount point, the pathname of the mount point.
|
|
mountPoint string
|
|
// Optional fieds, zero or more fields of the form "tag[:value]".
|
|
optionalFields []string
|
|
// The filesystem type in the form "type[.subtype]".
|
|
fsType string
|
|
// Per-mount options.
|
|
mountOptions []string
|
|
// Per-superblock options.
|
|
superOptions []string
|
|
}
|
|
|
|
// parseMountInfo parses /proc/xxx/mountinfo.
|
|
func parseMountInfo(filename string) ([]mountInfo, error) {
|
|
content, err := utilio.ConsistentRead(filename, maxListTries)
|
|
if err != nil {
|
|
return []mountInfo{}, err
|
|
}
|
|
contentStr := string(content)
|
|
infos := []mountInfo{}
|
|
|
|
for _, line := range strings.Split(contentStr, "\n") {
|
|
if line == "" {
|
|
// the last split() item is empty string following the last \n
|
|
continue
|
|
}
|
|
// See `man proc` for authoritative description of format of the file.
|
|
fields := strings.Fields(line)
|
|
if len(fields) < expectedAtLeastNumFieldsPerMountInfo {
|
|
return nil, fmt.Errorf("wrong number of fields in (expected at least %d, got %d): %s", expectedAtLeastNumFieldsPerMountInfo, len(fields), line)
|
|
}
|
|
id, err := strconv.Atoi(fields[0])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
parentID, err := strconv.Atoi(fields[1])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
info := mountInfo{
|
|
id: id,
|
|
parentID: parentID,
|
|
majorMinor: fields[2],
|
|
root: fields[3],
|
|
mountPoint: fields[4],
|
|
mountOptions: strings.Split(fields[5], ","),
|
|
}
|
|
// All fields until "-" are "optional fields".
|
|
i := 6
|
|
for ; i < len(fields) && fields[i] != "-"; i++ {
|
|
info.optionalFields = append(info.optionalFields, fields[i])
|
|
}
|
|
// Parse the rest 3 fields.
|
|
i++
|
|
if len(fields)-i < 3 {
|
|
return nil, fmt.Errorf("expect 3 fields in %s, got %d", line, len(fields)-i)
|
|
}
|
|
info.fsType = fields[i]
|
|
info.source = fields[i+1]
|
|
info.superOptions = strings.Split(fields[i+2], ",")
|
|
infos = append(infos, info)
|
|
}
|
|
return infos, nil
|
|
}
|