From 388a13d201defa86887145c91a0e7d8e883b6ad6 Mon Sep 17 00:00:00 2001 From: Igor Pecovnik Date: Mon, 20 Jan 2025 17:58:58 +0100 Subject: [PATCH] Module partitioner: frontend for new armbian-install --- tools/modules/system/a | 8 + tools/modules/system/module_partitioner.sh | 164 +++++++++++++++++++++ 2 files changed, 172 insertions(+) create mode 100644 tools/modules/system/a create mode 100644 tools/modules/system/module_partitioner.sh diff --git a/tools/modules/system/a b/tools/modules/system/a new file mode 100644 index 000000000..bf1fc479c --- /dev/null +++ b/tools/modules/system/a @@ -0,0 +1,8 @@ +#!/bin/bash +t=0 +while IFS== read key value; do + echo "$key=$value $t" +t=$(( t + 1 )) +done < <(lsblk -Alnp -io NAME,SIZE,FSUSED,TYPE,FSTYPE -e 252 --json | jq '.blockdevices[]? | select((.type | test ("disk")) and (.name | test ("boot")) | not)' | jq -r 'to_entries|map("\(.key)=\(.value|tostring)")|.[]') + + diff --git a/tools/modules/system/module_partitioner.sh b/tools/modules/system/module_partitioner.sh new file mode 100644 index 000000000..67f66dd7c --- /dev/null +++ b/tools/modules/system/module_partitioner.sh @@ -0,0 +1,164 @@ + + +declare -A module_options +module_options+=( + ["module_partitioner,author"]="@Tearran" + ["module_partitioner,feature"]="module_partitioner" + ["module_partitioner,example"]="new run create help" + ["module_partitioner,desc"]="Partitioner manager TUI" + ["module_partitioner,status"]="review" +) + +function module_partitioner() { + local title="Partitioner" + local condition=$(which "$title" 2>/dev/null) + + # Convert the example string to an array + local commands + IFS=' ' read -r -a commands <<< "${module_options["module_partitioner,example"]}" + + case "$1" in + "${commands[0]}") + echo "New partition $2" + exit + ;; + "${commands[1]}") + + devices=$( + lsblk -Alnp -io NAME,SIZE,FSUSED,TYPE,FSTYPE,MOUNTPOINT -e 252 --json \ + | jq '.blockdevices[]? | select((.name | test ("boot") | not) and (.name | test ("mtdblock0|nvme|mmcblk|sd")))' \ + | jq -r 'to_entries|map("\(.key)=\(.value|tostring)")|.[]' + ) + + list=() + periodic=1 + while IFS== read key value; do + #echo "$periodic $key $value" + case "$key" in + "name") name="$value" ;; + "size") size=$(printf "%14s" "$value") ;; + "type") type=$(printf "%4s" "$value") ;; + "fsused") fsused="$value" ;; + "fstype") fstype="$value" ;; + "mountpoint") mountpoint="$value" ;; + esac + if [ "$(($periodic % 6))" -eq 0 ]; then + + + +### +echo $mountpoint +if [[ $mountpoint == "/" ]]; then + partitions=$( + lsblk -Alnp -io NAME,SIZE,FSUSED,TYPE,FSTYPE,MOUNTPOINT -e 252 --json \ + | jq --arg name "$name" '.blockdevices[]? | select((.name | test ("disk") | not) and (.name | test ($name)))' \ + | jq -r 'to_entries|map("\(.key)=\(.value|tostring)")|.[]' + ) + while IFS== read a b; do + [[ $a == "mountpoint" ]] && echo ">>> $name $b" + done <<< "$partitions" +fi +### + + + + echo "$periodic $name $size $type $fsused $fstype $mountpoint" + if [[ "$mountpoint" != "/" && "$type" == "disk" ]]; then + echo "$periodic $name $size $type $fsused $fstype $mountpoint" + list+=("${name}" "$(printf "%-20s" "$name") $type ${size} $fstype" "$driveinfo") + fi + fi + periodic=$(($periodic + 1)) + done <<< "$devices" + + #done < <( \ + # lsblk -Alnp -io NAME,SIZE,FSUSED,TYPE,FSTYPE,MOUNTPOINT -e 252 --json \ + # | jq '.blockdevices[]? | select((.name | test ("boot") | not) and (.name | test ("mtdblock0|nvme|mmcblk|sd")))' \ + # | jq -r 'to_entries|map("\(.key)=\(.value|tostring)")|.[]' \ + # ) + exit + + #lsblk -Alnp -io NAME,SIZE,FSUSED,TYPE,FSTYPE,MOUNTPOINT -e 252 --json \ + #| jq '.blockdevices[]? | select((.type | test ("part") | not) and (.name | test ("mtdblock0|nvme|mmcblk|sd")))' \ + #| jq -r 'to_entries|map("\(.key)=\(.value|tostring)")|.[]' \ + + while true; do + #list=() + #local json=$(lsblk -Alnp -io NAME,SIZE,FSUSED,MAJ:MIN,TYPE,FSTYPE -e 252 --json | jq '.blockdevices[]? | select(.name | test ("lock4") | not)') + #local partitions=$(echo $json | jq -r '.[] | .[].name ') + #echo "Reading storage devices and partitions " + #for part in $partitions; do + # echo -en "." + # local size=$(printf "%14s" "$(echo $json | jq -r '.[]' | jq -r '.[] | select(.name == "'$part'") .size')") + # local type=$(printf "%4s" "$(echo $json | jq -r '.[]' | jq -r '.[] | select(.name == "'$part'") .type')") + # local fstype=$(printf "%8s" "$(echo $json | jq -r '.[]' | jq -r '.[] | select(.name == "'$part'") .fstype' | sed 's/null//g')") + + # smartctl reading + #if [[ $part == /dev/nvme* ]]; then + #local driveinfo=$(smartctl -ij $part | jq -r '.model_name') + #mapfile -t array < <(smartctl -ija $part | jq -r '.model_name, .nvme_total_capacity, .nvme_smart_health_information_log.data_units_written, .temperature.current') + #capacity=$(echo ${array[1]} | awk '{ printf "%.2f\n", $1/1024/1024/1024; }')" GB" + #tbw=$(echo ${array[2]} | awk '{ printf "%.2f\n", $1*500/1024/1024/1024; }')"" + #temperature=$(echo ${array[3]})"℃" + #fi + #if [[ -n "${temperature}" ]]; then + # driveinfo="Model: ${array[0]} | Capacity: ${capacity} | TBW: ${tbw} | Temperature: ${temperature}" + #fi + # if [[ ${type} == disk ]]; then + # driveinfo=$(udevadm info --query=all --name=$part | grep 'ID_MODEL=' | cut -d"=" -f2 | sed "s/_//g") + # else + # unset driveinfo + # fi + # list+=("${part}" "$(printf "%-20s" "$part") $type ${size} $fstype" "$driveinfo") + #done + + list_length=${#list[@]} + partitioner=$(dialog \ + --notags \ + --cancel-label "Cancel" \ + --defaultno \ + --ok-label "New" \ + --erase-on-exit \ + --extra-button \ + --help-button \ + --help-label "Select" \ + --item-help \ + --extra-label "Delete" \ + --title "$title" --menu "\nStorage device Type Size FS type" $((${list_length} + 5)) 56 $((${list_length} + 1)) "${list[@]}" 3>&1 1>&2 2>&3) + exitstatus=$? + echo "$partitioner $exitstatus" + + if [[ ${exitstatus} -eq 1 ]]; then break; fi + + ${module_options["module_partitioner,feature"]} ${commands[${exitstatus}]} $partitioner + + done + ;; + "${commands[2]}") + echo "Select $3" + exit + ;; + "${commands[3]}") + echo "Delete $2" + exit + # Removal logic here + ;; + "${commands[4]}") + echo -e "\nUsage: ${module_options["module_partitioner,feature"]} " + echo -e "Commands: ${module_options["module_partitioner,example"]}" + echo "Available commands:" + echo -e "\trun\t- Run $title." + echo + ;; + *) + ${module_options["module_partitioner,feature"]} ${commands[4]} + ;; + esac + } + +# uncomment to test the module +module_partitioner "$1" + + + +