linux booting and shutting down

51
Linux Booting and Shutting Down UNIT - IV

Upload: lev-dillon

Post on 31-Dec-2015

68 views

Category:

Documents


6 download

DESCRIPTION

UNIT - IV. Linux Booting and Shutting Down. How Linux boot?. Bootstrapping (starting up a computer). Booting is a bootstrapping process that starts operating systems when the user turns on a computer system. During bootstrapping the kernel is loaded into memory and begins to execute. - PowerPoint PPT Presentation

TRANSCRIPT

Linux Booting and Shutting Down

UNIT - IV

2

How Linux boot?

3

Booting is a bootstrapping process that starts operating systems when the user turns on a computer system.

During bootstrapping the kernel is loaded into memory and begins to execute.

A variety of initialization tasks are performed and the system is then made available to users.

A boot sequence is the set of operations the computer performs when it is switched on that load an operating system.

Bootstrapping (starting up a computer)

4

Steps in the boot process

1. Reading of the boot loader from the MBR (Master Boot Record)

2. Loading and initialization of the kernel3. Device detection and configuration4. Creation of kernel processes5. Administrator intervention (single-user-mode

only)6. Execution of system startup scripts

5

1. Reading of the boot loader from the MBR

Turn on CPU jump to address of BIOS BIOS runs POST (Power-On Self Test) and it also

performs system integrity check BIOS searches, loads, and executes boot loader program BIOS finds bootable devices BIOS gives control to MBR reads from the memory BIOS loads and execute boot sector form MBR

6

MBR (Master Boot Record)

OS is booted from a hard disk, where the Master Boot Record (MBR) contains the primary boot loader

The MBR is a 512-byte sector, located in the first sector of the bootable disk (sector 1 of cylinder 0, head 0)

After the MBR is loaded into RAM, the BIOS yields control to it.

7

MBR (Master Boot Record)

8

MBR (Master Boot Record)

The first 446 bytes are the primary boot loader, which contains both executable code and error message text

The next sixty-four bytes are the partition table, which contains a record for each of four partitions

The MBR ends with two bytes that are defined as the magic number (0xAA55). The magic number serves as a validation check of the MBR

9

Extracting the MBR

To see the contents of MBR, use this command:

# dd if=/dev/hda of=mbr.bin bs=512 count=1

# od -xa mbr.bin**T he dd command, which needs to be run from root,

reads the first 512 bytes from /dev/hda (the first In tegrated Drive Electronics, or IDE drive) and writes

them to the mbr.bin file.** The od command prints the binary file in hex and A

SCII formats.

10

Boot loader Boot loader could be more aptly called the kernel

loader. The task at this stage is to load the Linux kernel

MBR contains information about boot loaders MBR loads and executes GRUB (Linux boot loader) GRUB(Grand Unified Bootloader) contains images

of the kernel i.e. loads and execute kernel, it also loads initial RAM disk (init rd)

kernel uses Initial ram disk to stores filesystem temporarily

11

2. Loading and initialization of the kernel

Kernel is itself a program, and the first bootstrapping task is to get this program into memory so that it can be executed

Linux system kernel - /boot/vmlinuz Kernel mounts root filesystem i.e. loads

filesystems Kernel executes init program located in

/sbin/init

12

3. Device detection and configuration

Kernel loads device drivers

13

4. Creation of kernel processes

Kernel creates several “spontaneous” processes in user space

They are called spontaneous processes because they are not created through the normal system call fork

Processes sched have PID 0 and init have PID 1

14

15

5. Administrator intervention (single user mode – recovery mode)

Fsck cmd is run during a normal boot to check and repair filesystems.

When the system in single user mode, to need to run fsck by hand

16

6. Execution of startup scripts

Startup scripts are just normal shell scripts, and they are selected and run by init

Finally boot process completed

17

Booting PCs

When a machine boots, it begins by executing boot code stored in ROMs.

The exact location and nature of this boot code varies, depending on the type of machine you have.

