...
1
2
3
4
5
6
7
8
9
10
11
12
13
14 package fusedev
15
16 import (
17 "bufio"
18 "fmt"
19 "os"
20 "strings"
21
22 "github.com/pingcap/errors"
23 )
24
25
26 func GrantAccess() error {
27 pid := os.Getpid()
28 cgroupPath := fmt.Sprintf("/proc/%d/cgroup", pid)
29
30 cgroupFile, err := os.Open(cgroupPath)
31 if err != nil {
32 return err
33 }
34 defer cgroupFile.Close()
35
36
37 cgroupScanner := bufio.NewScanner(cgroupFile)
38 var deviceCgroupPath string
39 for cgroupScanner.Scan() {
40 var (
41 text = cgroupScanner.Text()
42 parts = strings.SplitN(text, ":", 3)
43 )
44 if len(parts) < 3 {
45 return errors.Errorf("invalid cgroup entry: %q", text)
46 }
47
48 if parts[1] == "devices" {
49 deviceCgroupPath = parts[2]
50 }
51 }
52
53 if err := cgroupScanner.Err(); err != nil {
54 return err
55 }
56
57 if len(deviceCgroupPath) == 0 {
58 return errors.Errorf("fail to find device cgroup")
59 }
60
61 deviceCgroupPath = "/sys/fs/cgroup/devices" + deviceCgroupPath + "/devices.allow"
62 f, err := os.OpenFile(deviceCgroupPath, os.O_WRONLY, 0)
63 if err != nil {
64 return err
65 }
66 defer f.Close()
67
68
69 content := "c 10:229 rwm"
70 _, err = f.WriteString(content)
71 if err != nil {
72 return err
73 }
74
75 return nil
76 }
77