Tuesday 12 November 2013

How to create LVM (Logical Volume Management)

LVM Creates a higher level of abstraction than traditional disk linux partitions. This allows great flexibility in allocating storage.

Logical volumes can be  resized and moved b/w physical devicess easily.

Here We can create two partations and toggle it as LVM, The imge showing here is different and use it as a reference,

In order to create a Logical Volume, first we need a
1.Physical Volume, then a 
2.Volume Group and
3.Logical Volume

First we need an unpartitioned hard disk /dev/sda. First you need to create physical volumes. To do this you need partitions or a whole disk.
 

It is possible to run pvcreate command on /dev/sda, but I prefer to use partitions and from partitions I later create physical volumes.

So create two partitions first to create a  physical volume.

# fdisk -l

Disk /dev/sda: 22.9 GB, 22922297344 bytes
255 heads, 63 sectors/track, 2786 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

 Device Boot  Start End  Blocks   Id  System
/dev/sda1   *  1    2525 20282031 83  Linux
/dev/sda2     2526  2786 2096482+ 82  Linux swap/Solaris


Creating two Partitions for PV

Two ways we can go here depends upon the partitions you have.If you have more than 3 primary partitions go with extended partition method. If only two primary partition are their you can also create two more and toggle it as lvm. Either ways you can go depends upon your disk partations.

# fdisk -l

Disk /dev/sda: 32.8 GB, 32841072640 bytes
255 heads, 63 sectors/track, 3992 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

  Device Boot  Start End  Blocks   Id System
/dev/sda1   *   1    3315 26627706 83 Linux
/dev/sda2      3316  3446 1052257+ 82 Linux swap/Solaris

Primary partitions have a disadvantage: You only have four of them. (Comes from my earlier days... 64k will always be sufficient... why more that 4 partitions on a 10MB disk?) Therefore, with bigger disks a specific type of primary partition was defined - the "extended partition", which is nothing more than a construct to create
more partitions (itselves sometimes called extended partitions, sometimes "logical drives"). But still the space defined for the "extended partition" (the one in the primary partition table) is valid for the sum of all sub-partitions.

Primary partitions are old-school... I guess nowadays you get away with defining one huge "extended partition" and creating lots of sub-partitions in there. Technically speaking extended partitions take time to scan, since an auto-created sub-partition table is at the beginning of each sub-partition, with a single entry plus a pointer to the next sub-partition... but that's no real show stopper: How often do you need to scan those tables?

If you know that you will have four or less partitions on the disk, stick with simple primary partitions. If unsure, let at least p4 be an extended partition with the remainder of the disk.

# fdisk /dev/sda

The number of cylinders for this disk is set to 3992.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (3447-3992, default 3447):
Using default value 3447
Last cylinder or +size or +sizeM or +sizeK (3447-3992, default 3992):
Using default value 3992

Command (m for help):
Command (m for help): p

Disk /dev/sda: 32.8 GB, 32841072640 bytes
255 heads, 63 sectors/track, 3992 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot  Start End  Blocks   Id System
/dev/sda1   *    1    3315 26627706 83 Linux
/dev/sda2       3316  3446 1052257+ 82 Linux swap/Solaris

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (3447-3992, default 3447):
Using default value 3447
Last cylinder or +size or +sizeM or +sizeK (3447-3992, default 3992): +600M

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Selected partition 4
First cylinder (3521-3992, default 3521):
Using default value 3521
Last cylinder or +size or +sizeM or +sizeK (3521-3992, default 3992): +500M

Command (m for help): p

Disk /dev/sda: 32.8 GB, 32841072640 bytes
255 heads, 63 sectors/track, 3992 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot  Start End   Blocks   Id  System
/dev/sda1   *    1    3315  26627706 83  Linux
/dev/sda2       3316  3446  1052257+ 82  Linux swap/Solaris
/dev/sda3       3447  3520  594405   83  Linux
/dev/sda4       3521  3582  498015   83  Linux

Command (m for help): t
Partition number (1-4): 3
Hex code (type L to list codes): 8e
Changed system type of partition 3 to 8e (Linux LVM)

Command (m for help): t
Partition number (1-4): 4
Hex code (type L to list codes): 8e
Changed system type of partition 4 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/sda: 32.8 GB, 32841072640 bytes
255 heads, 63 sectors/track, 3992 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot  Start End  Blocks   Id  System
/dev/sda1   *    1    3315 26627706 83  Linux
/dev/sda2       3316  3446 1052257+ 82  Linux swap/Solaris
/dev/sda3       3447  3520 594405   8e  Linux LVM
/dev/sda4       3521  3582 498015   8e  Linux LVM

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.

# partprobe

# fdisk -l

