-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
120 lines (105 loc) · 3.4 KB
/
main.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
package main
import (
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/spf13/cobra"
"github.com/javgh/sia-nbdserver/config"
"github.com/javgh/sia-nbdserver/nbd"
"github.com/javgh/sia-nbdserver/sia"
)
const (
defaultSize = 1099511627776
defaultHardMaxCached = 128
defaultSoftMaxCached = 96
defaultIdleIntervalSeconds = 120
defaultSiaDaemonAddress = "localhost:9980"
defaultSiaPasswordFileSuffix = ".sia/apipassword"
)
func installSignalHandlers(siaBackend *sia.Backend) {
c := make(chan os.Signal, 3)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM, syscall.SIGUSR1)
for {
sig := <-c
switch sig {
case syscall.SIGINT, syscall.SIGTERM:
log.Printf("Performing fast shutdown\n")
err := siaBackend.Shutdown(false)
if err != nil {
log.Fatal(err)
}
case syscall.SIGUSR1:
log.Printf("Performing thorough shutdown\n")
err := siaBackend.Shutdown(true)
if err != nil {
log.Fatal(err)
}
default:
panic("unexpected signal")
}
}
}
func serve(socketPath string, exportSize uint64, backendSettings sia.BackendSettings) {
siaBackend, err := sia.NewBackend(backendSettings)
if err != nil {
log.Fatal(err)
}
go installSignalHandlers(siaBackend)
err = nbd.Serve(socketPath, exportSize, siaBackend)
if err != nil {
log.Fatal(err)
}
siaBackend.Wait()
}
func main() {
socketPath, _ := config.GetSocketPath()
size := uint64(defaultSize)
hardMaxCached := defaultHardMaxCached
softMaxCached := defaultSoftMaxCached
idleIntervalSeconds := defaultIdleIntervalSeconds
siaDaemonAddress := defaultSiaDaemonAddress
siaPasswordFile := config.PrependHomeDirectory(defaultSiaPasswordFileSuffix)
rootDesc := "NBD server backed by Sia storage + local cache"
rootCmd := &cobra.Command{
Use: "sia-nbdserver",
Short: rootDesc,
Long: fmt.Sprintf("%s.", rootDesc),
Run: func(cmd *cobra.Command, args []string) {
if socketPath == "" {
fmt.Println("Default socket path is $XDG_RUNTIME_DIR/sia-nbdserver," +
" but $XDG_RUNTIME_DIR is not set. Please specify a socket path via -u flag.")
os.Exit(1)
}
backendSettings := sia.BackendSettings{
Size: size,
HardMaxCached: hardMaxCached,
SoftMaxCached: softMaxCached,
IdleInterval: time.Duration(idleIntervalSeconds * int(time.Second)),
SiaDaemonAddress: siaDaemonAddress,
SiaPasswordFile: siaPasswordFile,
}
serve(socketPath, size, backendSettings)
},
}
rootCmd.PersistentFlags().StringVarP(&socketPath, "unix", "u", socketPath,
"unix domain socket")
rootCmd.PersistentFlags().Uint64VarP(&size, "size", "s", size,
"size of block device; should ideally be a multiple of 67108864 (2 ^ 26)")
rootCmd.PersistentFlags().IntVarP(&hardMaxCached, "hard", "H", hardMaxCached,
"hard limit for number of 64 MiB pages in the cache")
rootCmd.PersistentFlags().IntVarP(&softMaxCached, "soft", "S", softMaxCached,
"soft limit for number of 64 MiB pages in the cache")
rootCmd.PersistentFlags().IntVarP(&idleIntervalSeconds, "idle", "i", idleIntervalSeconds,
"seconds to wait before a cache page is marked idle and upload begins")
rootCmd.PersistentFlags().StringVar(&siaPasswordFile, "sia-password-file", siaPasswordFile,
"path to Sia API password file")
rootCmd.PersistentFlags().StringVar(&siaDaemonAddress, "sia-daemon", siaDaemonAddress,
"host and port of Sia daemon")
err := rootCmd.Execute()
if err != nil {
log.Fatal(err)
}
}