Skip to content

Commit

Permalink
storage: remove logical volumes during wipe
Browse files Browse the repository at this point in the history
Fixes: clearlinux#623

If a whole disk is used that contains logical volumes, they
need to be removed before re-using the disk.

Signed-off-by: Mark D Horn <[email protected]>
  • Loading branch information
mdhorn committed Dec 11, 2019
1 parent 767ffb8 commit be11cbc
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 0 deletions.
7 changes: 7 additions & 0 deletions locale/en_US/LC_MESSAGES/clr-installer.po
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ msgstr "Test connectivity"
msgid "Test Network Settings"
msgstr "Test Network Settings"

#, c-format
msgid "Removing logical disk volume: %s"
msgstr "Removing logical disk volume: %s"

#, c-format
msgid "Writing partition table to: %s"
msgstr "Writing partition table to: %s"
Expand Down Expand Up @@ -508,6 +512,9 @@ msgstr "Selected media will have partitions added."
msgid "WARNING: New Partition table will be created."
msgstr "WARNING: New Partition table will be created."

msgid "WARNING: Logical Volume will be removed."
msgstr "WARNING: Logical Volume will be removed."

msgid "WARNING: Selected media will be erased."
msgstr "WARNING: Selected media will be erased."

Expand Down
7 changes: 7 additions & 0 deletions locale/es_MX/LC_MESSAGES/clr-installer.po
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ msgstr "Conectividad de prueba"
msgid "Test Network Settings"
msgstr "Probar la configuración de red"

#, c-format
msgid "Removing logical disk volume: %s"
msgstr "Eliminando volumen de disco lógico: %s"

#, c-format
msgid "Writing partition table to: %s"
msgstr "Escribiendo la tabla de particiones en %s"
Expand Down Expand Up @@ -508,6 +512,9 @@ msgstr "Se añadirán particiones a los medios seleccionados."
msgid "WARNING: New Partition table will be created."
msgstr "ADVERTENCIA: se creará una nueva tabla de particiones."

msgid "WARNING: Logical Volume will be removed."
msgstr "ADVERTENCIA: se eliminará el volumen lógico."

msgid "WARNING: Selected media will be erased."
msgstr "ADVERTENCIA: los medios seleccionados se borrarán."

Expand Down
7 changes: 7 additions & 0 deletions locale/zh_CN/LC_MESSAGES/clr-installer.po
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ msgstr "测试连接"
msgid "Test Network Settings"
msgstr "测试网络设置"

#, c-format
msgid "Removing logical disk volume: %s"
msgstr "正在删除逻辑磁盘卷:% s"

#, c-format
msgid "Writing partition table to: %s"
msgstr "正在将分区表写入:%s"
Expand Down Expand Up @@ -508,6 +512,9 @@ msgstr "选定的媒介将添加分区。"
msgid "WARNING: New Partition table will be created."
msgstr "警告: 将创建新的分区表。"

msgid "WARNING: Logical Volume will be removed."
msgstr "警告:逻辑卷将被删除。"

msgid "WARNING: Selected media will be erased."
msgstr "警告: 选定的媒介将被删除。"

Expand Down
78 changes: 78 additions & 0 deletions storage/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,78 @@ func getStartEndMB(start uint64, end uint64) string {
return strStart + " " + strEnd
}

func (bd *BlockDevice) removeLogicalVolume() error {
if bd.Type != BlockDeviceTypeLVM2Volume {
return errors.Errorf("Block Type is not logical volume")
}

mesg := utils.Locale.Get("Removing logical disk volume: %s", bd.Name)
prg := progress.NewLoop(mesg)
log.Info(mesg)
args := []string{
"dmsetup",
"remove",
bd.Name,
}

err := cmd.RunAndLog(args...)
if err != nil {
return errors.Wrap(err)
}

prg.Success()

return nil
}

// findLogicalVolumes finds lvm2 volumes defined on this block device
// called by WritePartitionTable to ensure we properly remove logical volumes
// prior to wiping the disk partition table.
func findLogicalVolumes(dryRun *[]string, blockDevices []*BlockDevice) error {
var err error

for _, bd := range blockDevices {
if bd.Type == BlockDeviceTypeLVM2Volume {
if dryRun != nil {
*dryRun = append(*dryRun, bd.Name+": "+utils.Locale.Get(LogicalVolumeWarning))
} else {
if err = bd.removeLogicalVolume(); err != nil {
break
}
}
}

if bd.Children != nil {
if err = findLogicalVolumes(dryRun, bd.Children); err != nil {
break
}
}
}

return err
}

func (bd *BlockDevice) removeAllLogicalVolumes(dryRun *[]string) error {
var err error
w := bytes.NewBuffer(nil)

err = cmd.Run(w, lsblkBinary, "-J", "-b", "-O", bd.GetDeviceFile())
if err != nil {
return fmt.Errorf("%s", w.String())
}

bds, err := parseBlockDevicesDescriptor(w.Bytes())
if err != nil {
return err
}

if err := findLogicalVolumes(dryRun, bds); err != nil {
return err
}

return nil
}

// WritePartitionLabel make a device a 'gpt' partition type
// Only call when we are wiping and reusing the entire disk
func (bd *BlockDevice) WritePartitionLabel() error {
Expand Down Expand Up @@ -271,6 +343,12 @@ func (bd *BlockDevice) WritePartitionTable(legacyBios bool, wholeDisk bool, dryR
return errors.Errorf("Type is partition, disk required")
}

if wholeDisk {
if err := bd.removeAllLogicalVolumes(dryRun); err != nil {
return err
}
}

var prg progress.Progress

//write the partition label
Expand Down
3 changes: 3 additions & 0 deletions storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ const (
// PartitioningWarning specifies the warning message for writing partition table
PartitioningWarning = "WARNING: New Partition table will be created."

// LogicalVolumeWarning specifies the warning message when removing a logical volume
LogicalVolumeWarning = "WARNING: Logical Volume will be removed."

// DestructiveWarning specifies the warning message for destructive installation
DestructiveWarning = "WARNING: Selected media will be erased."

Expand Down

0 comments on commit be11cbc

Please sign in to comment.