Disk /dev/sda: 32.8 GB, 32841072640 bytes
255 heads, 63 sectors/track, 3992 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot  Start   End  Blocks   Id  System
/dev/sda1   *  1     3315 26627706  83  Linux
/dev/sda2     3316   3446 1052257+  82  Linux swap/Solaris
/dev/sda3            3447 3520      594405   8e  Linux LVM
/dev/sda4            3521 3582      498015   8e  Linux LVM


Created two new partations and toggled it using LVM. Now create a Physical Volume,

Creating Physical Volume PV

# pvcreate /dev/sda{3,4}
  dev_is_mpath: failed to get device for 8:3
  Writing physical volume data to disk "/dev/sda3"
  Physical volume "/dev/sda3" successfully created
  dev_is_mpath: failed to get device for 8:4
  Writing physical volume data to disk "/dev/sda4"
  Physical volume "/dev/sda4" successfully created

# pvdisplay
  "/dev/sda3" is a new physical volume of "580.47 MB"
  --- NEW Physical volume ---
  PV Name               /dev/sda3
  VG Name
  PV Size               580.47 MB
  Allocatable           NO
  PE Size (KByte)       0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID         SaxPtr-TI0S-3ITI-0zrh-j9ZB-0xOf-yX76sB

  "/dev/sda4" is a new physical volume of "486.34 MB"
  --- NEW Physical volume ---
  PV Name               /dev/sda4
  VG Name
  PV Size               486.34 MB
  Allocatable           NO
  PE Size (KByte)       0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID         UXpSUV-auh9-2gDg-IBJ6-UXij-FAid-CNLLdL

OR
  

# pvs
  PV         VG   Fmt  Attr PSize   PFree
  /dev/sda3       lvm2 a--  580.47M 580.47M
  /dev/sda4       lvm2 a--  486.34M 486.34M
 

Now Create a volume group inside the Physical Volume

Creating a Volume Group VG 

# vgcreate vg-ctechz /dev/sda{3,4}
  Volume group "vg0" successfully created

# vgdisplay
  --- Volume group ---
  VG Name               vg-ctechz
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               1.04 GB
  PE Size               4.00 MB
  Total PE              266
  Alloc PE / Size       0 / 0
  Free  PE / Size       266 / 1.04 GB
  VG UUID          ui4JTr-JOwC-VCvG-5Evc-poOD-3Klr-hM3feq

Or

# vgs
  VG        #PV #LV #SN Attr   VSize VFree
  vg-ctechz   2   0   0 wz--n- 1.04G 1.04G
 
We can create a volume group with fixed size as well, 

Ex: vgcreate -s +PESize vgname /dev/sda3 /dev/sda4

Now Create a Logical Volume 

Creating Logical Volume LV 

lvcreate -L +600 -n lvmName /dev/vgName

# lvcreate -L +600 -n lvm-ctechz /dev/vg-ctechz
Logical volume "lvm-sda" created

# lvdisplay
  --- Logical volume ---
  LV Name                /dev/vg-ctechz/lvm-ctechz
  VG Name                vg-ctechz
  LV UUID                jZEuoN-16MG-30eX-SZaI-8ETO-ZguH-YRVyeN
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                600.00 MB
  Current LE             150
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

OR

# lvs
  LV           VG       Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  lvm-ctechz vg-ctechz -wi-a- 600.00M


Now format the LVM Volume with the needed file system and mounted in the drive,

# mkdir /lvm-ctechz


# mkfs.ext3 /dev/vg-ctechz/lvm-ctechz
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
76800 inodes, 153600 blocks
7680 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=159383552
5 block groups
32768 blocks per group, 32768 fragments per group
15360 inodes per group
Superblock backups stored on blocks: 32768, 98304

Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 36 mounts 

or 180 days, whichever comes first.Use tune2fs -c or -i to override.

Mount it in fstab 

# Vim /etc/fstab
 /dev/vg-ctechz/lvm-ctechz /lvm-ctechz  ext3  defaults  0 0

# mount -a


# mount/dev/sda1 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/mapper/vg0-lvm--sda on /LVM type ext3 (rw)

# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              25G  4.4G   20G  19% /
tmpfs                 593M     0  593M   0% /dev/shm
/dev/mapper/vg--ctechz-lvm--ctechz
                      591M   17M  545M   3% /lvm-ctechz


# fdisk -l

Disk /dev/sda: 31.8 GB, 31890341888 bytes
255 heads, 63 sectors/track, 3877 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot Start End  Blocks   Id System
/dev/sda1   *   1    3315 26627706 83 Linux
/dev/sda2      3316  3446 1052257+ 82 Linux swap/Solaris
/dev/sda3      3447  3520 594405   8e Linux LVM
/dev/sda4      3521  3582 498015   8e Linux LVM



Create a big file and put it in mounted lvm dir the test another scenario's

# dd if=/dev/zero of=lvm-sizefile bs=1000024 count=500
Created some files to put in lvm volume

No comments:

Post a Comment