Han Tree Tree

You adore the light so you will never fear the darkness. -- Summer Palace


  • Home

  • Archives

Debian-Package

Posted on 2019-06-28

Similar to .rpm, .pkg and .dmg, debian package(.deb) is a installation pacakge which is meant for debian OS. It is easy to build, install and interrept. Debian package can be used for file installation and also file compression. This article this a short guide to build a simple debian package and install it to another machine.
The example used in this article is to install a systemd service, which describe in the previous blog, to a new machine.

Debian Package Structure

Debian packages are standard Unix ar archives that include two tar archives. One archive holds the control information and another contains the installable data.

The following tree diagram shows a very simple structure of .deb package. Inside DEBIAN folder does it store all the control related scripts and information.
For other folders and files rather than DEBIAN folder, it is as if the debian package is the root directory. For example, the intended installation is /home/han/Documents, in debian package, just put the file inside folder example.deb/home/han/Documents, during installation, the file will be installed to the intended location. As simple as that!

To install a systemd service, the files should be in the directory /lib/systemd/system. So accoridng to the debian package structure, we should save the origial file under ./installer/lib/systemd/system/example.service

1
2
3
4
5
6
7
8
#!/bin/bash
#Path: ./installer/home/han/test_service.bash
/usr/bin/java -jar /home/structo/HelloWorld.jar
while :
do
echo "Looping...";
sleep 30;
done

Pre and Post Scripts

Pre and Post Scripts can be added to DEBIAN folder. As name suggests, they will be executed before and after the installation, carrying pre and post installation tasks.
In our example, the preinst scipt will archive the example.service file into a archive diretory and postinst script will execute systemctl command to enable the systemd service.

The following preinst script archive the old version of systemd service.

1
2
3
4
5
6
7
#!/bin/bash
CURRENT=/lib/systemd/system/example.service
TARGET=/home/han/archive/example.service
if [-f "$CURRENT" ]; then
rm $TARGET
mv $CURRENT $TARGET
fi

The following postinst script executes systemctl command to enable to systemd service.

1
echo "your_sudo_password" | sudo -S systemctl enable example

Build a Package

  1. Name the content folder accoridng to the naming convention
    Debian package naming convention is:
1
<project>_<major version>.<minor version>-<package revision>

For example, our example installation package can be named as

1
installer_1.0-1
  1. Create the folder structure according to the explanation in the previous section
1
2
3
4
5
6
7
8
example.deb
├── DEBIAN
│   ├── preinst
│   └── postinst
└── lib
└── systemd
└── system
└── example.service
  1. Add control file to DEBIAN folder
1
2
3
4
5
6
7
8
9
10
11
Package: example
Version: 1.0-1
Section: base
Priority: optional
Architecture: i386
Maintainer: Eugene Han <youremail@email.com>
Description: Hello World
This installer installs nothing at all :)
Yes, absolutely nothing :)))

(the space before each line in the description is important)
  1. Build the pacakge
    Build the debian package by executing
1
dpkg-deb --build installer_1.0-1

A file named installer_1.0-1.deb will be generated.

Install a Package

To install the package, simply execute

1
sudo dpkg -i installer_1.0-1.deb

Restart your PC and use sudo systemctl status example, you should be able to see the service running.

Systemd Basics

Posted on 2019-06-07

Systemd is a Linux system tool which can be used to start, schedule, monitor, manage system processes and customer applications. I used systemd while configuring my minimal Debian application and run some application upon OS bootup and manage the lifecycle of applications.

This blog can guides you through some of the basics of systemd services.

Create a Custom systemd Service (Use HelloWorld as an example)

  1. Create a script or executable that the service will manage.
1
2
3
4
5
6
7
8
#!/bin/bash
#Path: /home/han/test_service.bash
/usr/bin/java -jar /home/structo/HelloWorld.jar
while :
do
echo "Looping...";
sleep 30;
done
  1. Make it executable.
1
[sudo] chmod +x /home/structo/test_service.sh
  1. Create Service file.
1
2
3
4
5
6
7
8
9
10
11
#Path: /lib/systemd/system/myservice.service
[Unit]
Description=Example systemd service.

[Service]
Type=simple
ExecStart=/bin/bash /home/structo/test_service.sh
User=structo

[Install]
WantedBy=multi-user.target
  1. Start and enable the service.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# reload systemd service after editing
[sudo] systemctl daemon-reload

# start service immediately
[sudo] systemctl start myservice

# read the status and partial log of myservice
[sudo] systemctl status myservice

# stop myservice
[sudo] systemctl stop myservice

# restart myservice
[sudo] systemctl restart myservice

# my executing enable command, a symblic link will be formed to
multi-user.target.wants
# which means the service will be started every time system reboots
[sudo] systemctl enable myservice
Created symlink from /etc/systemd/system/multi-user.target.wants/
myservice.service to /lib/systemd/system/myservice.service.

# to view the full log of the service
[sudo] journalctl -u myservice.service

Eugene Han

a minion in software engineering industry

2 posts
2 tags
GitHub E-Mail Instagram
© 2019 Eugene Han
Powered by Hexo
|
Theme — NexT.Mist v5.1.4