Bài giảng Quản trị Linux: Service and boot loader management. nội dung chính trong chương này gồm có: Boot sequence, runlevels, bootloader configurationm, what is the “init system”? System V, upstart, systemd. Mời các bạn cùng tham khảo.
Đặng Thanh Bình Service and Boot Loader Management Contents • • • • • • • Boot sequence Runlevels Bootloader configuration What is the “init system”? System V Upstart systemd BOOT SEQUENCE Boot phases • BIOS performs integrity checks on memory and seeks instructions on the Master Boor Record (MBR) • The MBR points to the boot loader (GRUB or LILO) • Boot loader – asks for the OS label which will identify which kernel to run and where it is located (hard drive and partition specified) – loads the Linux operating system • The kernel executes init program (SysV/Upstart/Systemd) Boot phases • SysV – The first processes that init starts is a script /etc/rc.d/rc.sysinit – Based on the appropriate run-level, scripts are executed to start various processes to run the system and make it functional • Upstart: Some core boot tasks started by upstart are – Plymouth - The graphical boot animation and logger – mountall - Mounts all filesystems defined on /etc/fstab – network* - Network related services – Display Manager (GDM,KDM,XDM, ) RUNLEVEL Runlevel Concept • A runlevel is a state, or mode, that is defined by the services listed in the directory /etc/rc.d/rc.d, where is the number of the runlevel • Used by SysV init system and backward compatible by Upstart and SystemD Runlevel Types • Red Hat family — Halt — Single-user mode — Not used (user-definable) — Full multi-user mode — Not used (user-definable) — Full multi-user mode (with X-based login screen) — Reboot Runlevel Types • Debian family (Ubuntu/Mint/ ) – shuts the system down – single user-mode – full multi-user mode with GUI and networking 3-5 – The same as – reboots the system Default Runlevel • CentOS ● Default runlevel can be changed by modifying the /etc/inittab file, as following: id:5:initdefault: • Ubuntu ● Edit /etc/init/rc-sysinit.conf and change: • env DEFAULT_RUNLEVEL=2 ● To: • env DEFAULT_RUNLEVEL=N ● Where N is the desired runlevel 10 Differences to SysV • Entries in the /etc/init directory will replace the contents of the /etc/init.d and /etc/rc?.d directories • Runlevels will no longer be a formal feature of Ubuntu, although they will be maintained for compatibility with third-party software • Eventually Upstart will also replace crond 60 Software package The Upstart system is contained in one package, which is installed by default: • upstart — Provides the Upstart init daemon and initctl utility 61 Definition • Jobs – A job is a series of instructions that init reads – Categorized into tasks and services • Task – A task is a job that performs its work and returns to a waiting state when it is done – A task blocks the program/process that emitted the event that triggered it until the program it specifies is finished running 62 Definition • Service – A service is a job that does not normally terminate by itself – The init daemon monitors each service, restarting the service if it fails and killing the service if it is stopped either manually or by an event – A service blocks the program/process that emitted the event that triggered it until the program it specifies has started running • initctl – The initctl (init control) utility communicates with the Upstart init daemon 63 initctl Examples • To start/stop/restart/reload job/service a • To get the version of the init daemon #initctl start/stop/restart/reload JOB # initctl version • To get the status of a service/job • To set the log priority # initctl status JOB # initctl debug • To list all the initctl controlled # initctl info # initctl message services/jobs # initctl list # initctl warn • To reload the configuration file # initctl error # initctl reloadconfiguration JOB # initctl fatal 64 A Test Job • sudo nano /etc/init/testjob.conf description "A test job file" author "Your Name" start on runlevel [2345] exec echo Test /var/log/testjob.log Job ran at 'date' >> • Save and close this file • initcheckconf /etc/init/testjob.conf • sudo service testjob start • cat /var/log/testjob.log 65 SYSTEMD systemd • • • • systemd is a system and service manager for Linux Compatible with SysV and LSB init scripts It can work as a drop-in replacement for sysvinit Systemd – Provides aggressive parallelization capabilities – Uses socket and D-Bus activation for starting services – Offers on-demand starting of daemons – Implements transactional dependency-based service control logic – Tracks processes using Linux cgroups – Supports snapshotting and restoring – Maintains mount and automount points 67 Systemd Components 68 System Init Daemon • Ubuntu 15.04 (using Systemd by default): – Systemd runs with PID as /sbin/init Upstart runs with PID as /sbin/upstart • Prior versions (using Upstart by default): – – Upstart runs with PID as /sbin/init – Systemd runs with PID as /lib/systemd/systemd 69 High-level startup concept • Upstart's model for starting processes (jobs) is "greedy event-based" – all available jobs whose startup events happen are started as early as possible • systemd's model for starting processes (units) is "lazy dependency-based", i e a unit will only start if and when some other starting unit depends on it • During boot, systemd starts a "root unit" (default.target, can be overridden in grub), which then transitively expands and starts its dependencies • A new unit needs to add itself as a dependency of a unit of the boot sequence (commonly multi-user.target) in order to become active 70 Commands Operation Upstart Command Systemd equivalent Start service start $job systemctl start $unit Stop service stop $job systemctl stop $unit Restart service restart $job systemctl restart $unit See status of services systemctl status initctl list initCheck configuration is checkconf /tmp/foo.c valid onf systemd-analyze verify Show job environment initctl list-env systemctl show-environment Set job environment variable initctl setenv foo=bar systemctl setenvironment foo=bar 71 Commands Operation Upstart Command Systemd equivalent Remove job environment variable initctl unsetenv foo systemctl unsetenvironment foo View job log cat /var/log/upstart /$job.log sudo journalctl -u $unit tail -f job log tail f /var/log/upstart/ $job.log sudo journalctl -u $unit -f Show relationship between services initctl2dot systemctl listdependencies all 72 Example Upstart Service • /etc/init/foo.conf: # description "Job that runs the foo daemon" # start in normal runlevels when disks are mounted and networking is available start on runlevel [2345] # stop on shutdown/halt, singleuser mode and reboot stop on runlevel [016] env statedir=/var/cache/foo # create a directory needed by the daemon prestart exec mkdir p "$statedir" exec /usr/bin/foodaemon arg1 "hello world" statedir "$statedir" 73 Example Systemd service • /lib/systemd/system/foo.service: [Unit] Description=Job that runs the foo daemon Documentation=man:foo(1) [Service] Type=forking Environment=statedir=/var/cache/foo ExecStartPre=/usr/bin/mkdir p ${statedir} ExecStart=/usr/bin/foodaemon arg1 "hello world" statedir ${statedir} [Install] WantedBy=multiuser.target 74 ... remainder of the loader 16 Boot Loaders In Action • Boot loaders are usually configured in one of two ways – As a primary boot loader – As a secondary boot loader • Primary boot loaders are where the... first stage of the boot loader is installed on the MBR • Secondary boot loaders are where the first stage of the boot loader is installed onto a bootable partition – A separate boot loader must then... second stage boot loader, which holds the remainder of the loader 15 Boot Loaders Loading Stages • First stage – The BIOS loads a part of the boot loader known as the initial program loader, or