-
Notifications
You must be signed in to change notification settings - Fork 1
/
mount.go
66 lines (54 loc) · 1.4 KB
/
mount.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
package main
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/teemow/loopback/loop"
)
var (
mountCmd = &cobra.Command{
Use: "mount",
Short: "Mount a loopback device",
Long: `Mount a loopback device.
Attach an image as a loopback and then mount it.
`,
Run: mountRun,
}
mountFlags struct {
name string
imagePath string
mountPath string
}
)
func init() {
mountCmd.Flags().StringVar(&mountFlags.name, "name", "", "Name of the volume")
mountCmd.Flags().StringVar(&mountFlags.imagePath, "image-path", "/var/lib/loopback", "Path for the loopback images")
mountCmd.Flags().StringVar(&mountFlags.mountPath, "mount-path", "", "Path to mount loopback device into")
}
func mountRun(cmd *cobra.Command, args []string) {
if mountFlags.name == "" {
fmt.Fprintln(os.Stderr, "Image name parameter missing.")
os.Exit(1)
}
if mountFlags.mountPath == "" {
fmt.Fprintln(os.Stderr, "Mount path parameter missing.")
os.Exit(1)
}
_, err := os.Stat(mountFlags.mountPath)
if err != nil {
fmt.Fprintln(os.Stderr, "Mount path does not exist.")
os.Exit(1)
}
var device string
device, err = loop.Attach(mountFlags.name, mountFlags.imagePath)
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't attach loopback: %s\n", err)
os.Exit(1)
}
loop.Mount(device, mountFlags.mountPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't mount loopback: %s\n", err)
os.Exit(1)
}
os.Exit(0)
}