From 8276a1e9abf9607e82018c96001858d04420208e Mon Sep 17 00:00:00 2001 From: Vivek Goyal Date: Tue, 18 Apr 2017 16:25:52 -0400 Subject: [PATCH] Remove volume group and partitions from disk during reset When a storage configuration is removed, also remove the volume group and remove partitions from the disk. This is done only if container-storage-setup created a volume group. If a volume group is already found on the system at the time of storage creation, then it is left untouched and no disks are removed as well. Signed-off-by: Vivek Goyal Closes: #243 Approved by: rhatdan --- container-storage-setup.sh | 47 +++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/container-storage-setup.sh b/container-storage-setup.sh index 21cfaca..ed98c60 100755 --- a/container-storage-setup.sh +++ b/container-storage-setup.sh @@ -876,6 +876,38 @@ create_disk_partitions() { done } +remove_partition() { + local dev="$1" + + if [ -x "/usr/sbin/parted" ]; then + parted "$dev" rm 1 >/dev/null + else + sfdisk --delete "$dev" 1 >/dev/null + fi +} + +# Remove disk pvs and partitions. This is called in reset storage path. +# If partition or pv does not exist, it will still return success. Error +# will be returned only if pv or partition exists and removal fails. +remove_disk_pvs_parts() { + local devs="$1" part + + for dev in $devs; do + part=$(dev_query_first_child $dev) + [ -z "$part" ] && continue + + if ! remove_pv_if_exists $part; then + Error "Failed to remove physical volume label on device $part" + return 1 + fi + + if ! remove_partition $dev; then + Error "Failed to remove partition on device $dev" + return 1 + fi + done +} + create_extend_volume_group() { if [ -z "$_VG_EXISTS" ]; then vgcreate $VG $_PVS @@ -1790,7 +1822,7 @@ reset_extra_volume() { # Remove command processing reset_storage() { - local resolved_path + local resolved_path dev # Populate $_RESOLVED_MOUNT_DIR_PATH if [ -n "$_M_CONTAINER_ROOT_LV_MOUNT_PATH" ];then @@ -1812,6 +1844,19 @@ reset_storage() { fi fi + # If we created a volume group, remove volume group. + if [ "$_M_VG_CREATED" == "1" ];then + if ! remove_vg_if_exists "$_M_VG"; then + Error "Failed to remove volume group $_M_VG" + return 1 + fi + + # Cleanup any disks we added to volume group. + if ! remove_disk_pvs_parts "$_M_DEVS_RESOLVED";then + return 1 + fi + fi + # Get rid of config data rm -rf "$_CONFIG_DIR/$_CONFIG_NAME/" }