Transcript UNIX

Application Installation
Guntis Barzdins
Girts Folkmanis
Artūrs Lavrenovs
“Hello World” palaišana
unix% cat > hello.c
#include <stdio.h>
int main() {
printf("Hello World!\n");
return 0;
}
Ctrl/D
unix%gcc hello.c
unix%./a.out
Hello World!
unix%
gcc
the Gnu Compiler Collection
 Frontend for:

C: gcc

C++: g++

More (ada, java, objective-c, fortran, …)
 Backend for:

x86, ia-64, ppc, m68k, alpha, hppa, mips, sparc, mmix, pdp-11, vax, …
gcc: Usage
 gcc foo.cpp

Outputs ./a.out binary
 gcc -g -Wall –pedantic foo.cpp
bar.cpp baz.o –o foo



-g: produce debugging information
-Wall –pedantic: many types of warnings
-o foo: output to file “foo”
 Linking libraries:

eg math library: –lm

The library foo is named libfoo.a under unix
Linking
o
o
o
Combines the program object code with other object code to
produce the executable file.
The other object code can come from the Run-Time Library,
other libraries, or object files that you have created.
Saves the executable code to a disk file. On the Linux system,
that file is called a.out.
o
If any linker errors are received, no executable file will be generated.
Translation Process
Static Linking
bar.o
libm.a
foo.o
libc.a
printf.o & fopen.o
linker(ld)
fully linked executable object file
a.out
Merging Relocatable Object Files
into an Executable Object File
Relocatable Object Files
system code
.text
system data
.data
Executable Object File
0
headers
system code
main()
m.o
a.o
main()
.text
int e = 7
.data
a()
.text
int *ep = &e
int x = 15
int y
.data
.bss
.text
a()
more system code
system data
int e = 7
int *ep = &e
int x = 15
uninitialized data
.symtab
.debug
.data
.bss
Dynamic Linking
a.o
b.o
linker
bar.o
-fPIC
libfoo.so (position independent shared
object)
linker
Partially linked executable –
dependency on libfoo.so
a.out
loader (execve)
dynamic linker (ld-linux.so)
fully linked executable in memory
Creating a static library
 Indexed archive with *.o files
 static library for linking:
libsomething.a

create .o files: gcc –c helper.c

ar rlv libsomething.a *.o

ranlib libsomething.a == ar -s

use library as gcc –L/your/dir –lsomething
Creating a dynamic library
 Details differ for each platform

gcc –shared –fPIC –o libhelper.so *.o
 use same as for static (-llibrary)
 also $LD_LIBRARY_PATH

Today used rarely and sometimes disabled completely

Security and compatibility issues

Replaced by ldconfig /etc/ld.so.conf
Library distribution
 Distribution software management
systems provide libraries that
installed software depend on
 Mostly dynamic libraries
 Some static libraries – mostly for
development
 Or just to be safe provide both
Program Development Using gcc
Editor
Source File pgm.c
Preprocessor
Modified Source Code in RAM
Compiler
Program Object Code File pgm.o
Other Object Code Files (if any)
Linker
Executable File a.out





Unix sw development
environments
Emacs
Kdevelop, Eclipse, Netbeans
Xcode
Vim
Geany
Other Programming Tools
 There are tools to ease single
programmer development of multiple
file projects (make)
 There are version control tools
for multi programmer projects:
CVS, SVN, GIT
 There are “packaging” tools to
ease installing programs: Tarball,
RPM, DEB,...
The make program
If in a directory full of C files there is a text file with the
default name makefile then running make will cause it to be
used by the make program
The power of make is that it examines timestamps. If a
change is made to part2.c and make run again, only part2.c
is compiled. The linker links the old part1.o, old main.o
and the new part2.o to make the final program.
Makefiles
• When programming large applications
– Better to modularise.
– Keep individual source files small.
– Instead of one large file.
• Difficult to edit.
• Slow to compile.
– Not easy to do manually.
– Use a “makefile.”
Makefile example
• Consider, program contained in two
separate source files.
– “main.c”
– “sum.c”
– Both contain header “sum.h”
• Require executable file to be named “sum”
• Next slide shows a simple makefile that
could be used.
Makefile example
Comment line preceded with “#”
# Make file for sum executable
“Dependency line”
sum: main.o sum.o
gcc –o sum main.o sum.o
“Action line”
main.o: main.c sum.h
gcc –c main.c
sum.o: sum.c sum.h
Dependency line + Action
gcc –c sum.c
line = “Rule”
Action line must begin with a tab character
Dependency line must start in column 1
make
 make maintains dependency graphs
 based on modification times
 Makefile as default name
 make [–f makefile] [target]
 if node newer than child, remake
