M01: Introduction to Operating Systems TU3: Installing specific purpose software
ASIX1
Pràctical Exercise 2: How to create a .deb and .rpm package
16-05-2017

PRACTICAL EXERCISE 2: HOW TO CREATE A .deb AND .rpm  PACKAGE

GENERAL CONDITIONS

1- Deadline: 28-05-2016.
2- Send your files attached to an e-mail with the following specifications:
     a) E-mail address:
cf(at)collados.org or jordi.binefa(at)fje.edu depending who is your teacher
     b) File Name:

        b.1)
ASIX1 (Catalan): asix1_surname_name_m01tu03pr2.tar.gz
        b2.) DAW1 (English): daw1_surname_name_m01tu03pr2.tar.gz     
     c)
Subject:
         c.1)
ASIX1 (Catalan): asix1_surname_name_m01tu03pr2
         c.2)
DAW1 (English): daw1_surname_name_m01tu03pr2
3- Make this report individually.
4- Left, right, top and bottom margins: 2cm.
5- Character format: a) Font:Times New Roman (or Liberation Serif), b) Size: 10, c) Questions typeface: Bold, d) Answers typeface: Regular.
6- Page numbering on footer bar.


DOCUMENTATION

1- How to install software on Debian


Installing software on Linux involves most of the time running a package managers to download a sofware package from a software repositories, not downloading and running .exe files from websites like on Windows.  Additional methods of installing software on your system can involve download, compile and install sofware  from its source code or download a software package to your hard disk and install tje software with the help of a package installer

A software package is a special kind of file used to distribute and install software (text editors, web browsers, libraries, servers,compiler, interpreters, drivers....) on your operating system. A software package consists of  a set of files gathered into one single file. Some of those files are the software itself that will be installed on the system. Other files contains metada, in other words, information about the software (software's name, version number, dependencies and so on). Also, a package can contain extra related material such as manuals, guides, examples and tutorials. On Linux,  a usual method (not the only) to install software on the operating system is by downloading and installing a software package that provide us with the desired software. On Debian and Ubuntu, deb is the format, as well as extension, of software packages. For instance, geany_1.24.1+dfsg-1_amd64.deb is the software package
used to distribute and install geany on the 64 bits version of Debian 8.6. On RedHat, Fedora, CentOS or SuSE, rpm is the format, as well as extension, of software packages. For instance, geany_1.30.1-1.fc27x86_64.rpm is the software package used to distribute and install geany on the 64 bits version of Fedora.

A package management system or package manager
is a collection of software tools that provides to system administrators  an easy method of installing, upgrading, configuring, and removing software packages. Also, with the help of a package manager, a system administrator can effortlesly automate the process of installing, upgrading, configuring, and removing software packages in a consistent manner. The dpkg family of tools is used to install .deb packages  on Debian and its derivatives, such as Ubuntu and Mint. Programs such as dpkg, apt-get, aptitude or Synaptic are part of the dpkg family of tools.

A software repository
is a storage location from which software packages (both source and binary) may be retrieved and installed on a computer. Normally, a software repository is stored in a server  accessible via an internet connection. The dpkg family of tools usually downloads and intalls  .deb files from software repositories stored in a server accessible via an HTTP or FTP connection.

Another method of installing .deb packages on Debian is via a package installer  such as Gdeb that provides us with a simple way to install a local (i.e, stored in your computer) .deb package. Gdebi is a command line tool but  a graphical user interface is provide by the software developer as well.

Sometimes you will install software unpacking a binary archive because some Linux software is distributed in precompiled form designed to run on any Linux distribution without installation. For example, the “Dynamic” download Skype offers is a .tar.bz2 file. This is just an archive, like a ZIP file — you’d extract it to a folder on your computer and double-click the executable inside it to run it. In other words, it is like installing software on Windows systems. That is not by far a typycal way of installing software on Debian and its derivatives.

Also, you can install software via a tarball files. A Tarball archive  is a  .tar.gz or .tar.bz2 file usually stored in a server accessible via an internet. Tarballs are used for programs which are not compiled, i.e. they are presented as source code. In order to install this kind of software you should follow this procedure: a)  Extract data from the tarball archive, b) Compile the source code, c) Install the software on your system. Sometimes that is a difficult  way to install software and, in order to make it easier, developers provide us with an extra script called configure that checks if everything you need to complie your software is installed on your system (compilers, libraries,..)  and builds a new file called Makefile. Sometimes, Makefile is directly provided by the software developer. A software developement tool called make, with the help of information stored in Makefile,  will build and install the new software on your system. Most of the time. a Makefile archive provides make with information about how to uninstall and remove software previously installed.  


