LVM Basics: Creating A New Filesystem With fdisk lvm and mkfs

Logical Volume Management (LVM) is a disk management tool supported by all major Linux distributions.
With LVM, disks and partitions can be abstracted to contain multiple disks and partitions into one device.

Basic building blocks of LVM:
1.       Physical volume (PV): Partition on hard disk (or even the disk itself ) on which you can have volume groups. It is divided into physical extents(PE).  physical volumes = big building blocks used to build your hard drive.
2.       Volume group (VG): Group of physical volumes used as a storage volume (as one disk). They contain logical volumes. volume groups = as hard drives.
3.       Logical volume (LV): A logical partition of a volume group and is composed of physical extents. Logical volumes = normal partitions.
4.       Physical extent (PE): The smallest size in the physical volume that can be assigned to a logical volume (default 4MiB). Physical Extents = parts of disks that can be allocated to any partition.

Scenario:
In this example let us
·         Create 3 partitions of size each 100MB.
·         Convert them into physical  volumes.
·         Combine physical volumes into volume group.
·         Finally create a logical volume from the volume group.
·         Create filesystem and Mount it.

Step 1.  Rescan for new disk(s) which has been added to server  to create new LVM stuff.
Rescan for new disks can be done as below :-
[root@server ~]# lsscsi |wc  -l    //to take previous rescan count               
Ø  Find out how many SCSI controller configured
[root@server ~]# ls /sys/class/scsi_host/host
host0 host1 host2
In this case,you need to scan host0,host1 & host2.

Ø  Scan the SCSI disks using below command.
[root@server ~]# echo "- - -" > /sys/class/scsi_host/host0/scan
[root@server ~]# echo "- - -" > /sys/class/scsi_host/host1/scan
[root@server ~]# echo "- - -" > /sys/class/scsi_host/host2/scan
Ø  Now you can check for new disks by lsscsi
[root@server ~]# lsscsi
Note :- From Redhat Linux 5.4 onwards, redhat introduced “/usr/bin/rescan-scsi-bus.sh” script to scan all the SCSI bus and update the SCSI layer to reflect new devices. So rescan-scsi-bus.sh will do all above steps itself.
Step 2.  Create partitions on newly added disk
Now let us create three partions of each size 100MB using fdisk command.
[root@server ~]# fdisk /dev/sdb
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1044, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1044, default 1044): +100M
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (15-1044, default 15):
Using default value 15
Last cylinder, +cylinders or +size{K,M,G} (15-1044, default 1044): +100M
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (29-1044, default 29):
Using default value 29
Last cylinder, +cylinders or +size{K,M,G} (29-1044, default 1044): +100M
To check whether the partions have been created use the parameter “p”.
Command (m for help): p
Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0007b12c
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          14      112423+  83  Linux
/dev/sdb2              15          28      112455   83  Linux
/dev/sdb3              29          42      112455   83  Linux

To prepare the partition to be used by LVM use the following two commands.

t = change partition type
8e = changes to LVM partition type

 Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          14      112423+  8e  Linux LVM
/dev/sdb2              15          28      112455   8e  Linux LVM
/dev/sdb3              29          42      112455   8e  Linux LVM

Save the newly created partions.
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
Update the kernel to save the changes without restarting the system.
[root@server ~]# partprobe

Step 3.  Create Physical Volume
[root@server ~]# pvcreate /dev/sdb1 /dev/sdb2 /dev/sdb3
  Physical volume "/dev/sdb1" successfully created
  Physical volume "/dev/sdb2" successfully created
  Physical volume "/dev/sdb3" successfully created
To verify the newly created physical volumes use the command pvdisplay.
[root@server ~]# pvdisplay
  "/dev/sdb1" is a new physical volume of "109.79 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name              
  PV Size               109.79 MiB
  Allocatable           NO
  PE Size               0  
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               SkHu-4lhZ-Fwz8-fxTL-J3nQ-zax9-zGYhDO
  
  "/dev/sdb2" is a new physical volume of "109.82 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb2
  VG Name              
  PV Size               109.82 MiB
  Allocatable           NO
  PE Size               0  
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               vXwg-8hYB-Fwz8-fxTL-rDSqUY-zGYhDO
  
  "/dev/sdb3" is a new physical volume of "109.82 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb3
  VG Name               
  PV Size               109.82 MiB
  Allocatable           NO
  PE Size               0  
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               99qkNw-zGYhDO-vXwg-WE6U-zyKO-Ffs3

Step 4.  Create Volume Group
Create a new volume group called vg00 using two physical volumes /dev/sdb1 and /dev/sdb2 using the command vgcreate.
[root@server ~]# vgcreate vg00 /dev/sdb1 /dev/sdb2
  Volume group "vg00" successfully created
To verify the volume group has been created or not use the command vgdisplay.
[root@server ~]# vgdisplay
  --- Volume group ---
  VG Name               vg00
  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               216.00 MiB
  PE Size               4.00 MiB
  Total PE              54
  Alloc PE / Size       0 / 0  
  Free  PE / Size       54 / 216.00 MiB
  VG UUID               SkHu-4lhZ-Fwz8-fxTL-J3nQ-zax9-uIED-41gjqI

Step 5.  Create Logical Volume
To create logical volume use the command lvcreate. Let us create a logical volume called lv00 with size 200MB.
[root@server ~]# lvcreate -L 200M vg00 -n lv00
  Logical volume "lv00" created
Verify the logical volume is created or not using command lvdisplay.
[root@server ~]# lvdisplay
  --- Logical volume ---
  LV Name                /dev/vg00/lv00
  VG Name                vg1
  LV UUID                Fwz8-fxTL-JZdn-NUSF-fUS1-YVFk-36qs-SkHu-4lhZ
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                200.00 MiB
  Current LE             50
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

Step 6. Format and Mount  Logical Volume
Now format logical volume and mount it in the /test directory.
[root@server ~]# mkfs.ext4 /dev/vg00/lv00
mke2fs 1.41.12 (19-Dec-2011)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
51200 inodes, 204800 blocks
10240 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
25 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
 8193, 24577, 40961, 57345, 73729
Writing inode tables: done                           
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 35 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
And mount the logical volume in the /mnt mount point.
[root@server ~]# mount /dev/vg00/lv00 /test

[root@server ~]# df -h
Filesystem            Size      Used     Avail      Use%    Mounted on
/dev/vg00/lv00      200M   22M   178M     1%          /test
Note : To make this filesystem consistent across the reboot , please append below entry in /etc/fstab file
/dev/vg00/lv00 /test      ext4       defaults               0  0

That`s it , you have just wasted your life`s few minutes on creating a successful LVM filesystem  :)
Cheers !!
AJ







Comments

Popular posts from this blog

HP-UX virtual Machine Cheat Sheet

How to run fdisk in non-interactive mode