child
tab!
target ...: dependency
command
command
make environment
 Environment variables (PATH, HOME,
USER, etc.) are available as
$(PATH), etc.
 Also passed to commands invoked
 Can create new variables (gmake):
export FOOBAR = foobar
make variables
$@ name of current target
$? list of dependencies newer than target
$< name of dependency file
$* base name of current target
$% for libraries, the name of member
 implicit rules, e.g., a .c file
into .o
.c.o:
$(CC) $(CFLAGS) $<
make
all: hello clean
clean:
rm –f *.o
helper.o: helper.c
$(CC) $(CFLAGS) -o $@ $<
OBJ = helper.o \
hello.o
hello: $(OBJ)
$(CC) $(CFLAGS) $(LDFLAGS) –o $@ $(OBJ)
make depend
depend: $(CFILES) $(HFILES)
$(CC) $(CFLAGS) –M $(CFILES) > .state
# works for GNU make and BSD make
#if 0
include .state
#endif
#include “.state”
Revision Management
 Diffutils

Find differences among files (diff and diff3)

Update a set of files from generated differences (patch)
 cvs

Maintains a history of changes for groups of files

Including: who, when, what, and optionally why

Analogue of Visual SourceSafe

New alternatives: subversion (SVN) and bitkeeper
 git – SCM UNIX way
Revision Management
What CVS lets you do
 Modify source code locally until
it’s ready to be merged with what
everyone sees

Multiple people can modify the same file and be ok
 Develop multiple versions of
software simultaneously

And merge these branches later on
 Recall past versions of code base

Based on time ago or version
 See committer's comments on their
git
 Linux kernel needed SCM after
proprietary system BitKeeper withdraw
free usage
 Requirements – distributed workflow,
speed, safety, FOSS
 Non-existent at that time
 Linus Torvalds created such at 2005
 Now de-facto SCM for UNIX development
 Locally stored full repository, no
remote server required for working
Packaging Approaches:
Source vs. Binary
 There are two
fundamentally different
approaches for
packaging-based software
distributions:


providing source packages containing
the vendor sources plus instructions for
automated build and installation.
providing binary packages containing
the final installation files only.
 Most packaging
facilities support both
approaches, although
often not equally well.
 Both approaches have
source
package
binary
package
distribution size


package size


package
dependencies


installation
reproducability


installation
run-time stability


installation system
alignment


installation time


Dependencies



Dependencies management is a very useful feature of
package management software
They keep systems in a consistent state and
guarantee the applications to run in the expected way
rpm or dpkg commands have limited dependencies
management features

They can report which library a package relies on,
but the library can itself rely on other packages…
Main Package Distribution
Formats in Linux
 There is no standard package manager in Linux
 Packages Distributed in Binaries or Source
Code form
 Main Package Management Standards

Tarball files (.tar.gz/.tar.bz2)






RPM (RedHat Package Manager) (.rpm)


The old-fashioned way of distributing software in Linux/Unix
Usually available for all FOSS projects on their web pages
Default method of distribution for rarely used software, e.g., scientific
Compatible with all distros
Main package manager in Slackware, Gentoo
Introduced by RedHat and has been adopted by many other distributions (Fedora,
Mandrake, SuSe) .
DEB (Debian Package Manager) (.deb)
Installing from Tarball files
 Software Packages coming in source code
archives have to be compiled before
installed
 Usually come in .tar.gz or .tar.bz2 archives
 Typical compilation/installation steps

Unpack the archive:



Change to the extracted directory


make
Install the package as follows:


./configure
Build the source code using the GNU Make utility as follows:


cd <extracted_dir_name>
Run source configuration script as follows:


tar xzvf <package_name>.tar.gz
tar xvjf <package_name>.tar.bz2
make install
“INSTALL” or “README” files also exist in this directory giving application-specific
Backup Tools
 tar











tar jxvf mytarball.tar.bz2 - Extract files from mytarball.tar.bz2
tar zxvf mytarball.tar.gz - Extract files from mytarball.tar.gz
-z : Use gzip compression
-j : Use bzip2 compression
-x : Extract
-c: Create
-v : Verbose
-f : Use file
Gzip compression is used for tarballs with the extensions .tar.gz, .tgz, and .tar.Z.
Bzip2 compression which is slightly better but requires more CPU is used in tarballs with the
extensions .tar.bz2 and .tbz2.
tar jcvf mytarball.tar.bz2 my_files - Creates a tarball mytarball.tar.bz2 with the specified files.
 gzip file - Will compress file using gzip compression
and will rename "file" to "file.gz“
 gunzip file.gz - Will uncompress file compressed using
gzip compression and will rename "file.gz" to "file“
 bzip2 file - Will compress file using bzip2 compression
and will rename "file" to "file.bz2“
 bunzip2 file.bz2 - Will uncompress file compressed
