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 <daniel.verkamp@intel.com>
This commit is contained in:
Daniel Verkamp 2016-11-29 14:26:42 -07:00
parent 8b45a830c4
commit 56306a468f

View File

@ -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