From 4dda11646847a605bb4c555beda0fbf79c214aa9 Mon Sep 17 00:00:00 2001 From: weishanshan1084 Date: Mon, 29 Apr 2024 19:14:38 +0800 Subject: [PATCH] Makefile: add grant_sudo_perm.sh Signed-off-by: weishanshan1084 --- Makefile | 9 ++++- utils/grant_sudo_perm.sh | 77 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) create mode 100755 utils/grant_sudo_perm.sh diff --git a/Makefile b/Makefile index 855a8750..e82c3d2d 100644 --- a/Makefile +++ b/Makefile @@ -587,7 +587,7 @@ prepare: $(BUILD_DIR)/buildroot-config/auto.conf $(EXTRA_ENV) $(s) $(TARGET_DIR) $(call qstrip,$(BR2_ROOTFS_POST_SCRIPT_ARGS))$(sep)) .PHONY: world -world: target-post-image +world: grant_sudo_perm target-post-image .PHONY: prepare-sdk prepare-sdk: world @@ -823,6 +823,13 @@ target-post-image: $(TARGETS_ROOTFS) target-finalize staging-finalize $(call MESSAGE,"Executing post-image script $(s)"); \ $(EXTRA_ENV) $(s) $(BINARIES_DIR) $(call qstrip,$(BR2_ROOTFS_POST_SCRIPT_ARGS))$(sep)) +.PHONY: grant_sudo_perm +grant_sudo_perm: +ifeq ($(BR2_ROOTFS_SKELETON_UBUNTU)$(BR2_ROOTFS_SKELETON_DEBIAN),y) + @echo "*************************" + @$(TOPDIR)/utils/grant_sudo_perm.sh || exit 1 +endif + .PHONY: source source: $(foreach p,$(PACKAGES),$(p)-all-source) diff --git a/utils/grant_sudo_perm.sh b/utils/grant_sudo_perm.sh new file mode 100755 index 00000000..041c0d4b --- /dev/null +++ b/utils/grant_sudo_perm.sh @@ -0,0 +1,77 @@ +#!/bin/bash +SUDO_FILE=/etc/sudoers.d/buildroot_conf +permission_grant() +{ + echo "User(${USER[@]}) is applying the sudo permission." + file=$(tempfile) + if [ "x$?" != "x0" ] + then + echo "Failed to creat a tempfile."; + return 1 + fi + + if [ -f ${SUDO_FILE} ] + then + ${SUDO} rm -rf ${SUDO_FILE} + fi + echo "Host_Alias HOST = ${HOST}" >> ${file} + echo "User_Alias USER = "${USER[0]} >> ${file} + USER_RES=(${USER[@]}) + unset USER_RES[0] + for u in "${USER_RES[@]}" + do + ${SED} -i -e "/User_Alias/ s/$/,${u}/" ${file} + done + echo "Cmnd_Alias MOUNT = ${MOUNT},${UMOUNT}" >> ${file} + echo "Cmnd_Alias CHOWN = ${CHOWN}" >> ${file} + echo "Cmnd_Alias CHROOT1 = ${CHROOT1}" >> ${file} + echo "Cmnd_Alias CHMOD = ${CHMOD}" >> ${file} + echo "Cmnd_Alias FIND = ${FIND}" >> ${file} + echo "Cmnd_Alias CP = ${CP}" >> ${file} + echo "Cmnd_Alias MV = ${MV}" >> ${file} + echo "Cmnd_Alias DEBOOTSTRAP = ${DEBOOTSTRAP}" >> ${file} + echo "Cmnd_Alias MKDIR = ${MKDIR}" >> ${file} + echo "Cmnd_Alias TEE = ${TEE}" >> ${file} + echo "Cmnd_Alias RM = ${RM}" >> ${file} + echo "USER HOST=(root) NOPASSWD:MOUNT,CHMOD,CHROOT1,FIND,CP,MV,RM,MKDIR,TEE,CHOWN,DEBOOTSTRAP" >> ${file} + ${SUDO} ${CHOWN} root:root ${file} + ${SUDO} ${CHMOD} +r ${file} + ${SUDO} ${MV} ${file} ${SUDO_FILE} + echo "Buildroot User(${USER[@]}) is granted" + return 0 +} + + +HOST=ALL +SUDO=`which sudo` +MOUNT=`which mount` +UMOUNT=`which umount` +CHROOT1=`which chroot` +CHOWN=`which chown` +CHMOD=`which chmod` +FIND=`which find` +CP=`which cp` +MV=`which mv` +SED=`which sed` +MKDIR=`which mkdir` +TEE=`which tee` +RM=`which rm` +DEBOOTSTRAP=`which debootstrap` +if [ "x${DEBOOTSTRAP}" = "x" ] +then + echo "debootstrap not found. Try running \"install debootstrap\"" + exit 1 +fi +USER=() +confirmed=false + +if [[ $# -eq 0 ]] +then + USER=($(whoami)) +fi + +if [[ ${#USER[@]} -eq 0 ]] +then + USER=($(whoami)) +fi +permission_grant