510 likes | 755 Views
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.
E N D
UNIT - IV Linux Booting and Shutting Down
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.
Steps in the boot process • 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
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
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.
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
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.
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
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
3. Device detection and configuration • Kernel loads device drivers
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
5. Administrator intervention (single user mode – recovery mode) • Fsckcmd 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
6. Execution of startup scripts • Startup scripts are just normal shell scripts, and they are selected and run by init • Finally boot process completed
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.
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”.
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.
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.
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.
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.
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.
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
GRUB is an operating system independant boot loader • Flexible command line interface • File system access
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
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
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
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.
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.
$ 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.
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
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.
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.
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
|| 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
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. • $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
head and tail : read the beginning or end lines of a file • grep : search text
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
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
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.
Function definition and call. • Function call – use simply function name • Function definition syntax – function function_name { # Body of function } OR function_name() { statements }
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
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
Elementary bash comparison operators: String Numeric True if x = 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
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 $((….))
Loops : for variable in values do statements done While [ condition ] do statements done