Objective
# Rpm & Yum
Pkg general
- Foundation that all programs rely on: Linux kernel
- Package system maintain a database of installed files (with exact files & file locations)
Different ways of tracking pkgs & files:- Application files (track each individual file)
- Library dependencies
- Application version
1 - RPM
RedHat pkg manager
Naming of the package:
1 | Package name + Version number + Build number ()+ CPU architecture |
A pkg designed for one distro, may have unmet dependencies in another distro
- ✗: upgrade dependencies may break other pkgs
- ✗ : (Servers) distro-specific scripts / config files
- ✗ / ✓: rebuild target pkg from source (not guaranteed, if pkg naming is different for dependencies)
Commands
1 | # Install |
Extract data
rpm files = modified
cpio
archivesUsage
1
2
3
4
5
6
7
8
9
10
11# install this
sudo apt install rpm2cpio
# convert to cpio
rpm2cpio samba.src.rpm > samba.src.cpio
# extract data
cpio -i --make-directories < samba.src.cpio
# Use pipe to convert & extract to CURRENT dir (no intermediate cpio file)
rpm2cpio samba.src.rpm | cpio -i --make-directories
RPM config Files
Main rpm config file: /usr/lib/rpm/rpmc
Mostly related to CPU optimization
Optimize code from your CPU model, by passing appropriate compiler options
Make global changes: edit file
/etc/rpmrc
Build source rpm into binary rpm:
1
2
3# athlon: AMD athlon processor
# rpm pass (-02 -g -march=-686) to compiler, whenever building athlon
optflags: athlon -02 -g -marchi686Rpm can determine system architecture,
but in
/etc/rpmrc
, thebuildarchtranslate
lines causerpmbuild
to use one set of optimizations for a CPU family . e.g. For x86 systems1
2
3
4buildarchtranslate: athlon: i386
buildarchtranslate: i686: i386
buildarchtranslate: i386: i386
...By specifying below, will have a slight performance boost, but reduced portability
1
buildarchtranslate: athlon: athlon
2 - Yum
RPM meta-pkg, Yum (Yellow Dog Updater - Modified)
- Pro: Group pkgs together for distribution
Commands
1 | yum install |
Obtain pkg: yumdownloader
(download to the current dir)
GUI tools:
-
yum install yumex
-
yum install kyum
Yum config files
- Main file:
/etc/yum.conf
- Additional conf:
/etc/yum.repos.d/
Yum use files in /etc/yum.repos.d
to locate repos.
So adding new repos: add files to this directory manually, or installing an rpm (just like apt)
3 - ZYpp
openSUSE’s own pkg management
Commands
1 | # show all available plg update |
# Deb pkg
apt-cache
apt-get
dpkg
- Text-based & GUI tools (
dselect
,aptitue
,synaptic
)
1 - apt-cache
Provide info about Debian pkg database (package cache)
1 | # show pkg info |
2 - apt-get
- Config file:
/etc/apt/sources.list
- Controls apt & dselect options:
/etc/apt/apt.conf
Commands
1 | # after dselect |
Options
1 | # fix broken |
3 - dpkg
- Format:
dpkg [option] [action] [pkg]
Options
1 | # ignore deps info |
Actions
Basic
1 | # install |
Advanced
1 | # show all CURRENTLY installed pkg <== |
Other
1 | # reconfig installed pkg |
4 - Tools
dselect
(high level pkg browser)aptitude
(combines interactivedselect
& cmd-line options ofapt-get
)-
aptitude search xx
-
aptitude install xx
-
synaptic
(GUI tool)
Compare Deb pkg & other pkg formats
Deb source pkg are groups of files
- source tarball
- patch file (support only one patch file) | Rpm pkg can contain multiple patch files
-
.dsc
file (digital signature, for pkg verification)
Configure dpkg
Main dpkg config file:
/etc/dpkg/dpkg.cfg
1
2
3# alwasy dry run before install
# add this line in dpkg.cfg
no-actdpkg rely on files in
/var/lib/dpkg
(list of available & installed pkgs)This is the Debian installed file database
5 - Pkg deps & Conflicts
Convert between pkg formats (alien
)
Convert between: rpm, deb, tarballs
alien
requires both rpm & dpkg to be installed1
2
3
4
5
6
7
8# default is to .deb (--to-deb)
alien xx.rpm
# deb to rpm
alien --to-rpm xx.deb
# deb to tarball
alien --to-tgz xx.debConvert tarball to rpm
Convert a tarball: convert files in the dir structure of original tarball, use system’s root dir as base
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15# unpack
tar xvzf xx.tar.gz
# rename dir
# when create new tarball, will have file in correct locations such as /usr/bin, /usr/lib
mv xx-files usr
# pack
tar cvzf xx.tgz usr
# remove folder
rm -r usr
# convert to rpm
alien --ro-rpm xx.tgz
Deps problems
- Missing libs / support programs
- Incompatible libs / support programs
- Duplicate files / features
- Mismatched names
Workarounds
Force installation
1
2
3
4
5
6
7
8
9
10# ignore failed deps
rpm -i xx --nodeps
# ignore other errors
rpm -i xx --force
# for dpkg
dpkg --ignore-depends=[pkg] -i xx
dpkg --force-depends -i xx
dpkg --force-conflicts -i xxUpgrade / replace the depend-on pkg
Rebuild problematic pkg
1
rpmbuild --rebuild xx.src.rpm
Locate another version of the pkg
Startup Scripts Problem
- This problem only affect servers
- Old Linux use SysV startup scripts
- local startup scripts:
-
/etc/rc.d/rc.local
-
/etc/rc.d/boot.local
-
# Manage shared libs
Libraries are chosen by programmers, NOT by users.
Cannot substitute one library for another.
1 - Overview
Linux Libraries
- Multiple application that use same functions, can share same library files
- Two types of libs:
- Static Libs (statically linked libs, functions copied into applications when it’s compiled)
- Shared Libs (dynamic libs, functions loaded into memory and bound to application, when program is launched)
Library Principles
- Provide commonly used program fragments (use one copy multiple times)
- Linux widget sets: Qt, GTK+ (GIMP Tool Kit)
- Linux C library: glibc (GNU C library,
/lib/x86_64-linux-gnu/libc.so.6
)
- Most programs use libs as shared libraries (dynamic libraries)
- Executable include references to shared lib files
- May increase program load time, and other issues:
- programs must be able to locate shared libs
- If an important shared lib become inaccessible (due to overwritten), system might not even boot
Linux shared libs are similar to Windows DLLs (Dynamic Link Libraries,
.dll
)Linux shared libs:
.so
(shared objects )
Linux static libs:.a
(used by linkers for inclusion in programs)
2 - Locate lib files
System search for function’s lib file in a specific order
-
LD_LIBRARY_PATH
env var - Program’s
PATH
env var - Directory
/etc/ld.so.conf.d
- File
/etc/ld.so.conf
- Directory
/lib*/
and/usr/lib*/
Note:
/etc/ld.so.conf
loads config file from directory/etc/ld.so.conf.d
/lib*/
directories are for libs needed by system utilities, that reside in/bin/
and/sbin/
/usr/lib*/
are for libs needed by additional software (e.g. MySQL DB utilities)
If another library is located in /etc/ld.so.conf
, and listed above include
,
Then system will search that lib directory, before files in /etc/ld.so.conf.d
1 | cat /etc/ld.so.conf |
Configure library path (global config file / env var)
1 | # original /etc/ld.so.conf file (load all file in ld.so.conf.d) |
Change path temporarily
1 | # add 2 dirs |
Correcting problems
1 | # If system cannot find the lib file, usually because lib isn't installed |
3 - Loading Dynamically
When program starts, dynamic linker is responsible for finding program’s needed lib functions.
After they’re located, dynamic linker will copy them into memory, and bind to programs.
1 | # locate dynamic linker executable |
Summary
- Shared libs (dynamic libs)
- Load library: search
LD_LIBRARY_PATH
first - Update lib cache:
ldconfig
- View libs required by a program:
ldd
4 - Commands to manage libs
ldd
: show program’s shared libldconfig
: update system’s cache & links
Show shared lib deps
ldd
attempts to find true library (denote by=>
)When us
ldd
to track down problems,
ensure to check the needs of program’s all libs, and all libs used by first-tier libs
1 | ldd /bin/ls |
Update lib cache
Library cache is a catalog of lib directories & all the various libs contained within.
- Manage lib loading:
ld.so
,ld-linux.so
- Programs above don’t read from
ld.so.conf
every time, but from cached list (/etc/ld.so.cache
)- Need to update cache, every time when add / remove libs (
ldconfig
)- rpm & dpkg will run
ldconfig
automatically, after install / remove pkgs
1 | # print current info |
Develop New Libs
Add path to env var
LD_LIBRARY_PATH
1
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/kk/devs
After testing, move it to
/usr/lib*/
Create lib config file in
/etc/ld.so.conf.d/
, which points to lib file’s locationUpdate lib cache (
sudo ldconfig
)
# Manage Process
uname
ps
top
jobs
,nice
,kill
1 - Overview
When Linux system first boots, a special process is started (init process).
This is the core of Linux system.
Understand Kernel
uname
: print system info
1 | # node name |
2 - Multiple Screens
In a text-based system, use terminal multiplexer to perform operations on multiple screens
(use pts terminal, pseudo-terminal)
- screen
- tmux
screen
Prefix shortcut:
Ctrl + A
(C - A)Need to create window in each focus
1 | # see screen list |
Example: Steps to create screen with 3 windows
1 | # 1. vertical split |
Stress test system’s CPU & memory (stress-ng
)
1 | stress-ng --class cpu -a 10 -b 5 -t 3m \ |
tmux
screen
2.0 version- Prefix shortcut:
Ctrl + B
(C - B)
1 | # see window list |
Example: Steps to create tmux with 3 windows
1 | # 1. vertical split |
3 - Process Status
ps auxwf | grep bash
ps
supports 3 different styles of cmd-line options (historical reasons)
- BSD style (
xx
) - Unix style (
-xx
) - GNU long options (
--xx
)
1 | # all process by current shell (default) |
Select Process with ps
During trouble shooting: View only a selected subset of processes
1 | # remove restriction of association with terminal |
Real & effective groups / users:
- REAL (upper case): This is the user / group the account is associated with, when log into system
- effective (lower case): user / group uses temporary alternative user / group ID (SUID / GUID)
If you want to see all process, use both effective & real options.
1 | ps -u kk -U kk |
Displayed info
- PPID : parent process ID
- TTY : teletype (identify a terminal)
- C: processor utilization over process lifetime
- STIME: system time (when process starts)
- CPU Priority: default is
0
. Negative values have higher priority (need to set as root, for values < 0) - RSS : resident set size (memory used by the program & its data)
- CMD: commands to launch the process
Process States
ps -ef
: Some CMD are shown in brackets.
These processes are currently swapped out from physical memory into virtual memory on the hard drive (Process state: sleeping).
Linux kernel puts a process to sleep mode, while process is waiting for an event. When event triggers, kernel sends process a signal.
- Interruptible sleep: process receives signal immediately, and wake up
- Uninterruptible sleep: process only wakes up based on external event (e.g. hardware becomes available)
Zombie process: Parent process does not recognize process’ termination signal
4 - top
top
is the dynamicps
variantGUI variant;
kpm
,gnome-system-monitor
Only to check system uptime:
Shows system load, but not CPU
1 | uptime |
Benchmark: familiar with purposes & normal habits of programs running on your system
top
sorts processes based on %CPU by default
1 | # refresh rate (default is 5 sec) |
load average: can equal to number of CPU cores, before competition for CPU time begins (1-min, 5-min. 15-min)
e.g. Quad-core CPU (can be as high as 4.0)
It’s common for 1-min load to be high (short burst of activities)
If the 15-min load average is high, system might be in trouble
CPU info
- Process owner (user / system process)
- Process state (running / idle / waiting)
Columns
- PR: process priority
- NI: nice value
- VIRT: total virtual memory
- RES: total physical memory
- SHR: total memory shared with other processes
- %CPU: share of CPU time
- %MEM: share of available physical memory
- TIME+: total CPU time used, since process starts
- S: process status
-
D
: interruptible sleep -
I
: idle -
R
: running -
S
: sleeping -
T
: traced / stopped -
Z
: zombie
-
Use watch
for monitoring (refresh every 2 sec).
1 | watch uptime |
5 - jobs
Summarize process launched from your current shell
Provide job ID numbers
Ensure all programs have terminated, before logging out
No background process is running, that’s launched from the current shell
jobs
command
1 | -l # check job PID |
fg & bg
Move paused program to foreground:
1 | # pause program |
Send running program to background:
Each background process is tied to the session’s terminal 📌
1 | # pause program |
Note:
-
+
sign: last job added to bg stack -
-
sign: second-to-last job added to bg stack
All previously added jobs will show no sign.
Other commands:
Redirect nohup output messages -
nohup.out
1 | # run a job in background |
nohup
will force the application to ignore any input from stdin
.
It dis-associates process from terminal, and the process loses the output link to the monitor.
By default, stdout
and stderr
are redirected to ~/nohup.out
6 - Process priorities
nice
renice
nice
- Range:
[-20, 19]
(nice
default is10
) - Only root may launch program with negative priority value
- Default for program run without
nice
is0
1 | # assign 12 to program, and pass to txt |
renice
- Only root can modify other user’s process priority
- Only root can decrease priority value
1 | # assign 7 to pid 2390, for all process owned by kk |
Summary: Normal user cannot set value < 0 in
nice
, cannot decrease value inrenice
7 - Kill Process
Process Signals
Signals are also written as
SIG
+ Name.e.g.
TERM
->SIGTERM
Number | Name | Description |
---|---|---|
1 | SIGHUP |
Hang Up |
2 | SIGINT |
Ctrl + C - Interrupt |
9 | SIGKILL |
kill - Force kill |
15 | SIGTERM |
Soft kill |
11 | SIGSEGV |
Segments violation |
3 | SIGQUIT |
Stop running |
20 | SIGTSTP |
Ctrl + Z - Stop (but does not terminate, program is still in memory) |
18 | SIGCONT |
Restart a stopped process |
Kill
1 SIGHUP
: terminates interactive program, cause daemons to re-read config file
1 | # view all numbered signals |
Kill a process
- Try
TERM
first - Then
HUP
/INT
- Use
KILL
as last resort (might lead to corrupted files)
Killall: Variant of kill
(kills process based on name, but not pid)
1 | # kill all process with name vim |
Pkill: Variant of kill
(use selection criteria)
Use
pgrep
to test out selection criteria prior to sending signals
1 | # all process attached to terminal 2 |
# Practice
Source pkg & Binary pkg
Source pkg requires more time to compile (must be compiled prior to installation)
Both rpm & dpkg provide tools to create binary pkg (can be installed directly) from source code
Binary pkg creation is useful, if you’re running Linux on a peculiar CPU
(But cannot recompile for different CPU architecture)Tarballs are preferred, when distribute for other platforms (universal)
More About Source pkg & Binary pkg
- Source package include a tarball of the application’s source code, and instructions on building it
When you install the package, it builds and compiles everything on-site, then installs - [FAST] Binary packages have everything already built, and installing the package just takes everything out of it
Rpm & Dpkg
Databases for these two are separate. Hence these two are incompatible with one another
Pkg management system don’t share info, and databases don’t actively conflict
Dpkg often provides more extensive initial setup options
For problematic pkgs:
- rpm can rebuild from source (
rpmbuild --rebuild
) - Debian source pkgs are rare. Less likely to rebuild dpkg from source
- rpm can rebuild from source (