-
Notifications
You must be signed in to change notification settings - Fork 489
/
mount.go
259 lines (225 loc) · 7.19 KB
/
mount.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// +build linux
package mount
import (
"fmt"
"os"
"path"
"sync"
"github.com/autonomy/dianemo/src/initramfs/cmd/init/pkg/blkid"
"github.com/autonomy/dianemo/src/initramfs/cmd/init/pkg/constants"
"github.com/autonomy/dianemo/src/initramfs/cmd/init/pkg/kernel"
"golang.org/x/sys/unix"
)
var (
instance struct {
special map[string]*Point
blockdevices map[string]*Point
}
once sync.Once
special = map[string]*Point{
"dev": {"devtmpfs", "/dev", "devtmpfs", unix.MS_NOSUID, "mode=0755"},
"proc": {"proc", "/proc", "proc", unix.MS_NOSUID | unix.MS_NOEXEC | unix.MS_NODEV, ""},
"sys": {"sysfs", "/sys", "sysfs", unix.MS_NOSUID | unix.MS_NOEXEC | unix.MS_NODEV, ""},
"run": {"tmpfs", "/run", "tmpfs", 0, ""},
"tmp": {"tmpfs", "/tmp", "tmpfs", 0, ""},
}
)
// Point represents a linux mount point.
type Point struct {
source string
target string
fstype string
flags uintptr
data string
}
// BlockDevice represents the metadata on a block device probed by libblkid.
type BlockDevice struct {
dev string
TYPE string
UUID string
LABEL string
}
// Init initializes the mount points.
func Init(s string) (err error) {
once.Do(func() {
instance = struct {
special map[string]*Point
blockdevices map[string]*Point
}{
special,
map[string]*Point{},
}
})
if err = mountSpecialDevices(); err != nil {
return
}
if err = mountBlockDevices(s); err != nil {
return
}
return nil
}
// Move moves the mount points created in Init, to the new root.
func Move(s string) error {
if err := os.MkdirAll(s, os.ModeDir); err != nil {
return err
}
// Move the special mounts to the new root.
for label, mountpoint := range instance.special {
target := path.Join(s, mountpoint.target)
if err := unix.Mount(mountpoint.target, target, "", unix.MS_MOVE, ""); err != nil {
return fmt.Errorf("move mount point %s to %s: %s", mountpoint.target, target, err.Error())
}
if label == "dev" {
mountpoint = &Point{"devpts", path.Join(s, "/dev/pts"), "devpts", unix.MS_NOSUID | unix.MS_NOEXEC, "ptmxmode=000,mode=620,gid=5"}
if err := os.MkdirAll(mountpoint.target, os.ModeDir); err != nil {
return fmt.Errorf("create %s: %s", mountpoint.target, err.Error())
}
if err := unix.Mount(mountpoint.source, mountpoint.target, mountpoint.fstype, mountpoint.flags, mountpoint.data); err != nil {
return fmt.Errorf("mount %s: %s", mountpoint.target, err.Error())
}
}
}
return nil
}
// Finalize moves the mount points created in Init to the new root.
func Finalize(s string) error {
return unix.Mount(s, "/", "", unix.MS_MOVE, "")
}
// Mount moves the mount points created in Init to the new root.
func Mount(s string) error {
if err := os.MkdirAll(s, os.ModeDir); err != nil {
return err
}
mountpoint, ok := instance.blockdevices[constants.RootPartitionLabel]
if ok {
mountpoint.flags = unix.MS_RDONLY | unix.MS_NOATIME
if err := unix.Mount(mountpoint.source, mountpoint.target, mountpoint.fstype, mountpoint.flags, mountpoint.data); err != nil {
return fmt.Errorf("mount %s: %s", mountpoint.target, err.Error())
}
// MS_SHARED:
// Make this mount point shared. Mount and unmount events
// immediately under this mount point will propagate to the
// other mount points that are members of this mount's peer
// group. Propagation here means that the same mount or
// unmount will automatically occur under all of the other
// mount points in the peer group. Conversely, mount and
// unmount events that take place under peer mount points
// will propagate to this mount point.
// See http://man7.org/linux/man-pages/man2/mount.2.html
// https://github.com/kubernetes/kubernetes/issues/61058
if err := unix.Mount("", mountpoint.target, "", unix.MS_SHARED, ""); err != nil {
return fmt.Errorf("mount %s as shared: %s", mountpoint.target, err.Error())
}
}
mountpoint, ok = instance.blockdevices[constants.DataPartitionLabel]
if ok {
if err := unix.Mount(mountpoint.source, mountpoint.target, mountpoint.fstype, mountpoint.flags, mountpoint.data); err != nil {
return fmt.Errorf("mount %s: %s", mountpoint.target, err.Error())
}
}
return nil
}
// Unmount unmounts the ROOT and DATA block devices.
func Unmount() error {
mountpoint, ok := instance.blockdevices[constants.DataPartitionLabel]
if ok {
if err := unix.Unmount(mountpoint.target, 0); err != nil {
return fmt.Errorf("unmount mount point %s: %s", mountpoint.target, err.Error())
}
}
mountpoint, ok = instance.blockdevices[constants.RootPartitionLabel]
if ok {
if err := unix.Unmount(mountpoint.target, 0); err != nil {
return fmt.Errorf("unmount mount point %s: %s", mountpoint.target, err.Error())
}
}
return nil
}
func mountSpecialDevices() (err error) {
for _, mountpoint := range instance.special {
if err = os.MkdirAll(mountpoint.target, os.ModeDir); err != nil {
return fmt.Errorf("create %s: %s", mountpoint.target, err.Error())
}
if err = unix.Mount(mountpoint.source, mountpoint.target, mountpoint.fstype, mountpoint.flags, mountpoint.data); err != nil {
return fmt.Errorf("mount %s: %s", mountpoint.target, err.Error())
}
}
return nil
}
func mountBlockDevices(s string) (err error) {
probed, err := probe()
if err != nil {
return fmt.Errorf("probe block devices: %s", err.Error())
}
for _, b := range probed {
mountpoint := &Point{
source: b.dev,
fstype: b.TYPE,
flags: unix.MS_NOATIME,
data: "",
}
switch b.LABEL {
case constants.RootPartitionLabel:
mountpoint.target = s
case constants.DataPartitionLabel:
mountpoint.target = path.Join(s, "var")
default:
continue
}
if err = os.MkdirAll(mountpoint.target, os.ModeDir); err != nil {
return fmt.Errorf("create %s: %s", mountpoint.target, err.Error())
}
if err = unix.Mount(mountpoint.source, mountpoint.target, mountpoint.fstype, mountpoint.flags, mountpoint.data); err != nil {
return fmt.Errorf("mount %s: %s", mountpoint.target, err.Error())
}
instance.blockdevices[b.LABEL] = mountpoint
}
return nil
}
func probe() (b []*BlockDevice, err error) {
b = []*BlockDevice{}
arguments, err := kernel.ParseProcCmdline()
if err != nil {
return
}
if root, ok := arguments[constants.KernelParamRoot]; ok {
if _, err := os.Stat(root); os.IsNotExist(err) {
return nil, fmt.Errorf("device does not exist: %s", root)
}
pr, err := blkid.NewProbeFromFilename(root)
defer blkid.FreeProbe(pr)
if err != nil {
return nil, fmt.Errorf("failed to probe %s: %s", root, err)
}
ls := blkid.ProbeGetPartitions(pr)
nparts := blkid.ProbeGetPartitionsPartlistNumOfPartitions(ls)
for i := 0; i < nparts; i++ {
dev := fmt.Sprintf("%s%d", root, i+1)
pr, err = blkid.NewProbeFromFilename(dev)
defer blkid.FreeProbe(pr)
if err != nil {
return nil, fmt.Errorf("failed to probe %s: %s", dev, err)
}
blkid.DoProbe(pr)
UUID, err := blkid.ProbeLookupValue(pr, "UUID", nil)
if err != nil {
return nil, err
}
TYPE, err := blkid.ProbeLookupValue(pr, "TYPE", nil)
if err != nil {
return nil, err
}
LABEL, err := blkid.ProbeLookupValue(pr, "LABEL", nil)
if err != nil {
return nil, err
}
b = append(b, &BlockDevice{
dev: dev,
UUID: UUID,
TYPE: TYPE,
LABEL: LABEL,
})
}
}
return b, nil
}