The boot code is typically firmware that knows how to use the devices connected to the machine, how to talk to the network on a basic level, and how to understand disk-based filesystems.

On PCs, the initial boot code is generally called a BIOS.

18

Built-in BIOS knows about some of the devices that live on the motherboard, IDE and disks, network interface, power and temperature meters and system hardware.

BIOS select which devices you want the system to try to boot from.

You can usually specify an ordered list of preferences such as “try to boot from DVD, USB drive, hard disk”.

19

Once the BIOS has figured out what device to boot from, it tries to read the first block of the device.

This 512-byte segment is known as the master boot record or MBR.

MBR contains a program that tells the computer from which partition to load a secondary boot program, the “boot loader”.

Default MBR contains a simple program that tells the computer to get its boot loader from the first partition on the disk.

20

MBR that knows how to deal with multiple operating systems and kernels.

Once the MBR has chosen a partition to boot from, it tries to load the boot loader specific to that partition. This loader is then responsible for loading the kernel.

21

GRUB : The Grand Unified Boot Loader

GRUB, developed by the GNU (Gnu’s Not Unix)

project.

It is the default boot loader for most UNIX and

Linux systems with Intel processors.

GRUB’s job is to choose a kernel from a

previously assembled list and to load that kernel

with options specified by the administrator.

22

There are two branches of the GRUB lineage:

original GRUB called GRUB Legacy, and the

newer GRUB 2.

Grub 2 is similar in concept but varies in its

config file syntax.

By default, GRUB reads its default boot

configuration from /boot/grub/menu.lst or

/boot/grub/grub.conf.

23

GRUB reads the configuration file at startup

time.

menu.lst and grub.conf files are slightly different

but have a similar syntax.

Red hat system use grub.conf and solaris, suse

and ubuntu still use menu.lst.

24

Red Hat system - grub.conf file

default=0

timeout=10

splashimage=(hd0,0)/boot/grub/splash.xpm.gz - display splash

image

title Red Hat Enterprise Linux Server (2.6.18-92.1.10.el5)

root (hd0,0) – root file system

kernel /vmlinuz-2.6.18-92.1.10.el5ro root=LABEL=/

- Grub loads this kernel

25

Ubuntu system - menu.lst file

26

GRUB is an operating system independant boot loader

Flexible command line interface File system access

27

Multibootingdefault=0

timeout=5

splashimage=(hd0,2)/boot/grub/splash.xpm.gz

hiddenmenu

title Windows XP

rootnoverify (hd0,0)

chainloader +1

title Red Hat

root (hd0,1)

kernel /vmlinuz

28

GRUB command-line options

Command Meaning

reboot - Reboots the system

find - Finds files on all mountable partitions

root - Specifies the root device (a partition)

kernel - Loads a kernel from the root device

help - Gets interactive help for a command

boot - Boots the system from the specified kernel image

29

Booting To Single-User Mode

Boot options should be easily modifiable and

decided on the ‘a’ key as the appropriate tool.

To boot into single-user mode, add the single

flag to the end of the existing kernel options.

ro root=LABEL=/rhgb quiet single

30

Rebooting and Shutting Down

Always a good idea to shut down the machine

when possible.

Improper shutdown can result problem in the

system.

To modify or make significant system changes,

you should reboot just to make sure that the

system comes up successfully.

31

Shutdown : the gentle way to halt the system Shutdown sends messages to logged-in users at

progressively shorter intervals, warning them of the impending downtime.

Warning simply say that the system is being shut down and give the time remaining until the event.

32

33

$ sudo shutdown –h +15 "system is going down for system halt after 15 minutes! “

$ sudo shutdown –r +1 “system is going down for system halt after 1 minutes!”

Halt cmd performs the essential duties required to shut the system down.

Halt logs the shutdown, kills nonessential processes, executes the sync system call, waits for filesystem writes to complete, and then halts the kernel.

Reboot is almost identical to halt, but it reboots instead of halting.

34

Shell Basics

