-
Notifications
You must be signed in to change notification settings - Fork 3
/
fsfreeze.go
147 lines (120 loc) · 2.78 KB
/
fsfreeze.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
package main
import (
"bufio"
"encoding/json"
"os"
"path/filepath"
"strings"
"syscall"
)
// from linux/fs.h
const FIFREEZE = 0xC0045877
const FITHAW = 0xC0045878
var FROZEN bool = false
type MEntry struct {
FSSpec string
FSFile string
FSType string
}
func GetMountPoints() ([]MEntry, error) {
f, err := os.Open("/proc/self/mounts")
if err != nil {
return nil, err
}
defer f.Close()
m := make([]MEntry, 0, 10)
isExist := func(fsspec string) bool {
for _, v := range m {
if v.FSSpec == fsspec {
return true
}
}
return false
}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
fields := strings.Fields(scanner.Text())
if len(fields) < 2 || isExist(fields[0]) || fields[0][0] != '/' ||
fields[2] == "smbfs" || fields[2] == "cifs" {
continue
}
// Ignoring the loop devices
if strings.HasPrefix(fields[0], "/dev/loop") {
continue
}
// Ignoring the dm- devices
st, err := os.Lstat(fields[0])
if err != nil {
return nil, err
}
if st.Mode()&os.ModeSymlink != 0 {
if s, err := os.Readlink(fields[0]); err != nil {
return nil, err
} else {
fields[0] = filepath.Base(s)
}
}
if strings.HasPrefix(fields[0], "dm-") {
continue
}
m = append(m, MEntry{fields[0], fields[1], fields[2]})
}
if err = scanner.Err(); err != nil {
return nil, err
}
return m, nil
}
func ioctl(fd uintptr, request, argp uintptr) (err error) {
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, fd, request, argp)
if errno != 0 {
err = errno
}
return os.NewSyscallError("ioctl", err)
}
func GetFreezeStatus(cResp chan<- *Response, rawArgs *json.RawMessage, tag string) {
cResp <- &Response{FROZEN, tag, nil}
}
func FsFreeze(cResp chan<- *Response, rawArgs *json.RawMessage, tag string) {
m, err := GetMountPoints()
if err != nil {
cResp <- &Response{nil, tag, err}
return
}
FROZEN = true
for _, mp := range m {
fs, err := os.Open(mp.FSFile)
if err != nil {
cResp <- &Response{nil, tag, err}
return
}
if err := ioctl(fs.Fd(), FIFREEZE, 0); err != nil &&
err.(*os.SyscallError).Err.(syscall.Errno) != syscall.EOPNOTSUPP &&
err.(*os.SyscallError).Err.(syscall.Errno) != syscall.EBUSY {
cResp <- &Response{nil, tag, err}
return
}
fs.Close()
}
cResp <- &Response{true, tag, nil}
}
func FsUnFreeze(cResp chan<- *Response, rawArgs *json.RawMessage, tag string) {
m, err := GetMountPoints()
if err != nil {
cResp <- &Response{nil, tag, err}
return
}
for _, mp := range m {
fs, err := os.Open(mp.FSFile)
if err != nil {
cResp <- &Response{nil, tag, err}
return
}
if err := ioctl(fs.Fd(), FITHAW, 0); err != nil && err.(*os.SyscallError).Err.(syscall.Errno) != syscall.EINVAL {
cResp <- &Response{nil, tag, err}
return
}
fs.Close()
}
FROZEN = false
cResp <- &Response{true, tag, nil}
}