Read LPI Linux Certification in a Nutshell Online

Authors: Adam Haeder; Stephen Addison Schneiter; Bruno Gomes Pessanha; James Stanger

Tags: #Reference:Computers

LPI Linux Certification in a Nutshell (9 page)

BOOK: LPI Linux Certification in a Nutshell
10.04Mb size Format: txt, pdf, ePub
ads
Objective 3: Manage Shared Libraries

When a program is compiled under Linux, many of the
functions required by the program are linked from system
libraries
that handle disks, memory, and other
functions. For example, when the standard C-language
printf()
function is used in a program, the
programmer doesn’t provide the
printf()
source code, but instead expects that the system already has a library
containing such functions. When the compiler needs to link the code for
printf()
, it can be found in a system
library and copied into the executable. A program that contains executable
code from these libraries is said to be
statically
linked
because it stands alone, requiring no additional code at
runtime.

Statically linked programs can have a few liabilities. First, they
tend to get large because they include executable files for all of the
library functions linked into them. Also, memory is wasted when many
different programs running concurrently contain the same library
functions. To avoid these problems, many programs are
dynamically linked
. Such programs
utilize the same routines but don’t contain the library code. Instead,
they are linked into the executable at runtime. This dynamic linking
process allows multiple programs to use the same library code in memory
and makes executable files smaller. Dynamically linked libraries are
shared among many applications and are thus called
shared
libraries
. A full discussion of libraries is beyond the scope
of the LPIC Level 1 exams. However, a general understanding of some
configuration techniques is required.

Shared Library Dependencies

Any program that is dynamically linked will require at
least a few shared libraries. If the required libraries don’t exist or
can’t be found, the program will fail to run. This could happen, for
example, if you attempt to run an application written for the GNOME
graphical environment but haven’t installed the required GTK+ libraries.
Simply installing the correct libraries should eliminate such problems.
The
ldd
utility can be used to determine
which libraries are necessary for a particular executable.

Linking Shared Libraries

Dynamically linked executables are examined at runtime by
the
shared object dynamic linker,
ld.so
.
This program looks for dependencies in the executable being loaded and
attempts to satisfy any unresolved links to system-shared libraries. If
ld.so
can’t find a specified library, it fails, and
the executable won’t run.

To find a new library,
ld.so
must be
instructed to look in
/usr/local/lib
. There are a
few ways to do this. One simple way is to add a colon-separated list of
directories to the shell environment variable
LD_LIBRARY_PATH
, which will prompt
ld.so
to look in any directories it finds there.
However, this method may not be appropriate for system libraries,
because users might not set their
LD_LIBRARY_PATH
correctly.

To make the search of
/usr/local/lib
part of
the default behavior for
ld.so
, files in the new
directory must be included in an index of library names and locations.
This index is
/etc/ld.so.cache
. It’s a binary file,
which means it can be read quickly by
ld.so
. To add
the new library entry to the cache, first add its directory to the
ld.so.conf
file, which contains directories to be
indexed by the
ldconfig
utility.

Name

ldd

Syntax
ldd
programs
Description

Display shared libraries required by each of the
programs
listed on the command line. The
results indicate the name of the library and where the library is
expected to be in the filesystem.

Example

The
bash
shell requires three shared
libraries:

