Search code examples
centosredhatrpmbuildrpm-spec

How to write a spec file to build an rpm package?


How can I write a spec file for building an rpm package, if I have only 4 tasks?

  1. Place my_file in /usr/local/mydir
  2. Place the configuration file in the rsyslog.d
  3. Place the logrotate file in logrptate.d
  4. Echo "something" /etc/programm/programm.conf

My attempt:

        1. Name: my_file
        2. Version: 1.0
        3. Release: 1
        4. Summary: A sample package
        5. Group: Applications/Productivity
        6. License: GPL
        7. Source0: my_file-1.0.tar.gz
        8. BuildArch: i386
        9. BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}.x86-64
       10.
       11. %description
       12. This package basically does nothing, but it potentially could
       13. do something useful.
       14.
       15.
       16. %prep
       17. %setup -q 
       18.
       19. %build
       20. 
       21.
       22. %install
       23. mkdir -p $RPM_BUILD_ROOT/usr/local/myfile
       24. 
       25. install my_file $RPM_BUILD_ROOT/usr/local/myfile
       26. install myfile-rsyslog.conf $RPM_BUILD_ROOT/etc/rsyslog.d
       27.
       28. install myfile-logrotate $RPM_BUILD_ROOT/etc/logrotate.d
       29.
       30. %files
       31. %defattr(-,root,root)
       32. /usr/local/myfile/my_file
       33. /etc/rsyslog.d/myfile-rsyslog.conf
       34. /etc/logrotate.d/myfile-logrotate
       35.
       36. %clean
       37. 
       38. rm -rf $RPM_BUILD_ROOT
       39.
       40. %post
       41. echo "something " >> /etc/programm/programm.conf

What did I understand wrong? Since I do not create a directory in %install, then when building rpm, the build tries to make an install to rsyslog.d and fails. What should be the correct sequence of actions? And then the task is simple - only 3 files, but solutions are offered everywhere difficult.


Solution

  • You are missing Requires and yes, you need to create those directories, if you want to install your files into them..

    Somewhere between Line 9-11:

    Requires: logrotate
    Requires: rsyslog
    

    Then in the install section:

    %{__install} -Dm 0644 myfile-rsyslog.conf $RPM_BUILD_ROOT/etc/logrotate.d/myfile-rsyslog.conf
    %{__install} -Dm 0644 myfile-logrotate $RPM_BUILD_ROOT/etc/rsyslog.d/myfile-logrotate
    

    Here, %{__install} -Dm 0644 puts the file into the target directory and creates the target directory if it's missing. Your package shouldn't own the directory.