Thursday, 7 January 2016

Managing LVM in linux

Managing LVM in linux



  In LVM there are 3 main consideration and they are

1. Physical Volume  # deal with harddisk
2. Volume group       # deal with physical volume
3.Logical volume      # deal with OS

To create a Logical volume we need to have physical volume and volume group

Initial method:
i) Insert a new hard disk in system
ii) check the new harddisk partition name (eg: /dev/hd2  or /dev/sdb)
iii) follow the below command ,

fdisk -l                    # display all the partition
fdisk /dev/sdb      #  Incase ur new harddisk partition name is sdb
(in fdsik console)
n                              #  create a new partition
p                               # to create primary partion and e- for extended partion
1                               #  represent partition number

then press Enter button twice  # to use full partition and incase u required to create multiple partition choose cylinder(first & last) size as required.

Now partition is created and we need to select the partition type(i.e. LVM)

t                                #  to change partition type
8e                              # for LVM partition type and for use L for help list
p                               # print command to verify is created successfully or not
w                               # write the changes to the disk



a) To create a physical volume

pvcreate /dev/sdb1    # becoz we have partition the new disk as sdb1 in above command
pvdisplay                       #  to view the physical volume

b) To create a volume group

vgcreate  vol1 /dev/sdb1    # where vol1 is volume group name


c) To create a Logical volume

lvcreate -L 10G -n lv1 /dev/vol1  #  where -L size need to be assigned to logical volume
                                                               #  where 10G is the  size of logical volume
                                                               #   -n is to name needed to be given to logical volume                                                                       and -n is not need if already logical volume is present
                                                               #  lv1 is name of logical volume
                                                               #   vol1 is volume group that lv1 assigned to, so it take                                                                       space  from it

d) To format the logical volume

   mkfs.ext4 /dev/vol1/lv1          # to format lv1

e)  To mount the logical volume

mkdir /mnt   # to create a mount point where u like to mount the lvm

mount /dev/vol1/lv1 /mnt        # it will mount the lvm in /mnt directory


Note: the mount will be available only on current session to permanently mount this partition add the entry in /etc/fstab

to permanent mount edit /etc/fstab file and the entry mentioned below at the end of the file


/dev/vol1/lv1   /mnt    ext4   defaults   0  0 



f) To Extend a volume group

i) first requirement is we need new hard disk becoz we have used total harddisk in previous example
ii)  use the initial method mentioned above and give new partition number (e.g: /dev/sdc1)

vgextend vol1 /dev/sdc1  # how vol1 is extend


Note: if sdb1 is 5GB and sdc1 is 5GB and now vol1 size is 10GB after vgextend command


g) To extend logical volume

extend method can be achieved using two method one by giving fixed value and and another by using add .

i) using fixed method

lvextend -L 7GB /dev/vol1/lv1   # now lvm size is 7GB

ii) using add method

lvextend -L+4GB /dev/vol1/lv1   # if existing lvm size is 5GB and 4GB is added and now                                                                        total size of lvm is 9GB


h) To resize the logical volume

until you resize command in lvm, system cant use the extended lvm size.

resize2fs  /dev/vol1/lv1


h) To reduce logical volume


i) umount /dev/vol1/lv1           # unmount the logical volume

ii)  e2fsck -f /dev/vol1/lv1        # checking filesystem for any error

iii)resize2fs -p /dev/vol1/lv1  5G   #resize the filesystem to 5GB and -p for print

iv)  lvreduce -L 5GB /dev/vol1/lv1   # reducing lvm size

v) mount /dev/vol1/lv1   # remounting the lvm module


g) To snapshot logical volume manager

we need to add a new harddisk /dev/sdd1 and mount it in vol1 as follow or you have already using /dev/sdc1 in vol1 for this task.

pvcreate /dev/sdd1                                  # creating physical volume

vgextend vol1 /dev/sdd1                         #  extending volume group size

lvcreate -L 50GB -n lv1backup /dev/vol1     #  creating logical volume of 50GB size

mkfs.ext4 /dev/vol1/lv1backup              #   formating logical volume

mkdir /lvmtest                                           # creating mount point

mount /dev/vol1/lv1backup /lvmtest         # mounting logical volume

To mount permanently add the entry in /etc/fstab

/dev/vol1/lv1backup /lvmtest   ext4  defaults  0  0

lvcreate -L 20GB -s -n lv1snapshot /dev/vol1/lv1     # lvm snapshot creation

mkdir /backup              # creating mount point

mount /dev/vol1/lv1snapshot  /backup   # mounting snapshot

# for backup we can use two method using tar and dd(i.e: image)

using tar

tar -pczf  /lvmtest/lv1.tar.gz  /backup   # backup of lv1 in /backup dir

or using image

