8

I want to create simple RPM file with many small files.

[root@laptop rpm]# tree
.
├── BUILD
├── BUILDROOT
├── RPMS
├── SOURCES
│   └── some_agent-1.0.tar.gz
├── SPECS
│   ├── kernel.spec
│   └── kernel.spec~
└── SRPMS

6 directories, 3 files
[root@laptop rpm]# 

I have this spec file:

Summary: some_agent
Name: some_agent
Version: 1.0
Release: 1
License: Apache
Group: application
Source0: %{name}-%{version}.tar.gz
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-buildroot
%description
Test 

%prep
%setup -q

%build


%install
rm -rf $RPM_BUILD_ROOT
make root=$RPM_BUILD_ROOT install

%clean
rm -rf $RPM_BUILD_ROOT

%files
%defattr(-,root,root,-)
%doc
agent/*

But when I try to build the RPM package I get this error:

[root@laptop ~]$ rpmbuild -bb -v ~/rpm/SPECS/kernel.spec
Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.RUwFW5
+ umask 022
+ cd /home/rcbandit/rpm/BUILD
+ LANG=C
+ export LANG
+ unset DISPLAY
+ cd /home/rcbandit/rpm/BUILD
+ rm -rf some_agent-1.0
+ /usr/bin/gzip -dc /home/rcbandit/rpm/SOURCES/some_agent-1.0.tar.gz
+ /bin/tar -xf -
+ STATUS=0
+ '[' 0 -ne 0 ']'
+ cd some_agent-1.0
/var/tmp/rpm-tmp.RUwFW5: line 38: cd: some_agent-1.0: No such file or directory
error: Bad exit status from /var/tmp/rpm-tmp.RUwFW5 (%prep)


RPM build errors:
    Bad exit status from /var/tmp/rpm-tmp.RUwFW5 (%prep)
[root@laptop ~]$ ^C

Do you have any idea where is my mistake?

I copied the source file but for some reason it's not found.

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
user1285928
  • 1,328
  • 29
  • 98
  • 147
  • It looks like rpmbuild has some problems with changing directory to unpacked some_agent directory, check paths. Maybe your sources weren't compressed properly - without 'some_agent-1.0' directory, just files? Try to uncompress it manually and see what is the result. – jaroslawj Jul 26 '15 at 18:00
  • No, manually uncompressing files is not solving the problem. – user1285928 Jul 26 '15 at 18:23
  • 2
    When you unpack the `some_agent-1.0.tar.gz` tarball, do you end up with a directory called `some_agent-1.0`? Or something else? – larsks Jul 26 '15 at 19:31
  • yes, which directory I have to unzip it? SOURCES? – user1285928 Jul 26 '15 at 19:43
  • Now the generated RPM is empty. Looks like again I'm missing something. – user1285928 Jul 26 '15 at 21:58
  • I reverted back to the original since there was an answer that (at least partly) answered that question already by the time you modified the question and invalidated their answer. – Etan Reisner Jul 31 '15 at 18:31

2 Answers2

8

Look at the log from the build process. First, rpmbuild changes into the BUILD directory:

+ cd /home/rcbandit/rpm/BUILD

Then it untars your source tarball:

+ /usr/bin/gzip -dc /home/rcbandit/rpm/SOURCES/some_agent-1.0.tar.gz
+ /bin/tar -xf -

Then it attempts to cd into the some_agent-1.0 directory, which is presumably created by untarring the source archive:

+ cd some_agent-1.0
/var/tmp/rpm-tmp.RUwFW5: line 38: cd: some_agent-1.0: No such file or directory

This is failing. This suggests that unpacking the archive is not creating the directory RPM is looking for. You would typically solve this using an argument to the %setup macro. For example, if I have an archive named some_agent-1.0.tar.gz, and it contains:

an-unexpected-name/
an-unexpected-name/README

I would need to indicate that with the -n argument to the %setup macro:

%prep
%setup -q -n an-unexpected-name
larsks
  • 277,717
  • 41
  • 399
  • 399
  • @user1285928 If you unpack your some_agent-1.0.tar.gz, what do you get ? rpmbuild will assume it unpacks to a directory named some_agent-1.0, but from the output you show us, that is not the case. So, what's inside your tarball ? – nos Jul 26 '15 at 21:41
  • I have a small progress. I have a directory agent into the tar.gz file which is a parent to many small files. I managed to build the RPM but it's empty. any idea what I'm missing? – user1285928 Jul 26 '15 at 21:52
  • Your .spec files only create an empty directory in $RPM_BUILD_ROOT/opt/agent , you would need to copy the files you want to be installed into there as well, and you probably need to remove the `%dir` in your `%install` section too. – nos Jul 26 '15 at 21:59
  • Yes but I have 1000+ files into the package. I can't write every file name. Can you give me detailed information what I need to edit? – user1285928 Jul 26 '15 at 22:06
  • Perhaps just do `cp -ap agent/* $RPM_BUILD_ROOT/opt/agent/` Or instead of doing `install -m 0755 -d $RPM_BUILD_ROOT/opt/agent` , just copy the whole directory tree: `install -m 0755 -d $RPM_BUILD_ROOT/opt/ ` and `cp -R agent $RPM_BUILD_ROOT/opt/` – nos Jul 26 '15 at 22:21
  • I get cp: cannot stat `agent/*': No such file or directory – user1285928 Jul 26 '15 at 22:24
  • Run `pwd` in your spec file, then you will learn what the current working directory is in the %install section and discover that I made a mistake, and it should just be `cp -ap * $RPM_BUILD_ROOT/opt/agent/` . – nos Jul 26 '15 at 22:27
  • I tried to copy whole directory tree using cp -ap * $RPM_BUILD_ROOT/opt/agent/ but I get + /usr/lib/rpm/redhat/brp-java-repack-jars and the computer hangs. How I can run pwd into the spec file to see the location? – user1285928 Jul 26 '15 at 22:30
  • Thanks `%setup -q -n an-unexpected-name` was what I was searching for. – Veda Jul 18 '18 at 09:33
3
%install 
install -m 0755 -d %{buildroot}/opt/agent
cp -a your_milion_files/* %{buildroot}/opt/agent

%files 
/opt/agent

When you specify %dir, it will include just that directory and nothing else. Without %dir pragma, it will include that directory and EVERYTHING within it. So you just to copy those files in that directory in %install section.

Edit:

Let say that some-agent-1.0.tar.gz contains:

agent/binary/agent.sh
agent/data/data1.dat
agent/data/data2.dat
....
agent/data/data1000.dat
agent/LICENSE

then your sections should be:

%install
install -m 0755 -d %{buildroot}/%{_bindir}/some-agent
install -m 0755 -d %{buildroot}/%{_datadir}/
cp -a binary/agent.sh %{buildroot}/%{_bindir}/some-agent
cp -a data/* %{buildroot}/%{_datadir}/

%files
%doc LICENSE
%{_bindir}/some-agent
%{_datadir}/some-agent

Instead of the last line you can either use:

%dir %{_datadir}/some-agent
%{_datadir}/some-agent/*

but obviously

 %{_datadir}/some-agent

is shorter and have the same meaning.

In that %install section you can use any bash command. So do there what would you normally do on command line. Any shell expansion works so use that to your advance.

I used some macros - you should use them rather then hardcoded path. You can get full list of available macros by

rpm --showrc

And evaluate them by

rpm --eval '%{_datadir}'

which on recent RHEL and Fedoras evaluato to

/usr/share

I also presented little trick with LICENSE file. When it is present in topmost directory of tarfile, then you just pass the name to %doc macro and rpm will place it to correct %docdir automatically.

msuchy
  • 5,162
  • 1
  • 14
  • 26
  • I don't understand what I have to insert for `your_milion_files/*`? Maybe each file or specific directory? – user1285928 Jul 30 '15 at 16:45
  • In %prep section you correctly unpacked some-agent-1.0.tar.gz and at the beginning of %install section you are changed into ./agent/ directory, which should be topmost dirctory of your tar. So you basically copy those files to correct location in %{buildroot}. I will try to edit my answer and give specific example. – msuchy Jul 31 '15 at 12:29
  • Should use ${SourceX} instead of %{buildroot} – itsazzad Oct 08 '21 at 21:33