Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The...

Post on 15-Mar-2020

4 views 0 download

transcript

Đặ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

7

Runlevel 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

8

Runlevel Types

• Red Hat family0 — 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

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

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.

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.

12

Init a Runlevel

• You can init a runlevel by:– init 5

– teleint 5

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.

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.

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.

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.

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

20

IntroductionOriginal 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/

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

23

/var/log/anaconda.program.log13: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> 

24

Configure GRUB - /boot/grub/grub.conf

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.

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.

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

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.

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).

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

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.

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

33

Additional Configuration At Boot Time

● To modify kernel arguments, press A● To get to a BASH-like command-line

interface, press C

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

36

update-grub command

• update-grub is a stub for runninggrub­mkconfig ­o /boot/grub/grub.cfg

• to generate a grub2 config file.

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

38

Edit the GRUB2 Configuration File

● gksu gedit /etc/default/grub

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

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 .

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.

42

Make Your Changes Take Effect

● Save what u did to the /etc/default/grub file● Run sudo update-grub command●

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

45

Install Grub Customizer

● sudo add-apt-repository ppa:danielrichter2007/grub-customizer

● sudo apt-get update● sudo apt-get install grub-customizer

46

Use Grub Customizer

● It has a Graphical User Interface, try it by yourself

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.

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.

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.

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.

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

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.

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 .

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

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.

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.

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.

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.

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

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

64

initctl Examples

• To start/stop/restart/reload a job/service

#initctl start/stop/restart/reload JOB 

• To get the status of a service/job # initctl status JOB 

• To list all the initctl controlled services/jobs

# initctl list 

• To reload the configuration file# initctl reload­configuration JOB 

• To get the version of the init daemon

# initctl version 

• To set the log priority# initctl debug

# initctl info

# initctl message

# initctl warn

# initctl error

# initctl fatal 

65

A Test Job

• sudo nano /etc/init/testjob.confdescription "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

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

68

Systemd Components

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.

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.

71

CommandsOperation 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

initctl list systemctl status

Check configuration is valid

init-checkconf /tmp/foo.conf

systemd-analyze verify <unit_file>

Show job environment

initctl list-env systemctl show-environment

Set job environment variable

initctl set-env foo=bar

systemctl set-environment foo=bar

72

Commands

Operation Upstart Command Systemd equivalent

Remove job environment variable

initctl unset-env foo

systemctl unset-environment 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 list-dependencies --all

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"

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