diff --git a/src/config.sh b/src/config.sh index 2653675..b1877bd 100644 --- a/src/config.sh +++ b/src/config.sh @@ -17,15 +17,15 @@ ARGS=$(echo "$ARGS" | sed 's/\t/ /g' | tr -s ' ') if [[ "$RAM_CHECK" != [Nn]* ]]; then RAM_AVAIL=$(free -b | grep -m 1 Mem: | awk '{print $7}') - AVAIL_GB=$(( RAM_AVAIL/1073741824 )) + AVAIL_MEM=$(formatBytes "$RAM_AVAIL") if (( (RAM_WANTED + RAM_SPARE) > RAM_AVAIL )); then - msg="Your configured RAM_SIZE of $WANTED_GB GB is too high for the $AVAIL_GB GB of memory available, please set a lower value." + msg="Your configured RAM_SIZE of ${RAM_SIZE/G/ GB} is too high for the $AVAIL_MEM of memory available, please set a lower value." [[ "${FS,,}" != "zfs" ]] && error "$msg" && exit 17 info "$msg" else if (( (RAM_WANTED + (RAM_SPARE * 3)) > RAM_AVAIL )); then - msg="your configured RAM_SIZE of $WANTED_GB GB is very close to the $AVAIL_GB GB of memory available, please consider a lower value." + msg="your configured RAM_SIZE of ${RAM_SIZE/G/ GB} is very close to the $AVAIL_MEM of memory available, please consider a lower value." if [[ "${FS,,}" != "zfs" ]]; then warn "$msg" else diff --git a/src/disk.sh b/src/disk.sh index 7745e47..d776042 100644 --- a/src/disk.sh +++ b/src/disk.sh @@ -96,7 +96,7 @@ createDisk() { local DISK_DESC=$3 local DISK_FMT=$4 local FS=$5 - local DATA_SIZE DIR SPACE FA + local DATA_SIZE DIR SPACE GB FA DATA_SIZE=$(numfmt --from=iec "$DISK_SPACE") @@ -109,16 +109,16 @@ createDisk() { SPACE=$(df --output=avail -B 1 "$DIR" | tail -n 1) if (( DATA_SIZE > SPACE )); then - local SPACE_GB=$(( (SPACE + 1073741823)/1073741824 )) - error "Not enough free space to create a $DISK_DESC of $DISK_SPACE in $DIR, it has only $SPACE_GB GB available..." + GB=$(formatBytes "$SPACE") + error "Not enough free space to create a $DISK_DESC of ${DISK_SPACE/G/ GB} in $DIR, it has only $GB available..." error "Please specify a smaller ${DISK_DESC^^}_SIZE or disable preallocation by setting ALLOCATE=N." && exit 76 fi fi html "Creating a $DISK_DESC image..." - info "Creating a $DISK_SPACE $DISK_STYLE $DISK_DESC image in $DISK_FMT format..." + info "Creating a ${DISK_SPACE/G/ GB} $DISK_STYLE $DISK_DESC image in $DISK_FMT format..." - local FAIL="Could not create a $DISK_STYLE $DISK_FMT $DISK_DESC image of $DISK_SPACE ($DISK_FILE)" + local FAIL="Could not create a $DISK_STYLE $DISK_FMT $DISK_DESC image of ${DISK_SPACE/G/ GB} ($DISK_FILE)" case "${DISK_FMT,,}" in raw) @@ -180,7 +180,7 @@ resizeDisk() { local DISK_DESC=$3 local DISK_FMT=$4 local FS=$5 - local CUR_SIZE DATA_SIZE DIR SPACE + local CUR_SIZE DATA_SIZE DIR SPACE GB CUR_SIZE=$(getSize "$DISK_FILE") DATA_SIZE=$(numfmt --from=iec "$DISK_SPACE") @@ -194,17 +194,17 @@ resizeDisk() { SPACE=$(df --output=avail -B 1 "$DIR" | tail -n 1) if (( REQ > SPACE )); then - local SPACE_GB=$(( (SPACE + 1073741823)/1073741824 )) - error "Not enough free space to resize $DISK_DESC to $DISK_SPACE in $DIR, it has only $SPACE_GB GB available.." + GB=$(formatBytes "$SPACE") + error "Not enough free space to resize $DISK_DESC to ${DISK_SPACE/G/ GB} in $DIR, it has only $GB available.." error "Please specify a smaller ${DISK_DESC^^}_SIZE or disable preallocation by setting ALLOCATE=N." && exit 74 fi fi - local GB=$(( (CUR_SIZE + 1073741823)/1073741824 )) - MSG="Resizing $DISK_DESC from ${GB}G to $DISK_SPACE..." + GB=$(formatBytes "$CUR_SIZE") + MSG="Resizing $DISK_DESC from $GB to ${DISK_SPACE/G/ GB}..." info "$MSG" && html "$MSG" - local FAIL="Could not resize the $DISK_STYLE $DISK_FMT $DISK_DESC image from ${GB}G to $DISK_SPACE ($DISK_FILE)" + local FAIL="Could not resize the $DISK_STYLE $DISK_FMT $DISK_DESC image from ${GB} to ${DISK_SPACE/G/ GB} ($DISK_FILE)" case "${DISK_FMT,,}" in raw) @@ -257,7 +257,7 @@ convertDisk() { if [[ "$ALLOCATE" != [Nn]* ]]; then - local DIR CUR_SIZE SPACE + local DIR CUR_SIZE SPACE GB # Check free diskspace DIR=$(dirname "$TMP_FILE") @@ -265,8 +265,8 @@ convertDisk() { SPACE=$(df --output=avail -B 1 "$DIR" | tail -n 1) if (( CUR_SIZE > SPACE )); then - local SPACE_GB=$(( (SPACE + 1073741823)/1073741824 )) - error "Not enough free space to convert $DISK_DESC to $DST_FMT in $DIR, it has only $SPACE_GB GB available..." + GB=$(formatBytes "$SPACE") + error "Not enough free space to convert $DISK_DESC to $DST_FMT in $DIR, it has only $GB available..." error "Please free up some disk space or disable preallocation by setting ALLOCATE=N." && exit 76 fi fi @@ -382,7 +382,7 @@ createDevice () { result+=",if=none \ -device nvme,drive=${DISK_ID}${index},serial=deadbeaf${DISK_INDEX}" echo "$result" - ;; + ;; "ide" | "sata" ) result+=",if=none \ -device ich9-ahci,id=ahci${DISK_INDEX},addr=$DISK_ADDRESS \ @@ -416,7 +416,7 @@ addDisk () { local DISK_FMT=$7 local DISK_IO=$8 local DISK_CACHE=$9 - local DISK_EXT DIR DATA_SIZE FS PREV_FMT PREV_EXT CUR_SIZE + local DISK_EXT DIR SPACE DATA_SIZE FS PREV_FMT PREV_EXT CUR_SIZE DISK_EXT=$(fmt2ext "$DISK_FMT") local DISK_FILE="$DISK_BASE.$DISK_EXT" @@ -424,15 +424,16 @@ addDisk () { DIR=$(dirname "$DISK_FILE") [ ! -d "$DIR" ] && return 0 - [ -z "$DISK_SPACE" ] && DISK_SPACE="16G" - DISK_SPACE=$(echo "${DISK_SPACE^^}" | sed 's/MB/M/g;s/GB/G/g;s/TB/T/g') - [[ -z "${DISK_SPACE//[0-9]}" ]] && DISK_SPACE="${DISK_SPACE}G" + SPACE="${DISK_SPACE// /}" + [ -z "$SPACE" ] && SPACE="16G" + [ -z "${SPACE//[0-9. ]}" ] && SPACE="${SPACE}G" + SPACE=$(echo "${SPACE^^}" | sed 's/MB/M/g;s/GB/G/g;s/TB/T/g') - if ! numfmt --from=iec "$DISK_SPACE" &>/dev/null; then + if ! numfmt --from=iec "$SPACE" &>/dev/null; then error "Invalid value for ${DISK_DESC^^}_SIZE: $DISK_SPACE" && exit 73 fi - DATA_SIZE=$(numfmt --from=iec "$DISK_SPACE") + DATA_SIZE=$(numfmt --from=iec "$SPACE") if (( DATA_SIZE < 6442450944 )); then error "Please increase ${DISK_DESC^^}_SIZE to at least 6 GB." && exit 73 @@ -466,12 +467,12 @@ addDisk () { CUR_SIZE=$(getSize "$DISK_FILE") if (( DATA_SIZE > CUR_SIZE )); then - resizeDisk "$DISK_FILE" "$DISK_SPACE" "$DISK_DESC" "$DISK_FMT" "$FS" || exit $? + resizeDisk "$DISK_FILE" "$SPACE" "$DISK_DESC" "$DISK_FMT" "$FS" || exit $? fi else - createDisk "$DISK_FILE" "$DISK_SPACE" "$DISK_DESC" "$DISK_FMT" "$FS" || exit $? + createDisk "$DISK_FILE" "$SPACE" "$DISK_DESC" "$DISK_FMT" "$FS" || exit $? fi diff --git a/src/install.sh b/src/install.sh index 9835fda..82203a7 100644 --- a/src/install.sh +++ b/src/install.sh @@ -85,9 +85,9 @@ else TMP="/tmp/dsm" TMP_SPACE=2147483648 SPACE=$(df --output=avail -B 1 /tmp | tail -n 1) - SPACE_MB=$(( (SPACE + 1048575)/1048576 )) + SPACE_MB=$(formatBytes "$SPACE") if (( TMP_SPACE > SPACE )); then - error "Not enough free space inside the container, have $SPACE_MB MB available but need at least 2 GB." && exit 93 + error "Not enough free space inside the container, have $SPACE_MB available but need at least 2 GB." && exit 93 fi fi @@ -96,13 +96,13 @@ rm -rf "$TMP" && mkdir -p "$TMP" # Check free diskspace ROOT_SPACE=536870912 SPACE=$(df --output=avail -B 1 / | tail -n 1) -SPACE_MB=$(( (SPACE + 1048575)/1048576 )) -(( ROOT_SPACE > SPACE )) && error "Not enough free space inside the container, have $SPACE_MB MB available but need at least 500 MB." && exit 96 +SPACE_MB=$(formatBytes "$SPACE" "down") +(( ROOT_SPACE > SPACE )) && error "Not enough free space inside the container, have $SPACE_MB available but need at least 500 MB." && exit 96 MIN_SPACE=15032385536 SPACE=$(df --output=avail -B 1 "$STORAGE" | tail -n 1) -SPACE_GB=$(( (SPACE + 1073741823)/1073741824 )) -(( MIN_SPACE > SPACE )) && error "Not enough free space for installation in $STORAGE, have $SPACE_GB GB available but need at least 14 GB." && exit 94 +SPACE_GB=$(formatBytes "$SPACE") +(( MIN_SPACE > SPACE )) && error "Not enough free space for installation in $STORAGE, have $SPACE_GB available but need at least 14 GB." && exit 94 # Check if output is to interactive TTY if [ -t 1 ]; then @@ -310,10 +310,10 @@ rm -f "$SYSTEM" # Check free diskspace SYSTEM_SIZE=10738466816 SPACE=$(df --output=avail -B 1 "$STORAGE" | tail -n 1) -SPACE_MB=$(( (SPACE + 1048575)/1048576 )) +SPACE_MB=$(formatBytes "$SPACE") if (( SYSTEM_SIZE > SPACE )); then - error "Not enough free space in $STORAGE to create a 10 GB system disk, have only $SPACE_MB MB available." && exit 97 + error "Not enough free space in $STORAGE to create a 10 GB system disk, have only $SPACE_MB available." && exit 97 fi if ! touch "$SYSTEM"; then diff --git a/src/reset.sh b/src/reset.sh index b6789c3..073174c 100644 --- a/src/reset.sh +++ b/src/reset.sh @@ -74,7 +74,7 @@ CPU="${CPU// with Radeon Graphics/}" CPU="${CPU// with Radeon Vega Graphics/}" [ -z "${CPU// /}" ] && CPU="Unknown" -[[ -n ${CPU_CORES//[0-9]} ]] && error "Invalid amount of CPU_CORES: $CPU_CORES" && exit 15 +[ -n "${CPU_CORES//[0-9 ]}" ] && error "Invalid amount of CPU_CORES: $CPU_CORES" && exit 15 # Check system @@ -103,23 +103,46 @@ if [[ "${FS,,}" == "ecryptfs" ]] || [[ "${FS,,}" == "tmpfs" ]]; then DISK_CACHE="writeback" fi +formatBytes() { + local result + result=$(numfmt --to=iec "$1") + local unit="${result//[0-9. ]}" + if [ -z "$unit" ]; then + unit="bytes" + else + unit=$(echo "${unit^^}" | sed 's/K/KB/g;s/M/MB/g;s/G/GB/g;s/T/TB/g') + fi + result="${result//[a-zA-Z ]/}" + if [[ "${2:-}" == "up" ]]; then + if [[ "$result" == *"."* ]]; then + result="${result%%.*}" + result=$((result+1)) + fi + else + if [[ "${2:-}" == "down" ]]; then + result="${result%%.*}" + fi + fi + echo "$result $unit" + return 0 +} + # Read memory RAM_SPARE=500000000 RAM_AVAIL=$(free -b | grep -m 1 Mem: | awk '{print $7}') RAM_TOTAL=$(free -b | grep -m 1 Mem: | awk '{print $2}') -if [[ -z ${RAM_SIZE//[0-9]} ]]; then - [ "$RAM_SIZE" -lt "130" ] && RAM_SIZE="${RAM_SIZE}G" || RAM_SIZE="${RAM_SIZE}M" +RAM_SIZE="${RAM_SIZE// /}" +[ -z "$RAM_SIZE" ] && error "RAM_SIZE not specified!" && exit 16 + +if [ -z "${RAM_SIZE//[0-9. ]}" ]; then + [ "${RAM_SIZE%%.*}" -lt "130" ] && RAM_SIZE="${RAM_SIZE}G" || RAM_SIZE="${RAM_SIZE}M" fi RAM_SIZE=$(echo "${RAM_SIZE^^}" | sed 's/MB/M/g;s/GB/G/g;s/TB/T/g') -! numfmt --from=iec "$RAM_SIZE" &>/dev/null && error "Invalid RAM size: $RAM_SIZE" && exit 16 +! numfmt --from=iec "$RAM_SIZE" &>/dev/null && error "Invalid RAM_SIZE: $RAM_SIZE" && exit 16 RAM_WANTED=$(numfmt --from=iec "$RAM_SIZE") -[ "$RAM_WANTED" -lt "136314880 " ] && error "Invalid RAM size: $RAM_SIZE" && exit 16 - -AVAIL_GB=$(( RAM_AVAIL/1073741824 )) -TOTAL_GB=$(( (RAM_TOTAL + 1073741823)/1073741824 )) -WANTED_GB=$(( (RAM_WANTED + 1073741823)/1073741824 )) +[ "$RAM_WANTED" -lt "136314880 " ] && error "RAM_SIZE is too low: $RAM_SIZE" && exit 16 # Print system info SYS="${SYS/-generic/}" @@ -127,15 +150,18 @@ FS="${FS/UNKNOWN //}" FS="${FS/ext2\/ext3/ext4}" FS=$(echo "$FS" | sed 's/[)(]//g') SPACE=$(df --output=avail -B 1 "$STORAGE" | tail -n 1) -SPACE_GB=$(( (SPACE + 1073741823)/1073741824 )) +SPACE_GB=$(formatBytes "$SPACE" "down") +AVAIL_MEM=$(formatBytes "$RAM_AVAIL" "down") +TOTAL_MEM=$(formatBytes "$RAM_TOTAL" "up") -echo "❯ CPU: ${CPU} | RAM: $AVAIL_GB/$TOTAL_GB GB | DISK: $SPACE_GB GB (${FS}) | KERNEL: ${SYS}..." +echo "❯ CPU: ${CPU} | RAM: ${AVAIL_MEM%% *}/$TOTAL_MEM | DISK: $SPACE_GB (${FS}) | KERNEL: ${SYS}..." echo # Check available memory if [[ "$RAM_CHECK" != [Nn]* ]] && (( (RAM_WANTED + RAM_SPARE) > RAM_AVAIL )); then - msg="Your configured RAM_SIZE of $WANTED_GB GB is too high for the $AVAIL_GB GB of memory available, please set a lower value." + AVAIL_MEM=$(formatBytes "$RAM_AVAIL") + msg="Your configured RAM_SIZE of ${RAM_SIZE/G/ GB} is too high for the $AVAIL_MEM of memory available, please set a lower value." [[ "${FS,,}" != "zfs" ]] && error "$msg" && exit 17 info "$msg" fi @@ -151,7 +177,7 @@ rm -rf "$STORAGE/tmp" # Helper functions isAlive() { - local pid=$1 + local pid="$1" if kill -0 "$pid" 2>/dev/null; then return 0 @@ -161,7 +187,7 @@ isAlive() { } pKill() { - local pid=$1 + local pid="$1" { kill -15 "$pid" || true; } 2>/dev/null @@ -173,7 +199,7 @@ pKill() { } fWait() { - local name=$1 + local name="$1" while pgrep -f -l "$name" >/dev/null; do sleep 0.2 @@ -183,7 +209,7 @@ fWait() { } fKill() { - local name=$1 + local name="$1" { pkill -f "$name" || true; } 2>/dev/null fWait "$name"