Discussion:
env variable is not defined when running rpmbuild --rebuild
Erez Zilber
2008-08-11 07:46:23 UTC
Permalink
Hi,

I have a problem with an env variable while running rpmbuild --rebuild
/usr/src/redhat/SRPMS/mypackage-0-0.1.src.rpm:

before running rpmbuild, I run the following commands:

[***@klab002:~/work/]$ sudo bash -c 'export MY_ENV_VAR=some_val'
[***@klab002:~/work/]$ sudo echo $MY_ENV_VAR
some_val

In the spec file, I print this env var:

%build
echo "MY_ENV_VAR = $MY_ENV_VAR"

but when I run rpmbuild --rebuild
/usr/src/redhat/SRPMS/mypackage-0-0.1.src.rpm, I get:

+ LANG=C
+ export LANG
+ unset DISPLAY
+ echo 'MY_ENV_VAR = '
MY_ENV_VAR =

I guess that it opens another shell in which the env var is not
defined. How can I solve that (without adding the env var to .bashrc
which is not a possible solution for me).

Thanks,
Erez
Bob Proulx
2008-08-11 17:17:38 UTC
Permalink
This has no effect because in Unix and Unix-like systems every process
starts with a copy of the current environment and at the termination
of the program the copied environment for that program is reclaimed by
the system.

In the above you are spawning a new environment for the 'sudo'
command. That is spawning a new environment for the 'bash' command.
In the bash process the environment variable is being set. That is
the entirety of the bash script and the bash process terminates at
that point. The system frees and reclaims all of the process memory
associated with the bash process including all environment memory
space. The sudo command has been waiting for the bash process to
exit. It observes the program exit code and then the sudo process
itself exits. When the sudo process exits the process memory
including all environment memory is freed and reclaimed by the
system.
Post by Erez Zilber
some_val
This is because you have already set MY_ENV_VAR in the parent
environment. The $MY_ENV_VAR is expanded by the shell before it
invokes the sudo. The sudo then invokes echo with the some_val
argument. Try this for comparison:

$ echo debug: sudo echo $MY_ENV_VAR
debug: sudo echo some_val

The $MY_ENV_VAR will already have been expanded by the original
invoking command line shell.
Post by Erez Zilber
%build
echo "MY_ENV_VAR = $MY_ENV_VAR"
but when I run rpmbuild --rebuild
+ LANG=C
+ export LANG
+ unset DISPLAY
+ echo 'MY_ENV_VAR = '
MY_ENV_VAR =
I guess that it opens another shell in which the env var is not
defined. How can I solve that (without adding the env var to .bashrc
which is not a possible solution for me).
Export the variable in the parent environment. It will then be
inhereted by the child environments.

Either for the specific command:

$ MY_ENV_VAR=some_val rpmbuild --rebuild mypackage-0-0.1.src.rpm

Or:

$ export MY_ENV_VAR=some_val
$ rpmbuild --rebuild mypackage-0-0.1.src.rpm

Note that it is dangerous to use sudo to build rpm packages. This
causes the build to run as root. You don't want to do that. The
wisdom of thet 'net is that this should be avoided. Package building
should be done as a non-privileged user.

Bob
Erez Zilber
2008-08-12 07:05:57 UTC
Permalink
Post by Bob Proulx
This has no effect because in Unix and Unix-like systems every process
starts with a copy of the current environment and at the termination
of the program the copied environment for that program is reclaimed by
the system.
In the above you are spawning a new environment for the 'sudo'
command. That is spawning a new environment for the 'bash' command.
In the bash process the environment variable is being set. That is
the entirety of the bash script and the bash process terminates at
that point. The system frees and reclaims all of the process memory
associated with the bash process including all environment memory
space. The sudo command has been waiting for the bash process to
exit. It observes the program exit code and then the sudo process
itself exits. When the sudo process exits the process memory
including all environment memory is freed and reclaimed by the
system.
Post by Erez Zilber
some_val
This is because you have already set MY_ENV_VAR in the parent
environment. The $MY_ENV_VAR is expanded by the shell before it
invokes the sudo. The sudo then invokes echo with the some_val
$ echo debug: sudo echo $MY_ENV_VAR
debug: sudo echo some_val
The $MY_ENV_VAR will already have been expanded by the original
invoking command line shell.
Post by Erez Zilber
%build
echo "MY_ENV_VAR = $MY_ENV_VAR"
but when I run rpmbuild --rebuild
+ LANG=C
+ export LANG
+ unset DISPLAY
+ echo 'MY_ENV_VAR = '
MY_ENV_VAR =
I guess that it opens another shell in which the env var is not
defined. How can I solve that (without adding the env var to .bashrc
which is not a possible solution for me).
Export the variable in the parent environment. It will then be
inhereted by the child environments.
$ MY_ENV_VAR=some_val rpmbuild --rebuild mypackage-0-0.1.src.rpm
$ export MY_ENV_VAR=some_val
$ rpmbuild --rebuild mypackage-0-0.1.src.rpm
Note that it is dangerous to use sudo to build rpm packages. This
causes the build to run as root. You don't want to do that. The
wisdom of thet 'net is that this should be avoided. Package building
should be done as a non-privileged user.
Bob
Thanks! That was very helpful.

Erez

Loading...