Tuesday, 19 July 2011

Raid 0 Creation in Linux

RAID : A Redundant Array of Independent Disks (RAID) is a series of disks that can save our data.
    -> It
allows an administrator to form an array of several hard drives into one logical drive recognized as one drive by the operating system.
    ->
It also spreads the data stored over the array of drives to decrease disk access time
    ->
The data redundancy can be used to recover datashould one of the hard drives in the array crash

RAID 0 (
Striping) :   data is written across all hard drives in the array

Example: Hard disks are categorized in Block devices.because it will read/write data in terms of blocks

We will write A, B, C, D, E and F – blocks data to a Raid array ,then Raid 0 how it will receive is

RAID LEVEL 0




Requirements :
-> 2 hard disk (2 partitions should use)

Advantages:
        ->fast  I/O operations
        ->
the size of the logical RAID drive is equal to the size of all the hard drives in the array.

Disadvantages:
        ->There is no redundancy, recovering data from a hard drive crash is not possible


Implementation:

i) Create a Raid Partiotions
 mdadm --create /dev/md0 --level=0 --raid-devices=2 /dev/sdb1 /dev/sdb2

ii) Create file system on it
mke2fs -j /dev/md0

iii) Mount it
 mount /dev/md0 /raid
or
Update  /etc/fstab with following line
/dev/md0 /raid ext3 defaults 1 2 


Monitor :

1)sudo nohup mdadm --monitor /dev/md0 --daemonize --delay=3600 --alert /raid/monitor.sh &  
     
/raid/monitor.sh      
      $output=sudo mdadm --detail --scan | grep -i "raid0"           
      `sudo echo $output > /etc/mdadm/mdadm.conf`;

2)Check the development of the RAID.
cat /proc/mdstat

3)
tail /var/log/messages


4)To Remove a Faulty raid array element
mdadm /dev/md0 --remove /dev/sdb2

5)To add new disk to raid array element
mdadm /dev/md0 --add /dev/sdb3

Understanding /etc/fstab & mtab

Syntax Of Fstab File


The following is the syntax of the /etc/fstab file in Linux.