2- How to buid a simple tarball software packege

Step 1) Source code development


We are going to develop a software that shows us our login name at system. Make a new directory called loginteller on your system. Change to the newly created directory. Start an instance of Geany and write the folliwing source code in C language:

/* loginteller.c
 * Based on monousuar.c at http://www.binefa.net/gnu/gcc/processos/Informacio_d_usuari.html
 * www.binefa.cat
 * 20120507
*/
#include <stdio.h>
#include <unistd.h>    // getlogin()
#include <stdlib.h>    // exit()

int main(){
    char *szLogin;
   
    if((szLogin = getlogin())==NULL){
        perror("getlogin");
        exit(EXIT_FAILURE);
    }  
    printf("Your login name at the system is : %s\n",szLogin);

    return (0);
}


In order to check if your code works properly, run the following commnads:

dacomo@inf1-dacomo:~/loginteller$ gcc  loginteller.c  -o  loginteller
dacomo@inf1-dacomo:~/loginteller$ ./loginteller

Your terminal will show you the following message:

Your login name at the system is: dacomo

Of course, the message displayed by the terminal depends on what your username is in the system.

Step 2) Creating a Makefile archive

A Makefile typically starts with some variable definitions which are then followed by a set of target entries for building specific targets (typically .o & executable files in C) or executing a set of command associated with a target label. We are going to develop a Makefile for easily:
a) Compiling the source code
b) Cleaning binary files
c) Installing binary files on your system
d) Uninstalling binari files on your system

Start an instance of Geany and write the folliwing soure code in C language:


############################################################################
# Makefile for building: loginteller
# 20120508 - www.binefa.cat
#############################################################################
####### Compiler, tools and options

CC              = gcc
CFLAGS          = -o
INSTALL_PROGRAM = install -m 755 -p
DEL_FILE        = rm -f
####### Files
SOURCES   = loginteller.c
DESTDIR   = /usr/local/bin
TARGET    = loginteller
####### Compilation
loginteller:
    $(CC) $(SOURCES) $(CFLAGS) $(TARGET)
clean:
    $(DEL_FILE) $(TARGET)
####### Install
install: $(TARGET)
    $(INSTALL_PROGRAM) $(TARGET) $(DESTDIR)
#    install -m 755 $(TARGET) $(DESTDIR)
uninstall:
    $(DEL_FILE) $(DESTDIR)/$(TARGET)
#    rm -f $(DESTDIR)/$(TARGET)


Now, you can easily:
a) Compile loginteller.c running the following command: make (or make loginteller)
b) Remove an old binary
running the following command: make clean
c) Install a binary
on your system running as the root user the following command: make install
d) Uninstall a binary on your system running as the root user the following the command: make uninstall

Step 3) Creating a .tar.gz file

Run the following command:

dacomo@inf1-dacomo:~/loginteller$ tar xfz loginteller.tar.gz loginteller.c Makefile  

3- How to build a .deb package

In order to build a .deb package you need an application called checkintall, your source code and Makefile (both files in a same directory). If checkinstall is no installed, run the command: sudo aptitude install checkintall. Now, if checkinstall is intalled on your system and loginteller.c and Makefile are in a same directory:

a)
Remove any old binary running make clean and uninstall the software running make uninstall.

