mount_unix.go 3.4 KB
Newer Older
Vasily Kolobkov's avatar
Vasily Kolobkov committed
1
// +build linux darwin freebsd netbsd openbsd
Tonis Tiigi's avatar
Tonis Tiigi committed
2
// +build !nofuse
3 4 5 6 7

package commands

import (
	"fmt"
8
	"io"
9
	"strings"
10

11
	cmds "github.com/ipfs/go-ipfs/commands"
12
	nodeMount "github.com/ipfs/go-ipfs/fuse/node"
13
	config "github.com/ipfs/go-ipfs/repo/config"
14 15
)

16
var MountCmd = &cmds.Command{
17
	Helptext: cmds.HelpText{
rht's avatar
rht committed
18
		Tagline: "Mounts IPFS to the filesystem (read-only).",
19
		ShortDescription: `
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
20 21 22 23
Mount ipfs at a read-only mountpoint on the OS (default: /ipfs and /ipns).
All ipfs objects will be accessible under that directory. Note that the
root will not be listable, as it is virtual. Access known paths directly.

24
You may have to create /ipfs and /ipns before using 'ipfs mount':
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
25 26 27

> sudo mkdir /ipfs /ipns
> sudo chown ` + "`" + `whoami` + "`" + ` /ipfs /ipns
28
> ipfs daemon &
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
29 30 31
> ipfs mount
`,
		LongDescription: `
32 33 34
Mount ipfs at a read-only mountpoint on the OS. The default, /ipfs and /ipns,
are set in the configutation file, but can be overriden by the options.
All ipfs objects will be accessible under this directory. Note that the
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
35 36
root will not be listable, as it is virtual. Access known paths directly.

37
You may have to create /ipfs and /ipns before using 'ipfs mount':
38

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
39 40
> sudo mkdir /ipfs /ipns
> sudo chown ` + "`" + `whoami` + "`" + ` /ipfs /ipns
41
> ipfs daemon &
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
42 43
> ipfs mount

44
Example:
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
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

# setup
> mkdir foo
> echo "baz" > foo/bar
> ipfs add -r foo
added QmWLdkp93sNxGRjnFHPaYg8tCQ35NBY3XPn6KiETd3Z4WR foo/bar
added QmSh5e7S6fdcu75LAbXNZAFY2nGyZUJXyLCJDvn2zRkWyC foo
> ipfs ls QmSh5e7S6fdcu75LAbXNZAFY2nGyZUJXyLCJDvn2zRkWyC
QmWLdkp93sNxGRjnFHPaYg8tCQ35NBY3XPn6KiETd3Z4WR 12 bar
> ipfs cat QmWLdkp93sNxGRjnFHPaYg8tCQ35NBY3XPn6KiETd3Z4WR
baz

# mount
> ipfs daemon &
> ipfs mount
IPFS mounted at: /ipfs
IPNS mounted at: /ipns
> cd /ipfs/QmSh5e7S6fdcu75LAbXNZAFY2nGyZUJXyLCJDvn2zRkWyC
> ls
bar
> cat bar
baz
> cat /ipfs/QmSh5e7S6fdcu75LAbXNZAFY2nGyZUJXyLCJDvn2zRkWyC/bar
baz
> cat /ipfs/QmWLdkp93sNxGRjnFHPaYg8tCQ35NBY3XPn6KiETd3Z4WR
baz
71
`,
72
	},
73
	Options: []cmds.Option{
74 75
		cmds.StringOption("ipfs-path", "f", "The path where IPFS should be mounted."),
		cmds.StringOption("ipns-path", "n", "The path where IPNS should be mounted."),
76
	},
77
	Run: func(req cmds.Request, res cmds.Response) {
Jeromy's avatar
Jeromy committed
78
		cfg, err := req.InvocContext().GetConfig()
79
		if err != nil {
80 81
			res.SetError(err, cmds.ErrNormal)
			return
82 83
		}

Jeromy's avatar
Jeromy committed
84
		node, err := req.InvocContext().GetNode()
85
		if err != nil {
86 87
			res.SetError(err, cmds.ErrNormal)
			return
88
		}
89

90
		// error if we aren't running node in online mode
91
		if !node.OnlineMode() {
92 93
			res.SetError(errNotOnline, cmds.ErrClient)
			return
94 95
		}

96 97
		fsdir, found, err := req.Option("f").String()
		if err != nil {
98 99
			res.SetError(err, cmds.ErrNormal)
			return
100 101
		}
		if !found {
102
			fsdir = cfg.Mounts.IPFS // use default value
103 104 105
		}

		// get default mount points
106 107
		nsdir, found, err := req.Option("n").String()
		if err != nil {
108 109
			res.SetError(err, cmds.ErrNormal)
			return
110
		}
111
		if !found {
112
			nsdir = cfg.Mounts.IPNS // NB: be sure to not redeclare!
113 114
		}

115
		err = nodeMount.Mount(node, fsdir, nsdir)
116
		if err != nil {
117 118
			res.SetError(err, cmds.ErrNormal)
			return
119 120
		}

121 122 123
		var output config.Mounts
		output.IPFS = fsdir
		output.IPNS = nsdir
124
		res.SetOutput(&output)
125
	},
126
	Type: config.Mounts{},
127
	Marshalers: cmds.MarshalerMap{
128
		cmds.Text: func(res cmds.Response) (io.Reader, error) {
129 130 131
			v := res.Output().(*config.Mounts)
			s := fmt.Sprintf("IPFS mounted at: %s\n", v.IPFS)
			s += fmt.Sprintf("IPNS mounted at: %s\n", v.IPNS)
132
			return strings.NewReader(s), nil
133 134 135
		},
	},
}