#
ldd /bin/bash
/bin/bash:
libtermcap.so.2 => /lib/libtermcap.so.2 (0x40018000)
libc.so.6 => /lib/libc.so.6 (0x4001c000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
Name

ldconfig

Syntax
ldconfig [
options
]
lib_dirs
Description

Update the
ld.so
cache file with shared
libraries specified on the command line in
lib_dirs
, in trusted directories
/usr/lib
and
/lib
, and in
the directories found in
/etc/ld.so.conf
.

Frequently used options
-p

Display the contents of the current cache instead of
recreating it.

-v

Verbose mode. Display progress during execution.

Example 1

Examine the contents of the
ld.so
library
cache:

#
ldconfig -p
144 libs found in cache '/etc/ld.so.cache'
libz.so.1 (libc6) => /usr/lib/libz.so.1
libuuid.so.1 (libc6) => /lib/libuuid.so.1
libutil.so.1 (libc6, OS ABI: Linux 2.2.5) => /lib/libutil.so.1
libutil.so (libc6, OS ABI: Linux 2.2.5) => /usr/lib/libutil.so \
libthread_db.so.1 (libc6, OS ABI: Linux 2.2.5) => /lib/libthread_db.so.1
libthread_db.so (libc6, OS ABI: Linux 2.2.5) => /usr/lib/libthread_db.so
Example 2

Look for a specific library entry in the cache:

#
ldconfig -p | grep ncurses
libncurses.so.5 (libc6) => /usr/lib/libncurses.so.5
Example 3

Rebuild the cache:

#
ldconfig

After
/usr/local/lib
is added,
ld.so.conf
might look like this:

/usr/lib
/usr/i486-linux-libc5/lib
/usr/X11R6/lib
/usr/local/lib

Next,
ldconfig
is run to include
libraries found in
/usr/local/lib
in
/etc/ld.so.cache
. It is important to run
ldconfig
after any changes in system libraries
to be sure that the cache is up-to-date.

Objective 4: Use Debian Package Management

The Debian package management system is a versatile and
automated suite of tools used to acquire and manage software packages for
Debian Linux. The system automatically handles many of the management
details associated with interdependent software running on your
system.

Debian Package Management Overview

Each Debian package contains program and configuration files,
documentation, and noted dependencies on other packages. The names of
Debian packages have three common elements, including:

Package name

A Debian package name is short and descriptive. When
multiple words are used in the name, they are separated by
hyphens. Typical names include
binutils
,
kernel-source
, and
telnet
.

Version number

Each package has a version. Most package versions are the
same as that of the software they contain. The format of package
versions varies from package to package, but most are numeric
(
major.minor.patchlevel
).

A file extension

By default, all Debian packages end with the
.deb
file extension.

Figure 5-1
illustrates a Debian package name.

Figure 5-1. The structure of a Debian GNU/Linux package name

Managing Debian Packages

The original
Debian package management tool is
dpkg
, which operates directly on
.deb
package files and can be used to automate the
installation and maintenance of software packages. The alternative
apt-get
tool operates using package names,
obtaining them from a predefined source (such as CD-ROMs, FTP sites,
etc.). Both tools work from the command line.

The
dselect
command offers an interactive
menu that allows the administrator to select from a list of available
packages and mark them for subsequent installation. The
alien
command allows the use of non-Debian
packages, such as the Red Hat RPM format.

For complete information on Debian package management commands,
see details in their respective manpages.

Name

dpkg

Syntax
dpkg [
options
]
action
Description

The Debian package manager command,
dpkg
,
consists of an
action
that specifies a
major mode of operation as well as zero or more
options
, which modify the action’s
behavior.

The
dpkg
command maintains package
information in
/var/lib/dpkg
. There are two
files that are of particular interest:

available

The list of all available packages.

status

Contains package attributes, such as whether it is
installed or marked for removal.

These files are modified by
dpkg,
dselect
, and
apt-get
, and it is
unlikely that they will ever need to be edited.

Frequently used options
-E

Do not overwrite a previously installed package of the
same version.

-G

Do not overwrite a previously installed package with an
older version of that same package.

-R
(also
--
recursive
)

Recursively process package files in specified
subdirectories. Works with
-i
,
--install
,
--unpack
,
and so on.

Frequently used options
--configure
package

Configure an unpacked package. This involves setup of
configuration files.

-i
package_file
(also
--install
package_file
)

Install the package contained in
package_file
. This involves backing
up old files, unpacking and installation of new files, and
configuration.

-l
[
pattern
]
(also
--list
[
pattern
]
)

Display information for installed package names that
match
pattern
.

-L
package
(also
--listfiles
package
)

List files installed from
package
.

--print-avail
package

Display details found in
/var/lib/dpkg/available
about
package
.

--purge
package

Remove everything for
package
.

-r
package
(also
--remove
package
)

Remove everything except configuration files for
package
.

-s
package
(also
--status
package
)

Report the status of
package
.

-S
search_pattern
(also
--search
search_pattern
)

Search for a filename matching
search_pattern
from installed
packages.

--unpack
package_file

Unpack
package_file
, but
don’t install the package it contains.

Example 1

Install a package using
dpkg -i
with the
name of an available package file:

#
dpkg -i ./hdparm_3.3-3.deb
(Reading database ... 54816 files and directories
currently installed.)
Preparing to replace hdparm 3.3-3 (using hdparm_3.3-3.deb)
Unpacking replacement hdparm ...
Setting up hdparm (3.3-3) ...

Alternatively, use
apt-get install
with
the name of the package. In this case, the package comes from the
location or locations configured in
/etc/apt/sources.list
. For this example, the
location is
http://http.us.debian.org
:

#
apt-get install elvis
Reading Package Lists... Done
Building Dependency Tree... Done
The following extra packages will be installed:
libncurses4 xlib6g
The following NEW packages will be installed:
elvis
2 packages upgraded, 1 newly installed, 0 to remove
and 376 not upgraded.
Need to get 1678kB of archives. After unpacking 2544kB
will be used.
Do you want to continue? [Y/n]
y
Get:1 http://http.us.debian.org stable/main
libncurses4 4.2-9 [180kB]
Get:2 http://http.us.debian.org stable/main
xlib6g 3.3.6-11 [993kB]
Get:3 http://http.us.debian.org stable/main
elvis 2.1.4-1 [505kB]
Fetched 1678kB in 4m11s (6663B/s)
(Reading database ... 54730 files and directories
currently installed.)
Preparing to replace libncurses4 4.2-3 (using
.../libncurses4_4.2-9_i386.deb) ...
Unpacking replacement libncurses4 ...
(installation continues...)
Example 2

Upgrading a package is no different from installing one.
However, you should use the
-G
option when
upgrading with
dpkg
to ensure that the
installation won’t proceed if a newer version of the same package is
already installed.

Example 3

Use
dpkg -r
or
dpkg
--purge
to remove a package:

#
dpkg --purge elvis
(Reading database ... 54816 files and directories
currently installed.)
Removing elvis ...
(purge continues...)
Example 4

Use the
dpkg -S
command to find a package
containing specific files. In this example,
apt-get
is contained in the
apt
package:

#
dpkg -S apt-get
apt: /usr/share/man/man8/apt-get.8.gz
apt: /usr/bin/apt-get
Example 5

Obtain package status information, such as version, content,
dependencies, integrity, and installation status, using
dpkg -s
:

#
dpkg -s apt
Package: apt
Status: install ok installed
Priority: optional
Section: admin
Installed-Size: 1388
(listing continues...)
Example 6

List the files in a package using
dpkg -L
and process the output using
grep
or
less
:

#
dpkg -L apt | grep '^/usr/bin'
/usr/bin
/usr/bin/apt-cache
/usr/bin/apt-cdrom
/usr/bin/apt-config
/usr/bin/apt-get
Example 7

List the installed packages using
dpkg
-l
; if you don’t specify a pattern, all packages will be
listed:

#
dpkg -l xdm
ii xdm 3.3.2.3a-11 X display manager
Example 8

Use
dpkg -S
to determine the package from
which a particular file was installed with the
filename
:

#
dpkg -S /usr/bin/nl
textutils: /usr/bin/nl
BOOK: LPI Linux Certification in a Nutshell
10.04Mb size Format: txt, pdf, ePub
ads

Other books

Shylock Is My Name by Howard Jacobson
Luck in the Shadows by Lynn Flewelling
La Reina del Sur by Arturo Pérez-Reverte
Testament by Katie Ashley
The Search by Geoff Dyer
Bittersweet Revenge by J. L. Beck
House of Incest by Anaïs Nin
Team Player by Cindy Jefferies