Bài giảng Quản trị Linux - Chủ đề 5: Service and Boot Loader Management

pdf 74 trang phuongnguyen 5950
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Quản trị Linux - Chủ đề 5: Service and Boot Loader Management", để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên

Tài liệu đính kèm:

  • pdfbai_giang_quan_tri_linux_chu_de_5_service_and_boot_loader_ma.pdf

Nội dung text: Bài giảng Quản trị Linux - Chủ đề 5: Service and Boot Loader Management

  1. Đặng Thanh Bình Service and Boot Loader Management
  2. Contents • Boot sequence • Runlevels • Bootloader configuration • What is the “init system”? • System V • Upstart • systemd
  3. BOOT SEQUENCE
  4. 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)
  5. 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, )
  6. RUNLEVEL
  7. 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 7
  8. Runlevel Types • Red Hat family 0 — Halt 1 — Single-user mode 2 — 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 8
  9. Runlevel Types • Debian family (Ubuntu/Mint/ ) 0 – shuts the system down. 1 – single user-mode. 2 – full multi-user mode with GUI and networking. 3-5 – The same as 2. 6 – reboots the system 9
  10. 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
  11. Default 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 update­grub • You can also force a one-time boot to a different runlevel by editing the command line from the GRUB menu at boot. 11
  12. Init a Runlevel • You can init a runlevel by: – init 5 – teleint 5 12
  13. BOOT LOADER INTRODUCTION
  14. What 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. 14
  15. Boot Loaders Loading Stages • First stage – The BIOS loads a part of the boot loader known as the initial program loader, or IPL – The IPL interrogates the partition table and subsequently is able to load data wherever it may exist on the various media – This action is used initially to locate the second stage boot loader, which holds the remainder of the loader. 15
  16. Boot Loaders Loading Stages • First stage – The BIOS loads a part of the boot loader known as the initial program loader, or IPL – The IPL interrogates the partition table and subsequently is able to load data wherever it may exist on the various media – This action is used initially to locate the second stage boot loader, which holds the remainder of the loader. 16
  17. 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 be installed into the MBR and configured to pass control to the secondary boot loader. 17
  18. GNU GRUB
  19. Introduction • The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO • GNU GRUB is actively developed by the Free Software Foundation – Based on the original GRUB program, which was originally created by Erich Stefan Boleyn 19
  20. Introduction 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 Type Bootloader License GNU GPL version 3 Website www.gnu.org/software/grub/ 20
  21. CONFIGURE GRUB 1
  22. Changing 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 22
  23. /var/log/anaconda.program.log 13:00:23,089 INFO : Running ['/sbin/grub­install', '­­just­copy'] 13:00:23,134 INFO : Running ['/sbin/grub', '­­batch', '­­no­ floppy', '­­device­map=/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 BASH­like 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> 23
  24. Configure GRUB - /boot/grub/grub.conf 24
  25. Options ● 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. 25
  26. Options ● timeout= is the number of seconds the boot prompt will wait before automatically loading the default OS, in this case, Red Hat Linux. ● splashimage= is the location of the image to be used as the background for the GRUB GUI. 26
  27. Options ● 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 grub- md5-crypt (as root), which comes with GRUB. It will – 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 27
  28. Options ● 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. 28
  29. Options ● 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). 29
  30. Options ● kernel: vmlinuz-X.X.XX-XX is the name of the default boot kernel image within root directory. ● 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 30
  31. Options ● rootnoverify option tells GRUB to not try to vary the root of the OS. This saves load errors if the file system is not a supported by GRUB. ● chainloader +1 tells GRUB to use a chain loader to load this OS, which is required for loading Windows. 31
  32. Additional 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 32
  33. Additional Configuration At Boot Time ● To modify kernel arguments, press A ● To get to a BASH-like command-line interface, press C 33
  34. CONFIGURE GRUB 2
  35. No Traditional Ubuntu • Classical versions of Ubuntu used menu.lst as configuration file • What is “classical”? ==> GRUB1 and earlier ones • 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 35
  36. update-grub command • update-grub is a stub for running grub­mkconfig ­o /boot/grub/grub.cfg • to generate a grub2 config file. 36
  37. What 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 37
  38. Edit the GRUB2 Configuration File ● gksu gedit /etc/default/grub 38
  39. What to Focus? ● Save a Default Operating System: – If you choose GRUB_DEFAULT=saved – You also need to add a GRUB_SAVEDEFAULT=true line 39
  40. What 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 . 40
  41. What 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 GRUB_BACKGROUND=”/home/user/Pics/background.png” – GRUB supports JPG/JPEG images, but these are limited to 256 colors – Instead, you’ll probably want to use a PNG image that can have any number of colors – You could also use a TGA image file. 41
  42. Make Your Changes Take Effect ● Save what u did to the /etc/default/grub file ● Run sudo update-grub command ● 42
  43. GRUB CUSTOMIZER
  44. What 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 44
  45. Install Grub Customizer ● sudo add-apt-repository ppa:danielrichter2007/grub-customizer ● sudo apt-get update ● sudo apt-get install grub-customizer 45
  46. Use Grub Customizer ● It has a Graphical User Interface, try it by yourself 46
  47. INIT SYSTEM
  48. What 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. 48
  49. What 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. 49
  50. SYSTEM V (SYSTEM 5)
  51. System 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. 51
  52. System V • The configuration files for SysV init are located in the /etc/rc.d/ directory. • Within this directory, are the rc, rc.local, rc.sysinit, and, optionally, the rc.serial scripts as well as the following directories: init.d/ rc0.d/ rc1.d/ rc2.d/ rc3.d/ rc4.d/ rc5.d/ rc6.d/ • The init.d/ directory contains the scripts used by the /sbin/init command when controlling services. 52
  53. System V Commands • service frobozz start/stop/restart/status • ls /etc/rc.d/init.d/ • chkconfig frobozz on/off • chkconfig frobozz • chkconfig ­­list • chkconfig ­­list | grep httpd • chkconfig ­­level 35 httpd on • chkconfig ­­level 2345 postfix off • runlevel (view previuos and current runlevel) • init 0 • telinit 0 53
  54. System V Commands • update-rc.d - install and remove System-V style init script links • Syntax: – update­rc.d [­n] [­f] name remove – update­rc.d [­n] name defaults [NN | SS KK] – update­rc.d [­n] name start|stop NN runlevel [runlevel] . start|stop NN runlevel [runlevel] . – update­rc.d [­n] name disable|enable [S|2|3|4|5] ● Options: – -n Don't do anything, just show what we would do. – -f Force removal of symlinks even if /etc/init.d/name still exists. 54
  55. System V Commands update-rc.d samples • Insert links using the defaults: update­rc.d foobar defaults • Equivalent command using explicit argument sets: update­rc.d foobar start 20 2 3 4 5 . stop 20 0 1 6 . • Insert links at default runlevels when B requires A update­rc.d script_for_A defaults 80 20 update­rc.d script_for_B defaults 90 10 • Disabling a service: update­rc.d ­f foobar remove update­rc.d foobar stop 20 2 3 4 5 . • Installing a system initialization-and-shutdown script: update­rc.d foobar start 45 S . stop 31 0 6 . 55
  56. UPSTART
  57. Intro • System V init daemon (SysVinit) does not deal well with modern hardware, including – hotplug devices – USB hard and flash drives – network-mounted filesystems • Ubuntu replaced it with the Upstart init daemon – upstart.ubuntu.com – upstart.ubuntu.com/wiki 57
  58. Reliability • Upstart is written using the NIH Utility Library ("libnih"). – a very small, efficient and safe library of generic routines – designed for applications that run early in the boot sequence ("plumbing"). • Reliability and safety is critically important for an init daemon since: – it runs as the super-user. – it is responsible for managing critical system services. – if init exits for any reason, the kernel panics. 58
  59. Differences to SysV • SysVinit daemon uses runlevels (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. 59
  60. 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
  61. 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
  62. 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
  63. 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
  64. initctl Examples • To start/stop/restart/reload a • To get the version of job/service 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 services/jobs # initctl message # initctl list # initctl warn • To reload the configuration file # initctl error # initctl reload­configuration JOB # initctl fatal 64
  65. A Test Job • sudo nano /etc/init/testjob.conf description "A test job file" author "Your Name" start on runlevel [2345] exec echo Test Job ran at 'date' >> /var/log/testjob.log • Save and close this file. • init­checkconf /etc/init/testjob.conf • sudo service testjob start • cat /var/log/testjob.log 65
  66. SYSTEMD
  67. 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
  68. Systemd Components 68
  69. System 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. 69
  70. 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
  71. 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 initctl list systemctl status services Check init- systemd-analyze verify configuration is checkconf /tmp/foo.c valid onf Show job initctl list-env systemctl show-environment environment Set job initctl set- systemctl set- environment env foo=bar environment foo=bar variable 71
  72. Commands Operation Upstart Command Systemd equivalent Remove job initctl unset- systemctl unset- environment env foo environment foo variable View job log cat /var/log/upstart sudo journalctl -u $unit /$job.log tail -f job tail - sudo journalctl -u $unit -f log f /var/log/upstart/ $job.log Show initctl2dot systemctl list- relationship dependencies all between services 72
  73. 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, single­user mode and reboot stop on runlevel [016] env statedir=/var/cache/foo # create a directory needed by the daemon pre­start exec mkdir ­p "$statedir" exec /usr/bin/foo­daemon ­­arg1 "hello world" ­­statedir "$statedir" 73
  74. 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/foo­daemon ­­arg1 "hello world" ­­statedir ${statedir} [Install] WantedBy=multi­user.target 74