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.
Trang 1Đặng Thanh Bình
Service and Boot Loader
Management
Trang 3
BOOT SEQUENCE
Trang 4Boot 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)
Trang 5Boot 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, )
Trang 6
RUNLEVEL
Trang 7Runlevel Concept
• A runlevel is a state, or mode, that is defined by the services listed in the directory /etc/rc.d/rc<x>.d, where <x> is the number of the runlevel.
• Used by SysV init system and backward compatible
by Upstart and SystemD
Trang 82 — Not used (user-definable)
3 — Full multi-user mode
4 — Not used (user-definable)
5 — Full multi-user mode (with X-based login screen)
6 — Reboot
Trang 9Runlevel Types
• Debian family (Ubuntu/Mint/ )
0 – shuts the system down.
Trang 10Default Runlevel
• CentOS
modifying the /etc/inittab file, as following:
Trang 11Default Runlevel
• The runlevel can be set using kernel parameters Edit /etc/default/grub, and change:
GRUB_CMDLINE_LINUX="
• to
GRUB_CMDLINE_LINUX="N"
• where N is the runlevel you want
• Then run: sudo updategrub
• You can also force a one-time boot to a different runlevel by editing the command line from the GRUB menu at boot
Trang 13
BOOT LOADER
INTRODUCTION
Trang 14What is a boot loader?
• Upon starting, BIOS reads the first 512 bytes of the bootable media (master boot record, or MBR).
• You can store the boot record of only one OS in a single MBR
• Hence the need for more flexible boot loaders to support multiple operating systems
• The master boot record holds two things
– Some of or all of the boot loader program
– The partition table
• Using the data stored in the MBR, the BIOS activates the boot loader.
Trang 15– This action is used initially to locate the second stage boot loader, which holds the remainder of the loader.
Trang 16– This action is used initially to locate the second stage boot loader, which holds the remainder of the loader.
Trang 17Boot 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 be installed into the MBR and configured to pass control to the secondary boot loader.
Trang 18
GNU GRUB
Trang 20Introduction
Original author(s) Erich Boleyn
Developer(s) GNU Project
Initial release 1995; 20 years ago
Stable release 2.00 (GRUB 2) / June 27, 2012; 3 years ago
Preview release 2.02~beta2 (GRUB 2)/ December 24, 2013; 21 months ago Development status Active
Written in Assembly, C
Operating system Linux, OS X, BSD, Solaris (x86 port) and Windows (through
chainloading) Platform IA-32, x86-64, IA-64, ARM, PowerPC, MIPS and SPARC
Available in English and others
License GNU GPL version 3
Website www.gnu.org/software/grub/
Trang 21
CONFIGURE GRUB 1
Trang 22Changing boot menu
● You should edit /boot/grub/grub.conf file
● How about /boot/grub/menu.lst file? It's a symlink to the grub.conf file
● These files are initially created by anaconda during the install This is logged in
/var/log/anaconda.program.log
● We can see that this anaconda execution
uses grub.conf, not menu.lst
Trang 23/var/log/anaconda.program.log
13:00:23,089 INFO : Running ['/sbin/grubinstall', 'justcopy'] 13:00:23,134 INFO : Running ['/sbin/grub', 'batch', 'no
floppy', 'devicemap=/boot/grub/device.map']
13:00:23,346 INFO :
13:00:23,347 INFO :
13:00:23,347 INFO : GNU GRUB version 0.97 (640K lower / 3072K upper memory)
13:00:23,347 INFO :
13:00:23,348 INFO : [ Minimal BASHlike line editing is supported. For the first word, TAB
13:00:23,348 INFO : lists possible command completions. Anywhere else TAB lists the possible
13:00:23,348 INFO : completions of a device/filename.]
13:00:23,348 INFO : grub> root (hd0,0)
13:00:23,348 INFO : Filesystem type is ext2fs, partition type 0x83 13:00:23,348 INFO : grub> install stage2=/boot/grub/stage2
/boot/grub/stage1 d (hd0) /boot/grub/stage2 p (hd0,0)/boot/grub/grub.conf 13:00:23,348 INFO : grub>
Trang 24Configure GRUB - /boot/grub/grub.conf
Trang 25Options
● default= option signals to GRUB which
image to boot from by default after the
timeout period
● This relates to one of the images in the
grub.conf file 0 is the first specified, 1 is the second specified, etc
● If you don't specify this option in the
configuration file, it will boot the first image specified in the file
Trang 26● splashimage= is the location of the image to
be used as the background for the GRUB
GUI
Trang 27Options
● password option specifies the MD5-encrypted
password used to gain access to GRUB's
interactive boot options
– This does not stop users loading your defined OS
● To generate an md5 password, run the tool md5-crypt (as root), which comes with GRUB It will
grub-– prompt for the password you want to encrypt
– output the MD5-encrypted password
● Copy this into grub.conf after password md5 but
on the same line
Trang 28Options
● title identifies the specific OS that will be
booted from at the user interface at runtime
– Spaces could be included in this name.
● password is set in the same way as the
password above
– Do not set this password to the root password if you are planning on sharing this machine with other users.
Trang 29Options
● root option tells GRUB where the OS file
system actually lives
● GRUB references the media in a different
way than LILO
– In LILO, /dev/hdb3 is the third partition of the second disk
– Grub references this disk as (hd1,2), again the third partition of the second disk (disk 0 being the first disk, partition 0 being the first partition).
Trang 30● initrd: initrd-X.X.XX-XX.img is the name of
the default initrd file within root directory.
– initrd enables loading a RAM disk by the boot
loader
– Allow system startup in 2 phases:
● Kernel comes up with a minimal set of compiled-in drivers
● Additional modules are loaded from initrd
Trang 31● chainloader +1 tells GRUB to use a chain
loader to load this OS, which is required for loading Windows
Trang 32Additional Configuration At Boot Time
• Press the P key to enter your GRUB password
• For edit commands before booting, press E
– This allows user to edit the specific options for the currently highlighted OS
– If you need to access the machine in single-user mode (giving you root access without specifying a password!), select the Linux OS at the GRUB main screen.
• Press E and move to the kernel line
• Append single to the end of this line
• Press B to boot using changed grub.conf
– Any changes you make in edit mode are not saved to the grub.conf file
Trang 33Additional Configuration At Boot Time
● To modify kernel arguments, press A
● To get to a BASH-like command-line
interface, press C
Trang 34
CONFIGURE GRUB 2
Trang 35• GRUB 2 uses /boot/grub/grub.cfg file
• You shouldn’t edit this file by hand! This file is just for GRUB2’s own usage
• sudo update-grub to automatically create this file
Trang 37What to do?
● Two steps:
1 Edit the /etc/default/grub file
2 Run the sudo update-grub command
• How?
– When you run the update-grub command,
GRUB automatically combines
● the settings from the /etc/default/grub file
● the scripts from the /etc/grub.d/ directory
● and everything else
– to automatically create /boot/grub/grub.cfg
Trang 38Edit the GRUB2 Configuration File
● gksu gedit /etc/default/grub
Trang 39What to Focus?
● Save a Default Operating System:
– If you choose GRUB_DEFAULT=saved
– You also need to add a GRUB_SAVEDEFAULT=true line
Trang 40What to Focus?
● Choose Whether GRUB is Hidden:
– Set GRUB_HIDDEN_TIMEOUT=0
– If the number is 5, GRUB will display an empty screen
or splash screen for 5 secs, during which you can press any key to view the menu
– To prevent GRUB from being automatically hidden,
comment the line out — just add a # before it so that it reads #GRUB_HIDDEN_TIMEOUT=0
Trang 41What to Focus?
● Choose a Background Image:
– By default, GRUB uses a white-on-black monochrome look – The GRUB_BACKGROUND line controls whether a
background image is used
– You can edit like this
Trang 42Make Your Changes Take Effect
● Save what u did to the /etc/default/grub file
● Run sudo update-grub command
●
Trang 43
GRUB CUSTOMIZER
Trang 44What is Grub Customizer?
● Edit the menu entries (reorder, rename, add or remove)
● Edit the contents of menu entries or create new ones (internally it edits the 40_custom )
● Change the default boot entry
● Change menu visibility and timeout
● Disable recovery entries
● Change GRUB resolution, menu colors or background image
● GRUB repair & configuration using a Live CD
● Advanced options like booting an ISO, changing kernel parameters and lots more
Trang 45Install Grub Customizer
● sudo add-apt-repository
ppa:danielrichter2007/grub-customizer
● sudo apt-get update
● sudo apt-get install grub-customizer
Trang 46Use Grub Customizer
● It has a Graphical User Interface, try it by yourself
Trang 47
INIT SYSTEM
Trang 48What is an “init system”?
• It's the system that launches all other systems on your machine.
• When you boot Linux, the init system do such things:
– loads the drivers you need
– turns on your network connection
– fires up the necessary system service
– then loads the desktop
• Without an init system, you have no way to do
anything.
Trang 49What is an “init system”?
• Previous versions of Ubuntu (from 9.10 to 14.10) used a homegrown system called Upstart, which was Ubuntu's attempt to create an init to replace SysV
• However, Ubuntu remains a Debian derivative and with Debian moving to systemd, Ubuntu has no choice other than jumping to the systemd boat
Trang 50
SYSTEM V (SYSTEM 5)
Trang 51System V
• The SysV init runlevel system provides a standard process for controlling which programs init launches or halts when initializing a runlevel
• SysV init was chosen because it is easier to use and more flexible than the traditional BSD-style init process
Trang 52• The init.d/ directory contains the scripts used
by the /sbin/init command when controlling services
Trang 54– -n Don't do anything, just show what we would do.
– -f Force removal of symlinks even if /etc/init.d/name still
exists.
Trang 55System V Commands
update-rc.d samples
• Insert links using the defaults: updaterc.d foobar defaults
• Equivalent command using explicit argument sets:
• Installing a system initialization-and-shutdown script:
updaterc.d foobar start 45 S . stop 31 0 6 .
Trang 56
UPSTART
Trang 58Reliability
• Upstart is written using the NIH Utility Library (" libnih ").
– a very small, efficient and safe library of generic
– it runs as the super-user.
– it is responsible for managing critical system
services.
– if init exits for any reason, the kernel panics.
Trang 59Differences to SysV
(recovery/single-user, multiuser, and more) and links from the /etc/rc?.d directories to the init scripts in /etc/init.d to start and stop system services
• The event-based Upstart init daemon uses
events to start and stop system services
Trang 60Differences 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
Trang 62Definition
• 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
Trang 63– 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
Trang 66
SYSTEMD
Trang 67systemd
• 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
Trang 68Systemd Components
Trang 69System Init Daemon
• Ubuntu 15.04 (using Systemd by default):
– Systemd runs with PID 1 as /sbin/init
– Upstart runs with PID 1 as /sbin/upstart
• Prior versions (using Upstart by default):
– Upstart runs with PID 1 as /sbin/init
– Systemd runs with PID 1 as
/lib/systemd/systemd
Trang 70High-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
Trang 71Commands
Start service start $job systemctl start $unit
Stop service stop $job systemctl stop $unit
Restart service restart $job systemctl restart $unit
init-systemd-analyze verify <unit_file>
systemctl environment foo=bar
Trang 72systemctl environment foo
unset-View job log cat /var/log/upstart
/$job.log sudo journalctl -u $unit