From be11cbca1e0bc4db43e2c307e2ba5fc225db9b9a Mon Sep 17 00:00:00 2001 From: Mark D Horn Date: Tue, 10 Dec 2019 17:01:55 -0800 Subject: [PATCH] storage: remove logical volumes during wipe Fixes: #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 --- locale/en_US/LC_MESSAGES/clr-installer.po | 7 ++ locale/es_MX/LC_MESSAGES/clr-installer.po | 7 ++ locale/zh_CN/LC_MESSAGES/clr-installer.po | 7 ++ storage/ops.go | 78 +++++++++++++++++++++++ storage/storage.go | 3 + 5 files changed, 102 insertions(+) diff --git a/locale/en_US/LC_MESSAGES/clr-installer.po b/locale/en_US/LC_MESSAGES/clr-installer.po index e85829f..136b864 100644 --- a/locale/en_US/LC_MESSAGES/clr-installer.po +++ b/locale/en_US/LC_MESSAGES/clr-installer.po @@ -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" @@ -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." diff --git a/locale/es_MX/LC_MESSAGES/clr-installer.po b/locale/es_MX/LC_MESSAGES/clr-installer.po index 3c8bde3..5688c2e 100644 --- a/locale/es_MX/LC_MESSAGES/clr-installer.po +++ b/locale/es_MX/LC_MESSAGES/clr-installer.po @@ -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" @@ -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." diff --git a/locale/zh_CN/LC_MESSAGES/clr-installer.po b/locale/zh_CN/LC_MESSAGES/clr-installer.po index edb5e5f..c0e0278 100644 --- a/locale/zh_CN/LC_MESSAGES/clr-installer.po +++ b/locale/zh_CN/LC_MESSAGES/clr-installer.po @@ -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" @@ -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 "警告: 选定的媒介将被删除。" diff --git a/storage/ops.go b/storage/ops.go index 94da63d..64dddec 100644 --- a/storage/ops.go +++ b/storage/ops.go @@ -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 { @@ -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 diff --git a/storage/storage.go b/storage/storage.go index 353d007..644770a 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -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."