Gentoo Linux Kézikönyv: Munka a Gentoo operációs rendszerrel
Handbook:Parts/Working/Portage/hu
Mik azok a USE jelölőzászlók?
Az ötlet az USE jelölőzászlók mögött
A Gentoo telepítésekor a felhasználók döntenek arról, hogy milyen környezettel dolgoznak. A szerver beállítása eltér a munkaállomás beállításaitól. A játékra szánt munkaállomás különbözik a 3D renderelő munkaállomástól.
Ez nem csak a telepítendő csomagok kiválasztására igaz, hanem arra is, hogy egy adott csomagnak milyen funkciókat kell támogatnia. Ha nincs szükség OpenGL-re, akkor miért foglalkozna valaki azzal, hogy az OpenGL-t telepítse és tartsa karban, és ebből kiindulva akkor a legtöbb csomagba miért fordítsa bele az OpenLG támogatást? Ha valaki eleve nem akar KDE-t használni, akkor miért is foglalkozna az illető a KDE támogatással a csomagok lefordítása közben, tudván, hogy a binárisra lefordított kész csomagok majd anélkül is hibátlanul fognak működni?
Azért, hogy segítsen a felhasználóknak eldönteni, hogy mit telepítsenek/aktiváljanak és mit ne, a Gentoo azt akarta, hogy a felhasználó könnyen meghatározza a környezetét. Ez arra kényszeríti a felhasználót, hogy eldöntse, mit is akar valójában, és megkönnyíti a Portage csomagkezelő számára a hasznos döntések meghozatalát.
Az USE jelölőzászló definíciója
Amikor azt látja, hogy: "Írja be a USE jelölőzászlókat". Egy ilyen jelölőzászló egy kulcsszó, amely egy bizonyos koncepció támogatását és függőségi információit testesíti meg. Ha egy bizonyos USE jelölőzászló engedélyezve van, akkor a Portage csomagkezelő tudni fogja, hogy a rendszergazda támogatást kér a kiválasztott kulcsszóhoz. Természetesen ez megváltoztathatja a csomag függőségi információit. A USE jelölőzászlótól függően előfordulhat, hogy sok további függőséget kell behúzni a kért függőségi változtatások teljesítéséhez.
Nézzünk meg egy konkrét példát: A kde
USE jelölőzászlót. Ha ez a jelölőzászló nincs beállítva a USE változóban (vagy ha az érték előtt mínuszjel van: -kde
), akkor minden opcionális KDE támogatással rendelkező csomag a KDE támogatása nélkül lesz lefordítva. Minden csomag amely opcionális KDE-függőséggel rendelkezik az a KDE könyvtárak (mint függőségek) nélkül lesz telepítve.
Forráskódcsomagoknál, amikor a kde
jelölőzászló engedélyezve van, akkor azok a csomagok KDE támogatással lesznek forráskódból futtatható bináris programkódra lefordítva, és a KDE könyvtárak függőségként lesznek telepítve.
A USE jelölőzászlók helyes definiálásával a rendszer kifejezetten a rendszergazda igényeihez lesz szabva.
USE jelölőzászlók használata
Állandó USE jelölőzászlók deklarálása
Minden USE jelölőzászló (angolul "USE flag") a USE változóban (angolul "USE variable") van deklarálva. Annak érdekében, hogy a felhasználók könnyebben kereshessék és könnyebben választhassák ki a USE jelölőzászlókat, már biztosítunk egy alapértelmezett USE összeállítást. Ez az összeállítás olyan USE jelölőzászlók gyűjteménye, amelyekről azt gondoljuk, hogy a Gentoo felhasználók gyakran használják. Ez az alapértelmezett összeállítás a kiválasztott profil részét képező make.defaults fájlokban van deklarálva.
A rendszer által figyelt profilra az /etc/portage/make.profile symlink mutat. Minden profil a többi profilon felül működik, így a végeredmény az összes profil összege. A felső profil a base profil (/profiles/base).
Az (összes) aktuálisan aktív USE jelölőzászló megtekintéséhez, használja az emerge --info parancsot:
root #
emerge --info | grep ^USE
USE="a52 aac acpi alsa branding cairo cdr dbus dts ..."
Ez a változó már elég sok kulcsszót tartalmaz. Ne módosítson egyetlen make.defaults fájlt sem, azért hogy a USE változót a személyes igényekhez szabja. Ha a Gentoo kódtárolója frissül, akkor ezekben a fájlokban a módosítások visszavonásra kerülnek.
Ennek az alapértelmezett beállításnak a módosításához adjon hozzá kulcsszavakat a USE változóhoz, vagy távolítsa el azt. Ez megtehető globálisan a USE változó definiálásával az /etc/portage/make.conf fájlban. Ebben a változóban felveheti a szükséges extra USE jelölőzászlókat, vagy eltávolíthatja azokat a USE jelölőzászlókat, amelyekre már nincs szükség. Ez utóbbi úgy történik, hogy a kulcsszó elé mínuszjelet (-
) helyezünk.
Például a KDE és a Qt támogatásának megszüntetéséhez, de az LDAP támogatás hozzáadásához a következő USE jelölőzászló beállítás definiálható az /etc/portage/make.conf fájlban:
/etc/portage/make.conf
USE frissítése a make.conf fájlbanUSE="-kde -qt5 ldap"
USE jelölőzászlók deklarálása egyéni csomagok részére
Néha a felhasználók egy bizonyos USE jelölőzászlót szeretnének deklarálni egyetlen (vagy csak néhány) alkalmazáshoz, de nem rendszerszinten szeretnék azt megtenni. Ennek a műveletnek az eléréséhez a /etc/portage/package.use fájlt kell szerkeszteni. A package.use jellemzően egyetlen fájl, de lehet ugyanezen a néven könyvtár is amely fájlokat tartalmaz. Az egyezményes használattal kapcsolatban lásd az alábbi tippet, majd további információkért lásd a man 5 portage súgót. A következő példák feltételezik, hogy a package.use egyetlen darab fájl.
Például azért, hogy a VLC médialejátszó csomagnak csak Blu-ray támogatása legyen:
/etc/portage/package.use
Blu-ray támogatás engedélyezése a VLC médialejátszó számáramedia-video/vlc bluray
Ha a package.use már könyvtár formájában létezik (egyetlen fájllal szemben), akkor a csomagok USE jelölőzászlói módosíthatók a package.use/ könyvtárban található fájlok egyszerű létrehozásával. Bármely fájlelnevezési konvenció működhet, de bölcs dolog egy koherens elnevezési sémát megvalósítani. Az egyik konvenció az, hogy egyszerűen a csomag nevét használjuk a mappában lévő fájl címeként. Például a media-video/vlc csomag
bluray
használata USE jelölőzászlójának beállítása a következőképpen hajtható végre:root #
echo "media-video/vlc bluray" >> /etc/portage/package.use/vlc
Hasonlóképpen lehetőség van egy adott alkalmazásnál a USE jelölőzászlók használatának kifejezetten letiltására. Például PHP-ben a bzip2 támogatás letiltásához (de minden más csomagnál meglegyen a USE jelölőzászló deklarációjával a make.conf-ban):
/etc/portage/package.use
A bzip2 támogatás kikapcsolása a PHP részéredev-lang/php -bzip2
Ideiglenes USE jelölőzászlók deklarálása
Néha a felhasználóknak egy rövid pillanatra be kell állítaniuk egy USE jelölőzászlót. Az /etc/portage/make.conf kétszeri szerkesztése helyett (a USE változtatások végrehajtása és visszavonása helyett) csak deklarálja a USE változót környezeti változóként. Ne feledje, hogy ez a beállítás csak a megadott parancsra vonatkozik. Az alkalmazás újbóli emerge-legenerálása vagy frissítése (akár kifejezetten, akár a rendszerfrissítés részeként) visszavonja azokat a változtatásokat, amelyeket az (ideiglenes) USE jelölőzászló definíciója váltott ki.
A következő példa ideiglenesen eltávolítja a pulseaudio
értéket a USE változóból a SeaMonkey telepítése során:
root #
USE="-pulseaudio" emerge www-client/seamonkey
Elsőbbség
Természetesen van egy bizonyos elsőbbség, hogy melyik USE beállítás élvez elsőbbséget a melyik USE beállítással szemben. A USE beállítás elsőbbsége prioritás szerint rendezve (az elsőnek a legalacsonyabb a prioritása):
- Az alapértelmezett USE beállítás deklarálva van a make.defaults fájlban amely része az Ön profiljának.
- Felhasználó által definiált USE beállítás a /etc/portage/make.conf fájlban/mappában.
- Felhasználó által definiált USE beállítás a /etc/portage/package.use fájlban/mappában.
- Felhasználó által definiált USE beállítás környezeti változóként létrehozva.
A végső USE beállítás megtekintéséhez a Portage által látottak szerint futtassa az emerge --info parancsot. Ez felsorolja az összes releváns változót (beleértve a USE változót is) a jelenlegi definíciójukkal együtt, ahogyan azt a Portage ismeri.
root #
emerge --info
Az egész rendszer adaptálása az új USE jelölőzászlókhoz
A USE jelölőzászlók megváltoztatása után a rendszert frissíteni kell azért, hogy tükrözze a szükséges változtatásokat. Azért, hogy ezt elvégezze, futtassa az emerge parancsot a --newuse
kapcsolóval:
root #
emerge --update --deep --newuse @world
Ezután futtassa a Portage depclean parancsát, hogy eltávolítsa azokat a feltételes függőségeket, amelyek a "régi" rendszeren jelentek meg, de amelyeket az új USE jelölőzászlók elavultnak.
Ellenőrizze még egyszer az "elavult (obsoleted)" csomagok listáját, azért hogy megbizonyosodjon arról, hogy nem távolítja el azokat a csomagokat amelyekre szükség van. A következő példa szerint adja hozzá a
--pretend
(-p
) kapcsolót, azért hogy a depclean csak kilistázza a csomagokat anélkül, hogy ténylegesen megkezdődne azok eltávolítása:root #
emerge --pretend --depclean
Ha a depclean befejeződött, akkor az emerge kérheti az alkalmazások újbóli legenerálását, amelyek dinamikusan kapcsolódnak az esetlegesen eltávolított csomagok által biztosított megosztott objektumokhoz. A Portage megőrzi a szükséges könyvtárakat mindaddig, amíg ezt a műveletet el nem végzik, azért hogy meg legyen akadályozva az alkalmazások megtörése. Az újra legenerálandót a preserved-rebuild
készletben tárolja. Futtassa a szükséges csomagok újraépítéséhez:
root #
emerge @preserved-rebuild
Ha mindez megvalósult, akkor a rendszer az új USE jelölőzászlókat használja.
Csomagspecifikus USE jelölőzászlók
Elérhető USE jelölőzászlók megjelenítése
Vegyük a seamonkey példáját: Milyen USE jelölőzászlókra figyel? Ennek kiderítéséhez használjuk az emerge-t a --pretend
és --verbose
opciókkal:
root #
emerge --pretend --verbose www-client/seamonkey
These are the packages that would be merged, in order: Calculating dependencies... done! [ebuild N ] www-client/seamonkey-2.48_beta1::gentoo USE="calendar chatzilla crypt dbus gmp-autoupdate ipc jemalloc pulseaudio roaming skia startup-notification -custom-cflags -custom-optimization -debug -gtk3 -jack -minimal (-neon) (-selinux) (-system-cairo) -system-harfbuzz -system-icu -system-jpeg -system-libevent -system-libvpx -system-sqlite {-test} -wifi" L10N="-ca -cs -de -en-GB -es-AR -es-ES -fi -fr -gl -hu -it -ja -lt -nb -nl -pl -pt-PT -ru -sk -sv -tr -uk -zh-CN -zh-TW" 216,860 KiB Total: 1 package (1 new), Size of downloads: 216,860 KiB
Ehhez a munkához nem az emerge az egyetlen eszköz. Valójában van egy equery nevű eszköz is a csomaginformációkhoz, amely az app-portage/gentoolkit csomagban található.
root #
emerge --ask app-portage/gentoolkit
Most futtassa a equery parancsot a uses
argumentummal egy bizonyos csomag USE jelölőzászlóinak a megtekintéséhez. Például az app-portage/portage-utils esetében:
user $
equery --nocolor uses =app-portage/portage-utils-0.93.3
[ Legend : U - final flag setting for installation] [ : I - package is installed with flag ] [ Colors : set, unset ] * Found these USE flags for app-portage/portage-utils-0.93.3: U I + + nls : Add Native Language Support (using gettext - GNU locale utilities) + + openmp : Build support for the OpenMP (support parallel computing), requires >=sys-devel/gcc-4.2 built with USE="openmp" + + qmanifest : Build qmanifest applet, this adds additional dependencies for GPG, OpenSSL and BLAKE2B hashing + + qtegrity : Build qtegrity applet, this adds additional dependencies for OpenSSL - - static : !!do not set this during bootstrap!! Causes binaries to be statically linked instead of dynamically
REQUIRED_USE feltételek kielégítése
Néhány ebuild megköveteli vagy tiltja a USE jelölőzászlók bizonyos kombinációit a megfelelő működés érdekében. Ezt a REQUIRED_USE kifejezésben elhelyezett feltételek halmazán keresztül fejezzük ki. Ezek a feltételek biztosítják, hogy az összes szolgáltatás és függőség teljes legyen, és hogy a build sikeres lehessen, és a várt módon fog működni. Ha ezek közül bármelyik nem teljesül, az emerge figyelmezteti Önt, és felkéri a probléma megoldására.
Példa | Leírás |
---|---|
REQUIRED_USE="foo? ( bar )"
|
Ha a foo be van állítva, akkor a bar -t is be kell állítani.
|
REQUIRED_USE="foo? ( !bar )"
|
Ha foo be van állítva, akkor a bar nem szabad, hogy be legyen állítva.
|
REQUIRED_USE="foo? ( || ( bar baz ) )"
|
Ha foo be van állítva, akkor a bar vagy baz be kell, hogy legyen állítva.
|
REQUIRED_USE="^^ ( foo bar baz )"
|
A foo , bar , baz közül pontosan egy darab be kell, hogy legyen állítva.
|
REQUIRED_USE="|| ( foo bar baz )"
|
A foo , bar , baz közül legalább egy darab be kell, hogy legyen állítva.
|
REQUIRED_USE="?? ( foo bar baz )"
|
A foo , bar , baz közül legfeljebb egyetlen darabot be lehet állítani.
|
Portage jellemzői
Portage has several additional features that make the Gentoo experience even better. Many of these features rely on certain software tools that improve performance, reliability, security, ...
To enable or disable certain Portage features, edit /etc/portage/make.conf and update or set the FEATURES variable which contains the various feature keywords, separated by white space. In several cases it will also be necessary to install the additional tool on which the feature relies.
Not all features that Portage supports are listed here. For a full overview, please consult the make.conf man page:
user $
man make.conf
To find out what FEATURES are set by default, run emerge --info and search for the FEATURES variable or grep it out:
user $
emerge --info | grep ^FEATURES=
Distributed compiling
Using distcc
distcc is a program to distribute compilations across several, not necessarily identical, machines on a network. The distcc client sends all necessary information to the available distcc servers (running distccd) so they can compile pieces of source code for the client. The net result is a faster compilation time.
More information about distcc (and how to have it work with Gentoo) can be found in the Distcc article.
Installing distcc
Distcc ships with a graphical monitor to monitor tasks that the computer is sending away for compilation. This tool is automatically installed if USE="gtk"
is set.
root #
emerge --ask sys-devel/distcc
Activating Portage distcc support
Add distcc
to the FEATURES variable inside /etc/portage/make.conf. Next, edit the MAKEOPTS variable and increase the number of parallel build jobs that the system allows. A known guideline is to fill in -jN
where N
is the number of CPUs that run distccd (including the current host) plus one, but that is just a guideline.
Now run distcc-config and enter the list of available distcc servers. For a simple example assume that the available DistCC servers are 192.168.1.102 (the current host), 192.168.1.103 and 192.168.1.104 (two "remote" hosts):
root #
distcc-config --set-hosts "192.168.1.102 192.168.1.103 192.168.1.104"
Don't forget to run the distccd daemon as well:
root #
rc-update add distccd default
root #
/etc/init.d/distccd start
Caching compilation objects
About ccache
ccache is a fast compiler cache. Whenever an application is compiled, it will cache intermediate results so that, whenever the same program and version is recompiled, the compilation time is greatly reduced. The first time ccache is run, it will be much slower than a normal compilation. Subsequent recompiles however should be faster. ccache is only helpful if the same application version will be recompiled many times; thus it is mostly only useful for software developers.
For more information about ccache, please visit its homepage.
ccache is known to cause numerous compilation failures. Sometimes ccache will retain stale code objects or corrupted files, which can lead to packages that cannot be emerged. If this happens (errors like "File not recognized: File truncated" come up in build logs), try recompiling the application with ccache disabled (
FEATURES="-ccache"
in /etc/portage/make.conf or one-shot from the commandline with the following) before reporting a bug:
root #
FEATURES="-ccache" emerge --oneshot <category/package>
Installing ccache
To install ccache run the following command:
root #
emerge --ask dev-util/ccache
Activating Portage ccache support
Open /etc/portage/make.conf and add ccache
to any values defined in the FEATURES variable. If FEATURES does not exist, then create it. Next, add a new variable called CCACHE_SIZE and set it to 2G
:
/etc/portage/make.conf
Enabling Portage ccache supportFEATURES="ccache"
CCACHE_SIZE="2G"
To check if ccache functions, ask ccache to provide its statistics. Because Portage uses a different ccache home directory, it is necessary to temporarily set the CCACHE_DIR variable:
root #
CCACHE_DIR="/var/tmp/ccache" ccache -s
The /var/tmp/ccache/ location is Portage' default ccache home directory; it can be changed by setting the CCACHE_DIR variable in /etc/portage/make.conf.
When running ccache standalone, it would use the default location of ${HOME}/.ccache/, which is why the CCACHE_DIR variable needs to be set when asking for the (Portage) ccache statistics.
Using ccache outside Portage
To use ccache for non-Portage compilations, add /usr/lib/ccache/bin/ to the beginning of the PATH variable (before /usr/bin). This can be accomplished by editing ~/.bash_profile in the user's home directory. Using ~/.bash_profile is one way to define PATH variables.
~/.bash_profile
Setting the ccache location before any other PATHPATH="/usr/lib/ccache/bin:${PATH}"
Binary package support
Creating prebuilt packages
Portage supports the installation of prebuilt packages.
To create a prebuilt package use the quickpkg command if the package is already installed on the system, or emerge with the --buildpkg
or --buildpkgonly
options.
To have Portage create prebuilt packages of every single package that gets installed, add buildpkg
to the FEATURES variable.
More extended support for creating prebuilt package sets can be obtained with catalyst. For more information on catalyst please read the Catalyst FAQ.
Installing prebuilt packages
Although Gentoo doesn't provide one, it is possible to create a central repository where prebuilt packages are stored. In order to use this repository, it is necessary to make Portage aware of it by having the PORTAGE_BINHOST variable point to it. For instance, if the prebuilt packages are on ftp://buildhost/gentoo:
/etc/portage/make.conf
Add PORTAGE_BINHOST locationPORTAGE_BINHOST="ftp://buildhost/gentoo"
To install a prebuilt package, add the --getbinpkg
option to the emerge command alongside of the --usepkg
option. The former tells emerge to download the prebuilt package from the previously defined server while the latter asks emerge to try to install the prebuilt package first before fetching the sources and compiling it.
For instance, to install gnumeric with prebuilt packages:
root #
emerge --usepkg --getbinpkg gnumeric
More information about emerge's prebuilt package options can be found in the emerge man page:
user $
man emerge
Distributing prebuilt packages to others
If prebuilt packages are to be distributed to others, then make sure that this is permitted. Check the distribution terms of the upstream package for this. For example, for a package released under the GNU GPL, sources must be made available along with the binaries.
Ebuilds may define a bindist
restriction in their RESTRICT variable if built binaries are not distributable. Sometimes this restriction is conditional on one or more USE flags.
By default, Portage will not mask any packages because of restrictions. This can be changed globally by setting the ACCEPT_RESTRICT variable in /etc/portage/make.conf. For example, to mask packages that have a bindist
restriction, add the following line to make.conf:
/etc/portage/make.conf
Only accept binary distributable packagesACCEPT_RESTRICT="* -bindist"
It is also possible to override the ACCEPT_RESTRICT variable by passing the --accept-restrict
option to the emerge command. For example, --accept-restrict=-bindist
will temporarily mask packages with a bindist
restriction.
Also consider setting the ACCEPT_LICENSE variable when distributing packages. See the Licenses section for this.
It is entirely the responsibility of each user to comply with packages' license terms and with laws of each user's country. The metadata variables defined by ebuilds (RESTRICT or LICENSE) can provide guidance when distribution of binaries is not permitted, however output from Portage or questions answered by the Gentoo developers are not legal statements and should not be relied upon as such. Be cautious to abide by the law of your physical location.
Fetching files
Verify distfiles
To re-verify the integrity and (potentially) re-download previously removed/corrupted distfiles for all currently installed packages, run:
root #
emerge --ask --fetchonly --emptytree @world
The contents of this page do not apply to users that chose a systemd profile in Choosing the right profile.
Futási szintek
Booting the system
When the system is booted, lots of text floats by. When paying close attention, one will notice this text is (usually) the same every time the system is rebooted. The sequence of all these actions is called the boot sequence and is (more or less) statically defined.
First, the boot loader will load the kernel image that is defined in the boot loader configuration. Then, the boot loader instructs the CPU to execute kernel. When the kernel is loaded and run, it initializes all kernel-specific structures and tasks and starts the init process.
This process then makes sure that all filesystems (defined in /etc/fstab) are mounted and ready to be used. Then it executes several scripts located in /etc/init.d/, which will start the services needed in order to have a successfully booted system.
Finally, when all scripts are executed, init activates the terminals (in most cases just the virtual consoles which are hidden beneath Alt+F1, Alt+F2, etc.) attaching a special process called agetty to it. This process will then make sure users are able to log on through these terminals by running login.
Initscripts
Now init doesn't just execute the scripts in /etc/init.d/ randomly. Even more, it doesn't run all scripts in /etc/init.d/, only the scripts it is told to execute. It decides which scripts to execute by looking into /etc/runlevels/.
First, init runs all scripts from /etc/init.d/ that have symbolic links inside /etc/runlevels/boot/. Usually, it will start the scripts in alphabetical order, but some scripts have dependency information in them, telling the system that another script must be run before they can be started.
When all /etc/runlevels/boot/ referenced scripts are executed, init continues with running the scripts that have a symbolic link to them in /etc/runlevels/default/. Again, it will use the alphabetical order to decide what script to run first, unless a script has dependency information in it, in which case the order is changed to provide a valid start-up sequence. The latter is also the reason why commands used during the installation of Gentoo Linux used default
, as in rc-update add sshd default.
How init works
Of course init doesn't decide all that by itself. It needs a configuration file that specifies what actions need to be taken. This configuration file is /etc/inittab.
Remember the boot sequence that was just described - init's first action is to mount all file systems. This is defined in the following line from /etc/inittab:
/etc/inittab
Initialization commandsi::sysinit:/sbin/openrc sysinit
This line tells init that it must run /sbin/openrc sysinit to initialize the system. The /sbin/openrc script takes care of the initialization, so one might say that init doesn't do much - it delegates the task of initializing the system to another process.
Second, init executed all scripts that had symbolic links in /etc/runlevels/boot/. This is defined in the following line:
/etc/inittab
Boot command invocationrc::bootwait:/sbin/openrc boot
Again the OpenRC script performs the necessary tasks. Note that the option given to OpenRC (boot) is the same as the sub-directory of /etc/runlevels/ that is used.
Now init checks its configuration file to see what runlevel it should run. To decide this, it reads the following line from /etc/inittab:
/etc/inittab
Default runlevel selectionid:3:initdefault:
In this case (which the majority of Gentoo users will use), the runlevel id is 3. Using this information, init checks what it must run to start runlevel 3:
/etc/inittab
Runlevel definitionsl0:0:wait:/sbin/openrc shutdown
l1:S1:wait:/sbin/openrc single
l2:2:wait:/sbin/openrc nonetwork
l3:3:wait:/sbin/openrc default
l4:4:wait:/sbin/openrc default
l5:5:wait:/sbin/openrc default
l6:6:wait:/sbin/openrc reboot
The line that defines level 3, again, uses the openrc script to start the services (now with argument default
). Again note that the argument of openrc is the same as the subdirectory from /etc/runlevels/.
When OpenRC has finished, init decides what virtual consoles it should activate and what commands need to be run at each console:
/etc/inittab
Terminal definitionsc1:12345:respawn:/sbin/agetty 38400 tty1 linux
c2:12345:respawn:/sbin/agetty 38400 tty2 linux
c3:12345:respawn:/sbin/agetty 38400 tty3 linux
c4:12345:respawn:/sbin/agetty 38400 tty4 linux
c5:12345:respawn:/sbin/agetty 38400 tty5 linux
c6:12345:respawn:/sbin/agetty 38400 tty6 linux
Available runlevels
In a previous section, we saw that init uses a numbering scheme to decide what runlevel it should activate. A runlevel is a state in which the system is running and contains a collection of scripts (runlevel scripts or initscripts) that must be executed when entering or leaving a runlevel.
In Gentoo, there are seven runlevels defined: three internal runlevels, and four user-defined runlevels. The internal runlevels are called sysinit, shutdown and reboot and do exactly what their names imply: initialize the system, powering off the system, and rebooting the system.
The user-defined runlevels are those with an accompanying /etc/runlevels/ subdirectory: boot, default, nonetwork and single. The boot runlevel starts all system-necessary services which all other runlevels use. The remaining three runlevels differ in what services they start: default is used for day-to-day operations, nonetwork is used in case no network connectivity is required, and single is used when the system needs to be fixed.
Working with initscripts
The scripts that the openrc process starts are called init scripts. Each script in /etc/init.d/ can be executed with the arguments start
, stop
, restart
, zap
, status
, ineed
, iuse
, iwant
, needsme
, usesme
, or wantsme
.
To start, stop, or restart a service (and all depending services), the start
, stop
, and restart
arguments should be used:
root #
rc-service postfix start
Only the services that need the given service are stopped or restarted. The other depending services (those that use the service but don't need it) are left untouched.
To stop a service, but not the services that depend on it, use the --nodeps
option together with the stop
argument:
root #
rc-service --nodeps postfix stop
To see what status a service has (started, stopped, ...) use the status
argument:
root #
rc-service postfix status
If the status information shows that the service is running, but in reality it is not, then reset the status information to "stopped" with the zap
argument:
root #
rc-service postfix zap
To also ask what dependencies the service has, use iwant
, iuse
or ineed
. With ineed
it is possible to see the services that are really necessary for the correct functioning of the service. iwant
or iuse
, on the other hand, shows the services that can be used by the service, but are not necessary for the correct functioning.
root #
rc-service postfix ineed
Similarly, it is possible to ask what services require the service (needsme
) or can use it (usesme
or wantsme
):
root #
rc-service postfix needsme
Updating runlevels
rc-update
Gentoo's init system uses a dependency-tree to decide what service needs to be started first. As this is a tedious task that we wouldn't want our users to have to do manually, we have created tools that ease the administration of the runlevels and init scripts.
With rc-update it is possible to add and remove init scripts to a runlevel. The rc-update tool will then automatically ask the depscan.sh script to rebuild the dependency tree.
Adding and removing services
In earlier instructions, init scripts have already been added to the "default" runlevel. What "default" means has been explained earlier in this document. Next to the runlevel, the rc-update script requires a second argument that defines the action: add
, del
, or show
.
To add or remove an init script, just give rc-update the add
or del
argument, followed by the init script and the runlevel. For instance:
root #
rc-update del postfix default
The rc-update -v show command will show all the available init scripts and list at which runlevels they will execute:
root #
rc-update -v show
It is also possible to run rc-update show (without -v
) to just view enabled init scripts and their runlevels.
Configuring services
Why additional configuration is needed
Init scripts can be quite complex. It is therefore not really desirable to have the users edit the init script directly, as it would make it more error-prone. It is however important to be able to configure such a service. For instance, users might want to give more options to the service itself.
A second reason to have this configuration outside the init script is to be able to update the init scripts without the fear that the user's configuration changes will be undone.
conf.d directory
Gentoo provides an easy way to configure such a service: every init script that can be configured has a file in /etc/conf.d/. For instance, the apache2 initscript (called /etc/init.d/apache2) has a configuration file called /etc/conf.d/apache2, which can contain the options to give to the Apache 2 server when it is started:
/etc/conf.d/apache2
Example options for apache2 init scriptAPACHE2_OPTS="-D PHP5"
Such a configuration file contains only variables (just like /etc/portage/make.conf does), making it very easy to configure services. It also allows us to provide more information about the variables (as comments).
Writing initscripts
Another useful resource is OpenRC's service script guide.
Is it necessary?
No, writing an init script is usually not necessary as Gentoo provides ready-to-use init scripts for all provided services. However, some users might have installed a service without using Portage, in which case they will most likely have to create an init script.
Do not use the init script provided by the service if it isn't explicitly written for Gentoo: Gentoo's init scripts are not compatible with the init scripts used by other distributions! That is, unless the other distribution is using OpenRC!
Layout
The basic layout of an init script is shown below.
#!/sbin/openrc-run
depend() {
# (Dependency information)
}
start() {
# (Commands necessary to start the service)
}
stop() {
# (Commands necessary to stop the service)
}
#!/sbin/openrc-run
command=/usr/bin/foo
command_args="${foo_args} --bar"
pidfile=/var/run/foo.pid
name="FooBar Daemon"
description="FooBar is a daemon that drinks"
extra_started_commands="drink"
description_drink="Opens mouth and reflexively swallows"
depend() {
# (Dependency information)
}
start_pre() {
# (Commands necessary to prepare to start the service)
# Ensure that our dirs are correct
checkpath --directory --owner foo:foo --mode 0775 \
/var/run/foo /var/cache/foo
}
stop_post() {
# (Commands necessary to clean up after the service)
# Clean any spills
rm -rf /var/cache/foo/*
}
drink() {
ebegin "Starting to drink"
${command} --drink beer
eend $? "Failed to drink any beer :("
}
Every init script requires the start()
function or command
variable to be defined. All other sections are optional.
Dependencies
There are three dependency-alike settings that can be defined which influence the start-up or sequencing of init scripts: want
, use
and need
. Next to these, there are also two order-influencing methods called before
and after
. These last two are no dependencies per se - they do not make the original init script fail if the selected one isn't scheduled to start (or fails to start).
- The
use
settings informs the init system that this script uses functionality offered by the selected script, but does not directly depend on it. A good example would beuse logger
oruse dns
. If those services are available, they will be put in good use, but if the system does not have a logger or DNS server the services will still work. If the services exist, then they are started before the script that uses them. - The
want
setting is similar touse
with one exception.use
only considers services which were added to an init level.want
will try to start any available service even if not added to an init level. - The
need
setting is a hard dependency. It means that the script that is needing another script will not start before the other script is launched successfully. Also, if that other script is restarted, then this one will be restarted as well. - When using
before
, then the given script is launched before the selected one if the selected one is part of the init level. So an init script xdm that definesbefore alsasound
will start before the alsasound script, but only if alsasound is scheduled to start as well in the same init level. If alsasound is not scheduled to start too, then this particular setting has no effect and xdm will be started when the init system deems it most appropriate. - Similarly,
after
informs the init system that the given script should be launched after the selected one if the selected one is part of the init level. If not, then the setting has no effect and the script will be launched by the init system when it deems it most appropriate.
It should be clear from the above that need
is the only "true" dependency setting as it affects if the script will be started or not. All the others are merely pointers towards the init system to clarify in which order scripts can be (or should be) launched.
Now, look at many of Gentoo's available init scripts and notice that some have dependencies on things that are no init scripts. These "things" we call virtuals.
A virtual dependency is a dependency that a service provides, but that is not provided solely by that service. An init script can depend on a system logger, but there are many system loggers available (metalogd, syslog-ng, sysklogd, ...). As the script cannot need every single one of them (no sensible system has all these system loggers installed and running) we made sure that all these services provide a virtual dependency.
For instance, take a look at the postfix dependency information:
/etc/init.d/postfix
Dependency information of the postfix servicedepend() {
need net
use logger dns
provide mta
}
As can be seen, the postfix service:
- Requires the (virtual) net dependency (which is provided by, for instance, /etc/init.d/net.eth0).
- Uses the (virtual) logger dependency (which is provided by, for instance, /etc/init.d/syslog-ng).
- Uses the (virtual) dns dependency (which is provided by, for instance, /etc/init.d/named).
- Provides the (virtual) mta dependency (which is common for all mail servers).
Controlling the order
As described in the previous section, it is possible to tell the init system what order it should use for starting (or stopping) scripts. This ordering is handled both through the dependency settings use and need, but also through the order settings before and after. As we have described these earlier already, let's take a look at the portmap service as an example of such init script.
/etc/init.d/portmap
Dependency information of the portmap servicedepend() {
need net
before inetd
before xinetd
}
It is possible to use the "*" glob to catch all services in the same runlevel, although this isn't advisable.
depend() {
before *
}
If the service must write to local disks, it should need localmount. If it places anything in /var/run/ such as a PID file, then it should start after bootmisc:
depend() {
need localmount
after bootmisc
}
Standard functions
Next to the depend()
functionality, it is also necessary to define the start()
function. This one contains all the commands necessary to initialize the service. It is advisable to use the ebegin
and eend
functions to inform the user about what is happening:
start() {
if [ "${RC_CMD}" = "restart" ];
then
# Do something in case a restart requires more than stop, start
fi
ebegin "Starting my_service"
start-stop-daemon --start --exec /path/to/my_service \
--pidfile /path/to/my_pidfile
eend $?
}
Both --exec
and --pidfile
should be used in start and stop functions. If the service does not create a pidfile, then use --make-pidfile
if possible, though it is recommended to test this to be sure. Otherwise, don't use pidfiles. It is also possible to add --quiet
to the start-stop-daemon options, but this is not recommended unless the service is extremely verbose. Using --quiet
may hinder debugging if the service fails to start.
Another notable setting used in the above example is to check the contents of the RC_CMD variable. Unlike the previous init script system, the newer OpenRC system does not support script-specific restart functionality. Instead, the script needs to check the contents of the RC_CMD variable to see if a function (be it start()
or stop()
) is called as part of a restart or not.
Make sure that
--exec
actually calls a service and not just a shell script that launches services and exits - that's what the init script is supposed to do.For more examples of the start()
function, please read the source code of the available init scripts in the /etc/init.d/ directory.
Another function that can (but does not have to) be defined is stop()
. The init system is intelligent enough to fill in this function by itself if start-stop-daemon is used.
stop() {
ebegin "Stopping my_service"
start-stop-daemon --stop --exec /path/to/my_service \
--pidfile /path/to/my_pidfile
eend $?
}
If the service runs some other script (for example, Bash, Python, or Perl), and this script later changes names (for example, foo.py to foo), then it is necessary to add --name
to start-stop-daemon. This must specify the name that the script will be changed to. In this example, a service starts foo.py, which changes names to foo:
start() {
ebegin "Starting my_script"
start-stop-daemon --start --exec /path/to/my_script \
--pidfile /path/to/my_pidfile --name foo
eend $?
}
start-stop-daemon has an excellent man page available if more information is needed:
user $
man start-stop-daemon
Gentoo's init script syntax is based on the POSIX Shell so people are free to use sh-compatible constructs inside their init scripts. Keep other constructs, like bash-specific ones, out of the init scripts to ensure that the scripts remain functional regardless of the change Gentoo might do on its init system.
Adding custom options
If the initscript needs to support more options than the ones we have already encountered, then add the option to one of the following variables, and create a function with the same name as the option. For instance, to support an option called restartdelay
:
- extra_commands - Command is available with the service in any state
- extra_started_commands - Command is available when the service is started
- extra_stopped_commands - Command is available when the service is stopped
extra_started_commands="restartdelay"
restartdelay() {
stop
sleep 3 # Wait 3 seconds before starting again
start
}
The
restart()
function cannot be overridden in OpenRC!Service configuration variables
In order to support configuration files in /etc/conf.d/, no specifics need to be implemented: when the init script is executed, the following files are automatically sourced (i.e. the variables are available to use):
- /etc/conf.d/YOUR_INIT_SCRIPT
- /etc/conf.d/basic
- /etc/rc.conf
Also, if the init script provides a virtual dependency (such as net), the file associated with that dependency (such as /etc/conf.d/net) will be sourced too.
Changing runlevel behavior
Who might benefit
Many laptop users know the situation: at home they need to start net.eth0, but they don't want to start net.eth0 while on the road (as there is no network available). With Gentoo the runlevel behavior can be altered at will.
For instance, a second "default" runlevel can be created which can be booted that has other init scripts assigned to it. At boot time, the user can then select what default runlevel to use.
Using softlevel
First of all, create the runlevel directory for the second "default" runlevel. As an example we create the offline runlevel:
root #
mkdir /etc/runlevels/offline
Add the necessary init scripts to the newly created runlevel. For instance, to have an exact copy of the current default runlevel but without net.eth0:
root #
cd /etc/runlevels/default
root #
for service in *; do rc-update add $service offline; done
root #
rc-update del net.eth0 offline
root #
rc-update show offline
(Partial sample Output) acpid | offline domainname | offline local | offline net.eth0 |
Even though net.eth0 has been removed from the offline runlevel, udev might want to attempt to start any devices it detects and launch the appropriate services, a functionality that is called hotplugging. By default, Gentoo does not enable hotplugging.
To enable hotplugging, but only for a selected set of scripts, use the rc_hotplug variable in /etc/rc.conf:
/etc/rc.conf
Enable hotplugging of the WLAN interfacerc_hotplug="net.wlan !net.*"
For more information on device initiated services, please see the comments inside /etc/rc.conf.
Edit the bootloader configuration and add a new entry for the offline runlevel. In that entry, add softlevel=offline
as a boot parameter.
Using bootlevel
Using bootlevel is completely analogous to softlevel. The only difference here is that a second "boot" runlevel is defined instead of a second "default" runlevel.
Környezeti Változók
Bevezetés
An environment variable is a named object that contains information used by one or more applications. By using environment variables one can easily change a configuration setting for one or more applications.
Important examples
The following table lists a number of variables used by a Linux system and describes their use. Example values are presented after the table.
Variable | Description |
---|---|
PATH | This variable contains a colon-separated list of directories in which the system looks for executable files. If a name is entered of an executable (such as ls, rc-update, or emerge) but this executable is not located in a listed directory, then the system will not execute it (unless the full path is entered as the command, such as /bin/ls). |
ROOTPATH | This variable has the same function as PATH, but this one only lists the directories that should be checked when the root-user enters a command. |
LDPATH | This variable contains a colon-separated list of directories which the dynamic linker searches to find a library. |
MANPATH | This variable contains a colon-separated list of directories which the man(1) command searches for man pages. |
INFODIR | This variable contains a colon-separated list of directories which the info(1) command searches for info pages. |
PAGER | This variable contains the path to the program used to list the contents of files (such as less or more(1)). |
EDITOR | This variable contains the path to the program used to edit files (such as nano or vi). |
KDEDIRS | This variable contains a colon-separated list of directories which contain KDE-specific material. |
CONFIG_PROTECT | This variable contains a space-delimited list of directories which should be protected by Portage during package updates. |
CONFIG_PROTECT_MASK | This variable contains a space-delimited list of directories which should not be protected by Portage during package updates. |
Below is an example definition of all these variables:
PATH="/bin:/usr/bin:/usr/local/bin:/opt/bin:/usr/games/bin"
ROOTPATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"
LDPATH="/lib:/usr/lib:/usr/local/lib:/usr/lib/gcc-lib/i686-pc-linux-gnu/3.2.3"
MANPATH="/usr/share/man:/usr/local/share/man"
INFODIR="/usr/share/info:/usr/local/share/info"
PAGER="/usr/bin/less"
EDITOR="/usr/bin/vim"
KDEDIRS="/usr"
# Directories that are protected during package updates.
# Note the use of the \ (backslashes) on the end of the following lines which interprets to a single space-delimited line.
CONFIG_PROTECT="/usr/X11R6/lib/X11/xkb /opt/tomcat/conf \
/usr/kde/3.1/share/config /usr/share/texmf/tex/generic/config/ \
/usr/share/texmf/tex/platex/config/ /usr/share/config"
# Directories that are _not_ protected during package updates.
CONFIG_PROTECT_MASK="/etc/gconf"
Defining variables globally
The env.d directory
To centralize the definitions of these variables, Gentoo introduced the /etc/env.d/ directory. Inside this directory a number of files are available, such as 50baselayout, gcc/config-x86_64-pc-linux-gnu, etc. which contain the variables needed by the application mentioned in their name.
For instance, when gcc is installed, a file called gcc/config-x86_64-pc-linux-gnu was created by the ebuild which contains the definitions of the following variables:
/etc/env.d/gcc/config-x86_64-pc-linux-gnu
Default gcc enabled environment variables for GCC 13GCC_PATH="/usr/x86_64-pc-linux-gnu/gcc-bin/13"
LDPATH="/usr/lib/gcc/x86_64-pc-linux-gnu/13:/usr/lib/gcc/x86_64-pc-linux-gnu/13/32"
MANPATH="/usr/share/gcc-data/x86_64-pc-linux-gnu/13/man"
INFOPATH="/usr/share/gcc-data/x86_64-pc-linux-gnu/13/info"
STDCXX_INCDIR="g++-v13"
CTARGET="x86_64-pc-linux-gnu"
GCC_SPECS=""
MULTIOSDIRS="../lib64:../lib"
Other distributions might tell the system administrator to change or add such environment variable definitions in /etc/profile or other locations. Gentoo on the other hand makes it easy for the sysadmins (and for Portage) to maintain and manage the environment variables without having to pay attention to the numerous files that can contain environment variables.
For instance, when gcc is updated, the associated file(s) under /etc/env.d/gcc are updated too without requesting any administrative interaction.
There are still occasions where a system administrator is asked to set a certain environment variable system-wide. As an example, take the http_proxy variable. Instead of editing a file under the /etc/profile directory, create a file named /etc/env.d/99local and enter the definition in it:
/etc/env.d/99local
Setting a global environment variablehttp_proxy="proxy.server.com:8080"
By using the same file for all customized environment variables, system administrators have a quick overview on the variables they have defined themselves.
env-update
Several files within the /etc/env.d directory add definitions to the PATH variable. This is not a mistake: when the env-update command is executed, it will append the several definitions before it atomically updates each environment variable, thereby making it easy for packages (or system administrators) to add their own environment variable settings without interfering with the already existing values.
The env-update script will append the values in the alphabetical order of the /etc/env.d/ files. The file names must begin with two decimal digits.
09sandbox 50baselayout 51dconf
+------------+----------------+-----------+
CONFIG_PROTECT_MASK="/etc/sandbox.d /etc/gentoo-release /etc/dconf ..."
The concatenation of variables does not always happen, only with the following variables: ADA_INCLUDE_PATH, ADA_OBJECTS_PATH, CLASSPATH, KDEDIRS, PATH, LDPATH, MANPATH, INFODIR, INFOPATH, ROOTPATH, CONFIG_PROTECT, CONFIG_PROTECT_MASK, PRELINK_PATH, PRELINK_PATH_MASK, PKG_CONFIG_PATH, and PYTHONPATH. For all other variables the latest defined value (in alphabetical order of the files in /etc/env.d/) is used.
It is possible to add more variables into this list of concatenate-variables by adding the variable name to either COLON_SEPARATED or SPACE_SEPARATED variables (also inside an /etc/env.d/ file).
When executing env-update, the script will create all environment variables and place them in /etc/profile.env (which is used by /etc/profile). It will also extract the information from the LDPATH variable and use that to create /etc/ld.so.conf. After this, it will run ldconfig to recreate the /etc/ld.so.cache file used by the dynamical linker.
To notice the effect of env-update immediately after running it, execute the following command to update the environment. Users who have installed Gentoo themselves will probably remember this from the installation instructions:
root #
env-update && source /etc/profile
The above command only updates the variables in the current terminal, new consoles, and their children. Thus, if the user is working in X11, he needs to either type source /etc/profile in every new terminal opened or restart X so that all new terminals source the new variables. If a login manager is used, it is necessary to become root and restart the /etc/init.d/xdm service.
It is not possible to use shell variables when defining other variables. This means things like
FOO="$BAR"
(where $BAR is another variable) are forbidden.Defining variables locally
User specific
It might not be necessary to define an environment variable globally. For instance, one might want to add /home/my_user/bin and the current working directory (the directory the user is in) to the PATH variable but not want all other users on the system to have those directories in their PATH. To define an environment variable locally, use ~/.bashrc (for all interactive shell sessions) or ~/.bash_profile (for login shell sessions):
~/.bashrc
Extending PATH for local usage# A colon followed by no directory is treated as the current working directory
PATH="${PATH}:/home/my_user/bin:"
After logout/login, the PATH variable will be updated.
Session specific
Sometimes even stricter definitions are required. For instance, a user might want to be able to use binaries from a temporary directory without needing to use the full path to the binaries and without needing to temporarily change ~/.bashrc.
In this case, just define the PATH variable in the current session by using the export command. As long as the user does not log out, the PATH variable will be using the temporary settings.
root #
export PATH="${PATH}:/home/my_user/tmp/usr/bin"