Linux Booting Procedure
Download
Report
Transcript Linux Booting Procedure
UNIT - IV
Linux Booting and Shutting Down
How Linux boot?
2
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.
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.
3
Steps in the boot process
1.
2.
3.
4.
5.
6.
Reading of the boot loader from the MBR
(Master Boot Record)
Loading and initialization of the kernel
Device detection and configuration
Creation of kernel processes
Administrator intervention (single-user-mode
only)
Execution of system startup scripts
4
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
5
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.
6
MBR (Master Boot Record)
7
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
8
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
**The dd command, which needs to be run from
root, reads the first 512 bytes from /dev/hda (the
first Integrated Drive Electronics, or IDE drive)
and writes them to the mbr.bin file.
**The od command prints the binary file in hex and
ASCII formats.
9
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
10
filesystem temporarily
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
11
3. Device detection and configuration
Kernel loads device drivers
12
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
13
14
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
15
6. Execution of startup scripts
Startup scripts are just normal shell scripts,
and they are selected and run by init
Finally boot process completed
16
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.
17
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”.
18
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.
19
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.
20
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.
21
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.
22
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.
23
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
24
Ubuntu system - menu.lst file
25
GRUB is an operating system independant
boot loader
Flexible command line interface
File system access
26
Multibooting
default=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
27
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
28
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
29
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.
30
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.
31
32
$ sudo shutdown
system halt after
$ sudo shutdown
system halt after
–h +15 "system is going down for
15 minutes! “
–r +1 “system is going down for
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.
33
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
34
Pipes and Redirection
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.
35
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.
36
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
37
|| 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
38
Variables and quoting
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.
39
$echo “my age is ${temp} year”
Filter commands
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
40
head and tail : read the beginning or end lines of a
file
grep : search text
41
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
42
Input and output
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
43
Command-line arguments and Functions
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.
44
Function definition and call.
Function call – use simply function name
Function definition syntax –
function function_name
{
# Body of function
}
OR
function_name() {
statements
}
45
Variable scope
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
46
Control flow
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
47
Elementary bash comparison operators:
String
Numeric
True if
x=y
x != y
x<y
x <= y
x>y
x >= y
-n x
-z x
x -eq y
x -ne y
x -lt y
x -le y
x -gt y
x -ge y
–
–
x is equal to y
x is not equal to y
x is less than y
x is less than or equal to y
x is greater than y
x is greater than or equal to y
x is not null
x is null
48
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
$((….))
49
Loops :
for variable in values
do
statements
done
While [ condition ]
do
statements
done
50
Array :
Syntax:
array_variable=(values)
Accessing array element by their subscript
Syntax :
to access individual elements
${array_variable[subscript]}
51