Skip to content

Commit

Permalink
feat(lvol): add lvol detach parent API
Browse files Browse the repository at this point in the history
Longhorn 9922

Signed-off-by: Damiano Cipriani <[email protected]>
  • Loading branch information
DamiaSan committed Dec 16, 2024
1 parent 8e06fe7 commit 72cacd1
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
42 changes: 42 additions & 0 deletions app/cmd/basic/bdev_lvol.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func BdevLvolCmd() cli.Command {
BdevLvolCloneBdevCmd(),
BdevLvolSetParentCmd(),
BdevLvolDecoupleParentCmd(),
BdevLvolDetachParentCmd(),

Check warning on line 30 in app/cmd/basic/bdev_lvol.go

View check run for this annotation

Codecov / codecov/patch

app/cmd/basic/bdev_lvol.go#L30

Added line #L30 was not covered by tests
BdevLvolResizeCmd(),
BdevLvolStartShallowCopyCmd(),
BdevLvolCheckShallowCopyCmd(),
Expand Down Expand Up @@ -345,6 +346,47 @@ func bdevLvolDecoupleParent(c *cli.Context) error {
return util.PrintObject(decoupled)
}

func BdevLvolDetachParentCmd() cli.Command {
return cli.Command{
Name: "detach",
Flags: []cli.Flag{
cli.StringFlag{
Name: "alias",
Usage: "The alias of a lvol is <LVSTORE NAME>/<LVOL NAME>. Specify this or uuid",
},
cli.StringFlag{
Name: "uuid",
Usage: "Specify this or alias",
},
},
Usage: "detach a lvol from its parent lvol: \"detach --alias <LVSTORE NAME>/<LVOL NAME>\", or \"detach --uuid <LVOL UUID>\"",
Action: func(c *cli.Context) {
if err := bdevLvolDetachParent(c); err != nil {
logrus.WithError(err).Fatalf("Failed to run detach parent bdev lvol command")
}

Check warning on line 366 in app/cmd/basic/bdev_lvol.go

View check run for this annotation

Codecov / codecov/patch

app/cmd/basic/bdev_lvol.go#L349-L366

Added lines #L349 - L366 were not covered by tests
},
}
}

func bdevLvolDetachParent(c *cli.Context) error {
spdkCli, err := client.NewClient(context.Background())
if err != nil {
return err
}

Check warning on line 375 in app/cmd/basic/bdev_lvol.go

View check run for this annotation

Codecov / codecov/patch

app/cmd/basic/bdev_lvol.go#L371-L375

Added lines #L371 - L375 were not covered by tests

name := c.String("alias")
if name == "" {
name = c.String("uuid")
}

Check warning on line 380 in app/cmd/basic/bdev_lvol.go

View check run for this annotation

Codecov / codecov/patch

app/cmd/basic/bdev_lvol.go#L377-L380

Added lines #L377 - L380 were not covered by tests

decoupled, err := spdkCli.BdevLvolDetachParent(name)
if err != nil {
return err
}

Check warning on line 385 in app/cmd/basic/bdev_lvol.go

View check run for this annotation

Codecov / codecov/patch

app/cmd/basic/bdev_lvol.go#L382-L385

Added lines #L382 - L385 were not covered by tests

return util.PrintObject(decoupled)

Check warning on line 387 in app/cmd/basic/bdev_lvol.go

View check run for this annotation

Codecov / codecov/patch

app/cmd/basic/bdev_lvol.go#L387

Added line #L387 was not covered by tests
}

func BdevLvolSetParentCmd() cli.Command {
return cli.Command{
Name: "set-parent",
Expand Down
18 changes: 18 additions & 0 deletions pkg/spdk/client/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,24 @@ func (c *Client) BdevLvolDecoupleParent(name string) (decoupled bool, err error)
return decoupled, json.Unmarshal(cmdOutput, &decoupled)
}

// BdevLvolDetachParent detach the parent of a logical volume.
// No new clusters are allocated to the child blob, no data are copied from the parent to the child, so lvol's data are not modified.
// The parent must be a standard snapshot, not an external snapshot. All dependencies on the parent are removed
//
// "name": Required. UUID or alias of the logical volume to detach the parent of it. The alias of a lvol is <LVSTORE NAME>/<LVOL NAME>.
func (c *Client) BdevLvolDetachParent(name string) (decoupled bool, err error) {
req := spdktypes.BdevLvolDetachParentRequest{
Name: name,
}

cmdOutput, err := c.jsonCli.SendCommandWithLongTimeout("bdev_lvol_detach_parent", req)
if err != nil {
return false, err
}

Check warning on line 403 in pkg/spdk/client/basic.go

View check run for this annotation

Codecov / codecov/patch

pkg/spdk/client/basic.go#L395-L403

Added lines #L395 - L403 were not covered by tests

return decoupled, json.Unmarshal(cmdOutput, &decoupled)

Check warning on line 405 in pkg/spdk/client/basic.go

View check run for this annotation

Codecov / codecov/patch

pkg/spdk/client/basic.go#L405

Added line #L405 was not covered by tests
}

// BdevLvolSetParent sets a snapshot as the parent of a lvol, making the lvol a clone/child of this snapshot.
// The previous parent of the lvol can be another snapshot or an external snapshot, if the lvol is not a clone must be thin-provisioned.
// Lvol and parent snapshot must have the same size and must belong to the same lvol store.
Expand Down
4 changes: 4 additions & 0 deletions pkg/spdk/types/lvol.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ type BdevLvolDecoupleParentRequest struct {
Name string `json:"name"`
}

type BdevLvolDetachParentRequest struct {
Name string `json:"name"`
}

type BdevLvolSetParentRequest struct {
LvolName string `json:"lvol_name"`
ParentName string `json:"parent_name"`
Expand Down

0 comments on commit 72cacd1

Please sign in to comment.