using bzip2 compression and will rename "file.bz2" to
Apache Example (Historic)
1.
2.
3.
4.
5.
6.
7.
8.
9.
gunzip apache_xxx.tar.gz
tar -xvf apache_xxx.tar
gunzip php-xxx.tar.gz
tar -xvf php-xxx.tar
cd apache_xxx
./configure --prefix=/www --enable-module=so
make
make install
cd ../php-xxx
10. Now, configure your PHP. This is where you customize your PHP
with various options, like which extensions will be enabled. Do a
./configure --help for a list of available options. In our example
we'll do a simple configure with Apache 1 and MySQL support. Your
path to apxs may differ from our example.
./configure --with-mysql --with-apxs=/www/bin/apxs
11. make
12. make install
If you decide to change your configure options after installation,
you only need to repeat the last three steps. You only need to
restart apache for the new module to take effect. A recompile of
Apache is not needed.
Note that unless told otherwise, 'make install' will also install PEAR,
various PHP tools such as phpize, install the PHP CLI, and more.
Managing Software in RedHatbased distributions
 Using the command line, packages are
installed using rpm utility program

Install a package


Update an existing package


rpm -i <package_name>.rpm
rpm –U <package_name>.rpm
Remove a package

rpm –e <package_name>
 yum - Yellowdog Updater, Modified
 Package management utility for RPM
 Used by most rpm based distros – RHEL, Fedora, CentOS,
etc.
 Solves dependency and update issues
Three ways to manage software
packages in Debian
 dpkg: Used on .deb files like rpm

Install: dpkg -i <package_name>.deb


If an older version of the package is installed it updates it automatically by
replacing it with the new
Remove: dpkg -r <package_name>
 dselect: dpkg console front-end
 apt-get: The most frequently used way of
managing software packages in Debian.

Install: apt-get install <package_name>


e.g. apt-get install kde to install KDE Window Manager
Remove: apt-get remove <package_name>
Package Management
 Using RPM




To install or upgrade a package: rpm -Uvh package-1.0.i386.rpm
To remove a package: rpm -e package-1.0
To determine what version of a package you have installed: rpm -qa | grep
package_name
To determine what package a file "belongs" to: rpm -qf filePackage
Management Cont.
 Using Apt






To update your package lists: apt-get update
To install all updated packages: apt-get dist-upgrade
To install a package: apt-get install package
To upgrade a package: apt-get upgrade package
To then remove that package (and all packages that depend on it): apt-get
remove package
To search for a package: apt-cache search search_string
Other Packaging Methods
 The Gentoo Linux case (emerge)

Deals mostly with source files


Fetches packages and compiles them according to compilation parameters given
in /etc/make.conf
E.g. emerge kde #Fetches, compiles and installs
packages for KDE
 YAST, in Suse
 Aptitude – replacement for apt-get
 Better dependency managment and state tracking
 Less broken systems and unsuccessful installs
 Today almost no advantages
Gentoo Portage features
 Provides ebuild scripts which
download, patch, compile, and
install packages.
 Modeled on the ports-based BSD
distributions.
 Dependency checking, extreme
customization.
 Original source tarballs are
downloaded.

No need to wait for someone to make a binary package for your
distribution.
 The user specifies what they want,
and the system is built to their
Portage tree
 The Portage tree is a collection
of ebuilds, files that contain all
information Portage needs to
maintain software (install,
search, query, ...). These ebuilds
reside in /usr/portage by default.
 The Portage tree is usually
updated with rsync:


# emerge --sync
# emerge-webrsync
 To search for all packages who
Package Lifecycle
BEGIN
developer
administrator
Evaluation of
application
Checkout RPM
Specification
from CVS
Fetch Source
RPM from
Repository
Fetch Binary
RPM from
Repository
Edit RPM
Specification
(.spec)
Track Vendor
Sources
Unpack
Source RPM
Install
Application
into Instance
Fetch Vendor
Sources
Unpack Vendor
Sources
Remove
Application
from Instance
upgrade
Configure & Build
Application
Install
Application
Commit RPM
Specification
to CVS
Roll Source RPM
Package
Roll Binary RPM
Package
Store Source
RPM into
Repository
Store Binary
RPM into
Repository
END
My hack: This universe.
Just one little problem:
core keeps dumping.
On-line Package Repositories
 Large package bases on the Web



Accessible via FTP or HTTP
http://rpm.pbone.net/
http://www.apt-get.org/text/?site=4
You can learn how to build Your own distribution
http://www.linuxfromscratch.org/
Using rpm
 rpm –i install package, check for
dependencies
 rpm –e erase package
 rpm –U
upgrade package
 rpm –q query packages (e.g., -a =
all)
rpm -q
rpm -q -i telnet
Name
: telnet
Relocations: (not relocateable)
Version
: 0.17
Vendor: Red Hat, Inc.
Release
: 18.1
Build Date: Wed Aug 15 15:08:03 2001
Install date: Fri Feb 8 16:50:03 2002
Build Host:
stripples.devel.redhat.com
Group
: Applications/Internet
Source RPM: telnet-0.17-18.1.src.rpm
Size
: 88104
License: BSD
Packager
: Red Hat, Inc. <http://bugzilla.redhat.com/bugzilla>
Summary
: The client program for the telnet remote login protocol.
Description :
Telnet is a popular protocol for logging into remote systems over the
Internet. The telnet package provides a command line telnet client.
Install the telnet package if you want to telnet to remote machines.
This version has support for IPv6.
Building your own rpm: spec
#
# spec file for hello world app
#
Summary: hello world
Name: hello
Version: 1.0
Release: 1
Copyright: GPL
Group: Applications/Test
Source: http://www.cs.columbia.edu/IRT/software/
URL: http://www.cs.columbia.edu/IRT/software/
Distribution: Columbia University
Vendor: IRT
Packager: Henning Schulzrinne <[email protected]>
BuildRoot: /home/hgs/src/rpm
%description
The world's most famous C program.
Building your own rpm: spec
%prep
rm -rf $RPM_BUILD_DIR/hello-1.0
zcat $RPM_SOURCE_DIR/hello-1.0.tgz | tar -xvf -
%build
make
%install
make ROOT="$RPM_BUILD_ROOT" install
%files
%doc README
/usr/local/bin/hello
/usr/local/man/man1/hello.1
%clean
Building your own rpm
 create ~/.rpmmacros
%_topdir /home/hgs/src/test/rpm
 cd /home/hgs/src/test/rpm/SPECS
 rpm -ba --buildroot /home/hgs/tmp hello-1.0.spec
 creates binary and source RPM
APT





Most popular package manager in the world
APT is a system created in the Debian community to
automatically manage the packages dependencies
APT can install, remove and upgrade packages,
managing dependencies and downloading the
packages
It’s a frontend to other tools, and it uses the
underlying package management system, like the
rpm or dpkg commands
It’s able to fetch packages from several media
(cdrom, ftp, http, nfs), and it can be used to create
ad-hoc software repositories
APT – Using
(1/3)
[root]@[/] # apt-get install nautilus
Reading Package Lists... Done
Building Dependency Tree... Done
The following extra packages will be installed:
bonobo libmedusa0 libnautilus0
The following NEW packages will be installed:
bonobo libmedusa0 libnautilus0 nautilus
0 packages upgraded, 4 newly installed, 0 to remove and 1 not
upgraded.
Need to get 8329kB of archives. After unpacking 17.2MB will be
used.
Do you want to continue? [Y/n]
APT – Using
(2/3)
[root]@[/] # apt-get remove gnome-panel
Reading Package Lists... Done
Building Dependency Tree... Done
The following packages will be REMOVED:
gnome-applets gnome-panel gnome-panel-data gnome-session
0 packages upgraded, 0 newly installed, 4 to remove and 1 not
upgraded.
Need to get 0B of archives. After unpacking 14.6MB will be freed.
Do you want to continue? [Y/n]
APT – Using
(3/3)
[root]@[/] # apt-cache search pdf
kghostview - PostScript viewer for KDE
tetex - The TeX text formatting system.
xpdf - A PDF file viewer for the X Window System
…
[root]@[/] # apt-cache show xpdf
…
Filename: xpdf-1.00-3.i386.rpm
Description: A PDF file viewer for the X Window System.
Xpdf is an X Window System based viewer for Portable Document
Format (PDF) files. Xpdf is a small and efficient program which
uses standard X fonts.
FreeBSD – Hybrid package management
 Binary packages
 Uses FreeBSD repositories
 Common ./configure options for compilation
 pkg_add -r <package_name>
 pkg_info
 pkg_delete <package_name>\*
 Rather simplistic, no updates
pkg_delete <package_name>
 pkg_add -r <package_name>
pkgng – new generation pkg
 solves lots of issues
 new advanced functionality


FreeBSD – Ports Collection (Source packages)
 Download snapshot (index) of ports
collection from FreeBSD mirrors
 portsnap fetch
 Extract index as directory tree to
/usr/ports
 portsnap extract
 Install software package
 cd /usr/ports/*/lighttpd
 make install clean
 Downloads and installs (compiles) tarballs (also all
dependencies)
 Asks for configuration options (frontend for ./configure)
FreeBSD – what is Port?
 Contents of directory in
/usr/ports/<cat>/<name>
 Some info files
 Dependency list
 Link to download original tarball from
developer site
 List of tarball files and SHA256
 Patch files
 Configuration files
 Configuration options
 Makefile which does all the work