Skip to content

Commit

Permalink
fix:[close #38] Remove LVM devices when labelling disk
Browse files Browse the repository at this point in the history
* Remove LVs before labelling disk

* Remove vgs and pvs too

* Run pvscan --cache on pvremove
  • Loading branch information
matbme authored Jan 29, 2024
1 parent 16f6a5c commit 9e421b3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
30 changes: 29 additions & 1 deletion core/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"strconv"
"strings"

"github.com/vanilla-os/albius/core/lvm"
)

const (
Expand Down Expand Up @@ -137,13 +139,39 @@ func (disk *Disk) waitForNewPartition() error {
func (disk *Disk) LabelDisk(label DiskLabel) error {
labelDiskCmd := "parted -s %s mklabel %s"

// Unmount partitions
for _, part := range disk.Partitions {
if err := part.UnmountPartition(); err != nil {
return fmt.Errorf("failed to unmount partition %s: %s", part.Path, err)
}
}

err := RunCommand(fmt.Sprintf(labelDiskCmd, disk.Path, label))
// Remove VGs and PVs belonging to disk
vgs, err := lvm.Vgs()
if err != nil {
return fmt.Errorf("failed to list vgs: %s", err)
}
pvsToRemove := []*lvm.Pv{}
for _, vg := range vgs {
for _, pv := range vg.Pvs {
if strings.Contains(pv.Path, disk.Path) {
pvsToRemove = append(pvsToRemove, &pv)
err = vg.Remove()
if err != nil {
return fmt.Errorf("failed to remove vg %s: %s", vg.Name, err)
}
break
}
}
}
for _, pv := range pvsToRemove {
err = pv.Remove()
if err != nil {
return fmt.Errorf("failed to remove pv %s: %s", pv.Path, err)
}
}

err = RunCommand(fmt.Sprintf(labelDiskCmd, disk.Path, label))
if err != nil {
return fmt.Errorf("failed to label disk: %s", err)
}
Expand Down
4 changes: 4 additions & 0 deletions core/lvm/lvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ func Pvremove(pv interface{}) error {
return fmt.Errorf("pvremove: %v", err)
}

for _, path := range pvPaths {
pvscanCache(path)
}

return nil
}

Expand Down

0 comments on commit 9e421b3

Please sign in to comment.