-
Notifications
You must be signed in to change notification settings - Fork 4
/
storage.go
154 lines (131 loc) · 4.63 KB
/
storage.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"context"
pb "github.com/0xef53/kvmrun/api/services/machines/v1"
flag_types "github.com/0xef53/kvmrun/cmd/vmm/types"
cli "github.com/urfave/cli/v2"
grpc "google.golang.org/grpc"
)
var storageCommands = &cli.Command{
Name: "storage",
Usage: "manage storage devices (attach, detach, modify)",
HideHelp: true,
Category: "Configuration",
Subcommands: []*cli.Command{
cmdStorageAttach,
cmdStorageDetach,
cmdStorageSet,
cmdStorageResize,
},
}
var cmdStorageAttach = &cli.Command{
Name: "attach",
Usage: "attach a new disk device",
ArgsUsage: "VMNAME DISK",
HideHelp: true,
Flags: []cli.Flag{
&cli.GenericFlag{Name: "driver", Value: flag_types.DefaultDiskDriver(), Usage: "block device driver `name`"},
&cli.IntFlag{Name: "iops-rd", DefaultText: "not set", Usage: "read I/O operations `limit` per second (0 - unlimited)"},
&cli.IntFlag{Name: "iops-wr", DefaultText: "not set", Usage: "write I/O operations `limit` per second (0 - unlimited)"},
&cli.IntFlag{Name: "index", Value: -1, DefaultText: "not set", Usage: "disk index `number` (0 -- is bootable device)"},
&cli.StringFlag{Name: "proxy", DefaultText: "not set", Usage: "`path` to a proxy binary file that will be launched for this disk"},
&cli.GenericFlag{Name: "proxy-env", Aliases: []string{"e"}, Value: flag_types.NewStringMap("="), DefaultText: "not set", Usage: "proxy environment variables (key=value)"},
&cli.BoolFlag{Name: "live", Usage: "affect running machine"},
},
Action: func(c *cli.Context) error {
return executeGRPC(c, attachDisk)
},
}
func attachDisk(ctx context.Context, vmname string, c *cli.Context, conn *grpc.ClientConn) error {
req := pb.AttachDiskRequest{
Name: vmname,
DiskPath: c.Args().Tail()[0],
Driver: c.Generic("driver").(*flag_types.DiskDriver).Value(),
IopsRd: int32(c.Int("iops-rd")),
IopsWr: int32(c.Int("iops-wr")),
Index: int32(c.Int("index")),
ProxyCommand: c.String("proxy"),
ProxyEnvs: c.Generic("proxy-env").(*flag_types.StringMap).Value(),
Live: c.Bool("live"),
}
_, err := pb.NewMachineServiceClient(conn).AttachDisk(ctx, &req)
return err
}
var cmdStorageDetach = &cli.Command{
Name: "detach",
Usage: "detach an existing disk device",
ArgsUsage: "VMNAME DISK",
HideHelp: true,
Flags: []cli.Flag{
&cli.BoolFlag{Name: "live", Usage: "affect running machine"},
},
Action: func(c *cli.Context) error {
return executeGRPC(c, detachDisk)
},
}
func detachDisk(ctx context.Context, vmname string, c *cli.Context, conn *grpc.ClientConn) error {
req := pb.DetachDiskRequest{
Name: vmname,
DiskName: c.Args().Tail()[0],
Live: c.Bool("live"),
}
_, err := pb.NewMachineServiceClient(conn).DetachDisk(ctx, &req)
return err
}
var cmdStorageSet = &cli.Command{
Name: "set",
Usage: "set various disk parameters",
ArgsUsage: "VMNAME DISK",
HideHelp: true,
Flags: []cli.Flag{
&cli.IntFlag{Name: "iops-rd", Value: -1, DefaultText: "not set", Usage: "read I/O operations `limit` per second (0 - unlimited)"},
&cli.IntFlag{Name: "iops-wr", Value: -1, DefaultText: "not set", Usage: "write I/O operations `limit` per second (0 - unlimited)"},
&cli.BoolFlag{Name: "remove-bitmap", Usage: "stop write tracking and remove the dirty bitmap (if exists)"},
&cli.BoolFlag{Name: "live", Usage: "affect running machine"},
},
Action: func(c *cli.Context) error {
return executeGRPC(c, setDiskParameters)
},
}
func setDiskParameters(ctx context.Context, vmname string, c *cli.Context, conn *grpc.ClientConn) error {
client := pb.NewMachineServiceClient(conn)
if c.Bool("remove-bitmap") {
req := pb.RemoveDiskBitmapRequest{
Name: vmname,
DiskName: c.Args().Tail()[0],
}
if _, err := client.RemoveDiskBitmap(ctx, &req); err != nil {
return err
}
}
if c.IsSet("iops-rd") || c.IsSet("iops-wr") {
req := pb.SetDiskLimitsRequest{
Name: vmname,
DiskName: c.Args().Tail()[0],
IopsRd: int32(c.Int("iops-rd")),
IopsWr: int32(c.Int("iops-wr")),
Live: c.Bool("live"),
}
if _, err := client.SetDiskLimits(ctx, &req); err != nil {
return err
}
}
return nil
}
var cmdStorageResize = &cli.Command{
Name: "resize",
Usage: "resize a disk device and send an event to the guest",
ArgsUsage: "VMNAME DISK",
HideHelp: true,
Action: func(c *cli.Context) error {
return executeGRPC(c, resizeDisk)
},
}
func resizeDisk(ctx context.Context, vmname string, c *cli.Context, conn *grpc.ClientConn) error {
req := pb.ResizeQemuBlockdevRequest{
Name: vmname,
DiskName: c.Args().Tail()[0],
}
_, err := pb.NewMachineServiceClient(conn).ResizeQemuBlockdev(ctx, &req)
return err
}