Tuesday 26 January 2016

Step by step to create LVM (PV, VG and LV) in Linux 7

Scenario : Here we will use two HDD (10 GB and 5 GB) to create 3 LVM ( LV1, LV2 and LV3) which will be sized as 2 GB, 3 GB and 4 GB respectively and will mapped as /data1, /data2 and /data3 accordingly.


Solution:

Step 1: Check that 2 HDD is attached or not 

[root@rhel7 ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000aab0f

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    21506047    10240000   83  Linux
/dev/sda3        21506048    25700351     2097152   82  Linux swap / Solaris
/dev/sda4        25700352    41943039     8121344    5  Extended
/dev/sda5        25702400    40038399     7168000   83  Linux

Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x44245958

   Device Boot      Start         End      Blocks   Id  System

Disk /dev/sdc: 5368 MB, 5368709120 bytes, 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


[root@rhel7 ~]#

 Step 2: Now we prepare this HDD s as for Linux LVM

 [root@rhel7 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): n    [n for new partition]
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p):                 [default p for primary partition]
Using default response p
Partition number (1-4, default 1):          [default 1 for 1st partition]
First sector (2048-20971519, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +6G          [Partition size 6 GB]
Partition 1 of type Linux and of size 6 GiB is set

Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x44245958

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    12584959     6291456   83  Linux                        [Here partition is created for Linux now we have to change it for Linux LVM]

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x44245958

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    12584959     6291456   8e  Linux LVM

Command (m for help): w                              [w for write and save]
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

 Step 3: Now we can check that /dev/sdb1 partition is prepared for Linux LVM or not

[root@rhel7 ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000aab0f

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    21506047    10240000   83  Linux
/dev/sda3        21506048    25700351     2097152   82  Linux swap / Solaris
/dev/sda4        25700352    41943039     8121344    5  Extended
/dev/sda5        25702400    40038399     7168000   83  Linux

Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x44245958

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    12584959     6291456   8e  Linux LVM

Disk /dev/sdc: 5368 MB, 5368709120 bytes, 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

[root@rhel7 ~]#

Step 4: Now we will create another two partition using above process with 2 GB and 1 GB size for Linux LVM 

[root@rhel7 ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000aab0f

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    21506047    10240000   83  Linux
/dev/sda3        21506048    25700351     2097152   82  Linux swap / Solaris
/dev/sda4        25700352    41943039     8121344    5  Extended
/dev/sda5        25702400    40038399     7168000   83  Linux

Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x44245958

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    12584959     6291456   8e  Linux LVM
/dev/sdb2        12584960    14682111     1048576   8e  Linux LVM

Disk /dev/sdc: 5368 MB, 5368709120 bytes, 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x6cba2f17

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048     4196351     2097152   8e  Linux LVM
[root@rhel7 ~]#


Step 5: Now we will create two PV , one using /dev/sdb1 and /sdc1 and another using /dev/sdb2

[root@rhel7 ~]# pvcreate /dev/sdb1 /dev/sdc1
  Physical volume "/dev/sdb1" successfully created
  Physical volume "/dev/sdc1" successfully created
[root@rhel7 ~]# pvcreate /dev/sdb2
  Physical volume "/dev/sdb2" successfully created

[root@rhel7 ~]# pvdisplay
  "/dev/sdb1" is a new physical volume of "6.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name
  PV Size               6.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               dntVQt-iu1N-S3aD-PnCm-di0p-UPwd-QDkAJy

  "/dev/sdc1" is a new physical volume of "2.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdc1
  VG Name
  PV Size               2.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               FDZxdu-fGSk-PPgx-k5in-OJ8X-uTBO-RkKNsq

  "/dev/sdb2" is a new physical volume of "1.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb2
  VG Name
  PV Size               1.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               MqHd3I-QbR0-vVS9-qmJ0-jPc7-oiHE-nMdH8R

[root@rhel7 ~]#

Step 6: Now we will create a vg named linux and allocate two pv into it

[root@rhel7 ~]# vgcreate -s 16M linux /dev/sdb1 /dev/sdb2 /dev/sdc1
  Volume group "linux" successfully created

[root@rhel7 ~]# vgdisplay
  --- Volume group ---
  VG Name               linux
  System ID
  Format                lvm2
  Metadata Areas        3
  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                3
  Act PV                3
  VG Size               8.95 GiB
  PE Size               16.00 MiB
  Total PE              573
  Alloc PE / Size       0 / 0
  Free  PE / Size       573 / 8.95 GiB
  VG UUID               HLlZVq-29Wg-FpcI-0P3R-9NI1-aTKE-EJswOC

Note: Here physical extent (PE) size 16 MB, 4 MB is default

Step 7: Now we will create a lv1, lv2 and lv3 named lv1 with vg linux

[root@rhel7 ~]# lvcreate --name lv1 --size 2G linux
WARNING: ext4 signature detected on /dev/linux/lv1 at offset 1080. Wipe it? [y/n] y
  Wiping ext4 signature on /dev/linux/lv1.
  Logical volume "lv1" created

[root@rhel7 ~]# lvcreate --name lv2 --size 3G linux
WARNING: ext4 signature detected on /dev/linux/lv2 at offset 1080. Wipe it? [y/n] y
  Wiping ext4 signature on /dev/linux/lv2.
  Logical volume "lv2" created

[root@rhel7 ~]# lvcreate --name lv3 --size 3.5G linux
  Logical volume "lv3" created
[root@rhel7 ~]#

[root@rhel7 ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/linux/lv1
  LV Name                lv1
  VG Name                linux
  LV UUID                ZhsY0L-LVEY-8RIS-fZue-wWaf-TMNK-7mPivm
  LV Write Access        read/write
  LV Creation host, time rhel7, 2016-01-26 16:22:47 +0600
  LV Status              available
  # open                 0
  LV Size                2.00 GiB
  Current LE             128
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

  --- Logical volume ---
  LV Path                /dev/linux/lv2
  LV Name                lv2
  VG Name                linux
  LV UUID                FmRcHL-2GfR-dBFQ-VYP1-NaA1-J1Mw-dbxxBU
  LV Write Access        read/write
  LV Creation host, time rhel7, 2016-01-26 16:23:09 +0600
  LV Status              available
  # open                 0
  LV Size                3.00 GiB
  Current LE             192
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1

  --- Logical volume ---
  LV Path                /dev/linux/lv3
  LV Name                lv3
  VG Name                linux
  LV UUID                ez17be-QV8v-KG8b-mcTp-g8rx-sm36-tctaX1
  LV Write Access        read/write
  LV Creation host, time rhel7, 2016-01-26 16:23:27 +0600
  LV Status              available
  # open                 0
  LV Size                3.50 GiB
  Current LE             224
  Segments               3
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2

[root@rhel7 ~]#

Step 8: Now create file system (ext4)

[root@rhel7 ~]# mkfs.ext4 /dev/linux/lv1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131072 inodes, 524288 blocks
26214 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912

Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

[root@rhel7 ~]# mkfs.ext4 /dev/linux/lv2
[root@rhel7 ~]# mkfs.ext4 /dev/linux/lv3
[root@rhel7 ~]#

Step 9: Now create three directory (/data1, /data2 and /data3) to mount three lvm 

[root@rhel7 ~]# mkdir /data1
[root@rhel7 ~]# mkdir /data2
[root@rhel7 ~]# mkdir /data3

Step 10: Now mount three lvm

[root@rhel7 /]# mount /dev/linux/lv1 /data1
[root@rhel7 /]# mount /dev/linux/lv2 /data2
[root@rhel7 /]# mount /dev/linux/lv3 /data3
[root@rhel7 /]# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/sda2              9.8G  7.3G  2.6G  75% /
devtmpfs               486M     0  486M   0% /dev
tmpfs                  494M   80K  494M   1% /dev/shm
tmpfs                  494M  7.1M  487M   2% /run
tmpfs                  494M     0  494M   0% /sys/fs/cgroup
/dev/sda5              6.9G   33M  6.8G   1% /data
/dev/sda1              497M  118M  380M  24% /boot
/dev/mapper/linux-lv1  2.0G  6.0M  1.8G   1% /data1
/dev/mapper/linux-lv2  2.9G  9.0M  2.8G   1% /data2
/dev/mapper/linux-lv3  3.4G   14M  3.2G   1% /data3
[root@rhel7 /]#



No comments:

Post a Comment