Update wipedisk for LVM based rootfs

Now that the root filesystem is based on an LVM logical volume, discover
the root disk by searching for the boot partition.

Changes include:
 - remove detection of rootfs_part/rootfs and adjust rootfs related
   references with boot_disk.
 - run bashate on the script and resolve indentation and syntax related
   errors. Leave long-line errors alone for improved readability.

Test Plan:
PASS - run 'wipedisk', answer prompts, and ensure all partitions are
       cleaned up except for the platform backup partition
PASS - run 'wipedisk --include-backup', answer prompts, and ensure all
       partitions are cleaned up
PASS - run 'wipedisk --include-backup --force' and ensure all partitions
       are cleaned up

Change-Id: I036ce745353b6a26bc2615ffc6e3b8955b4dd1ec
Closes-Bug: #1998204
Signed-off-by: Robert Church <robert.church@windriver.com>
This commit is contained in:
Robert Church 2022-11-29 03:31:50 -06:00
parent b0066dcd27
commit 1796ed8740
1 changed files with 42 additions and 55 deletions

View File

@ -1,6 +1,6 @@
#! /bin/bash #!/bin/bash
# #
# Copyright (c) 2013-2017 Wind River Systems, Inc. # Copyright (c) 2013-2022 Wind River Systems, Inc.
# #
# SPDX-License-Identifier: Apache-2.0 # SPDX-License-Identifier: Apache-2.0
# #
@ -15,7 +15,7 @@ usage ()
echo "Options:" echo "Options:"
echo " -h display this help" echo " -h display this help"
echo " --force do not ask for confirmation" echo " --force do not ask for confirmation"
echo " --include-backup also removes data from platform backup directory" echo " --include-backup removes data from platform backup directory"
exit 1 exit 1
} }
@ -24,26 +24,25 @@ usage ()
# because remounting deleted filesystems at shutdown will throw errors # because remounting deleted filesystems at shutdown will throw errors
unmount_fs() unmount_fs()
{ {
local fs=$1 local fs=$1
local ret_code=0 local ret_code=0
echo "Trying to unmount $fs" echo "Trying to unmount $fs"
if findmnt $fs > /dev/null 2>&1 ; then if findmnt $fs > /dev/null 2>&1 ; then
if umount -f $fs ; then if umount -f $fs ; then
echo "$fs has been successfully unmounted" echo "$fs has been successfully unmounted"
else else
echo "Error! Failed to unmount $fs" echo "Error! Failed to unmount $fs"
ret_code=1 ret_code=1
fi fi
else else
echo "Warning! $fs is not mounted" echo "Warning! $fs is not mounted"
ret_code=2 ret_code=2
fi fi
return $ret_code return $ret_code
} }
OPTS=`getopt -o h -l force,include-backup -- "$@"` OPTS=`getopt -o h -l force,include-backup -- "$@"`
if [ $? != 0 ] if [ $? != 0 ] ; then
then
exit 1 exit 1
fi fi
@ -58,61 +57,54 @@ while true ; do
esac esac
done done
if [ $# != 0 ] if [ $# != 0 ] ; then
then
echo "Invalid argument. Use -h for help." echo "Invalid argument. Use -h for help."
exit 1 exit 1
fi fi
declare WIPE_HDD= declare WIPE_HDD=
# Only wipe the rootfs and boot device disks # Only wipe the boot device disks
rootfs_part=$(df --output=source / | tail -1)
rootfs=$(readlink -f $(find -L /dev/disk/by-path/ -samefile $rootfs_part | sed 's/-part[0-9]*'//))
boot_disk_part=$(df --output=source /boot | tail -1) boot_disk_part=$(df --output=source /boot | tail -1)
boot_disk=$(readlink -f $(find -L /dev/disk/by-path/ -samefile $boot_disk_part | sed 's/-part[0-9]*'//)) boot_disk=$(readlink -f $(find -L /dev/disk/by-path/ -samefile $boot_disk_part | sed 's/-part[0-9]*'//))
WIPE_HDD=$rootfs if [ -z "$boot_disk" ] ; then
if [ "$rootfs" != "$boot_disk" ] echo "Boot disk not found. Failed to wipe disk."
then exit 1
WIPE_HDD="$WIPE_HDD $boot_disk" else
WIPE_HDD="$boot_disk"
fi fi
# Due to dynamic partitioning, volume groups can have PVs across multiple disks. # Due to dynamic partitioning, volume groups can have PVs across multiple disks.
# When deleting the rootfs, we should also delete all PVs (across all disks) that # When deleting the boot disk volume groups, we should also delete all PVs
# are part of volume groups that are also present on the rootfs. # (across all disks) that are part of volume groups that are also present on the
rootfs_vgs=$(pvdisplay -C --separator ' | ' -o pv_name,vg_name | grep $rootfs | awk '{print $3}' | sort -u) # boot disk.
boot_disk_vgs=$(pvdisplay -C --separator ' | ' -o pv_name,vg_name | grep $boot_disk | awk '{print $3}' | sort -u)
pvs_to_delete="" pvs_to_delete=""
for vg in $rootfs_vgs for vg in $boot_disk_vgs ; do
do
pv=$(pvdisplay --select "vg_name=$vg" | awk '/PV Name/{print $3}') pv=$(pvdisplay --select "vg_name=$vg" | awk '/PV Name/{print $3}')
pvs_to_delete="$pvs_to_delete $pv" pvs_to_delete="$pvs_to_delete $pv"
done done
WIPE_HDD="$pvs_to_delete $WIPE_HDD" WIPE_HDD="$pvs_to_delete $WIPE_HDD"
if [ ! $FORCE ] if [ ! $FORCE ] ; then
then
echo "This will result in the loss of all data on the hard drives and" echo "This will result in the loss of all data on the hard drives and"
echo "will require this node to be re-installed." echo "will require this node to be re-installed."
echo "The following disks will be wiped:" echo "The following disks will be wiped:"
for dev in $WIPE_HDD for dev in $WIPE_HDD ; do
do
echo " $dev" echo " $dev"
done | sort done | sort
echo echo
read -p "Are you absolutely sure? [y/n] " -r read -p "Are you absolutely sure? [y/n] " -r
if [[ ! $REPLY =~ ^[Yy]$ ]] if [[ ! $REPLY =~ ^[Yy]$ ]] ; then
then
echo "Aborted" echo "Aborted"
exit 1 exit 1
fi fi
read -p "Type 'wipediskscompletely' to confirm: " -r read -p "Type 'wipediskscompletely' to confirm: " -r
if [[ ! $REPLY = "wipediskscompletely" ]] if [[ ! $REPLY = "wipediskscompletely" ]] ; then
then
echo "Aborted" echo "Aborted"
exit 1 exit 1
fi fi
@ -127,14 +119,11 @@ part_type_guid_str="Partition GUID code"
# get the nodetype variable to check later if this node is a controller # get the nodetype variable to check later if this node is a controller
. /etc/platform/platform.conf . /etc/platform/platform.conf
for dev in $WIPE_HDD for dev in $WIPE_HDD ; do
do if [[ -e $dev ]] ; then
if [[ -e $dev ]] if [[ "$dev" == "$boot_disk" && "${nodetype}" == "controller" ]] ; then
then
if [[ "$dev" == "$rootfs" && "${nodetype}" == "controller" ]]
then
part_numbers=( $(parted -s $dev print | awk '$1 == "Number" {i=1; next}; i {print $1}') ) part_numbers=( $(parted -s $dev print | awk '$1 == "Number" {i=1; next}; i {print $1}') )
for part_number in "${part_numbers[@]}"; do for part_number in "${part_numbers[@]}" ; do
part=$dev$part_number part=$dev$part_number
case $part in case $part in
*"nvme"*) *"nvme"*)
@ -143,7 +132,7 @@ do
esac esac
sgdisk_part_info=$(flock $dev sgdisk -i $part_number $dev) sgdisk_part_info=$(flock $dev sgdisk -i $part_number $dev)
part_type_guid=$(echo "$sgdisk_part_info" | grep "$part_type_guid_str" | awk '{print $4;}') part_type_guid=$(echo "$sgdisk_part_info" | grep "$part_type_guid_str" | awk '{print $4;}')
if [[ "$part_type_guid" == $BACKUP_PART_GUID && ! $INCLUDE_BACKUP ]]; then if [[ "$part_type_guid" == $BACKUP_PART_GUID && ! $INCLUDE_BACKUP ]] ; then
echo "Skipping wipe backup partition $part..." echo "Skipping wipe backup partition $part..."
continue continue
fi fi
@ -153,8 +142,7 @@ do
# Delete the first few bytes at the start and end of the partition. This is required with # Delete the first few bytes at the start and end of the partition. This is required with
# GPT partitions, they save partition info at the start and the end of the block. # GPT partitions, they save partition info at the start and the end of the block.
# Skip / or we will lose access to the tools on the system. # Skip / or we will lose access to the tools on the system.
if [[ $part != $rootfs_part ]] if [[ $part != $boot_disk_part ]] ; then
then
unmount_fs $part unmount_fs $part
dd if=/dev/zero of=$part bs=512 count=34 dd if=/dev/zero of=$part bs=512 count=34
dd if=/dev/zero of=$part bs=512 count=34 seek=$((`blockdev --getsz $part` - 34)) dd if=/dev/zero of=$part bs=512 count=34 seek=$((`blockdev --getsz $part` - 34))
@ -180,8 +168,7 @@ do
fi fi
done done
if [[ -z $WIPE_HDD ]] if [[ -z $WIPE_HDD ]] ; then
then
echo "No disks were detected." echo "No disks were detected."
else else
sync sync