[2/3] runqemu-gen-tapdevs: Allow run --help without sudo

Submitted by Robert Yang on Dec. 27, 2018, 8:59 a.m. | Patch ID: 157455

Details

Message ID 97c9fb76d7d46684593fe7b1c7786bdd1344b165.1545901142.git.liezhi.yang@windriver.com
State New
Headers show

Commit Message

Robert Yang Dec. 27, 2018, 8:59 a.m.
Then we can get user's UID and GID rather than hardcode to 1000, e.g.:

$ sudo ../poky/scripts/runqemu-gen-tapdevs 15220 100 4 tmp/sysroots-components/x86_64/qemu-helper-native/usr/bin

Signed-off-by: Robert Yang <liezhi.yang@windriver.com>
---
 scripts/runqemu-gen-tapdevs | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

Patch hide | download patch | download mbox

diff --git a/scripts/runqemu-gen-tapdevs b/scripts/runqemu-gen-tapdevs
index 11de318..67c4fcc 100755
--- a/scripts/runqemu-gen-tapdevs
+++ b/scripts/runqemu-gen-tapdevs
@@ -29,15 +29,10 @@  usage() {
 	echo "<num> is the number of tap devices to create (0 to remove all)"
 	echo "<native-sysroot-basedir> is the path to the build system's native sysroot"
 	echo "e.g. $ bitbake qemu-helper-native"
-	echo "$ sudo $0 1000 1000 4 tmp/sysroots-components/x86_64/qemu-helper-native/usr/bin"
+	echo "$ sudo $0 `id -u` `id -g` 4 tmp/sysroots-components/x86_64/qemu-helper-native/usr/bin"
 	exit 1
 }
 
-if [ $EUID -ne 0 ]; then
-	echo "Error: This script must be run with root privileges"
-	exit
-fi
-
 if [ $# -ne 4 ]; then
 	echo "Error: Incorrect number of arguments"
 	usage
@@ -54,6 +49,11 @@  if [[ ! -x "$TUNCTL" || -d "$TUNCTL" ]]; then
 	usage
 fi
 
+if [ $EUID -ne 0 ]; then
+	echo "Error: This script must be run with root privileges"
+	exit
+fi
+
 SCRIPT_DIR=`dirname $0`
 RUNQEMU_IFUP="$SCRIPT_DIR/runqemu-ifup"
 if [ ! -x "$RUNQEMU_IFUP" ]; then

Comments

Richard Purdie Dec. 27, 2018, 11:32 a.m.
On Thu, 2018-12-27 at 00:59 -0800, Robert Yang wrote:
> Then we can get user's UID and GID rather than hardcode to 1000,
> e.g.:
> 
> $ sudo ../poky/scripts/runqemu-gen-tapdevs 15220 100 4 tmp/sysroots-
> components/x86_64/qemu-helper-native/usr/bin
> 
> Signed-off-by: Robert Yang <liezhi.yang@windriver.com>
> ---
>  scripts/runqemu-gen-tapdevs | 12 ++++++------
>  1 file changed, 6 insertions(+), 6 deletions(-)

This fixes the help output without sudo but breaks the help output
under sudo. How about we check SUDO_UID and SUDO_GID? Something like:

uid=`id -u`
if [ -n "$SUDO_UID" ]; then
    uid=$SUDO_UID
fi

?

Cheers,

Richard