Search This Blog

Wednesday, December 16, 2015

RHEL 7 - Adding a Logical Volume

LVM Management Essential Commands:

Command
Explanation
pvcreate
Creates physical volumes
pvs
Shows a summary of available physical volumes
pvdisplay
Shows a list  of physical volumes and their properties
vgcreate
Creates volume groups
vgs
Shows a summary of available volume groups.
vgdisplay
Shows a details list of volume groups and their properties
lvcreate
Create a logical volumes
lvs
Shows a summary of all available logical volumes.
lvdisplay
Shows a details list of available logical volumes and their properties.


A 400Mib logical volume called storage in the volume group shzzam, mounted at /storage. The volume group consist of two phiysical volumes, each 256Mib in size.


1.Create the Physical Resources
                  1.1 Use fdisk to create two partition of 256Mib apeice and set them to type Linux LVM.

[root@server1 /]# fdisk /dev/sdb

                  1.2 Add a new primary Partition of 256MiB.

[root@server1 /]# 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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x9d32561a.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-10485759, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759): +256M
Partition 1 of type Linux and of size 256 MiB is set

                  1.3 Change the Partition type to Linux LVM - 0X8e

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'

                  1.4 Repeat the previous two steps to add a second primary partition of the same size in the next available partition space.

                  1.5 Write the changes to the partition table and quit.

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

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

                  1.6 Use partprobe to register the new partitions with the kernel.

[root@server1 /]# partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.

[root@server1 /]# cat /proc/partitions
major minor  #blocks  name

  11        0    3655680 sr0
   8        0   20971520 sda
   8        1    1048576 sda1
   8        2   10485760 sda2
   8       16    5242880 sdb
   8       17     262144 sdb1
   8       18     262144 sdb2

2.Create the Physical Volumes
Use pvcreate to add the two new partitions as PVs

[root@server1 /]# pvcreate /dev/sdb1 /dev/sdb2
  Physical volume "/dev/sdb1" successfully created
  Physical volume "/dev/sdb2" successfully created

Now type pvs to verify that the physical volume has been created sucessfully.
[root@server1 /]# pvs
  PV         VG   Fmt  Attr PSize   PFree 
  /dev/sdb1       lvm2 a--  256.00m 256.00m
  /dev/sdb2       lvm2 a--  256.00m 256.00m

As an alternative to pvs command. which shows a summary of the physical volumes and their attributes, you can also use the pvdisplay command to show some more details.

[root@server1 /]# pvdisplay
  "/dev/sdb1" is a new physical volume of "256.00 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name              
  PV Size               256.00 MiB
  Allocatable           NO
  PE Size               0  
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               1ZqhwC-06DD-1emQ-GKzz-28Iy-wAst-dGU9Jr
  
  "/dev/sdb2" is a new physical volume of "256.00 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb2
  VG Name              
  PV Size               256.00 MiB
  Allocatable           NO
  PE Size               0  
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               JLOu34-HlEH-AGne-3YPg-UyVU-ZStF-HTAAqA

If you want a very synthetic overview of the current configuration, you might also like the lsblk command. This command gives a hierarchical overwiew of which disks and partitions are used in what LVM volume groups and logical volumes.

[root@server1 /]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   20G  0 disk
+-sda1   8:1    0    1G  0 part [SWAP]
+-sda2   8:2    0   10G  0 part /
sdb      8:16   0    5G  0 disk
+-sdb1   8:17   0  256M  0 part
+-sdb2   8:18   0  256M  0 part
sr0     11:0    1  3.5G  0 rom  /run/media/root/RHEL-7.0 Server.x86_64

3. Create a Volume Group

Use vgcreate to create a new VG named shazam built from the two PVs.

[root@server1 /]# vgcreate shazam /dev/sdb1 /dev/sdb2
  Volume group "shazam" successfully created

You can do this in one-step procedure as well (where using a seprated pvcreate command will not be necessary) if you are adding a partition to the volume group, howeiver it must be marked as partition type 8e already. Both Initializing and adding disk to volume group are done in one single step.

[root@server1 /]# vgcreate shazam /dev/sdb1 /dev/sdb2
  Physical volume "/dev/sdb1" successfully created
  Physical volume "/dev/sdb2" successfully created
  Volume group "shazam" successfully created

After creating the Volume Group, you can request details about the volume group using vgs command for a short summary, or the vgdisplay to get more information.


[root@server1 /]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree 
  shazam   2   0   0 wz--n- 504.00m 504.00m

[root@server1 /]# vgdisplay
  --- Volume group ---
  VG Name               shazam
  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               504.00 MiB
  PE Size               4.00 MiB
  Total PE              126
  Alloc PE / Size       0 / 0  
  Free  PE / Size       126 / 504.00 MiB
  VG UUID               wWqgm1-BjlR-yByw-3XVN-11RD-25ay-Hfzj3Y

4. Create the Logical Volume
Use lvcreate to create a 400MiB LV name storage from the shazam VG.

[root@server1 /]# lvcreate -n storage -L 400M shazam
  Logical volume "storage" created

This will create a device called /dev/shazam/storage, currently without a file system on it.
After creating the Logical Volume, you can request details about the Logical Volume using the lvs command in short summary, or using lvdisplay command to get more information.


