Skip to content

Commit

Permalink
support spdk tgt
Browse files Browse the repository at this point in the history
Signed-off-by: YLShiJustFly <[email protected]>
  • Loading branch information
asignmisspast committed Sep 26, 2023
1 parent 3b6b0a1 commit 0dd0708
Show file tree
Hide file tree
Showing 21 changed files with 1,054 additions and 51 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,3 @@ upload:
lint:
go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCILINT_VERSION)
$(GOBIN_GOLANGCILINT) run -v

8 changes: 8 additions & 0 deletions cli/command/client/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ func ParseSize(size string) (int, error) {
return n, nil
}

func ParseDevno(size string) (int, error) {
n, err := strconv.Atoi(size)
if err != nil || n < 0 {
return 0, errno.ERR_SPDK_TARGET_DEVNO_IS_ABNORMAL
}
return n, nil
}

func ParseBlockSize(blocksize string) (uint64, error) {
if !strings.HasSuffix(blocksize, "B") {
return 0, errno.ERR_VOLUME_BLOCKSIZE_MUST_END_WITH_BYTE_SUFFIX.
Expand Down
81 changes: 71 additions & 10 deletions cli/command/target/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,23 @@ var (
}
)

var (
ADD_SPDK_PLAYBOOK_STEPS = []int{
//playbook.CREATE_VOLUME,
playbook.ADD_SPDK_TARGET,
}
)

type addOptions struct {
image string
host string
size string
create bool
filename string
blocksize string
image string
host string
size string
create bool
filename string
blocksize string
devno string
originbdevname string
spdk bool
}

func checkAddOptions(curveadm *cli.CurveAdm, options addOptions) error {
Expand All @@ -60,6 +70,17 @@ func checkAddOptions(curveadm *cli.CurveAdm, options addOptions) error {
return err
} else if _, err = client.ParseBlockSize(options.blocksize); err != nil {
return err
} else if options.spdk {
if len(options.devno) != 0 {
if _, err = client.ParseDevno(options.size); err != nil {
return err
}
}
if len(options.originbdevname) != 0 {
if err = client.ParseOriginDevname(options.size); err != nil {
return err
}
}
} else if !utils.PathExist(options.filename) {
return errno.ERR_CLIENT_CONFIGURE_FILE_NOT_EXIST.
F("file path: %s", utils.AbsPath(options.filename))
Expand Down Expand Up @@ -91,9 +112,41 @@ func NewAddCommand(curveadm *cli.CurveAdm) *cobra.Command {
flags.StringVar(&options.size, "size", "10GiB", "Specify volume size")
flags.StringVarP(&options.filename, "conf", "c", "client.yaml", "Specify client configuration file")
flags.StringVar(&options.blocksize, "blocksize", "4096B", "Specify volume blocksize")
flags.BoolVar(&options.spdk, "spdk", false, "create iscsi spdk target")
flags.StringVar(&options.devno, "devno", "0", "Specify bdev num(when set --spdk flag)")
flags.StringVar(&options.originbdevname, "originbdevname", "/dev/cbd0", "Specify cbd dev(when set --spdk flag)")
return cmd
}

func genAddSpdkPlaybook(curveadm *cli.CurveAdm,
ccs []*configure.ClientConfig,
options addOptions) (*playbook.Playbook, error) {
user, name, _ := client.ParseImage(options.image)
size, _ := client.ParseSize(options.size)
blocksize, _ := client.ParseBlockSize(options.blocksize)
steps := ADD_SPDK_PLAYBOOK_STEPS
pb := playbook.NewPlaybook(curveadm)
for _, step := range steps {
pb.AddStep(&playbook.PlaybookStep{
Type: step,
Configs: ccs,
Options: map[string]interface{}{
comm.KEY_SPDK_TARGET_OPTIONS: bs.SpdkTargetOption{
Host: options.host,
User: user,
Volume: name,
Size: size,
Blocksize: blocksize,
Create: options.create,
Devno: options.devno,
OriginBdevname: options.originbdevname,
},
},
})
}
return pb, nil
}

func genAddPlaybook(curveadm *cli.CurveAdm,
ccs []*configure.ClientConfig,
options addOptions) (*playbook.Playbook, error) {
Expand Down Expand Up @@ -130,11 +183,19 @@ func runAdd(curveadm *cli.CurveAdm, options addOptions) error {
return errno.ERR_REQUIRE_CURVEBS_KIND_CLIENT_CONFIGURE_FILE.
F("kind: %s", cc.GetKind())
}

var pb *playbook.Playbook
// 2) generate map playbook
pb, err := genAddPlaybook(curveadm, []*configure.ClientConfig{cc}, options)
if err != nil {
return err
if options.spdk {
pb, err = genAddSpdkPlaybook(curveadm, []*configure.ClientConfig{cc}, options)
if err != nil {
return err
}

} else {
pb, err = genAddPlaybook(curveadm, []*configure.ClientConfig{cc}, options)
if err != nil {
return err
}
}

// 3) run playground
Expand Down
2 changes: 1 addition & 1 deletion cli/command/target/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewTargetCommand(curveadm *cli.CurveAdm) *cobra.Command {
cmd := &cobra.Command{
Use: "target",
Short: "Manage SCSI target of CurveBS",
Args: cliutil.NoArgs,
Args: cliutil.ExactArgs(1),
RunE: cliutil.ShowHelp(curveadm.Err()),
}

Expand Down
47 changes: 44 additions & 3 deletions cli/command/target/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,17 @@ var (
}
)

var (
DELETE_SPDK_PLAYBOOK_STEPS = []int{
playbook.DELETE_SPDK_TARGET,
}
)

type deleteOptions struct {
host string
tid string
host string
tid string
devno string
spdk bool
}

func NewDeleteCommand(curveadm *cli.CurveAdm) *cobra.Command {
Expand All @@ -60,6 +68,7 @@ func NewDeleteCommand(curveadm *cli.CurveAdm) *cobra.Command {

flags := cmd.Flags()
flags.StringVar(&options.host, "host", "localhost", "Specify target host")
flags.BoolVar(&options.spdk, "spdk", false, "delete iscsi spdk target")

return cmd
}
Expand All @@ -82,9 +91,41 @@ func genDeletePlaybook(curveadm *cli.CurveAdm, options deleteOptions) (*playbook
return pb, nil
}

func genDeleteSpdkPlaybook(curveadm *cli.CurveAdm, options deleteOptions) (*playbook.Playbook, error) {
steps := DELETE_SPDK_PLAYBOOK_STEPS
pb := playbook.NewPlaybook(curveadm)
for _, step := range steps {
pb.AddStep(&playbook.PlaybookStep{
Type: step,
Configs: nil,
Options: map[string]interface{}{
comm.KEY_SPDK_TARGET_OPTIONS: bs.SpdkTargetOption{
Host: options.host,
Tid: options.tid,
Devno: options.devno,
},
},
})
}
return pb, nil
}

func runDelete(curveadm *cli.CurveAdm, options deleteOptions) error {
// 1) generate list playbook
pb, err := genDeletePlaybook(curveadm, options)
var pb *playbook.Playbook
var err error
if options.spdk {
pb, err = genDeleteSpdkPlaybook(curveadm, options)
if err != nil {
return err
}
} else {
pb, err = genDeletePlaybook(curveadm, options)
if err != nil {
return err
}
}

if err != nil {
return err
}
Expand Down
51 changes: 45 additions & 6 deletions cli/command/target/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,15 @@ var (
}
)

var (
LIST_SPDK_PLAYBOOK_STEPS = []int{
playbook.LIST_SPDK_TARGETS,
}
)

type listOptions struct {
host string
spdk bool
}

func NewListCommand(curveadm *cli.CurveAdm) *cobra.Command {
Expand All @@ -59,6 +66,7 @@ func NewListCommand(curveadm *cli.CurveAdm) *cobra.Command {

flags := cmd.Flags()
flags.StringVar(&options.host, "host", "localhost", "Specify target host")
flags.BoolVar(&options.spdk, "spdk", false, "list iscsi spdk target")

return cmd
}
Expand All @@ -80,9 +88,31 @@ func genListPlaybook(curveadm *cli.CurveAdm, options listOptions) (*playbook.Pla
return pb, nil
}

func displayTargets(curveadm *cli.CurveAdm) {
func genListSpdkPlaybook(curveadm *cli.CurveAdm, options listOptions) (*playbook.Playbook, error) {
steps := LIST_SPDK_PLAYBOOK_STEPS
pb := playbook.NewPlaybook(curveadm)
for _, step := range steps {
pb.AddStep(&playbook.PlaybookStep{
Type: step,
Configs: nil,
Options: map[string]interface{}{
comm.KEY_SPDK_TARGET_OPTIONS: bs.SpdkTargetOption{
Host: options.host,
},
},
})
}
return pb, nil
}

func displayTargets(curveadm *cli.CurveAdm, spdk bool) {
targets := []step.Target{}
value := curveadm.MemStorage().Get(comm.KEY_ALL_TARGETS)
var value interface{}
if spdk {
value = curveadm.MemStorage().Get(comm.KEY_ALL_SPDK_TARGETS)
} else {
value = curveadm.MemStorage().Get(comm.KEY_ALL_TARGETS)
}
if value != nil {
m := value.(map[string]*step.Target)
for _, target := range m {
Expand All @@ -97,9 +127,18 @@ func displayTargets(curveadm *cli.CurveAdm) {

func runList(curveadm *cli.CurveAdm, options listOptions) error {
// 1) generate list playbook
pb, err := genListPlaybook(curveadm, options)
if err != nil {
return err
var pb *playbook.Playbook
var err error
if options.spdk {
pb, err = genListSpdkPlaybook(curveadm, options)
if err != nil {
return err
}
} else {
pb, err = genListPlaybook(curveadm, options)
if err != nil {
return err
}
}

// 2) run playground
Expand All @@ -109,6 +148,6 @@ func runList(curveadm *cli.CurveAdm, options listOptions) error {
}

// 3) print targets
displayTargets(curveadm)
displayTargets(curveadm, options.spdk)
return nil
}
42 changes: 39 additions & 3 deletions cli/command/target/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,16 @@ var (
}
)

var (
START_SPDK_PLAYBOOK_STEPS = []int{
playbook.START_SPDK_TARGET_DAEMON,
}
)

type startOptions struct {
host string
filename string
spdk bool
}

func NewStartCommand(curveadm *cli.CurveAdm) *cobra.Command {
Expand All @@ -62,6 +69,7 @@ func NewStartCommand(curveadm *cli.CurveAdm) *cobra.Command {
flags := cmd.Flags()
flags.StringVar(&options.host, "host", "localhost", "Specify target host")
flags.StringVarP(&options.filename, "conf", "c", "client.yaml", "Specify client configuration file")
flags.BoolVar(&options.spdk, "spdk", false, "start iscsi spdk target")

return cmd
}
Expand All @@ -85,6 +93,25 @@ func genStartPlaybook(curveadm *cli.CurveAdm,
return pb, nil
}

func genStartSpdkPlaybook(curveadm *cli.CurveAdm,
ccs []*configure.ClientConfig,
options startOptions) (*playbook.Playbook, error) {
steps := START_SPDK_PLAYBOOK_STEPS
pb := playbook.NewPlaybook(curveadm)
for _, step := range steps {
pb.AddStep(&playbook.PlaybookStep{
Type: step,
Configs: ccs,
Options: map[string]interface{}{
comm.KEY_SPDK_TARGET_OPTIONS: bs.SpdkTargetOption{
Host: options.host,
},
},
})
}
return pb, nil
}

func runStart(curveadm *cli.CurveAdm, options startOptions) error {
// 1) parse client configure
cc, err := configure.ParseClientConfig(options.filename)
Expand All @@ -96,9 +123,18 @@ func runStart(curveadm *cli.CurveAdm, options startOptions) error {
}

// 2) generate map playbook
pb, err := genStartPlaybook(curveadm, []*configure.ClientConfig{cc}, options)
if err != nil {
return err
var pb *playbook.Playbook
if options.spdk {
pb, err = genStartSpdkPlaybook(curveadm, []*configure.ClientConfig{cc}, options)
if err != nil {
return err
}
} else {
pb, err = genStartPlaybook(curveadm, []*configure.ClientConfig{cc}, options)
if err != nil {
return err
}

}

// 3) run playground
Expand Down
Loading

0 comments on commit 0dd0708

Please sign in to comment.