mount_unix.go 3.29 KB
Newer Older
1
// +build !windows,!nofuse
2 3 4 5 6

package commands

import (
	"fmt"
7
	"io"
8

9
	cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
10
	nodeMount "github.com/ipfs/go-ipfs/fuse/node"
Jan Winkelmann's avatar
Jan Winkelmann committed
11

Jakub Sztandera's avatar
Jakub Sztandera committed
12 13
	cmds "github.com/ipfs/go-ipfs-cmds"
	config "github.com/ipfs/go-ipfs-config"
14 15 16 17 18
)

const (
	mountIPFSPathOptionName = "ipfs-path"
	mountIPNSPathOptionName = "ipns-path"
19 20
)

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

29
You may have to create /ipfs and /ipns before using 'ipfs mount':
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
30 31

> sudo mkdir /ipfs /ipns
Łukasz Magiera's avatar
Łukasz Magiera committed
32
> sudo chown $(whoami) /ipfs /ipns
33
> ipfs daemon &
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
34 35 36
> ipfs mount
`,
		LongDescription: `
37
Mount IPFS at a read-only mountpoint on the OS. The default, /ipfs and /ipns,
flowed's avatar
flowed committed
38
are set in the configuration file, but can be overridden by the options.
39
All IPFS objects will be accessible under this directory. Note that the
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
40 41
root will not be listable, as it is virtual. Access known paths directly.

42
You may have to create /ipfs and /ipns before using 'ipfs mount':
43

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
44
> sudo mkdir /ipfs /ipns
Łukasz Magiera's avatar
Łukasz Magiera committed
45
> sudo chown $(whoami) /ipfs /ipns
46
> ipfs daemon &
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
47 48
> ipfs mount

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

# 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
76
`,
77
	},
Steven Allen's avatar
Steven Allen committed
78 79 80
	Options: []cmds.Option{
		cmds.StringOption(mountIPFSPathOptionName, "f", "The path where IPFS should be mounted."),
		cmds.StringOption(mountIPNSPathOptionName, "n", "The path where IPNS should be mounted."),
81
	},
82 83
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
		cfg, err := cmdenv.GetConfig(env)
84
		if err != nil {
85
			return err
86 87
		}

88
		nd, err := cmdenv.GetNode(env)
89
		if err != nil {
90
			return err
91
		}
92

93
		// error if we aren't running node in online mode
94
		if !nd.IsOnline {
Overbool's avatar
Overbool committed
95
			return ErrNotOnline
96 97
		}

98
		fsdir, found := req.Options[mountIPFSPathOptionName].(string)
99
		if !found {
100
			fsdir = cfg.Mounts.IPFS // use default value
101 102 103
		}

		// get default mount points
104
		nsdir, found := req.Options[mountIPNSPathOptionName].(string)
105
		if !found {
106
			nsdir = cfg.Mounts.IPNS // NB: be sure to not redeclare!
107 108
		}

109
		err = nodeMount.Mount(nd, fsdir, nsdir)
110
		if err != nil {
111
			return err
112 113
		}

114 115 116
		var output config.Mounts
		output.IPFS = fsdir
		output.IPNS = nsdir
117
		return cmds.EmitOnce(res, &output)
118
	},
119
	Type: config.Mounts{},
120 121
	Encoders: cmds.EncoderMap{
		cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, mounts *config.Mounts) error {
122 123
			fmt.Fprintf(w, "IPFS mounted at: %s\n", cmdenv.EscNonPrint(mounts.IPFS))
			fmt.Fprintf(w, "IPNS mounted at: %s\n", cmdenv.EscNonPrint(mounts.IPNS))
Overbool's avatar
Overbool committed
124

125 126
			return nil
		}),
127 128
	},
}