[root@server1 /]# lvs
  LV      VG     Attr       LSize   Pool Origin Data%  Move Log Cpy%Sync Convert
  storage shazam -wi-a----- 400.00m        

                                   
[root@server1 /]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/shazam/storage
  LV Name                storage
  VG Name                shazam
  LV UUID                tAaxA1-RPIB-cjhy-FOFu-gwYN-cWpo-4HHhBz
  LV Write Access        read/write
  LV Creation host, time server1.example.com, 2015-12-16 01:05:30 +0100
  LV Status              available
  # open                 0
  LV Size                400.00 MiB
  Current LE             100
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

For Naming LVM volumes, another system plays a role: device mapper. The device mapper (abbreviated as dm) is a generic interface that the Linux Kernel uses to address storage devices. This devices are created in two locations; as devices that are sequentially number in the /dev/  directory, such as /dev/dm-0, /dev/dm-1 and further. Because these devices name do not provide any information about the device and therefore are confusing, symbolic links are create in /dev/mapper directory. These symbolic links use a name that uses the vgname-lvname patterns. So the device /dev/shazam/storage would also be known as /dev/mapper/shazam-storage.

[root@server1 /]# \ls -l /dev/shazam/storage
lrwxrwxrwx. 1 root root 7 Dec 16 01:05 /dev/shazam/storage -> ../dm-0
[root@server1 /]# \ls -l /dev/mapper/
total 0
crw-------. 1 root root 10, 236 Dec 16 00:17 control
lrwxrwxrwx. 1 root root       7 Dec 16 01:05 shazam-storage -> ../dm-0
[root@server1 /]# \ls -l /dev/dm-0
brw-rw----. 1 root disk 253, 0 Dec 16 01:05 /dev/dm-0


5. Add a Persistent File System
                  5.1 Use mkfs to place an xfs file system on the storage LV; use the LV device name.


[root@server1 /]# mkfs -t xfs /dev/shazam/storage
meta-data=/dev/shazam/storage    isize=256    agcount=4, agsize=25600 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=102400, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

                  5.2 Use mkdir to create a mount point at /storage.

[root@server1 /]# mkdir /storage

                  5.3 Use vim to add the following line to the bottom of /etc/fstab.

/dev/shazam/storage     /storage        xfs     default s1 2

                  5.4 Use mount to verify the /etc/fstab entry and mount the new storage lv.

[root@server1 /]# mount -a

6. Test and Review Your Work
                  6.1 As a final test, copy some file onto /storage and verify how many were copied.

[root@server1 /]# cp -a /etc/*.conf /storage
[root@server1 /]# ls /storage |wc -l
49

                  6.2 fdisk -l /dev/sdb will show you the partitions that exist on /dev/sdb.

[root@server1 /]# fdisk -l /dev/sdb
Disk /dev/sdb: 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: 0x9d32561a   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048      526335      262144   8e  Linux LVM
/dev/sdb2          526336     1050623      262144   8e  Linux LVM

Check /dev/sdb1 and /dev/sdb2 entries, and notice the ID and System colums showing 8e and Linux LVM respectively.

                  6.3 mount will show all the devices that are mounted and any mount options. It should include /dev/shazam/storage.
                  Note: Many tools will reprot the device mapper name instead  /dev/mapper/shazam-storage; it is the same logical volume.

[root@server1 /]# mount
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel)
devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=1443016k,nr_inodes=360754,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,seclabel)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,seclabel,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,seclabel,mode=755)
tmpfs on /sys/fs/cgroup type tmpfs (rw,nosuid,nodev,noexec,seclabel,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpuacct,cpu)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)
cgroup on /sys/fs/cgroup/net_cls type cgroup (rw,nosuid,nodev,noexec,relatime,net_cls)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb)
configfs on /sys/kernel/config type configfs (rw,relatime)
/dev/sda2 on / type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
selinuxfs on /sys/fs/selinux type selinuxfs (rw,relatime)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=31,pgrp=1,timeout=300,minproto=5,maxproto=5,direct)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,seclabel)
mqueue on /dev/mqueue type mqueue (rw,relatime,seclabel)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw,relatime)
sunrpc on /proc/fs/nfsd type nfsd (rw,relatime)
gvfsd-fuse on /run/user/0/gvfs type fuse.gvfsd-fuse (rw,nosuid,nodev,relatime,user_id=0,group_id=0)
fusectl on /sys/fs/fuse/connections type fusectl (rw,relatime)
/dev/sr0 on /run/media/root/RHEL-7.0 Server.x86_64 type iso9660 (ro,nosuid,nodev,relatime,uid=0,gid=0,iocharset=utf8,mode=0400,dmode=0500,uhelper=udisks2)
/dev/mapper/shazam-storage on /storage type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
[root@server1 /]#

                  6.4             df -h will show human-readable disk free space. Optionally, include the mount points to limit details to that file systems.


[root@server1 /]# df -h /storage
Filesystem                  Size  Used Avail Use% Mounted on
/dev/mapper/shazam-storage  397M   21M  377M   6% /storage


No comments:

Post a Comment