File: mount.go

package info (click to toggle)
continuity 0.0~git20180216.d8fb858-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 620 kB
  • sloc: makefile: 46; sh: 28; asm: 3
file content (115 lines) | stat: -rw-r--r-- 2,496 bytes parent folder | download
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
// +build linux darwin freebsd

package commands

import (
	"io/ioutil"
	"log"
	"os"
	"os/signal"
	"path/filepath"
	"syscall"
	"time"

	"bazil.org/fuse"
	"bazil.org/fuse/fs"

	"github.com/containerd/continuity"
	"github.com/containerd/continuity/continuityfs"
	"github.com/containerd/continuity/driver"
	"github.com/sirupsen/logrus"
	"github.com/spf13/cobra"
)

var MountCmd = &cobra.Command{
	Use:   "mount <mountpoint> [<manifest>] [<source directory>]",
	Short: "Mount the manifest to the provided mountpoint using content from a source directory",
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) != 3 {
			log.Fatal("Must specify mountpoint, manifest, and source directory")
		}
		mountpoint := args[0]
		manifest, source := args[1], args[2]

		manifestName := filepath.Base(manifest)

		p, err := ioutil.ReadFile(manifest)
		if err != nil {
			log.Fatalf("error reading manifest: %v", err)
		}

		m, err := continuity.Unmarshal(p)
		if err != nil {
			log.Fatalf("error unmarshaling manifest: %v", err)
		}

		driver, err := driver.NewSystemDriver()
		if err != nil {
			logrus.Fatal(err)
		}

		provider := continuityfs.NewFSFileContentProvider(source, driver)

		contfs, err := continuityfs.NewFSFromManifest(m, mountpoint, provider)
		if err != nil {
			logrus.Fatal(err)
		}

		c, err := fuse.Mount(
			mountpoint,
			fuse.ReadOnly(),
			fuse.FSName(manifestName),
			fuse.Subtype("continuity"),
			// OSX Only options
			fuse.LocalVolume(),
			fuse.VolumeName("Continuity FileSystem"),
		)
		if err != nil {
			logrus.Fatal(err)
		}

		<-c.Ready
		if err := c.MountError; err != nil {
			c.Close()
			logrus.Fatal(err)
		}

		errChan := make(chan error, 1)
		go func() {
			// TODO: Create server directory to use context
			err = fs.Serve(c, contfs)
			if err != nil {
				errChan <- err
			}
		}()

		sigChan := make(chan os.Signal, 1)
		signal.Notify(sigChan, os.Interrupt)
		signal.Notify(sigChan, syscall.SIGTERM)

		select {
		case <-sigChan:
			logrus.Infof("Shutting down")
		case err = <-errChan:
		}

		go func() {
			if err := c.Close(); err != nil {
				logrus.Errorf("Unable to close connection %s", err)
			}
		}()

		// Wait for any inprogress requests to be handled
		time.Sleep(time.Second)

		logrus.Infof("Attempting unmount")
		if err := fuse.Unmount(mountpoint); err != nil {
			logrus.Errorf("Error unmounting %s: %v", mountpoint, err)
		}

		// Handle server error
		if err != nil {
			logrus.Fatalf("Error serving fuse server: %v", err)
		}
	},
}