Unverified Commit 7cee1876 authored by Kroese's avatar Kroese Committed by GitHub
Browse files

fix: Format filesizes (#685)

parent dd6fc887
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -52,15 +52,15 @@ fi
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
+23 −22
Original line number Diff line number Diff line
@@ -90,7 +90,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")

@@ -103,16 +103,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)
@@ -174,7 +174,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")
@@ -188,17 +188,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)
@@ -251,7 +251,7 @@ convertDisk() {

  if [[ "$ALLOCATE" != [Nn]* ]]; then

    local DIR CUR_SIZE SPACE
    local DIR CUR_SIZE SPACE GB

    # Check free diskspace
    DIR=$(dirname "$TMP_FILE")
@@ -259,8 +259,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
@@ -459,7 +459,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"
@@ -467,15 +467,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 < 104857600 )); then
    error "Please increase ${DISK_DESC^^}_SIZE to at least 100 MB." && exit 73
@@ -509,12 +510,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

+9 −7
Original line number Diff line number Diff line
@@ -36,7 +36,7 @@ downloadFile() {

  local url="$1"
  local base="$2"
  local msg rc total progress
  local msg rc total total_mb progress

  local dest="$STORAGE/$base.tmp"
  rm -f "$dest"
@@ -60,8 +60,9 @@ downloadFile() {

  if (( rc == 0 )) && [ -f "$dest" ]; then
    total=$(stat -c%s "$dest")
    total_gb=$(formatBytes "$total")
    if [ "$total" -lt 100000 ]; then
      error "Invalid image file: is only $total bytes?" && return 1
      error "Invalid image file: is only $total_gb ?" && return 1
    fi
    html "Download finished successfully..."
    mv -f "$dest" "$STORAGE/$base"
@@ -83,8 +84,8 @@ convertImage() {
  local source_fmt=$2
  local dst_file=$3
  local dst_fmt=$4
  local dir base fs fa space
  local cur_size src_size disk_param
  local dir base fs fa space space_gb
  local cur_size cur_gb src_size disk_param

  [ -f "$dst_file" ] && error "Conversion failed, destination file $dst_file already exists?" && return 1
  [ ! -f "$source_file" ] && error "Conversion failed, source file $source_file does not exists?" && return 1
@@ -106,8 +107,8 @@ convertImage() {
    space=$(df --output=avail -B 1 "$dir" | tail -n 1)

    if (( src_size > space )); then
      local space_gb=$(( (space + 1073741823)/1073741824 ))
      error "Not enough free space to convert image in $dir, it has only $space_gb GB available..." && return 1
      space_gb=$(formatBytes "$space")
      error "Not enough free space to convert image in $dir, it has only $space_gb available..." && return 1
    fi
  fi

@@ -143,8 +144,9 @@ convertImage() {
    if [ -n "$ALLOCATE" ] && [[ "$ALLOCATE" != [Nn]* ]]; then
      # Work around qemu-img bug
      cur_size=$(stat -c%s "$tmp_file")
      cur_gb=$(formatBytes "$cur_size")
      if ! fallocate -l "$cur_size" "$tmp_file"; then
        error "Failed to allocate $cur_size bytes for image!"
        error "Failed to allocate $cur_gb for image!"
      fi
    fi
  fi
+42 −16
Original line number Diff line number Diff line
@@ -75,7 +75,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

@@ -96,23 +96,46 @@ if [ ! -d "$STORAGE" ]; then
  error "Storage folder ($STORAGE) not found!" && exit 13
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/}"
@@ -121,9 +144,11 @@ 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 compatibilty
@@ -142,7 +167,8 @@ fi
# 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
@@ -150,7 +176,7 @@ fi
# Helper functions

isAlive() {
  local pid=$1
  local pid="$1"

  if kill -0 "$pid" 2>/dev/null; then
    return 0
@@ -160,7 +186,7 @@ isAlive() {
}

pKill() {
  local pid=$1
  local pid="$1"

  { kill -15 "$pid" || true; } 2>/dev/null

@@ -172,7 +198,7 @@ pKill() {
}

fWait() {
  local name=$1
  local name="$1"

  while pgrep -f -l "$name" >/dev/null; do
    sleep 0.2
@@ -182,7 +208,7 @@ fWait() {
}

fKill() {
  local name=$1
  local name="$1"

  { pkill -f "$name" || true; } 2>/dev/null
  fWait "$name"