#1427 HDD formatting (#1585)

This commit is contained in:
Christian Rotzoll 2020-09-23 17:15:09 +02:00 committed by rootzoll
parent bcb7706cb3
commit 69a093dc94
2 changed files with 24 additions and 8 deletions

View File

@ -127,7 +127,7 @@ THIS WILL DELETE ALL DATA ON THAT DEVICE!
if [ ${isDeviceName} -eq 1 ]; then
hdd="${uuid}"
# check if mounted
checkIfAlreadyMounted=$(lsblk | grep "${hdd}" | grep -c '/mnt/')
checkIfAlreadyMounted=$(lsblk -o NAME,UUID,MOUNTPOINT | grep "${hdd}" | grep -c '/mnt/')
if [ ${checkIfAlreadyMounted} -gt 0 ]; then
echo "# cannot format a device that is mounted"
echo "error='device is in use'"
@ -136,7 +136,9 @@ THIS WILL DELETE ALL DATA ON THAT DEVICE!
echo "# OK found device name ${hdd} that will now be formatted ..."
echo "# Wiping all partitions (sfdisk/wipefs)"
sudo sfdisk --delete /dev/${hdd} 1>&2
sleep 4
sudo wipefs -a /dev/${hdd} 1>&2
sleep 4
partitions=$(lsblk | grep -c "${hdd}")
if [ ${partitions} -gt 0 ]; then
echo "# WARNING: partitions are still not clean"

View File

@ -330,10 +330,14 @@ if [ "$1" = "format" ]; then
>&2 echo "# Unmounting all partitions of this device"
# remove device from all system mounts (also fstab)
lsblk -o NAME,UUID | grep "${hdd}" | awk '$1=$1' | cut -d " " -f 2 | grep "-" | while read -r uuid ; do
>&2 echo "# Cleaning /etc/fstab from ${uuid}"
sudo sed -i "/UUID=${uuid}/d" /etc/fstab
sync
lsblk -o UUID,NAME | grep "${hdd}" | cut -d " " -f 1 | grep "-" | while read -r uuid ; do
if [ ${#uuid} -gt 0 ]; then
>&2 echo "# Cleaning /etc/fstab from ${uuid}"
sudo sed -i "/UUID=${uuid}/d" /etc/fstab
sync
else
>&2 echo "# skipping empty result"
fi
done
sudo mount -a
@ -344,16 +348,19 @@ if [ "$1" = "format" ]; then
sudo umount /mnt/storage 2>/dev/null
unmounted1=$(df | grep -c "/mnt/hdd")
if [ ${unmounted1} -gt 0 ]; then
>&2 echo "# ERROR: failed to unmount /mnt/hdd"
echo "error='failed to unmount /mnt/hdd'"
exit 1
fi
unmounted2=$(df | grep -c "/mnt/temp")
if [ ${unmounted2} -gt 0 ]; then
>&2 echo "# ERROR: failed to unmount /mnt/temp"
echo "error='failed to unmount /mnt/temp'"
exit 1
fi
unmounted3=$(df | grep -c "/mnt/storage")
if [ ${unmounted3} -gt 0 ]; then
>&2 echo "# ERROR: failed to unmount /mnt/storage"
echo "error='failed to unmount /mnt/storage'"
exit 1
fi
@ -362,7 +369,9 @@ if [ "$1" = "format" ]; then
# wipe all partitions and write fresh GPT
>&2 echo "# Wiping all partitions (sfdisk/wipefs)"
sudo sfdisk --delete /dev/${hdd}
sleep 4
sudo wipefs -a /dev/${hdd}
sleep 4
partitions=$(lsblk | grep -c "${hdd}")
if [ ${partitions} -gt 0 ]; then
>&2 echo "# WARNING: partitions are still not clean - try Quick & Dirty"
@ -370,10 +379,11 @@ if [ "$1" = "format" ]; then
fi
partitions=$(lsblk | grep -c "${hdd}")
if [ ${partitions} -gt 0 ]; then
>&2 echo "# ERROR: partition cleaning failed"
echo "error='partition cleaning failed'"
exit 1
fi
sudo parted -s /dev/${hdd} mklabel gpt 1>/dev/null
sudo parted -s /dev/${hdd} mklabel gpt 1>/dev/null 1>&2
sleep 2
sync
@ -385,7 +395,7 @@ if [ "$1" = "format" ]; then
# write new EXT4 partition
>&2 echo "# Creating the one big partition"
sudo parted /dev/${hdd} mkpart primary ext4 0% 100% 1>/dev/null 2>/dev/null
sudo parted /dev/${hdd} mkpart primary ext4 0% 100% 1>&2
sleep 6
sync
# loop until the partion gets available
@ -399,15 +409,18 @@ if [ "$1" = "format" ]; then
loopdone=$(lsblk -o NAME | grep -c ${hdd}1)
loopcount=$(($loopcount +1))
if [ ${loopcount} -gt 10 ]; then
>&2 echo "# partion failed"
echo "error='partition failed'"
exit 1
fi
done
>&2 echo "# partion available"
# make sure /mnt/hdd is unmounted before formatting
sudo umount -f /tmp/ext4 2>/dev/null
unmounted=$(df | grep -c "/tmp/ext4")
if [ ${unmounted} -gt 0 ]; then
>&2 echo "# ERROR: failed to unmount /tmp/ext4"
echo "error='failed to unmount /tmp/ext4'"
exit 1
fi
@ -424,6 +437,7 @@ if [ "$1" = "format" ]; then
loopdone=$(lsblk -o NAME,LABEL | grep -c BLOCKCHAIN)
loopcount=$(($loopcount +1))
if [ ${loopcount} -gt 10 ]; then
>&2 echo "# ERROR: formatting ext4 failed"
echo "error='formatting ext4 failed'"
exit 1
fi
@ -655,7 +669,7 @@ if [ "$1" = "fstab" ]; then
fi
# remove old entries from fstab
lsblk -o NAME,UUID | grep "${hdd}" | awk '$1=$1' | cut -d " " -f 2 | grep "-" | while read -r uuid ; do
lsblk -o UUID,NAME | grep "${hdd}" | cut -d " " -f 1 | grep "-" | while read -r uuid ; do
>&2 echo "# Cleaning /etc/fstab from ${uuid}"
sudo sed -i "/UUID=${uuid}/d" /etc/fstab
sync