dd if=/backup of=/lvmtest/lv1.dd    #   backup of lv1 in /backup

umount /backup                                  # unmounting for freeing the resource
lvremove /dev/vol1/lv1snapshot      # removing lv1snapshot




h) To rescue the lvm using lvmsnapshot backup

i) first we need to boot the os in rescue mode

ii)then mount the partition

mount            # mounting the file systems

dd if=/lvmtest/lv1.dd of /dev/vol1/lv1    #  rescue of lv1
































 

Wednesday, 6 January 2016

Slow linux system

Slow linux system:

 System slow performance may be caused primary due to four reason,

1. RAM problem
2. I/O problem
3. App problem
4. CPU problem.


to check the real time system usage use command top
#top


                                                       top(wa)
                                                                  |
                                            ---high--------|------low---------------
                                          |                                                         |
                                         |                                                      |
                  Check swap(free -m)                       check %CPU (id)
                                          |                                                         |
                   -----high-------|---low-------                   -----high----|-------low-------
                   |                                       |                 |                                         |
        RAM problem              I/O problem       App problem               CPU problem
              (free)                           (iotop)         (strace,lsof)              (%us in top to check,
                                                                                                  which process consuming cpu)



Rescuing a Non-booting GRUB on Linux

Rescuing a Non-booting GRUB on Linux

1. To check what partition available in linux use
grub> ls

Output:
(hd0,1) (hd0,msdos1)

2. To verify the file available in particular partition


grub> ls(hd0,1)

Output:
ost+found/ bin/ boot/ cdrom/ dev/ etc/ home/  lib/
lib64/ media/ mnt/ opt/ proc/ root/ run/ sbin/
srv/ sys/ tmp/ usr/ var/ vmlinuz vmlinuz.old
initrd.img initrd.img.old


In the output we can find the root file system is present in (hd0,1) partition. Now we can start our rescue of linux

3. booting from grub

a) to set the boot files and boot the system from the grub> prompt. We know from running the ls command that there is a Linux root filesystem on (hd0,1), and you can keep searching until you verify where /boot/grub is. Then run these commands, using your own root partition, kernel, and initrd image:

grub> set root=(hd0,1) # sets the partition that the root filesystem is on
grub> linux /boot/vmlinuz-3.08.0-28-generic root=/dev/sda1 # tells GRUB the location of the kernel you want to use
grub> initrd /boot/initrd.img-3.08.0-28-generic # sets the initrd file
grub> boot #boots your system

b) On some Linux systems the current kernels and initrds are symlinked into the top level of the root filesystem:
$ ls -l /
vmlinuz -> boot/vmlinuz-3.08.0-28-generic
initrd.img -> boot/
initrd.img-3.08.0-28-generic
to boot grub like this follow the below steps,
grub> set root=(hd0,1)
grub> linux /vmlinuz root=/dev/sda1
grub> initrd /initrd.img
grub> boot

4. Boot from grub-rescue mode:

If you're in the GRUB rescue shell the commands are different, and you have to load thenormal.mod andlinux.mod modules:
grub rescue> set prefix=(hd0,1)/boot/grub
grub rescue> set root=(hd0,1)
grub rescue> insmod normal
grub rescue> normal
grub rescue> insmod linux
grub rescue> linux /boot/vmlinuz-3.08.0-28-generic root=/dev/sda1
grub rescue> initrd /boot/initrd.img-3.08.0-28-generic
grub rescue> boot


5. to make the permanent repair:

When you have successfully booted your system, run these commands to fix GRUB permanently:
# update-grub
# grub-install /dev/sda
When you run grub-install remember you're installing it to the boot sector of your hard drive and not to a partition, so do not use a partition number like /dev/sda1.




Tuesday, 13 October 2015

Installation of ROR and postgres in Ubuntu 14.04

Installation of ROR  and postgres in Ubuntu 14.04 (Reference from various website only used for personal study purpose)

Step 1: installing Ruby on Rails with rvm is to run the following commands as a regular user


gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
\curl -sSL https://get.rvm.io | bash -s stable --rails
Step 2:  source the rvm scripts using  source ~/.rvm/scripts/rvm
Step 3:specify ruby version on your requirement
rvm install ruby_version
To verify list of ruby available in RVM using    
rvm list
To Switch to specific ruby version use
rvm use ruby-*.*.*
Step 4: To install postgres package use folowing command
sudo apt-get install postgresql postgresql-contrib
The installation procedure created a user account called postgres that is associated with the default Postgres role. In order to use Postgres, we'll need to log into that account. You can do that by typing:
sudo -i -u postgres
to exit from postgres using \q command
To create new role in postgres 
createuser --interactive
enter user as postgres and give super admin privilage .
Now ROR and Postgres database is installed on your ubuntu 14.04 machine