virtualmin / slib

Library of POSIX shell functions used by Virtualmin install scripts
BSD 3-Clause "New" or "Revised" License
6 stars 6 forks source link

'df' prints number with 'K' suffix making field non-numeric #1

Open fortran77 opened 5 years ago

fortran77 commented 5 years ago

In the following code at around line 634

# Check for enough space.
root_fs_avail=$(df /|grep -v Filesystem|awk '{print $4}')
if [ "$root_fs_avail" -lt $((swap_min + 358400)) ]; then
  root_fs_avail_h=$((root_fs_avail / 1024))
  log_fatal "Root filesystem only has $root_fs_avail_h MB available, which is too small."
  log_fatal "You'll either need to use the --minimal installation of add more space to '/'."
  return 3
fi

on some systems (e.g., newer CentOS 7) the df command may print a filesystem size with a 'K' suffix. On the next line, the 'if [ "$root_fs_avail" -lt ...] ...' then reports an error because '-lt' expects a numeric argument.

An easy fix would be to pipe df's output through sed -e 's/[^0-9]//g' thus deletng all non-numeric characters.

jcameron commented 5 years ago

I don't see this on my CentOS 7 system. Can you post exactly what the df command outputs for you?

fortran77 commented 5 years ago

Here's some information, empty lines added for readability, and a hostname redacted. This is a CentOS 7 server instance at upcloud.com. Also cmp /bin/df /usr/bin/df shows that they are identical.

bash-4.2# df /
Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/vda1      26203136K 7074856K 19128280K  28% /

bash-4.2# df
Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/vda1      26203136K 7074904K 19128232K  28% /
devtmpfs         496728K       0K   496728K   0% /dev
tmpfs            507444K       0K   507444K   0% /dev/shm
tmpfs            507444K   25936K   481508K   6% /run
tmpfs            507444K       0K   507444K   0% /sys/fs/cgroup
tmpfs            101492K       0K   101492K   0% /run/user/0

bash-4.2# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

bash-4.2# which df
/bin/df

bash-4.2# alias df
bash: alias: df: not found

bash-4.2# uname -a
Linux (HOSTNAME REDACTED) 3.10.0-957.10.1.el7.x86_64 #1 SMP Mon Mar 18 15:06:45 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
fortran77 commented 5 years ago

By the way, df -P with the portability option will print the integers without the K suffix. However -P might not be available in older systems, so that makes it non-portable.