forked from rancher/os
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathutil_linux.go
More file actions
158 lines (141 loc) · 3.08 KB
/
util_linux.go
File metadata and controls
158 lines (141 loc) · 3.08 KB
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
//go:build linux
// +build linux
package util
import (
"bufio"
"bytes"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"github.com/SvenDowideit/cpuid"
"github.com/docker/docker/pkg/mount"
)
func mountProc() error {
if _, err := os.Stat("/proc/self/mountinfo"); os.IsNotExist(err) {
if _, err := os.Stat("/proc"); os.IsNotExist(err) {
if err = os.Mkdir("/proc", 0755); err != nil {
return err
}
}
if err := syscall.Mount("none", "/proc", "proc", 0, ""); err != nil {
return err
}
}
return nil
}
func Mount(device, target, fsType, options string) error {
if err := mountProc(); err != nil {
return nil
}
bindMount := false
for _, v := range strings.Split(options, ",") {
if v == "bind" {
bindMount = true
break
}
}
if bindMount {
deviceInfo, err := os.Stat(device)
if err != nil {
return err
}
mode := deviceInfo.Mode()
switch {
case mode.IsDir():
if err := os.MkdirAll(target, 0755); err != nil {
return err
}
case mode.IsRegular():
err := os.MkdirAll(filepath.Dir(target), 0755)
if err != nil {
return err
}
file, err := os.OpenFile(target, os.O_CREATE, mode&os.ModePerm)
if err != nil {
return err
}
if err := file.Close(); err != nil {
return err
}
default:
return os.ErrInvalid
}
} else {
err := os.MkdirAll(target, 0755)
if err != nil {
return err
}
if fsType == "auto" || fsType == "" {
inferredType, err := GetFsType(device)
if err != nil {
return err
}
fsType = inferredType
}
}
return mount.Mount(device, target, fsType, options)
}
func Unmount(target string) error {
return mount.Unmount(target)
}
func Blkid(label string) (deviceName, deviceType string, err error) {
// Not all blkid's have `blkid -L label (see busybox/alpine)
cmd := exec.Command("blkid")
cmd.Stderr = os.Stderr
out, err := cmd.Output()
if err != nil {
return
}
r := bytes.NewReader(out)
s := bufio.NewScanner(r)
for s.Scan() {
line := s.Text()
if !strings.Contains(line, `LABEL="`+label+`"`) {
continue
}
d := strings.Split(line, ":")
deviceName = d[0]
s1 := strings.Split(line, `TYPE="`)
s2 := strings.Split(s1[1], `"`)
deviceType = s2[0]
return
}
return
}
func BlkidType(deviceType string) (deviceNames []string, err error) {
// Not all blkid's have `blkid -L label (see busybox/alpine)
cmd := exec.Command("blkid")
cmd.Stderr = os.Stderr
out, err := cmd.Output()
if err != nil {
return nil, err
}
r := bytes.NewReader(out)
s := bufio.NewScanner(r)
for s.Scan() {
line := s.Text()
if !strings.Contains(line, `TYPE="`+deviceType+`"`) {
continue
}
d := strings.Split(line, ":")
deviceName := d[0]
deviceNames = append(deviceNames, deviceName)
}
return deviceNames, nil
}
// GetHypervisor tries to detect if we're running in a VM, and returns a string for its type
func GetHypervisor() string {
hv := cpuid.CPU.HypervisorName
if hv == "hyperv" {
data, err := os.ReadFile("/proc/sys/kernel/osrelease")
if err != nil {
return hv
}
if strings.Contains(string(data), "microsoft-standard-WSL2") {
hv = "wsl2"
}
}
return hv
}