[Device nhttp://www.google.com/notebook/#b=BDQ-QDAoQ_pa-kZQmame | UUID] [Mount Point] [File system] [Options] [dump] [fsck ord]
Here's an example of the contents of /etc/fstab:
/dev/hda2/ext2defaults1 1
/dev/hdb1/homeext2defaults1 2
/dev/cdrom/media/cdromautoro,noauto,user,exec0 0
/dev/fd0/media/floppyautorw,noauto,user,sync0 0
proc/procprocdefaults0 0
/dev/hda1swapswappri=420 0
 The first column contains the device name, the second one its mount point, third its filesystem type, fourth the mount options, fifth (a number) dump options, and sixth (another number) filesystem check options. Let's take a closer look at this stuff.

< 1st and 2nd columns: Device and default mount point >

 these lines mean that /dev/hda2 will be mounted to / and /dev/hdb1 to /home. This is done automatically when your Linux system boots up

< 3rd column: Filesystem type >

The third column in /etc/fstab specifies the filesystem type of the device or partition.
ext2 and ext3
reiserfs
swap The filesystem name is self-explanatory. The filesystem type "swap" is used in your swap partitions.
vfat and ntfs
auto No, this isn't a filesystem type :-) The option "auto" simply means that the filesystem type is detected automatically.

< 4th column: Mount options >

auto and noauto With the auto option, the device will be mounted automatically (at bootup, like when you issue the mount -a command). auto is the default option. I With noauto, the device can be mounted only explicitly.
user and nouser These are very useful options. The user option allows normal users to mount the device, whereas nouser lets only the root to mount the device. nouser is the default, 
exec and noexec exec lets you execute binaries that are on that partition, whereas noexec doesn't let you do that. exec is the default option.
ro Mount the filesystem read-only.
rw Mount the filesystem read-write.
sync and async How the input and output to the filesystem should be done. sync means it's done synchronously.async is the default. 
defaults Uses the default options that are rw, suid, dev, exec, auto, nouser, and async.

< 5th and 6th columns: Dump and fsck options >

Dump is a backup utility and fsck is a filesystem check utility. If it's zero, dump will ignore that filesystem.If it is 1 it will take the back of file system 
The 6th column is a fsck option. fsck looks at the number in the 6th column to determine in which order the filesystems should be checked. If it's zero, fsck won't check the filesystem.


Mtab:
-> mtab is the currently mounted filesystems
->This will automatically updated by mount command
mtab is a dynamic file. it's automatically updated by the mount command and will only list mounted devices/partitions 
before the mount command updates mtab it looks at fstab to see what actions to take when mounting specific devices.
Think of it this way 

fstab = file system table 
mtab = mounted file system table

How to gain Root Access as a Normal user in Unix/Linux

linux sudoers files
























The sudo command allows users listed in /etc/sudoers to run administrative commands. You can configure /etc/sudoers to set limits on the root privileges granted to a specific user.

linux sudo commands 

















To use sudo commands you don't need to give root password. A user with appropriate right from /etc/sudoers can execute root privilege command form his own passwords.

Limiting Access to sudo

You can limit access to the sudo command. Regular users who are authorized in /etc/sudoers can access administrative commands with their own password. You don't need to give out the administrative password to everyone who thinks they know as much as you do about Linux. To access /etc/sudoers in the vi editor, run the visudo command.
linux vi /etc/sudoers
From the following directive, the root user is allowed full access to administrative commands:



For example, if you want to allow user vinita full administrative access, add the following directive to/etc/sudoers:

root      ALL=(ALL) ALL
vinita    ALL=(ALL) ALL
In this case, all vinita needs to do to run an administrative command such as starting the network service from her regular account is to run the following command, entering her own user password (note the regular user prompt, $):

$ sudo /sbin/service network restart
Password:

linux sudo commands


You can even allow special users administrative access without a password. As suggested by the comments, the following directive in /etc/sudoers would allow all users in the wheel group to run administrative commands without a password:

%wheel   ALL=(ALL) NOPASSWD: ALL
But you don't have to allow full administrative access. For example, if you want to allow those in the %users group to shut down the local system, you can activate the following directive:

%users  localhost=/sbin/shutdown -h now

User Management in Unix/Linux


System administrations User managements -1




 In Linux user and group management is done by these for important files.

Linux files responsible for User managements


/etc/shadow      store all the Linux password in MD5 encryptions format
/etc/passwd      store all user related information's
/etc/group       store all group related information's

back-up files responsible for User managements

In this assignment we will modify these files. So it's better to take back-up before doing this assignment because your little mistake can crash Linux systems.

#mkdir  /backup
#cp /etc/passwd  /backup
#cp /etc/group   /backup
#cp /etc/shadow  /backup
linux User managements

Create a simple user

useradd is used to create user. Several options are used with useradd command but you will learn about then in our next assignments. In this assignment your task is to learn what exactly happens in these files when a new user is added. First observe the last line for these files.

#cat /etc/passwd |more
#cat /etc/shadow |more
#cat /etc/group  |more
Now add a simple user.

#useradd vinita
#passwd vinita
linux User managements
Now read these files again with cat command alternate you can use |grep switch to filter the output

#cat /etc/passwd  |grep vinita
#cat /etc/shadow  |grep vinita
#cat /etc/group   |grep vinita
# cd /home
#ls –ld vinita
linux User managements

User's entry in passwd

All these files are changed when a user is created In passwd files entries are in following formats separated by :

vinita           users login name
x                password required to login
503              unique user id
504              unique group id
/home/vinita     users home directory
/bin/bash        user shell
In shadow files entry is straight forwards. Whatever showing beside the user name is the password of user vinita in MD5 encrypt format.

User's entry in group

Whenever you create a normal user, users primary group form same name is automatically created. As you can verify by looking in /etc/group. 504 is the unique group id.

User's home directory

Same as group, users home directory is also created in /home partition and user get the ownership of this directory.


How to create a user without password.

linux User managements
To create a user without password use –d switch .

#useradd nikki
#passwd -d nikki

How to create a group.

To create group use groupadd commands. Group created by this command is called secondary group.

#groupadd test
#cat /etc/group |grep test

How to add user in groups

To add user in this group use usermod commands

#usermod –G test vinita
This command will make vinita user to member of test group.

How to delete secondary group

You can delete a group by groupdel commands

#groupdel test
#cat /etc/group |grep test
You cannot delete users primary group until user exist for example

#groupdel nikki

How to delete User

userdel command is used to delete user. When a users is deleted user’s primary group will automatically be deleted.

#userdel nikki
#groupdel nikki
groupdel: group nikki does not exist.
linux User managements
Whenever you delete user with userdel command. entry of user will be removed from these files. But users home folder and mail folder will not be deleted. As you can see in image. If you want completely remove user including his home folder and mail folder use –r switch with userdel commands.

System administrations User managements -2




To add a new user, use the useradd command. The basic syntax is

# useradd  [username]
The username is the only information required to add a new user; however, for exam prospective you should know some additional command-line arguments for useradd. The useradd command creates the account, butthe account is locked.
To unlock the account and create a password for the user, use the command passwd [username]. By default, the user's home directory is created and the files from /etc/skel/ are copied into it.
The two exceptions are if the –M option is used or if the home directory already exists.
Create a user with additional command-line arguments.In this example you are going to assign home directory on other locations so first create it and same as create first desired user's secondary group.

#mkdir /test
#groupadd example
#useradd –u 700 –d  /test/user1 –g example –c “testing user” –s /bin/sh –m user1
#passwd user1
useradd command

-c [fullname]    Full name of the user (or a comment about the user).
                 If more than one word is needed, place quotation marks
                 around the value.
-d [directory]   Home directory for the user. The default value is /home/[username]/.
-g [group]       Default group for the user specified as a group name or group ID
                 number. The group name or GID must already exist. The default is
                 to create a private user group. If a private user group is not
                 created, the default is the users group.
-m               Create a home directory for the user if it doesn't exist. Files from
                 /etc/skel/ are copied into the home directory.
-s [shell]       Specify the user login shell for the user. The default shell if not
                 specified is /bin/bash.
-u [uid]         Integer to use for the user ID. Must be unique unless -o is used.
                 Values less than 500 are reserved for system users.
-M               Do not create a home directory for the user. By default, a home
                 directory is created unless this option is used or unless the
                  directory already exists.
Now login form this user. And check where did this user logged in and why its shell prompt is looking different.

$pwd
/test/user1
useradd command
By default user gets bash sell prompts. But we modified this by –s switch and given user to /bin/sh shell. Now change user shell again

#usermod –s /bin/bash user1
useradd command
Verify by login again from user1
useradd command


How to manage bulk users

Consider a situation where you need to create more then thousand user. It will be really tedious task if you will do it by simple useradd commands. Here you have to switch to Linux shell scripts.
loop for creating user

# for USER in _ _ _ _ _ _ _ _ _  _ _
> do
>useradd $USER
>echo  _ _ _  _ |passwd  --stdin $USER
>done

Wednesday, 11 May 2011

Linux Booting sequence

Embedded Linux contains

1) Kernel
2) Initrd
3) Root-file system
4) Grub

Booting Sequence of  embedded linux

-> BIOS Loads the Grub (Stage1)
       Control passed from BIOS to Grub-Stage1

-> Grub Stage1 loads the Grub Stage2 with the help of stage1.5 (File system specific information),this stage2 will be at /boot/grub/stage2
      Control passed from Grub-stage1 to Grub-stage2

-> Stage2 reads it's configuration file i.e menu.lst (/boot/hrub/menu.lst)

-> So,Stage2 will loads the kernel image and initrd
     Control passed from Grub-stage2 to kernel (initrd)

->Initrd will mount the root file-system and pass the control to root file-system
     Control passed from initrd to root-file-system