b)
Run checkinstall:
dacomo@inf1-dacomo:~/loginteller$ sudo checkinstall

checkinstall 1.6.2, Copyright 2009 Felipe Eduardo Sanchez Diaz Duran
           This software is released under the GNU GPL.


The package documentation directory ./doc-pak does not exist.
Should I create a default set of package docs?  [y]:

Preparing package documentation...OK

*** No known documentation files were found. The new package
*** won't include a documentation directory.

Please write a description for the package.
End your description with an empty line or EOF.
>> It tells current login name at the system
>>

*****************************************
**** Debian package creation selected ***
*****************************************

This package will be built according to these values:

0 -  Maintainer: [ root@inf1-dacomo ]
1 -  Summary: [ It tells current login name at the system ]
2 -  Name:    [ loginteller ]
3 -  Version: [ 20170516 ]
4 -  Release: [ 1 ]
5 -  License: [ GPL ]
6 -  Group:   [ checkinstall ]
7 -  Architecture: [ amd64 ]
8 -  Source location: [ loginteller ]
9 -  Alternate source location: [  ]
10 - Requires: [  ]
11 - Provides: [ loginteller ]
12 - Conflicts: [  ]
13 - Replaces: [  ]

Enter a number to change any of them or press ENTER to continue: 0
Enter the maintainer's name and e-mail address:
>> dacomo@inf1-dacomo  (NOTE: dacomo must be change into your user name)

This package will be built according to these values:

0 -  Maintainer: [ dacomo@inf1-dacomo ]
1 -  Summary: [ It tells current login name at the system ]
2 -  Name:    [ loginteller ]
3 -  Version: [ 20170516 ]
4 -  Release: [ 1 ]
5 -  License: [ GPL ]
6 -  Group:   [ checkinstall ]
7 -  Architecture: [ amd64 ]
8 -  Source location: [ loginteller ]
9 -  Alternate source location: [  ]
10 - Requires: [  ]
11 - Provides: [ loginteller ]
12 - Conflicts: [  ]
13 - Replaces: [  ]

Enter a number to change any of them or press ENTER to continue: 3
Enter new version:
>> 0.1

This package will be built according to these values:

0 -  Maintainer: [ dacomo@inf1-dacomo ]
1 -  Summary: [ It tells current login name at the system ]
2 -  Name:    [ loginteller ]
3 -  Version: [ 0.1 ]

loginteller-0.1-2.i386 rpm 4 -  Release: [ 1 ]
5 -  License: [ GPL ]
6 -  Group:   [ checkinstall ]
7 -  Architecture: [ amd64 ]
8 -  Source location: [ loginteller ]
9 -  Alternate source location: [  ]
10 - Requires: [  ]
11 - Provides: [ loginteller ]

12 - Conflicts: [  ]
13 - Replaces: [  ]

Enter a number to change any of them or press ENTER to continue: 6
Enter the new software group:
>> utilities

This package will be built according to these values:

0 -  Maintainer: [ dacomo@inf1-dacomo ]
1 -  Summary: [ It tells current login name at the system ]
2 -  Name:    [ loginteller ]
3 -  Version: [ 0.1 ]
4 -  Release: [ 1 ]
5 -  License: [ GPL ]
6 -  Group:   [ utilities ]
7 -  Architecture: [ amd64 ]
8 -  Source location: [ loginteller ]
9 -  Alternate source location: [  ]
10 - Requires: [  ]
11 - Provides: [ loginteller ]
12 - Conflicts: [  ]
13 - Replaces: [  ]

Enter a number to change any of them or press ENTER to continue:

Installing with make install...


========================= Installation results ===========================
gcc loginteller.c -o loginteller
install -m 755 -p loginteller /usr/local/bin

======================== Installation successful ==========================

Copying files to the temporary directory...OK

Stripping ELF binaries and libraries...OK

Compressing man pages...OK

Building file list...OK

Building Debian package...OK

Installing Debian package...OK

Erasing temporary files...OK

Writing backup package...OK
OK