It is the script programming language.

Default shell is bash(“Bourne-again” shell) but

sh(original Bourne shell) and ksh(korn shell) are

used on a few UNIX system.

Shell scripts are used for light tasks such as

automating a sequence of commands.

Command editing – vi

35

Every process has at least three communication channels available to it : “standard input” (STDIN), “standard output” (STDOUT) and “standard error” (STDERR).

Each channel is named with a small integer called a file descriptor.

STDIN, STDOUT, STDERR are corresponds to file descriptors 0, 1, and 2.

STDIN reads from the keyboard and both STDOUT and STDERR write their output to the screen.

Pipes and Redirection

36

Shell interprets the symbols <, > and >> as instructions to reroute a commands input or output to or from a file.

< symbol connects the commands STDIN to the contents of an existing file.

> and >> symbols redirect STDOUT, > replaces the file’s existing contents, and >> appends to them.

37

To connect the STDOUT of one command to the STDIN of another, use the | symbol, commonly known as a pipe.

E.g. $ps –ef | grep httpd To select lines that contain the word httpd && - to execute a second command only if its

precursor completes successfully to separate the cmds with an && symbol.

E.g. mkdir se1 && ls

38

|| symbol executes the following cmd only if the preceding cmd fails(produces a nonzero exit status)

E.g. mkdir se1 || ls Use backslash to break a cmd onto multiple lines. Use ; - multiple cmds combined onto one line E.g. $ls;mkdir temp;ls

39

Variable names are unmarked in assignments but prefixed with a dollar sign when their values are referenced.

E.g. $temp=‘/root’ - variable names are case sensitive $echo “$temp” When referencing a variable, to surround its name with

curly braces to clarify to the parser and to human readers where the variable name stops and other text begins.

$echo “my age is ${temp} year”

Variables and quoting

40

wc : word count use -l, -w, -c Count lines, words, and characters E.g. $wc filename $wc –l filename find: to find location of files with there names E.g. find / -name findname

Filter commands

41

head and tail : read the beginning or end lines of a file

grep : search text

42

Bash Scripting

Bash script consist of a series of command lines.

Bash comments start with a hash mark (#)

# simple bash script

echo “good morning”

Compile and execute – bash bashscript_file-name

OR

source bashscript_file-name

43

To display message or value of variable using echo and printf cmd.

E.g. $echo –n “good\tmorning” $printf “good\tday” To take input from keyboad using read cmd. e.g. read username

Input and output

44

Command-line arguments to a script become variables whose names are numbers.

$1 is the first cmd-line argument $2 second c-l argu. $0 is the name by which the script was invoked. To specify arguments at the time of giving the

execution cmd i.e. cmd-line argument.

Command-line arguments and Functions

45

Function definition and call. Function call – use simply function name Function definition syntax – function function_name

{

# Body of function

} OR

function_name() {

statements

}

46

Scope of variable is – local and global to function. Variables are global within a script, but functions

can create their own local variables with a local declaration.

E.g. local variable_name

Variable scope

47

if – then syntax:

if [ condition ] ; then

# body of if

fi #terminator for an if stmt. If – then – else syntax:

if [ condition ] ; then

#body of if

else

#body of else

fi

Control flow

48

Elementary bash comparison operators:

String Numeric True ifx = y x -eq y x is equal to y

x != y x -ne y x is not equal to y

x < y x -lt y x is less than y

x <= y x -le y x is less than or equal to y

x > y x -gt y x is greater than y

x >= y x -ge y x is greater than or equal to y

-n x – x is not null

-z x – x is null

49

Arithmetic : All bash variables are string valued, so bash does not

distinguish between the number 1 and the character string “1” in assignments.

To force numeric evaluation, then enclose an expression in $((….))

50

Loops :

for variable in values

do

statements

done

While [ condition ]

do

statements

done

51

Array :

Syntax:

array_variable=(values)

Accessing array element by their subscript

Syntax : to access individual elements

${array_variable[subscript]}