Skip to content
Open
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
28 changes: 23 additions & 5 deletions cmd/provisioner/deletelv.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package main

import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"path"
"strings"

"github.com/google/lvmd/commands"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -60,23 +62,40 @@ func deleteLV(c *cli.Context) error {

klog.Infof("delete lv %s vg:%s dir:%s block:%t", lvName, vgName, dirName, blockMode)

output, err := umountLV(lvName, vgName, dirName)
found := false
lvs, err := commands.ListLV(context.Background(), vgName)
if err != nil {
return fmt.Errorf("unable to delete lv: %w output:%s", err, output)
return fmt.Errorf("unable to list existing logicalvolumes:%v", err)
}
for _, lv := range lvs {
if strings.Contains(lv.Name, lvName) {
found = true
break
}
}
if !found {
klog.Infof("lv %s not found anymore", lvName)
return nil
}

output, err = commands.RemoveLV(context.Background(), vgName, lvName)
umountLV(lvName, vgName, dirName)

output, err := commands.RemoveLV(context.Background(), vgName, lvName)
if err != nil {
return fmt.Errorf("unable to delete lv: %w output:%s", err, output)
}
klog.Infof("lv %s vg:%s deleted", lvName, vgName)
return nil
}

func umountLV(lvname, vgname, directory string) (string, error) {
func umountLV(lvname, vgname, directory string) {
lvPath := fmt.Sprintf("/dev/%s/%s", vgname, lvname)
mountPath := path.Join(directory, lvname)

if _, err := os.Stat(mountPath); errors.Is(err, os.ErrNotExist) {
klog.Infof("mount point %s not found anymore", mountPath)
return
}
cmd := exec.Command("umount", "--lazy", "--force", mountPath)
out, err := cmd.CombinedOutput()
if err != nil {
Expand All @@ -86,5 +105,4 @@ func umountLV(lvname, vgname, directory string) (string, error) {
if err != nil {
klog.Errorf("unable to remove mount directory:%s err:%w", mountPath, err)
}
return "", nil
}