Friday, July 3, 2015

Linux Booting Process

1.Reading the Boot Loader

The boot loader is a small piece of software usually at the very beginning of a large format like a disk or external storage device that contains only enough code to launch the operating system.

"A boot loader, also called a boot manager, is a small program that places the operating system of a computer into memory".

There are two types of boot loader which Linux uses: 

a) LILO(LInux LOader)
  • Is a boot loader for Linux and was the default boot loader for most Linux distributions.
  • Today most distributions use GRUB as the default boot loader. 
b) GRUB(GRand Unified Bootloader)
  • Is a boot loader package from the GNU project.
  • Provides a user the choice to boot one of multiple OS installed on a computer or select a specific kernel configuration.
Screen shot of GRUB boot loader. 

  • The boot loader is located on the Master Boot Record(MBR) or GUID Partition Table(GPT).
  • Firmware (BIOS or UEFI) provides a list of the boot devices to choose from (DVD, USB, Harddisk, Network via PXE).

2.Kernel Initialization

Boot loader opens and initializes the kernel.

  • Compressed kernel image is decompressed.
  • Kernel detects amount of RAM available and creates internal data structures.
  • kernel memory is unavailable to user space programs.

3.Device Detection

Detect and configure devices.

  • Kernel begins probing devices such as processors, I/O subsystems, storage devices.
  • Compressed Initial RAM disk "initrd" is decompressed and mounted.
  • "initrd" is for laoding a temporary root file system into memory, which may be used as a part of the Linux startup process.
  • "initrd" contains various executables and drivers that permit the real root file system to be mounted, after which the initrd is unmounted and its memory is freed.
  • Essential drivers such as RAID, LVM , encrypted file systems are loaded before "initrd" is unmounted and memory is freed. 

In the above screen shot-
  • 1st line tells about the hard-drive to boot from.
  • 2nd line tell what kernel to boot from i.e. the location of compressed kernel image. Here vmlinuz-2.6.32-358.e.16.x86 is the compressed kernel image.
  • 3rd line tells about the "initrd" (Initial RAM disk) location.Here initramfs-2.6.32-358.e.16.x86_64.img is image file of "initrd" to be mounted.


4.Process Creation

  • Kernel creates important processes that it uses internally such as kjournald, kswapd, khubd.
  • The Upstart init daemon, the "parent of all process" is spawned by the kernel with PID (Process ID) 1.
  • Upstart init daemon startup all the other processes on the system.
  • Local, virtual and swap filesystems are mounted.


5.Service startup
  • The init process which is started by the kernel and managed by "upstart" system runs a variety of jobs and scripts to load different system services such as power management, hardware interfaces and network daemon.
  • Traditionally in Linux, the startup process is handled by System V init. System V was old type of UNIX and Linux inherited System V.
  • System V has no. of limitations, starting to be replaced by much more flexible, powerful yet simple daemon called upstart.
  • SysV scripts are in /etc/init.d
  • Upstart maintains its job in /etc/init
  • For each runlevel there is links to /etc/rc?.d directory.




Tuesday, May 12, 2015

How To Use Flash drive From Terminal

Usually by default ubuntu mounts your flash drive
on /media/<user_name>/<drive_name>

For e.g. my flash drive name is "usb_drive"
             my user name is also "tarun" on ubuntu
             So I can access my flash drive contents on media/tarun/usb_drive



How to access flash drive from terminal ?

Well you can directly go to the above directory
/media/<your_username>/<drive_name> using cd command and access your drive if it is already mounted.

What if your device is not mounted ?

  •  You need to know what the drive is called to mount it. 
                                       sudo fdisk -l



Here you can see all your disks and their partitions
From above shot , I have two disk /dev/sda & /dev/sdb.
/dev/sda is my hard drive and it has one partition /dev/sda1
/dev/sdb is my flash drive and it also has single partition /dev/sdb1

Now i know that my flash drive is called "/dev/sdb1" on ubuntu as of now.

  • Create a mount point
Create a new directory in /media so you can mount the drive onto the filesystem:

                            sudo  mkdir /media/usb


  •  Mount your flash drive
                           sudo mount /dev/sdb1 /media/usb


  •   When you are done, unmount your flash drive
                            sudo umount /media/usb


Monday, April 27, 2015

Different ways to run a shell script

Let the script be demoscript



Method1:

Make the script executable and run by calling its name (here "./" is used to signify that the script is present in current directory)

$ ./demoscript
  • The script must be executable.
        This can be done using chmod command as shown:
                        

  • A new shell is loaded into memory, which reads the commands from the file.
  • If any variables are set during the running of the script, then the values of those variables are immediately lost as soon as the shell  script stops running.
  •  It can be seen here that the variable GREETING set in the script is no longer set here in the current shell as here a new shell was used to run the script.



Method2:

Use any shell(here bourne shell is used) to execute the script.

$ sh demoscript


  • Same as method 1,but the file does not need to be made executable




Method3:

Use the current shell to run the script.

$ . demoscript


  • The commands in the script are executed by the "current" shell, if any variables are set, they still remain set after the script is finished
  • It can be seen that the variable GREETING set in the script is still set here in the current shell as here the current shell itself is used to run the script.




Method4:

Using source command.
$ source demoscript


  •  "source" command can be used to load any functions file into the current shell script or a command prompt.
  • It reads & execute commands from given filename & return





Method5:

Using exec command.
$ exec ./demoscript

  • The current shell terminates, spawning a new shell in its place to execute the script.
  • As soon as the script has terminated, the user is logged off.
  • Note that exec command takes the path of the file to be executed as argument.

Saturday, January 3, 2015

Screen Brightness Setting in Ubuntu

Sometimes in Ubuntu, screen brightness reset to the lowest or highest on every boot. It’s inconvenient to configure the screen brightness on every login.

Here are some steps that one should follow to save the brightness settings.

step 1:Set the brightness of your choice that you want to have on every startup.
         


step 2: Open /etc/rc.local file in terminal window using editor.
            
           sudo nano /etc/rc.local

           I am using nano text editor, you can use gedit or any other editor.



step 3: Now add these two lines in rc.local file
           
           echo X > /sys/class/backlight/intel_backlight/brightness

           echo Y >  /sys/class/backlight/acpi_video0/brightness
          
           X and Y are actually values of brightness level

           The values of X and Y can be found as follows:
          
            - To get the value of X
            - go to /sys/class/backlight/intel_backlight folder
            - open 'brightness' file.
            - It will contain the some numeric value. This value is value of X.

            - Similarly to get the value of Y
            - go to /sys/class/backlight/acpi_video0 folder
            - open 'brightness' file.
            - It will contain the some numeric value. This value is value of Y



 Finally your rc.local file will look like:



Note here my value of X = 260 and Y=28.



Sunday, December 14, 2014

Running a command at startup

In this post I'll explain how to run particular application or command at startup(i.e when you login to your system.)

Here i am taking example of "Docky".

Docky is a dock for the GNOME desktop, very similar in functionality to Mac OS X's Dock.It is available for distributions such as Debian, Ubuntu, Fedora, Sabayon, OpenSUSE and Arch Linux.



To install docky on Ubuntu you can type the following command in terminal: