test new used drive filter

This commit is contained in:
rootzoll
2025-01-05 14:20:12 +01:00
parent d0c72a397f
commit a679e2cec6

View File

@@ -2,7 +2,7 @@
if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "-help" ]; then if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "-help" ]; then
>&2 echo "# managing the data drive(s) with new bootable setups for RaspberryPi, VMs and Laptops" >&2 echo "# managing the data drive(s) with new bootable setups for RaspberryPi, VMs and Laptops"
>&2 echo "# blitz.data.sh status # check if system is setup and what drives are used" >&2 echo "# blitz.data.sh status # check if system is setup and what drives are used"
>&2 echo "# blitz.data.sh explore # find the best drives to use for storage, system and data" >&2 echo "# blitz.data.sh layout # auto detect the old/best drives to use for storage, system and data"
>&2 echo "# blitz.data.sh setup" >&2 echo "# blitz.data.sh setup"
echo "error='missing parameters'" echo "error='missing parameters'"
exit 1 exit 1
@@ -64,88 +64,103 @@ if [ "$1" = "status" ]; then
fi fi
################### ###################
# EXPLORE # LAYOUT
# find the best drives to use for storage, system and data # auto detect the old/best drives to use for storage, system and data
################### ###################
if [ "$1" = "explore" ]; then if [ "$1" = "layout" ]; then
# array of device names to exclude # initial values for drives to determine
alreadyUsedDevices=("sda") storageDevice=""
systemDevice=""
dataDevice=""
# get a list of all connected drives >63GB ordered by size (biggest first) without already used devices # get a list of all connected drives >63GB ordered by size (biggest first)
listOfDevices=$(lsblk -dno NAME,SIZE | grep -E "^(sd|nvme)" | \ listOfDevices=$(lsblk -dno NAME,SIZE | grep -E "^(sd|nvme)" | \
awk -v exclude="$(printf "%s|" "${alreadyUsedDevices[@]}" | sed 's/|$//')" ' awk '{
BEGIN {
split(exclude, excludeArray, "|")
}
{
size=$2 size=$2
if(size ~ /T/) { if(size ~ /T/) {
sub("T","",size) sub("T","",size); size=size*1024
size=size*1024
} else if(size ~ /G/) { } else if(size ~ /G/) {
sub("G","",size) sub("G","",size); size=size*1
size=size*1
} else if(size ~ /M/) { } else if(size ~ /M/) {
sub("M","",size) sub("M","",size); size=size/1024
size=size/1024
}
excludeDevice=0
for (dev in excludeArray) {
if ($1 == excludeArray[dev]) {
excludeDevice=1
break
}
}
if (size >= 63 && excludeDevice == 0) {
printf "%s %.0f\n", $1, size
} }
if (size >= 63) printf "%s %.0f\n", $1, size
}' | sort -k2,2nr -k1,1 ) }' | sort -k2,2nr -k1,1 )
echo "listOfDevices='${listOfDevices}'"
# take the biggest drive as the storage drive # remove lines with already used drives
_storageDevice=$(echo "${listOfDevices}" | head -n1 | awk '{print $1}') if [ -n "${storageDevice}" ]; then
_storageSizeGB=$(echo "${listOfDevices}" | head -n1 | awk '{print $2}') listOfDevices=$(echo "${listOfDevices}" | grep -v "${storageDevice}")
fi
if [ -n "${systemDevice}" ]; then
listOfDevices=$(echo "${listOfDevices}" | grep -v "${systemDevice}")
fi
if [ -n "${dataDevice}" ]; then
listOfDevices=$(echo "${listOfDevices}" | grep -v "${dataDevice}")
fi
echo "listOfDevices='${listOfDevices}'"
# take the second biggest drive as the system drive (only in VM setups) # Set STORAGE
_systemDevice=$(echo "${listOfDevices}" | sed -n '2p' | awk '{print $1}') if [ ${#storageDevice} -eq 0 ]; then
_systemSizeGB=$(echo "${listOfDevices}" | sed -n '2p' | awk '{print $2}') # when no storage device yet: take the biggest drive as the storage drive
storageDevice=$(echo "${listOfDevices}" | head -n1 | awk '{print $1}')
storageSizeGB=$(echo "${listOfDevices}" | head -n1 | awk '{print $2}')
# remove the storage device from the list
listOfDevices=$(echo "${listOfDevices}" | grep -v "${storageDevice}")
fi
# if there is no spereated system drive # Set SYSTEM
_bootFromStorage=0 bootFromStorage=0 # signales if there is no extra system drive add boot partition to storage drive
_bootFromSD=0 bootFromSD=0 # signales if there is no extra system drive keep booting from SD card (only RaspberryPi)
if [ ${#systemDevice} -eq 0 ]; then
# when no system device yet: take the next biggest drive as the system drive
systemDevice=$(echo "${listOfDevices}" | head -n1 | awk '{print $1}')
systemSizeGB=$(echo "${listOfDevices}" | head -n1 | awk '{print $2}')
# remove the system device from the list
listOfDevices=$(echo "${listOfDevices}" | grep -v "${systemDevice}")
fi
# if there is was no spereated system drive left
if [ ${#systemDevice} -eq 0 ]; then if [ ${#systemDevice} -eq 0 ]; then
if [ "${computerType}" = "raspberrypi" ] && [ ${gotNVMe} = "0" ]; then if [ "${computerType}" = "raspberrypi" ] && [ ${gotNVMe} = "0" ]; then
# if its a RaspberryPi with a USB drive - keep system drive empty and keep booting from SD # if its a RaspberryPi with a USB drive - keep system drive empty and keep booting from SD
_bootFromSD=1 bootFromSD=1
else else
# all other like VM, RaspberryPi with a NVMe or a laptop - use the storage drive as system drive # all other like VM, RaspberryPi with a NVMe or a laptop - use the storage drive as system drive
_bootFromStorage=1 bootFromStorage=1
fi fi
fi fi
# take the third biggest drive as the data drive (only in VM setups) # Set DATA
_dataDevice=$(echo "${listOfDevices}" | sed -n '3p' | awk '{print $1}') if [ ${#dataDevice} -eq 0 ]; then
_dataSizeGB=$(echo "${listOfDevices}" | sed -n '3p' | awk '{print $2}') # when no data device yet: take the second biggest drive as the data drive
dataDevice=$(echo "${listOfDevices}" | head -n1 | awk '{print $1}')
dataSizeGB=$(echo "${listOfDevices}" | head -n1 | awk '{print $2}')
# remove the data device from the list
listOfDevices=$(echo "${listOfDevices}" | grep -v "${dataDevice}")
fi
# count remaining devices
remainingDevices=$(echo "${listOfDevices}" | wc -l)
# if there is no spereated data drive - run combine data & storage partiton # if there is no spereated data drive - run combine data & storage partiton
_combinedDataStorage=0
if [ ${#dataDevice} -eq 0 ]; then if [ ${#dataDevice} -eq 0 ]; then
_combinedDataStorage=1 combinedDataStorage=1
fi fi
# output the result # output the result
echo "_storageDevice='${storageDevice}'" echo "storageDevice='${storageDevice}'"
echo "_storageSizeGB='${storageSizeGB}'" echo "storageSizeGB='${storageSizeGB}'"
echo "_systemDevice='${systemDevice}'" echo "storageRecoverPartition='${storageRecoverPartition}'"
echo "_systemSizeGB='${systemSizeGB}'" echo "systemDevice='${systemDevice}'"
echo "_dataDevice='${dataDevice}'" echo "systemSizeGB='${systemSizeGB}'"
echo "_dataSizeGB='${dataSizeGB}'" echo "dataDevice='${dataDevice}'"
echo "_combinedDataStorage='${combinedDataStorage}'" echo "dataSizeGB='${dataSizeGB}'"
echo "_bootFromStorage='${bootFromStorage}'" echo "combinedDataStorage='${combinedDataStorage}'"
echo "_bootFromSD='${bootFromSD}'" echo "bootFromStorage='${bootFromStorage}'"
echo "bootFromSD='${bootFromSD}'"
echo "remainingDevices='${remainingDevices}'"
exit 0 exit 0
fi fi