Deleting temp dir...OK


c) 
At this moment a new .deb package loginteller_0.1-1_amd.64.deb has been build in your working directory and automatically the package has been installed on your system. Run the following command:

dacomo@inf1-dacomo:~/loginteller$ ls -ls

and your terminal will show you the following files:


backup-051620171311-pre-logiteller.tgz  description.pak  loginteller  loginteller_0.1-1_amd64.deb loginteller.c Makefile

 and the the command:

dacomo@inf1-dacomo:~/loginteller$ aptitude search loginteller

and your terminal will show you the following message:

i    loginteller                - It tells current login name at the system

d) If you want to remove loginteller from your system run the following command: sudo  dpkg  -r  loginteller

e) If you want to install loginteller through the terminal run the following command:  sudo  dpkg  --install  loginteller_0.1-1_amd64.deb

f) To install loginteller from GUI --> click on loginteller_0.1-1_amd64.deb with the right button of your mouse and select the option "OptionWith" --> "GDebi Package Installer". Press the button "Install Pacakge" in order to install loginteller on your system.




4- How to build a .deb package for a bash script

In this section we will learn how to build a .deb package for a bash script with dependencies. In order to follow this section you should download   loginteller2.tar.gz.  The contents of loginteller2.tar.gz are a  script called loginteller2.sh. It shows your current login name and the contents of your personal folder in a tree-like format. In order to run loginteller2.sh, a command called tree (which is able to list contents of directories in a tree-like format) must be installed in your system. In other words, tree is a dependency of loginteller2.sh.  This is the loginteller2.sh source code:

#!/bin/bash
# loginteller2.sh -  shows your current login name and the contents of your personal folder in a tree-like format
# 20160506 - Binefa & Collados
clear
echo "Your login name at the system is : $USER"
echo
echo "Press  the <Enter> key to show the contents of your personal folder"
read
echo
tree /home/$USER
exit 0


and a  Makefile archive to install and uninstall loginteller2.sh on your system. This is the Makefile content:

#############################################################################
# Makefile for building: loginteller2.sh
# 20160506 - Binefa & Collados
#############################################################################

####### Files
DESTDIR       = /usr/local/bin
TARGET        = loginteller2.sh

####### Install and uninstall
install: $(TARGET)
    install -m 755 $(TARGET) $(DESTDIR)

uninstall:
    rm -f $(DESTDIR)/$(TARGET)


Make a new directory called loginteller2 on your system. Change to the newly created directory. Download and unpackage loginteller2.tar.gz. Now, run checkinstall and create a new .deb package with the following configuration:

0 -  Maintainer: [ dacomo@inf1-dacomo ]
1 -  Summary: [
It tells current login name at the system ]
2 -  Name:    [ loginteller2 ]
3 -  Version: [ 0.1 ]
4 -  Release: [ 1 ]
5 -  License: [ GPL ]
6 -  Group:   [ utilities ]
7 -  Architecture: [ amd64 ]
8 -  Source location: [ loginteller2 ]
9 -  Alternate source location: [  ]
10 - Requires: [ tree ]
11 - Provides: [ loginteller2 ]
12 - Conflicts: [  ]
13 - Replaces: [  ]


Pay close attention to the entry 10 - Requires:. In order to work properly, loginteller2.sh needs tree installed on the system and therefore, you have to fill correctly this entry. If tree is no intalledon your system, the following message will be displayed:

Installing Debian package... FAILED!

*** Failed to install the package

Do you want to see the log file?  [y]:

But, if tree was previously installed on your system, everything will work fine and the following message will be displayed:

  ======================== Installation successful ==========================

Copying files to the temporary directory...OK

Stripping ELF binaries and libraries...OK

1-Compressing man pages...OK

Building file list...OK

Building Debian package...OK

Installing Debian package...OK

Erasing temporary files...OK

Writing backup package...OK
OK

Deleting temp dir...OK


