smarterase/bin/main.sh

132 lines
3.3 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#! /bin/bash
#set -e
APP_PATH=$( cd $(dirname $0) && pwd)
SRC_PATH=$( cd $(dirname $0)/../src && pwd)
cd $APP_PATH
clear
echo -e "## DEVICES\n"
DEVICES=$(lsblk /dev/sd? --nodeps --output NAME,MODEL,VENDOR,SIZE,TYPE,STATE)
echo "$DEVICES"
MOUNT_ROOT=$(mount |grep " / " |awk '{print $1}' |sed -r 's=/dev/(...)[0-9]$=\1=')
declare -a NOT_ROOT
while read device others; do
[[ "$device" != $MOUNT_ROOT ]] && [[ "$device" != "NAME" ]] && NOT_ROOT+=( $device )
done <<< "$DEVICES"
NOT_ROOT_STR=$( echo ${NOT_ROOT[@]} )
echo -e "\n## ROOT MOUNTED DEVICE\n$MOUNT_ROOT"
echo -e "\n## OTHER DEVICES\n${NOT_ROOT_STR}\n"
echo -e "\n## SMARTCTL TESTS"
read -e -p "Do you want to run tests? [y/N] " -n 1
REPLY=${REPLY:-N}
if [[ "N" != "${REPLY^^}" ]] ; then
echo -e "\n## SMARTCTL DISKS SELECTION"
echo -e "Do you want to USE ALL non root devices (empty reply)? \nOr else please type specific DEVICES NAMES to include (ex: '${NOT_ROOT_STR}')?"
read -e -p "Type your answer: "
CMD="$SRC_PATH/smartjson.py long "
if [[ -z "$REPLY" ]] ; then
CMD+=" -e $MOUNT_ROOT"
else
DEVICES=${REPLY}
for i in $DEVICES; do
CMD+=" -d $i"
done
fi
echo -e "\n## READY"
read -e -i Y -n 1 -p "About to run command '$CMD'. OK [Y/n]? "
# $CMD
START=$(date "+%s")
set +e
while true; do
clear
echo -e "\n## CHECKING STATUS"
date
echo -e "\nRunning since: $(( $( date +%s ) - $START )) seconds.\n"
$SRC_PATH/smartjson.py status && break
echo -e "\n## MANUAL HALT"
read -e -i Y -p "Stop ?" -t 15
[[ ${REPLY^^} == "Y" ]] && break
done
fi
echo -e "\n## SMARTCTL DISKS STATUS"
$SRC_PATH/smartjson.py list
echo -e "\n## DISKS BACKGROUND ERASE\nCaution: this might cause data loss."
read -e -p "Do you want to run a background erasure? [y/N] " -n 1
REPLY=${REPLY:-N}
if [[ "N" != "${REPLY^^}" ]] ; then
ERASE_DEVICES=""
while [[ -z "$ERASE_DEVICES" ]] ; do
echo "Please provide device names. Ex: 'sda sdc'"
read -e -p "Devices to erase: " -i "${NOT_ROOT_STR}" ERASE_DEVICES
declare -A DD_CMD
ERROR="false"
for f in $ERASE_DEVICES; do
if [[ ! -b /dev/$f ]] || [[ "$MOUNT_ROOT" == $f ]] ; then
echo "ERROR. $f is not a valid device."
ERROR="true"
break
fi
DD_CMD["$f"]="dd if=/dev/zero of=/dev/$f bs=512K"
done
if [[ "$ERROR" == "true" ]]; then
ERASE_DEVICES=""
continue
fi
done
echo -e "\n## CONFIRMATION\nYou are about to run the following commands."
for i in ${!DD_CMD[@]}; do
echo "Disk $i: ${DD_CMD[$i]}"
done
read -e -p "Please type 'Yes' to validate: " VALIDATE
if [[ "YES" != "${VALIDATE^^}" ]]; then
echo "EXIT"
exit
else
echo -e "\n## RUNNING ERASURE\n"
declare -A DD_PID
for i in ${!DD_CMD[@]}; do
${DD_CMD[$i]}&
DD_PID[$i]=$!
echo "$i PID ${DD_PID[$i]}..."
sleep 1
done
sleep 3
fi
fi
[[ -n ${DD_PID[@]} ]] && while true; do
clear
echo -e "\n## CHECKING STATUS"
FINISHED_COUNT=O
if [[ ${#DD_PID[@]} -ne 0 ]] ; then
for i in ${!DD_PID[@]}; do
PID=${DD_PID[$i]}
ps -f -p $PID &>/dev/null
R=$?
if [[ 0 -ne $R ]] ; then
MSG="Finished"
let $(( FINISHED_COUNT++ ))
else
MSG="Process running"
fi
echo "DISK $i PID $i : $MSG"
done
fi
[[ ${#DD_PID[@]} -eq $FINISHED_COUNT ]] && break
echo -e "\n## MANUAL HALT"
read -e -i Y -p "Stop ?" -t 15
[[ ${REPLY^^} == "Y" ]] && break
done