-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
80 lines (61 loc) · 1.11 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
package main
import (
"flag"
"log"
"os"
"os/signal"
"sync"
"github.com/pojntfx/go-nbd/pkg/backend"
"github.com/pojntfx/r3map/pkg/mount"
"github.com/pojntfx/r3map/pkg/utils"
)
func main() {
size := flag.Int64("size", 536870912, "Size of the resource in byte")
flag.Parse()
f, err := os.CreateTemp("", "")
if err != nil {
panic(err)
}
defer os.RemoveAll(f.Name())
if err := f.Truncate(*size); err != nil {
panic(err)
}
b := backend.NewFileBackend(f)
devPath, err := utils.FindUnusedNBDDevice()
if err != nil {
panic(err)
}
devFile, err := os.Open(devPath)
if err != nil {
panic(err)
}
defer devFile.Close()
mnt := mount.NewDirectFileMount(
b,
devFile,
nil,
nil,
)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
if err := mnt.Wait(); err != nil {
panic(err)
}
}()
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt)
go func() {
<-done
log.Println("Exiting gracefully")
_ = mnt.Close()
}()
defer mnt.Close()
file, err := mnt.Open()
if err != nil {
panic(err)
}
log.Println("Resource available on", file.Name())
wg.Wait()
}