**********************************************************************

 Done. The new package has been installed and saved to

 /home/dacomo/loginteller2/loginteller2_0.1-1_amd64.deb

 You can remove it from your system anytime using:

      dpkg -r loginteller2

**********************************************************************


Now a new .deb package loginteller2_0.1-1_amd.64.deb has been build in your working directory and automatically the package has been installed on your system. Run the following command:

dacomo@inf1-dacomo:~/loginteller$ ls -ls

and your terminal will show you the following files:


backup-051620171648-pre-logiteller2.tgz  description.pak  loginteller2_0.1-1_amd64.deb loginteller2.sh Makefile

 and the the command:

dacomo@inf1-dacomo:~/loginteller$ aptitude search loginteller

and your terminal will show you the following message:

i    loginteller2                - It tells current login name at the system

5- How to convert a  .deb package into a .rpm pacakage

According to
How to Convert DEB to RPM  (or RPM to DEB), if you want to convert a .deb package into a .rpm package you should

a) Install a software package alien.

b) Run alien -r  package_name.deb to convert a deb file into a rpm file.

In the the following example, firstly, alien is installed on the system, and secondly, alien converts loginteller_0.1-1_amd64.deb into loginteller-0.1-2.i386 rpm. Now, you can install loginteller on Red Hat, Fedora, SuSE or CentOS.

dacomo@inf1-dacomo:~$ sudo aptitude install alien
dacomo@inf1-dacomo:~/loginteller$ sudo alien -r loginteller_0.1-1_amd64.deb
loginteller-0.1-2.x86_64.rpm generated
dacomo@inf1-dacomo:~/loginteller$ ls -ls loginteller*
8 -rwxrwxr-x 1 jordi jordi 7281 2012-05-08 12:16 loginteller
4 -rw-r--r-- 1 root  root  2414 2012-05-08 11:37 loginteller_0.1-1_amd64.deb
4 -rw-r--r-- 1 root  root  4076 2012-05-09 19:58 loginteller-0.1-2.x86_64.rpm
4 -rw-rw-r-- 1 jordi jordi  462 2012-05-08 11:28 loginteller.c


PRACTICAL EXERCISE

1- Build
loginteller_0.1-1_amd.64.deb. Write your personal information in the Maintainer entry of the checkinstall form.
2-
Build loginteller2_0.1-1_amd.64.deb. Write your personal information in the Maintainer entry of the checkinstall form.
3-
Create a .rpm file for each .deb file created previously.
4-
Download the following source code: http://www.collados.org/asix1/m01/tu3/userinfo.c. Create a Makefile to compile, clean, install and uninstall userinfo on your system.
5- Build a .deb package called userinfo_0.3-2_amd64.deb with userinfo.c and the Makefile archive created in the previous question.
6- Convert
userinfo_0.3-2_amd64.deb into a .rpm package.
7- Create a Makefile archive to install and uninstall script03.sh on your system. Build a new .deb package called script03_0.2-5_amd64.deb.
8-
Download the following  code: http://www.collados.org/asix1/m01/tu3/fjeclot.tar.gz and:
    a) Modify Makefile to install and uninstall fjeclot.py.
    b) Create and install a new .deb package called fjeclot_0.1-1_amd64.deb with the following information:
         Maintainer: jordi.binefa@fje.edu
        Sumary: Python sample
        Name: fjeclot
        Version: 0.1
        Release: 1
        License: GPL
        Group: utilities
        Architecture: amd64
        Source location: fjeclot.py
        Requires: python-wxgtk3.0
        Provides: fjeclot.py
    c)
If your system shows any error message, read carefully the message and try to solve the problem installing the proper software. Check if the package has been installed on your system.
    d) Run fjeclot.
9- Save loginteller_0.1-1_amd64.deb, loginteller-0.1-2.x86_64.rpmuserinfo_0.3-2_amd64.deb, script03_0.2-5_amd64.deb and fjeclot_0.1-1_amd64.deb files into a .tar.gz (Read carefully GENERAL CONDITIONS at the top of this web page)