%attr in rpm.spec does not take env variable's

2019-08-21 04:19发布

问题:

I have a rpm.spec file in which i have to give file permissions for a file.

I want %attr to take $user and $group values during the rpm installation.(where i will be doing "export user=" and "export group=") but it does not take these values,instead gives a syntax error while installing the rpm.

I have something like this in my specfile

%pre

%files
%defattr(-,root,root,-)
<some_path>
%config /etc/akshatha
%doc /usr/share/doc/akshatha
%attr(0700,$user,$group) %dir directory_path
%attr(0700,$user,$group) %dir directory_path
%attr(0600,$user,$group) path_to_file 

this gives me an error, while installing the rpm as -

warning: user $user does not exist - using root
warning: group $group does not exist - using root

I have even tried giving %attr(0700,%{getenv:user},%{getenv:group}) but this fails while preparing the rpm itself saying

RPM build errors:
Bad syntax: %attr(0700)
make: *** [all] Error 1

回答1:

This is not possible. %attr is fixed at build time; you specifically asked for "during the rpm installation" which won't work.

In your %post you can chown but then that will break your verification.



回答2:

EDIT: As @AaronD.Marasco said: this is not possible during rpm installation. If you want to define it during build; then you can proceed like this:

you can define macros on the command line when invoking rpmbuild:

in the spec file:

%attr(0700,%{file_user},%{file_group}) /path/to/file

invoke rpmbuild like this:

rpmbuild --define='file_user some_user' --define='file_group some_group'


回答3:

As others pointed out, it is not possible this way. Considering what you are trying to achieve, I would do it this way:

%pre
getent group GROUPNAME >/dev/null || groupadd -r GROUPNAME

%files
%config /etc/akshatha
%doc /usr/share/doc/akshatha
%attr(0770,root, GROUPNAME) %dir directory_path
...

and then I would instruct admin to put the user into group GROUPNAME.