Skip to content

Commit df10f9f

Browse files
marc-hbkv2019i
authored andcommitted
Add new sudo-cwd.sh - partial cherry-pick from main branch.
sudo-cwd.sh was developed on the main branch for zephyr but now we want to re-use it for building topologies. Cherry-pick only that script, not the zephyr parts. Original commit message: Besides making things more obvious, the important functional change is that the user switch is now performed for _every_ invoked, command, not just for the build command. Signed-off-by: Marc Herbert <marc.herbert@intel.com> (cherry picked from commit 027be98)
1 parent 06a52cd commit df10f9f

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

scripts/sudo-cwd.sh

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/bin/sh
2+
# SPDX-License-Identifier: BSD-3-Clause
3+
# Copyright(c) 2022 Intel Corporation. All rights reserved.
4+
5+
# This is a "brute force" solution to filesystem permission issues:
6+
#
7+
# If the current user does not own the current directory then this
8+
# wrapper script switches to the user who does own the current directory
9+
# before running the given command.
10+
11+
# If no user owns the current directory, a user who does gets created
12+
# first!
13+
14+
# The main use case is to run this first thing inside a container to
15+
# solve file ownership mismatches.
16+
17+
# `docker run --user=$(id -un) ...` achieves something very similar
18+
# without any code except the resulting user many not exist inside the
19+
# container. Some commands may not like that.
20+
#
21+
# To understand more about the Docker problem solved here take a look at
22+
# https://stackoverflow.com/questions/35291520/docker-and-userns-remap-how-to-manage-volume-permissions-to-share-data-betwee
23+
# and many other similar questions.
24+
25+
# TODO: replace sudo with gosu?
26+
27+
set -e
28+
set -x
29+
30+
# TODO: rename the "sof_" bits
31+
32+
main()
33+
{
34+
sof_uid="$(stat --printf='%u' .)"
35+
local current_uid; current_uid="$(id -u)"
36+
if test "$current_uid" = "$sof_uid"; then
37+
exec "$@"
38+
else
39+
exec_as_sof_uid "$@"
40+
fi
41+
}
42+
43+
exec_as_sof_uid()
44+
{
45+
# Add new container user matching the host user owning the SOF
46+
# checkout
47+
local sof_user; sof_user="$(id "$sof_uid")" || {
48+
sof_user=sof_zephyr_docker_builder
49+
50+
local sof_guid; sof_guid="$(stat --printf='%g' .)"
51+
52+
getent group "$sof_guid" ||
53+
sudo groupadd -g "$sof_guid" sof_zephyr_docker_group
54+
55+
sudo useradd -m -u "$sof_uid" -g "$sof_guid" "$sof_user"
56+
57+
local current_user; current_user="$(id -un)"
58+
59+
# Copy sudo permissions just in case the build needs it
60+
sudo sed -e "s/$current_user/$sof_user/" /etc/sudoers.d/"$current_user" |
61+
sudo tee -a /etc/sudoers.d/"$sof_user"
62+
sudo chmod --reference=/etc/sudoers.d/"$current_user" \
63+
/etc/sudoers.d/"$sof_user"
64+
}
65+
66+
# Double sudo to work around some funny restriction in
67+
# zephyr-build:/etc/sudoers: 'user' can do anything but... only as
68+
# root.
69+
sudo sudo -u "$sof_user" REAL_CC="$REAL_CC" "$@"
70+
exit "$?"
71+
}
72+
73+
main "$@"

0 commit comments

Comments
 (0)