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

74
Đặng Thanh Bình Service and Boot Loader Management

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

Page 1: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

Đặng Thanh Bình

Service and Boot Loader Management

Page 2: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

Contents

• Boot sequence• Runlevels• Bootloader configuration• What is the “init system”?• System V• Upstart• systemd

Page 3: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

BOOT SEQUENCE

Page 4: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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)

Page 5: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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

Page 6: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

RUNLEVEL

Page 7: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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

Page 8: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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

Page 9: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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

Page 10: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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.

Page 11: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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.

Page 12: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

12

Init a Runlevel

• You can init a runlevel by:– init 5

– teleint 5

Page 13: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

BOOT LOADER INTRODUCTION

Page 14: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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.

Page 15: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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.

Page 16: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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.

Page 17: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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.

Page 18: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

GNU GRUB

Page 19: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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

Page 20: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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/

Page 21: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

CONFIGURE GRUB 1

Page 22: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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

Page 23: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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> 

Page 24: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

24

Configure GRUB - /boot/grub/grub.conf

Page 25: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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.

Page 26: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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.

Page 27: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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

Page 28: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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.

Page 29: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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

Page 30: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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

Page 31: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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.

Page 32: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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

Page 33: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

33

Additional Configuration At Boot Time

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

interface, press C

Page 34: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

CONFIGURE GRUB 2

Page 35: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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

Page 36: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

36

update-grub command

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

• to generate a grub2 config file.

Page 37: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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

Page 38: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

38

Edit the GRUB2 Configuration File

● gksu gedit /etc/default/grub

Page 39: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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

Page 40: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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 .

Page 41: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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.

Page 42: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

42

Make Your Changes Take Effect

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

Page 43: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

GRUB CUSTOMIZER

Page 44: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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

Page 45: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

45

Install Grub Customizer

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

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

Page 46: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

46

Use Grub Customizer

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

Page 47: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

INIT SYSTEM

Page 48: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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.

Page 49: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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.

Page 50: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

SYSTEM V (SYSTEM 5)

Page 51: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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.

Page 52: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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.

Page 53: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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

Page 54: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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.

Page 55: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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 .

Page 56: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

UPSTART

Page 57: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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

Page 58: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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.

Page 59: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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.

Page 60: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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.

Page 61: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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.

Page 62: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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

Page 63: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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

Page 64: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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 

Page 65: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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

Page 66: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

SYSTEMD

Page 67: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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

Page 68: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

68

Systemd Components

Page 69: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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.

Page 70: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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.

Page 71: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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

Page 72: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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

Page 73: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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"

Page 74: Service and Boot Loader Management - WordPress.comJan 05, 2018  · GNU GRUB. 19 Introduction •The GRand Unified Boot loader (GRUB) is a choice of nowadays instead of LILO •GNU

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