From 56306a468f26140b26b0ef20a8e10314f6d404ec Mon Sep 17 00:00:00 2001 From: Daniel Verkamp Date: Tue, 29 Nov 2016 14:26:42 -0700 Subject: [PATCH] scripts/setup.sh: add logname fallback In some circumstances, no utmp entry is available, so logname fails. In particular, gnome-terminal no longer creates a utmp entry: https://bugzilla.gnome.org/show_bug.cgi?id=747046 As a workaround, try $SUDO_USER - the use case here is to determine the (unprivileged) user name so we can give them ownership of certain files, so this is usually the right thing to use anyway. If we are not running under sudo, the caller should have passed the username as a parameter to scripts/setup.sh anyway, since we can't reliably determine which user is intended. Also check if username is actually set before using it to run chmod - it is possible that the scripts/setup.sh caller does not want to provide access to an unprivilieged user and just wants to run everything as root. Change-Id: I20631c325b52884a378029dcf38568a2b311b457 Signed-off-by: Daniel Verkamp --- scripts/setup.sh | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/scripts/setup.sh b/scripts/setup.sh index 1d754bb07f..fb25932d0f 100755 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -35,7 +35,9 @@ function linux_bind_driver() { iommu_group=$(basename $(readlink -f /sys/bus/pci/devices/$bdf/iommu_group)) if [ -e "/dev/vfio/$iommu_group" ]; then - chown "$username" "/dev/vfio/$iommu_group" + if [ "$username" != "" ]; then + chown "$username" "/dev/vfio/$iommu_group" + fi fi } @@ -76,7 +78,9 @@ function configure_linux { echo "$NRHUGE" > /proc/sys/vm/nr_hugepages if [ "$driver_name" = "vfio-pci" ]; then - chown "$username" /dev/hugepages + if [ "$username" != "" ]; then + chown "$username" /dev/hugepages + fi MEMLOCK_AMNT=`ulimit -l` if [ "$MEMLOCK_AMNT" != "unlimited" ] ; then @@ -174,7 +178,10 @@ if [ "$mode" == "" ]; then fi if [ "$username" = "" ]; then - username=`logname` + username="$SUDO_USER" + if [ "$username" = "" ]; then + username=`logname 2>/dev/null` || true + fi fi if [ `uname` = Linux ]; then