forked from u-root/u-root
/
fusermount.go
215 lines (185 loc) · 5.16 KB
/
fusermount.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
// Copyright 2018-2019 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build linux
// fusermount is a very limited replacement for the C fusermount. It
// is invoked by other programs, or interactively only to unmount.
//
// Synopsis:
// fusermount [-u|--unmount] [-z|--lazy] [-v|--verbose] <mountpoint>
//
// For mounting, per the FUSE model, the environment variable
// _FUSE_COMMFD must have the value of a file descriptor variable on
// which we pass the fuse fd.
//
// There is some checking we don't do, e.g. for the number of active
// mount points. Last time I checked, that's the kind of stuff
// kernels do.
//
// Description:
// invoke fuse mount operations
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strconv"
"syscall"
flag "github.com/spf13/pflag"
"golang.org/x/sys/unix"
)
const (
// CommFD is the environment variable which contains the comms fd.
CommFD = "_FUSE_COMMFD"
fuseDev = "/dev/fuse"
)
var (
unmount = flag.BoolP("unmount", "u", false, "unmount")
lazy = flag.BoolP("lazy", "z", false, "lazy unmount")
verbose = flag.BoolP("verbose", "v", false, "verbose")
debug = func(string, ...interface{}) {}
mpt string
)
var (
help = "usage: fusermount [-u|--unmount] [-z|--lazy] [-v|--verbose] <mountpoint>"
)
func usage() {
log.Fatalf(help)
}
func umount(n string) error {
// we're not doing all the folderol of standard
// fusermount for euid() == 0.
// Let's see how that works out.
flags := 0
if *lazy {
flags |= unix.MNT_DETACH
}
// TODO: anything we need here if unit.Getuid() == 0.
// So far there is nothing.
err := unix.Unmount(n, flags)
return err
}
func openFUSE() (int, error) {
return unix.Open("/dev/fuse", unix.O_RDWR, 0)
}
// MountPointOK performs validation on the mountpoint.
// Bury all your magic in here.
func MountPointOK(mpt string) error {
// We wait until we can drop privs to test the mpt
// parameter, since ability to walk the path can
// differ for root and the real user id.
if err := dropPrivs(); err != nil {
return err
}
defer restorePrivs()
mpt = filepath.Clean(mpt)
r, err := filepath.EvalSymlinks(mpt)
if err != nil {
return err
}
if r != mpt {
return fmt.Errorf("resolved path %q and mountpoint %q are not the same", r, mpt)
}
// I'm not sure why fusermount wants to open the mountpoint, so let's mot for now.
// And, for now, directories only? We don't see a current need to mount
// FUSE on any other type of file.
if err := os.Chdir(mpt); err != nil {
return err
}
return nil
}
func getCommFD() (int, error) {
commfd, ok := os.LookupEnv(CommFD)
if !ok {
return -1, fmt.Errorf(CommFD + "was not set and this program can't be used interactively")
}
debug("CommFD %v", commfd)
cfd, err := strconv.Atoi(commfd)
if err != nil {
return -1, fmt.Errorf("%s: %v", CommFD, err)
}
debug("CFD is %v", cfd)
var st unix.Stat_t
if err := unix.Fstat(cfd, &st); err != nil {
return -1, fmt.Errorf("_FUSE_COMMFD: %d: %v", cfd, err)
}
debug("cfd stat is %v", st)
return cfd, nil
}
func doMount(fd int) error {
var flags = uintptr(unix.MS_NODEV | unix.MS_NOSUID)
// From the kernel:
//if (!d->fd_present || !d->rootmode_present ||
// !d->user_id_present || !d->group_id_present)
// return 0;
// Yeah. You get EINVAL if any one of these is not set.
// Docs? what? Docs?
return unix.Mount("nodev", ".", "fuse", flags, fmt.Sprintf("rootmode=%o,user_id=0,group_id=0,fd=%d", unix.S_IFDIR, fd))
}
// returnResult returns the result from earlier operations.
// It is called with the control fd, a FUSE fd, and an error.
// If the error is not nil, then we are shutting down the cfd;
// If it is nil then we try to send the fd back.
// We return either e or the error result and e
func returnResult(cfd, ffd int, e error) error {
if e != nil {
if err := unix.Shutdown(cfd, unix.SHUT_RDWR); err != nil {
return fmt.Errorf("shutting down after failed mount with %v: %v", e, err)
}
return e
}
oob := unix.UnixRights(int(ffd))
if err := unix.Sendmsg(cfd, []byte(""), oob, nil, 0); err != nil {
return fmt.Errorf("%s: %d: %v", CommFD, cfd, err)
}
return nil
}
func main() {
flag.Parse()
if *verbose {
debug = log.Printf
}
if len(flag.Args()) != 1 {
usage()
}
mpt = flag.Arg(0)
debug("mpt %v", mpt)
// We let "ability to open /dev/fuse" stand in as an indicator or
// "we support FUSE".
FuseFD, err := openFUSE()
if err != nil {
log.Printf("%v", err)
os.Exit(int(syscall.ENOENT))
}
debug("FuseFD %v", FuseFD)
// Bad design. All they had to do was make a -z and -u and have
// them both mean unmount. Oh well.
if *lazy && !*unmount {
log.Fatalf("-z can only be used with -u")
}
// Fuse has to be seen to be believed.
// The only interactive use of fusermount is to unmount
if *unmount {
if err := umount(mpt); err != nil {
log.Fatal(err)
}
return
}
if err := MountPointOK(mpt); err != nil {
log.Fatal(err)
}
if err := preMount(); err != nil {
log.Fatal(err)
}
cfd, err := getCommFD()
if err != nil {
log.Fatal(err)
}
if err := doMount(FuseFD); err != nil {
log.Fatal(err)
}
if err := returnResult(cfd, FuseFD, err); err != nil {
log.Fatal(err)
}
}