From a679e2cec6776b4fe39c1880198ac266e1fa448d Mon Sep 17 00:00:00 2001 From: rootzoll Date: Sun, 5 Jan 2025 14:20:12 +0100 Subject: [PATCH] test new used drive filter --- home.admin/config.scripts/blitz.data.sh | 139 +++++++++++++----------- 1 file changed, 77 insertions(+), 62 deletions(-) diff --git a/home.admin/config.scripts/blitz.data.sh b/home.admin/config.scripts/blitz.data.sh index f1862f2e0..fa546e562 100644 --- a/home.admin/config.scripts/blitz.data.sh +++ b/home.admin/config.scripts/blitz.data.sh @@ -2,7 +2,7 @@ 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 "# 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" echo "error='missing parameters'" exit 1 @@ -64,88 +64,103 @@ if [ "$1" = "status" ]; then fi ################### -# EXPLORE -# find the best drives to use for storage, system and data +# LAYOUT +# 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 - alreadyUsedDevices=("sda") - - # get a list of all connected drives >63GB ordered by size (biggest first) without already used devices + # initial values for drives to determine + storageDevice="" + systemDevice="" + dataDevice="" + + # get a list of all connected drives >63GB ordered by size (biggest first) listOfDevices=$(lsblk -dno NAME,SIZE | grep -E "^(sd|nvme)" | \ - awk -v exclude="$(printf "%s|" "${alreadyUsedDevices[@]}" | sed 's/|$//')" ' - BEGIN { - split(exclude, excludeArray, "|") + awk '{ + size=$2 + if(size ~ /T/) { + sub("T","",size); size=size*1024 + } else if(size ~ /G/) { + sub("G","",size); size=size*1 + } else if(size ~ /M/) { + sub("M","",size); size=size/1024 } - { - size=$2 - if(size ~ /T/) { - sub("T","",size) - size=size*1024 - } else if(size ~ /G/) { - sub("G","",size) - size=size*1 - } else if(size ~ /M/) { - sub("M","",size) - 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 - } - }' | sort -k2,2nr -k1,1) + if (size >= 63) printf "%s %.0f\n", $1, size + }' | sort -k2,2nr -k1,1 ) + echo "listOfDevices='${listOfDevices}'" - # take the biggest drive as the storage drive - _storageDevice=$(echo "${listOfDevices}" | head -n1 | awk '{print $1}') - _storageSizeGB=$(echo "${listOfDevices}" | head -n1 | awk '{print $2}') - - # take the second biggest drive as the system drive (only in VM setups) - _systemDevice=$(echo "${listOfDevices}" | sed -n '2p' | awk '{print $1}') - _systemSizeGB=$(echo "${listOfDevices}" | sed -n '2p' | awk '{print $2}') + # remove lines with already used drives + if [ -n "${storageDevice}" ]; then + 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}'" - # if there is no spereated system drive - _bootFromStorage=0 - _bootFromSD=0 + # Set STORAGE + if [ ${#storageDevice} -eq 0 ]; then + # 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 + + # Set SYSTEM + bootFromStorage=0 # signales if there is no extra system drive add boot partition to storage drive + 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 [ "${computerType}" = "raspberrypi" ] && [ ${gotNVMe} = "0" ]; then # if its a RaspberryPi with a USB drive - keep system drive empty and keep booting from SD - _bootFromSD=1 + bootFromSD=1 else # all other like VM, RaspberryPi with a NVMe or a laptop - use the storage drive as system drive - _bootFromStorage=1 + bootFromStorage=1 fi fi - # take the third biggest drive as the data drive (only in VM setups) - _dataDevice=$(echo "${listOfDevices}" | sed -n '3p' | awk '{print $1}') - _dataSizeGB=$(echo "${listOfDevices}" | sed -n '3p' | awk '{print $2}') + # Set DATA + if [ ${#dataDevice} -eq 0 ]; then + # 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 - _combinedDataStorage=0 if [ ${#dataDevice} -eq 0 ]; then - _combinedDataStorage=1 + combinedDataStorage=1 fi # output the result - echo "_storageDevice='${storageDevice}'" - echo "_storageSizeGB='${storageSizeGB}'" - echo "_systemDevice='${systemDevice}'" - echo "_systemSizeGB='${systemSizeGB}'" - echo "_dataDevice='${dataDevice}'" - echo "_dataSizeGB='${dataSizeGB}'" - echo "_combinedDataStorage='${combinedDataStorage}'" - echo "_bootFromStorage='${bootFromStorage}'" - echo "_bootFromSD='${bootFromSD}'" + echo "storageDevice='${storageDevice}'" + echo "storageSizeGB='${storageSizeGB}'" + echo "storageRecoverPartition='${storageRecoverPartition}'" + echo "systemDevice='${systemDevice}'" + echo "systemSizeGB='${systemSizeGB}'" + echo "dataDevice='${dataDevice}'" + echo "dataSizeGB='${dataSizeGB}'" + echo "combinedDataStorage='${combinedDataStorage}'" + echo "bootFromStorage='${bootFromStorage}'" + echo "bootFromSD='${bootFromSD}'" + echo "remainingDevices='${remainingDevices}'" exit 0 fi