forked from openebs-archive/node-disk-manager
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mountutil.go
187 lines (168 loc) · 6.14 KB
/
mountutil.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
Copyright 2019 The OpenEBS 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 (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
)
// DiskMountUtil contains the mountfile path, devpath/mountpoint which can be used to
// detect partition of a mountpoint or mountpoint of a partition.
type DiskMountUtil struct {
filePath string
devPath string
mountPoint string
}
type getMountData func(string) (DeviceMountAttr, bool)
// NewMountUtil returns DiskMountUtil struct for given mounts file path and mount point
func NewMountUtil(filePath, devPath, mountPoint string) DiskMountUtil {
MountUtil := DiskMountUtil{
filePath: filePath,
devPath: devPath,
mountPoint: mountPoint,
}
return MountUtil
}
// GetDiskPath returns os disk devpath
func (m DiskMountUtil) GetDiskPath() (string, error) {
mountAttr, err := m.getDeviceMountAttr(m.getPartitionName)
if err != nil {
return "", err
}
devPath, err := getDiskDevPath(mountAttr.DevPath)
if err != nil {
return "", err
}
_, err = filepath.EvalSymlinks(devPath)
if err != nil {
return "", err
}
return devPath, err
}
// getDeviceMountAttr read mounts file and returns device mount attributes, which includes partition name,
// mountpoint and filesystem
func (m DiskMountUtil) getDeviceMountAttr(fn getMountData) (DeviceMountAttr, error) {
mountAttr := DeviceMountAttr{}
// Read file from filepath and get which partition is mounted on given mount point
file, err := os.Open(m.filePath)
if err != nil {
return mountAttr, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
if err := scanner.Err(); err != nil {
return mountAttr, err
}
for scanner.Scan() {
line := scanner.Text()
/*
read each line of given file in below format -
/dev/sda4 / ext4 rw,relatime,errors=remount-ro,data=ordered 0 0
/dev/sda4 /var/lib/docker/aufs ext4 rw,relatime,errors=remount-ro,data=ordered 0 0
*/
// we are interested only in lines that start with /dev
if !strings.HasPrefix(line, "/dev") {
continue
}
if mountAttr, ok := fn(line); ok {
return mountAttr, nil
}
}
return mountAttr, fmt.Errorf("could not get device mount attributes, Path/MountPoint not present in mounts file")
}
// getDiskSysPath takes disk/partition name as input (sda, sda1, sdb, sdb2 ...) and
// returns syspath of that disk from which we can generate ndm given uuid of that disk.
func getDiskDevPath(partition string) (string, error) {
// dev path be like /dev/sda4 we need to remove /dev/ from this string to get sys block path.
softlink := "/sys/class/block/" + partition
link, err := filepath.EvalSymlinks(softlink)
if err != nil {
return "", err
}
parentDisk, ok := getParentBlockDevice(link)
if !ok {
return "", fmt.Errorf("could not find parent device for %s", link)
}
return "/dev/" + parentDisk, nil
}
// getParentBlockDevice returns the parent blockdevice of a given blockdevice by parsing the syspath
//
// syspath looks like - /sys/devices/pci0000:00/0000:00:1f.2/ata1/host0/target0:0:0/0:0:0:0/block/sda/sda4
// parent disk is present after block then partition of that disk
//
// for blockdevices that belong to the nvme subsystem, the symlink has a different format,
// /sys/devices/pci0000:00/0000:00:0e.0/nvme/nvme0/nvme0n1/nvme0n1p1. So we search for the nvme subsystem
// instead of `block`. The blockdevice will be available after the NVMe instance, nvme/instance/namespace.
// The namespace will be the blockdevice.
func getParentBlockDevice(sysPath string) (string, bool) {
blockSubsystem := "block"
nvmeSubsystem := "nvme"
parts := strings.Split(sysPath, "/")
// checking for block subsystem, return the next part after subsystem only
// if the length is greater. This check is to avoid an index out of range panic.
for i, part := range parts {
if part == blockSubsystem &&
len(parts)-1 >= i+1 {
return parts[i+1], true
}
}
// checking for nvme subsystem, return the 2nd item in hierarchy, which will be the
// nvme namespace. Length checking is to avoid index out of range in case of malformed
// links (extremely rare case)
for i, part := range parts {
if part == nvmeSubsystem &&
len(parts)-1 >= i+2 {
return parts[i+2], true
}
}
return "", false
}
// getPartitionName gets the partition name from the mountpoint. Each line of a mounts file
// is passed to the function, which is parsed and partition name is obtained
// A mountLine contains data in the order:
// device mountpoint filesystem mountoptions
// eg: /dev/sda4 / ext4 rw,relatime,errors=remount-ro,data=ordered 0 0
func (m *DiskMountUtil) getPartitionName(mountLine string) (DeviceMountAttr, bool) {
mountAttr := DeviceMountAttr{}
isValid := false
if len(mountLine) == 0 {
return mountAttr, isValid
}
// mountoptions are ignored. device-path and mountpoint is used
if parts := strings.Split(mountLine, " "); parts[1] == m.mountPoint {
mountAttr.DevPath = strings.Replace(parts[0], "/dev/", "", 1)
isValid = true
}
return mountAttr, isValid
}
// getMountName gets the mountpoint, filesystem etc from the partition name. Each line of a mounts
// file is passed to the function, which is parsed to get the information
// A mountLine contains data in the order:
// device mountpoint filesystem mountoptions
// eg: /dev/sda4 / ext4 rw,relatime,errors=remount-ro,data=ordered 0 0
func (m *DiskMountUtil) getMountName(mountLine string) (DeviceMountAttr, bool) {
mountAttr := DeviceMountAttr{}
isValid := false
if len(mountLine) == 0 {
return mountAttr, isValid
}
// mountoptions are ignored. devicepath, mountpoint and filesystem is used
if parts := strings.Split(mountLine, " "); parts[0] == m.devPath {
mountAttr.MountPoint = parts[1]
mountAttr.FileSystem = parts[2]
isValid = true
}
return mountAttr, isValid
}