13

I need to create deb package that will create directory for logs. I want to create directory /var/logs/my_package and to change it's owner to my_user.

In the docs there is information that I can create file debian/dir. But there is info that this is not the best way to do it. And there is no info how one should change there directory owner (I'm thinking about placing command chown my_user.my_user /var/logs/my_packageit in debian/postinst file).

What is the recommended way to create directory with the deb package?

Avinash Raj
  • 78,556
bessarabov
  • 2,002

3 Answers3

11

You were right, you need a debian/my_package.postinst file to perform such operation:

#!/bin/sh

#DEBHELPER#

set -e

DIR="/var/log/my_package/" USER="my_user"

mkdir -p ${DIR}
if id -u ${USER} > /dev/null 2>&1; then
chown ${USER}:${USER} ${DIR} fi

Note: The script checks if the user exists before calling chown.

kayn
  • 121
  • 2
    I'm extremely sorry, but this does not answer my question. – bessarabov Mar 31 '14 at 05:10
  • Please explain why this postinst script does not answer your original question. I'm a bit puzzled now. – Sylvain Pineau Mar 31 '14 at 06:34
  • 1
    I'm sorry that I haven't explain it in the proper manner. Your solution works fine and this is what I have implemented. But the docs say This generally means there is a problem with the Makefile. I understand this message that there is some other way to do it. And I want to find out what is the recomened way to do this task. – bessarabov Apr 01 '14 at 18:30
  • 1
    The directory creation could be done in debian/rules which is the Makefile for debian packages. Since I didn't know your debian/rules I preferred to propose a solution based on a single file. But the chown command must be a post-installation process. debian/dirs should be avoided but my initial answer is perfectly fine. Please vote and accept it, thanks. – Sylvain Pineau Apr 01 '14 at 18:49
  • There is a typo. The ending should be postinst and not postint. – kayn Oct 26 '20 at 12:20
6

You do not need to create a postinst script, but the solution is still a bit tricky. I use dh wildcard in debian/rules:

#!/usr/bin/make -f
%:
        dh $@

binary: dh $@

which does all the needed stuff. But I need to override some dir's ownership (lets call it data). So I must make some exception, I use special directives as follow:

    override_dh_install:
        dh_install   #calls default *.install and *.dirs installation
        install -d -o www-data -g www-data $(CURDIR)/debian/<package_name>/var/www/<something>/data 

The data directory do not need to be in *.dirs file. But still there is one trick. Debhelper contains script dh_fixperms, which would fix ownership back to root, so we need to override too:

override_dh_fixperms:
    dh_fixperms --exclude data

That's all. Keep in mind the data dir does not count as a conffile, so it will be removed on package removal. If you need to make it a conffile, it is another story.

Alexis Wilke
  • 2,707
  • 1
    Does this require that the user and group exists on the system where the package is built? And if yes, is there a way to ensure that with a Debian-based mechanism? – moritz Jun 12 '16 at 11:54
  • 1
    This will create the directories on the system used to build the debian package, not the system the package is installed on. – gerardw Mar 03 '18 at 17:22
  • @moritz Yes. That is the drawback of this technique. It needs to exist on both systems. – Alexis Wilke Jan 15 '22 at 17:37
  • I don't think that it makes sense to have a *.dirs and then use install -d ... since both create the directories. On my end, I simply use chown ... in the rules file. – Alexis Wilke Jan 15 '22 at 17:38
0

I've read that if you have to use the debian/[<package>.]dirs file, that means your make install step did not do its job properly. That step should create that folder.

Here are the issues I can see with this:

  1. make install can't install anything under a directory other than /usr (for a Debian package), so it's useless if you want a directory under /etc/..., /var/lib/<package>/..., etc.

  2. If you use mkdir -p ... in your debian/[<package>.]postinst script, then you need to manually remove it with an rmdir ... in your debian/[<package>.].postrm script.

So the solution proposed by Marek Šimon is the same as mine, the only difference is that I use chown ... instead of install -d ... although with the install -d ... you can avoid the debian/[<package>.]dirs altogether. Personally, I like to see a list of the directories I create in a separate file. It looks much clearer.

Example

Create the directories:

# in debian/package.dirs
/etc/package/magic/configuration
/var/www/package/data

Change the ownership:

# in debian/rules
override_dh_fixperms:
    dh_fixperms
    chmod 700 debian/tmp/var/www/package/data
    chown www-data:www-data debian/tmp/var/www/package/data

WARNING: The debian/rules file uses tabs to indent the code under a target name. This is important.

As shown in the example, I also often change the permissions (chmod).

The debian/tmp/... introducer is required if you create two or more packages. I'm not too sure what it is if you are creating a single package (in part because it's really rare for my projects to have a single package and also many paths change when you add a second package so instead I always include at least a package-doc.install even if mostly empty so that way it won't change on me).

Final Note

If you are creating a public Debian package which is to be accepted by Debian or Ubuntu, you probably want to follow Sylvain Pineau's solution. Keep in mind that on a purge you need to remove your directory (unless the user created its own files in it).

Alexis Wilke
  • 2,707