Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add curvebs volume target blocksize #161

Merged
merged 1 commit into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions cli/command/client/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package client

import (
"github.com/dustin/go-humanize"
"strconv"
"strings"

Expand Down Expand Up @@ -88,24 +89,40 @@ func ParseImage(image string) (user, name string, err error) {
}

func ParseSize(size string) (int, error) {
if !strings.HasSuffix(size, "GB") {
return 0, errno.ERR_VOLUME_SIZE_MUST_END_WITH_GB_SUFFIX.
if !strings.HasSuffix(size, "GiB") {
return 0, errno.ERR_VOLUME_SIZE_MUST_END_WITH_GiB_SUFFIX.
F("size: %s", size)
}

size = strings.TrimSuffix(size, "GB")
size = strings.TrimSuffix(size, "GiB")
n, err := strconv.Atoi(size)
if err != nil || n <= 0 {
return 0, errno.ERR_VOLUME_SIZE_REQUIRES_POSITIVE_INTEGER.
F("size: %sGB", size)
F("size: %sGiB", size)
} else if n%10 != 0 {
return 0, errno.ERR_VOLUME_SIZE_MUST_BE_MULTIPLE_OF_10_GB.
F("size: %sGB", size)
return 0, errno.ERR_VOLUME_SIZE_MUST_BE_MULTIPLE_OF_10_GiB.
F("size: %sGiB", size)
}

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.
F("blocksize: %s", blocksize)
}
Comment on lines +111 to +114
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not needed here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

blocksize = strings.TrimSuffix(blocksize, "B")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to remove B.

// You can edit this code!
// Click here and start typing.
package main

import (
	"fmt"

	"github.com/dustin/go-humanize"
)

func main() {
	blocksize := "1024B"
	str, _ := humanize.ParseBytes(blocksize)
	fmt.Println(str)
}

the output is:

1024

m, err := humanize.ParseBytes(blocksize)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can look at its source code

if err != nil || m <= 0 {
return 0, errno.ERR_VOLUME_BLOCKSIZE_REQUIRES_POSITIVE_INTEGER.
F("blocksize: %s", humanize.IBytes(m))
} else if m%512 != 0 {
return 0, errno.ERR_VOLUME_BLOCKSIZE_BE_MULTIPLE_OF_512.
F("blocksize: %s", humanize.IBytes(m))
}
return m, nil
}
func checkMapOptions(curveadm *cli.CurveAdm, options mapOptions) error {
if _, _, err := ParseImage(options.image); err != nil {
return err
Expand Down Expand Up @@ -140,9 +157,8 @@ func NewMapCommand(curveadm *cli.CurveAdm) *cobra.Command {
flags.StringVar(&options.host, "host", "localhost", "Specify target host")
flags.BoolVar(&options.create, "create", false, "Create volume iff not exist")
flags.BoolVar(&options.noExclusive, "no-exclusive", false, "Map volume non exclusive")
flags.StringVar(&options.size, "size", "10GB", "Specify volume size")
flags.StringVar(&options.size, "size", "10GiB", "Specify volume size")
flags.StringVarP(&options.filename, "conf", "c", "client.yaml", "Specify client configuration file")

return cmd
}

Expand Down
29 changes: 17 additions & 12 deletions cli/command/target/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,21 @@ var (
)

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

func checkAddOptions(curveadm *cli.CurveAdm, options addOptions) error {
if _, _, err := client.ParseImage(options.image); err != nil {
return err
} else if _, err = client.ParseSize(options.size); err != nil {
return err
} else if _, err = client.ParseBlockSize(options.blocksize); 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 @@ -85,9 +88,9 @@ func NewAddCommand(curveadm *cli.CurveAdm) *cobra.Command {
flags := cmd.Flags()
flags.StringVar(&options.host, "host", "localhost", "Specify target host")
flags.BoolVar(&options.create, "create", false, "Create volume iff not exist")
flags.StringVar(&options.size, "size", "10GB", "Specify volume size")
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")
return cmd
}

Expand All @@ -96,6 +99,7 @@ func genAddPlaybook(curveadm *cli.CurveAdm,
options addOptions) (*playbook.Playbook, error) {
user, name, _ := client.ParseImage(options.image)
size, _ := client.ParseSize(options.size)
blocksize, _ := client.ParseBlockSize(options.blocksize)
steps := ADD_PLAYBOOK_STEPS
pb := playbook.NewPlaybook(curveadm)
for _, step := range steps {
Expand All @@ -104,11 +108,12 @@ func genAddPlaybook(curveadm *cli.CurveAdm,
Configs: ccs,
Options: map[string]interface{}{
comm.KEY_TARGET_OPTIONS: bs.TargetOption{
Host: options.host,
User: user,
Volume: name,
Size: size,
Create: options.create,
Host: options.host,
User: user,
Volume: name,
Size: size,
Blocksize: blocksize,
Create: options.create,
},
},
})
Expand Down
21 changes: 12 additions & 9 deletions internal/errno/errno.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,18 @@ var (
// 220: commad options (client common)
ERR_UNSUPPORT_CLIENT_KIND = EC(220000, "unsupport client kind")
// 221: command options (client/bs)
ERR_INVALID_VOLUME_FORMAT = EC(221000, "invalid volume format")
ERR_ROOT_VOLUME_USER_NOT_ALLOWED = EC(221001, "root as volume user is not allowed")
ERR_VOLUME_NAME_MUST_START_WITH_SLASH_PREFIX = EC(221002, "volume name must start with \"/\" prefix")
ERR_VOLUME_SIZE_MUST_END_WITH_GB_SUFFIX = EC(221003, "volume size must end with \"GB\" suffix")
ERR_VOLUME_SIZE_REQUIRES_POSITIVE_INTEGER = EC(221004, "volume size requires a positive integer")
ERR_VOLUME_SIZE_MUST_BE_MULTIPLE_OF_10_GB = EC(221005, "volume size must be a multiple of 10GB, like 10GB, 20GB, 30GB...")
ERR_CLIENT_CONFIGURE_FILE_NOT_EXIST = EC(221006, "client configure file not exist")
ERR_NO_CLIENT_MATCHED = EC(221007, "no client matched")
ERR_VOLUME_NAME_CAN_NOT_CONTAIN_UNDERSCORE = EC(221008, "volume name can't contain \"_\" symbol")
ERR_INVALID_VOLUME_FORMAT = EC(221000, "invalid volume format")
ERR_ROOT_VOLUME_USER_NOT_ALLOWED = EC(221001, "root as volume user is not allowed")
ERR_VOLUME_NAME_MUST_START_WITH_SLASH_PREFIX = EC(221002, "volume name must start with \"/\" prefix")
ERR_VOLUME_SIZE_MUST_END_WITH_GiB_SUFFIX = EC(221003, "volume size must end with \"GiB\" suffix")
ERR_VOLUME_SIZE_REQUIRES_POSITIVE_INTEGER = EC(221004, "volume size requires a positive integer")
ERR_VOLUME_SIZE_MUST_BE_MULTIPLE_OF_10_GiB = EC(221005, "volume size must be a multiple of 10GiB, like 10GiB, 20GiB, 30GiB...")
ERR_CLIENT_CONFIGURE_FILE_NOT_EXIST = EC(221006, "client configure file not exist")
ERR_NO_CLIENT_MATCHED = EC(221007, "no client matched")
ERR_VOLUME_NAME_CAN_NOT_CONTAIN_UNDERSCORE = EC(221008, "volume name can't contain \"_\" symbol")
ERR_VOLUME_BLOCKSIZE_MUST_END_WITH_BYTE_SUFFIX = EC(201009, "volume block size must end with \"B\" suffix")
ERR_VOLUME_BLOCKSIZE_REQUIRES_POSITIVE_INTEGER = EC(221010, "volume block size requires a positive integer")
ERR_VOLUME_BLOCKSIZE_BE_MULTIPLE_OF_512 = EC(221011, "volume block size be a multiple of 512B, like 1KiB, 2KiB, 3KiB...")
// 222: command options (client/fs)
ERR_FS_MOUNTPOINT_REQUIRE_ABSOLUTE_PATH = EC(222000, "mount point must be an absolute path")

Expand Down
6 changes: 4 additions & 2 deletions internal/task/scripts/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ package scripts
* Example: target curve test true 10
* See Also: https://linux.die.net/man/8/tgtadm
*/

var TARGET = `
#!/usr/bin/env bash

g_user=$1
g_volume=$2
g_create=$3
g_size=$4
g_blocksize=$5
g_tid=1
g_image=cbd:pool/${g_volume}_${g_user}_
g_image_md5=$(echo -n ${g_image} | md5sum | awk '{ print $1 }')
Expand All @@ -50,7 +52,6 @@ if [ $g_create == "true" ]; then
fi
fi
fi

for ((i=1;;i++)); do
tgtadm --lld iscsi --mode target --op show --tid $i 1>/dev/null 2>&1
if [ $? -ne 0 ]; then
Expand All @@ -75,7 +76,8 @@ tgtadm --lld iscsi \
--tid ${g_tid} \
--lun 1 \
--bstype curve \
--backing-store ${g_image}
--backing-store ${g_image} \
--blocksize ${g_blocksize}
if [ $? -ne 0 ]; then
echo "tgtadm logicalunit new failed"
exit 1
Expand Down
3 changes: 2 additions & 1 deletion internal/task/task/bs/add_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type TargetOption struct {
Create bool
Size int
Tid string
Blocksize uint64
}

func NewAddTargetTask(curveadm *cli.CurveAdm, cc *configure.ClientConfig) (*task.Task, error) {
Expand All @@ -58,7 +59,7 @@ func NewAddTargetTask(curveadm *cli.CurveAdm, cc *configure.ClientConfig) (*task
containerId := DEFAULT_TGTD_CONTAINER_NAME
targetScriptPath := "/curvebs/tools/sbin/target.sh"
targetScript := scripts.TARGET
cmd := fmt.Sprintf("/bin/bash %s %s %s %v %d", targetScriptPath, user, volume, options.Create, options.Size)
cmd := fmt.Sprintf("/bin/bash %s %s %s %v %d %d", targetScriptPath, user, volume, options.Create, options.Size, options.Blocksize)
toolsConf := fmt.Sprintf(FORMAT_TOOLS_CONF, cc.GetClusterMDSAddr())

t.AddStep(&step.ListContainers{
Expand Down