How to build package using directory structure and custom metadata

The simplest possible way to create Debian package is to use defined package metadata and prepared target directory structure.

Create package directory.

$ mkdir /tmp/personal-script_0.1_all

Create package metadata.

$ mkdir -p /tmp/personal-script_0.1_all/DEBIAN
$ cat << EOF | tee /tmp/personal-script_0.1_all/DEBIAN/control
Package: personal-script
Architecture: all
Depends: debconf, bash
Version: 0.1
Maintainer: Milosz <[email protected]>
Homepage: https://example.org
Priority: optional
Description: personal shell script
 personal shell script with configuration
EOF
$ cat << EOF | tee /tmp/personal-script_0.1_all/DEBIAN/conffiles
/usr/local/etc/personal-script.cfg
EOF

Create target directory structure and files.

$ mkdir -p /tmp/personal-script_0.1_all/usr/local/bin/
$ mkdir -p /tmp/personal-script_0.1_all/usr/local/etc
$ cat << EOF | tee /tmp/personal-script_0.1_all/usr/local/etc/personal-script.cfg
# configuration file
message="my message"
EOF
$ cat << EOF | tee /tmp/personal-script_0.1_all/usr/local/bin/personal-script.sh
#!/bin/bash
# shell script
source /usr/local/etc/personal-script.cfg
echo "My message is \${message}"
EOF

Set target owner and permissions.

$ sudo chown -R root:root /tmp/personal-script_0.1_all
$ sudo chmod 755 /tmp/personal-script_0.1_all/usr/local/bin/personal-script.sh
$ sudo chmod 644 /tmp/personal-script_0.1_all/usr/local/etc/personal-script.cfg

Build package.

$ dpkg-deb --build /tmp/personal-script_0.1_all /tmp

Verify package contents and metadata.

$ dpkg --contents  /tmp/personal-script_0.1_all.deb 
drwxrwxr-x root/root         0 2017-11-27 23:25 ./
drwxrwxr-x root/root         0 2017-11-27 23:25 ./usr/
drwxrwxr-x root/root         0 2017-11-27 23:25 ./usr/local/
drwxrwxr-x root/root         0 2017-11-27 23:26 ./usr/local/etc/
-rw-r--r-- root/root        42 2017-11-27 23:26 ./usr/local/etc/personal-script.cfg
drwxrwxr-x root/root         0 2017-11-27 23:26 ./usr/local/bin/
-rwxr-xr-x root/root       101 2017-11-27 23:26 ./usr/local/bin/personal-script.sh
$ dpkg --info /tmp/personal-script_0.1_all.deb 
 new debian package, version 2.0.
 size 892 bytes: control archive=344 bytes.
      35 bytes,     1 lines      conffiles            
     245 bytes,     9 lines      control              
 Package: personal-script
 Architecture: all
 Depends: debconf, bash
 Version: 0.1
 Maintainer: Milosz <[email protected]>
 Homepage: https://example.org
 Priority: optional
 Description: personal shell script
  personal shell script with configuration

Install package on target system.

$ sudo dpkg --install /tmp/personal-script_0.1_all.deb 
Selecting previously unselected package personal-script.
(Reading database ... 35902 files and directories currently installed.)
Preparing to unpack .../personal-script_0.1_all.deb ...
Unpacking personal-script (0.1) ...
Setting up personal-script (0.1) ...
$ personal-script.sh 
My message is my message

Additional information

Debian New Maintainers' Guide

Milosz Galazka's Picture

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system.