Tuesday, September 6, 2016

Perform post install scripting for Debian packages. Especially useful for setting file permissions

There are plenty of thorough references for creating Debian .deb packages, but not enough short examples that present the bare essentials. This is a minimal example with post install scripting -especially useful for setting file permissions- which is done specifically with the DEBIAN/postinst file.



The files that configure and command the Debian packaging and subsequent installation all reside in a directory named DEBIAN. Here are all the files I include in my DEBIAN directory:
  1. DEBIAN/conffiles
  2. DEBIAN/control
  3. DEBIAN/postinst
  4. DEBIAN/rules



Below are the contents of the files (contents changed to protect the proprietary)

  1. DEBIAN/conffiles
    ...blank...
    
  2. DEBIAN/control
    Package: foo-for-bar
    Version: 1.0.40
    Section: Network
    Priority: optional
    Architecture: all
    Depends:
    Maintainer: Jane Doe jane.doe@example.com
    Description: Acme FooBar WWW UI
     User interface for Acme FooBar.
    
  3. DEBIAN/postinst
    #!/usr/bin/env bash
    cat $0
    chmod 777 /var/lib/foobar/conf/www/*
    chmod 777 /var/lib/foobar/*
    chmod 777 /var/lib/foobar/foobaringdaemon.py
    chmod 777 /etc/local.d/80_figlets_with_foobar.sh
    echo "hello there from postinst file"
    
  4. DEBIAN/rules
    ...blank...
    

General File Structure

Let's say I want a .deb package to install files that will reside in the /var/www and /etc/local.d directories. In my case I'll create everything in the /home/ directory. Here is the root and first layer of directories.
  • /home/FOOBAR/DEBIAN
  • /home/FOOBAR/etc
  • /home/FOOBAR/var
With all the above as environment to operate on, here is a .deb creation command:

cd /home/
dpkg-deb --build FOOBAR

The above dpkg-deb would create a file named FOOBAR.deb
To install the FOOBAR.deb package on a machine, you would run:

dpkg -i FOOBAR.deb

A parting note. I would rename my FOOBAR.deb to FOOBAR.1.0.40.deb to make evident the version number of the package.