Linux LVM Example 01

Linux LVM Command Example

Logical Volume Management Example 01


In the following example we are going to use Virtual Box to demonstrate Logical Volume Management. Under Virtual Box I have created a CentOS 6.3 operating system and added 2 x 1GB Virtual Disks. In this example we learn how to create new partitions for use with LVM. You will learn the basics of the "pvcreate", "pvdisplay", "pvs", "vgcreate", "vgdisplay", "vgs", "vgscan", "vgrename", "vgremove", "lvcreate", "lvdisplay", "lvs", "mkfs", "mount" and "umount" commands.



Display Current Disk Summary


To display a summary of the current attached disks to our system we are going to issue the "fdisk -l" command:



[root@centos ~]# fdisk -l

Disk /dev/sdc: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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: 0x00000000


Disk /dev/sda: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 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: 0x0005b98c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64         523     3681280   8e  Linux LVM
Partition 2 does not end on cylinder boundary.

Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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: 0x00000000


Disk /dev/mapper/vg_centos-lv_root: 1652 MB, 1652555776 bytes
255 heads, 63 sectors/track, 200 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: 0x00000000


Disk /dev/mapper/vg_centos-lv_swap: 2113 MB, 2113929216 bytes
255 heads, 63 sectors/track, 257 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: 0x00000000

From the above output we can see that we have 2 x disks:


Disk /dev/sdb: 1073 MB, 1073741824 bytes
Disk /dev/sdc: 1073 MB, 1073741824 bytes

The next stage is to create our partitions using the above /dev/sdb and /dev/sdc. We will again use the fdisk utility to create our partitions and and mark them for use with LVM.



fdisk /dev/sdb

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
   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)

After selecting "m" to show the help menu, we select the following options from the interactive menu:



Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-130, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-130, default 130):     
Using default value 130

In the above, "n" is selected for adding a new partition, "p" is selected for a primary partition. We accept the default size parameters by pressing return. Next we need to define the partition id type, as we are using "LVM", we select type "8e":



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


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

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

After selecting our partition id type of "8e", we selected "w" to write table to disk. If you don't select this option, then no changes will actually be applied!

Next we repeat the same process for "/dev/sdc"



fdisk /dev/sdc

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-130, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-130, default 130): 
Using default value 130

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

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

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

Now lets issue the "fdisk -l" command again to check our changes: (Amongst the output we should now see our new entries!



   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         130     1044193+  8e  Linux LVM

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1         130     1044193+  8e  Linux LVM

From the above, we can see that we now have our two devices "/dev/sdb1" and "/dev/sdc1". The number indicates that this is partition number "1". The "Id" has been set to "8e" which denotes "Linux LVM".


Prepare partitions for LVM


Our next step is to prepare our partitions for use with LVM. To do this we are going to use the "pvcreate" command to define our "physical volumes" (PV):



[root@centos ~]# pvcreate /dev/sdb1
  Writing physical volume data to disk "/dev/sdb1"
  Physical volume "/dev/sdb1" successfully created

[root@centos ~]# pvcreate /dev/sdc1
  Writing physical volume data to disk "/dev/sdc1"
  Physical volume "/dev/sdc1" successfully created

For training purposes we will now remove these partitions using the "pvremove" command:



[root@centos ~]# pvremove /dev/sdb1
  Labels on physical volume "/dev/sdb1" successfully wiped
[root@centos ~]# pvremove /dev/sdc1
  Labels on physical volume "/dev/sdc1" successfully wiped

Now lets create our partitions again:



[root@centos ~]# pvcreate /dev/sdb1
  Writing physical volume data to disk "/dev/sdb1"
  Physical volume "/dev/sdb1" successfully created
[root@centos ~]# pvcreate /dev/sdc1
  Writing physical volume data to disk "/dev/sdc1"
  Physical volume "/dev/sdc1" successfully created

Now we will issue the "pvdisplay" command to display our physical volumes:



[root@centos ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               vg_centos
  PV Size               3.51 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              898
  Free PE               0
  Allocated PE          898
  PV UUID               wao7Km-9soe-2bmb-c5ge-R30C-JQlZ-JKazwd
   
  "/dev/sdb1" is a new physical volume of "1019.72 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               1019.72 MiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               25MwHz-Z1Rp-oYIp-kpIl-kM5e-XiqM-1rhF73
   
  "/dev/sdc1" is a new physical volume of "1019.72 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdc1
  VG Name               
  PV Size               1019.72 MiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               FHwkyp-SMxc-ppBr-K0ke-lZM8-3ZHm-npwB20

Here we can see our New Physical Volumes "/dev/sdb1" and "/dev/sdc1". Note, these have no associated "VG" volume group.

We can also issue the "pvs" command to display our new PVs:



[root@centos ~]# pvs
  PV         VG        Fmt  Attr PSize    PFree   
  /dev/sda2  vg_centos lvm2 a--     3.51g       0 
  /dev/sdb1            lvm2 a--  1019.72m 1019.72m
  /dev/sdc1            lvm2 a--  1019.72m 1019.72m


Creating Volume Groups (VG)


The next stage in our process is to create a Volume Group (VG). To accomplish this we use the "vgcreate" command:



[root@centos ~]# vgcreate vg01 /dev/sdb1 /dev/sdc1
  Volume group "vg01" successfully created

In the above, we issued "vgcreate vg01 /dev/sdb1 /dev/sdc1". This will create the association of our devices to our volume group "vg01". The basic syntax of the command is vgcreate VolumeGroupName /dev/sdxx

To view our new Volume Group, we use the command "vgdisplay"



[root@centos ~]# vgdisplay
  --- Volume group ---
  VG Name               vg01
  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.98 GiB
  PE Size               4.00 MiB
  Total PE              508
  Alloc PE / Size       0 / 0   
  Free  PE / Size       508 / 1.98 GiB
  VG UUID               S6g0t0-OJ8w-vRxu-hk0M-u288-NOQi-lUYNL1

We can issue the "vgs" command:



[root@centos ~]# vgs
  VG        #PV #LV #SN Attr   VSize VFree
  vg01        2   0   0 wz--n- 1.98g 1.98g
  vg_centos   1   2   0 wz--n- 3.51g    0 

Another useful command to issue is the "vgscan" command:



[root@centos ~]# vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "vg01" using metadata type lvm2
  Found volume group "vg_centos" using metadata type lvm2

For training purposes we will now rename the volume group to "testvg" using the "vgrename" command:



[root@centos ~]# vgrename vg01 testvg
  Volume group "vg01" successfully renamed to "testvg"

[root@centos ~]# vgs
  VG        #PV #LV #SN Attr   VSize VFree
  testvg      2   0   0 wz--n- 1.98g 1.98g
  vg_centos   1   2   0 wz--n- 3.51g    0 

[root@centos ~]# vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "testvg" using metadata type lvm2
  Found volume group "vg_centos" using metadata type lvm2

For training purposes we will now remove the volume group "testvg" with the "vgremove" command:



[root@centos ~]# vgremove testvg
  Volume group "testvg" successfully removed

If we issue our "vgs" command again, we will see that we have no volume group "testvg".

Now lets create our Volume Group again:



[root@centos ~]# vgcreate vg01 /dev/sdb1 /dev/sdc1
  Volume group "vg01" successfully created
[root@centos ~]# vgs
  VG        #PV #LV #SN Attr   VSize VFree
  vg01        2   0   0 wz--n- 1.98g 1.98g
  vg_centos   1   2   0 wz--n- 3.51g    0

Creating a Logical Volume


Our next step now is to create a Logical Volume using the "lvcreate" command:



[root@centos ~]# lvcreate --name lvex01 --size 1.5G vg01
  Logical volume "lvex01" created

Here we created a Logical Volume with the name "lvex01" and a size of 1.5GB on Volume Group "vg01"

We can now issue the command "lvdisplay" to display our new logical volume:



[root@centos ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg01/lvex01
  LV Name                lvex01
  VG Name                vg01
  LV UUID                CB2tzn-kF4l-KuGS-102L-Souk-wCJH-rbxzKn
  LV Write Access        read/write
  LV Creation host, time centos, 2013-04-09 21:20:48 +0100
  LV Status              available
  # open                 0
  LV Size                1.50 GiB
  Current LE             384
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2

We can also issue the "lvscan" command:



[root@centos ~]# lvscan
  ACTIVE            '/dev/vg01/lvex01' [1.50 GiB] inherit
  ACTIVE            '/dev/vg_centos/lv_root' [1.54 GiB] inherit
  ACTIVE            '/dev/vg_centos/lv_swap' [1.97 GiB] inherit

Or even the "lvs" command:



[root@centos ~]# lvs
  LV      VG        Attr     LSize Pool Origin Data%  Move Log Copy%  Convert
  lvex01  vg01      -wi-a--- 1.50g                                           
  lv_root vg_centos -wi-ao-- 1.54g                                           
  lv_swap vg_centos -wi-ao-- 1.97g

Adding a Filesystem - mkfs


Now to use our Logical volume, we need to add a filesystem. To do this we will use one of the "mkfs" commands.

On my test CentOS system I have the following filesystem types available:



mkfs          mkfs.ext2     mkfs.ext4     mkfs.xfs      
mkfs.cramfs   mkfs.ext3     mkfs.ext4dev

In our example, we are going to use an "ext3" filesystem type, so our command will take the form of:



[root@centos ~]# mkfs.ext3 /dev/vg01/lvex01
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
98304 inodes, 393216 blocks
19660 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=402653184
12 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912

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

This filesystem will be automatically checked every 29 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

Creating a Mount Point


Before we can use any of this storage we need to create a mount point on our system:



[root@centos ~]# mkdir /var/example01

Next we use the "mount" command to mount our Logical volume:



[root@centos ~]# mount /dev/vg01/lvex01 /var/example01

To verify we can see our new area we can issue the "df -h" command:



[root@centos ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_centos-lv_root
                      1.6G  524M  950M  36% /
tmpfs                 504M     0  504M   0% /dev/shm
/dev/sda1             485M   38M  422M   9% /boot
/dev/mapper/vg01-lvex01
                      1.5G   35M  1.4G   3% /var/example01

Now we need to add our entries into "/etc/fstab" the mount table. This will make sure our filesystem is mounted at reboot:

To accomplish this, we need to add the following line:



/dev/vg01/lvex01                    /var/example01                   ext3    defaults        0 0

we can then issue the "mount -a" command to mount all entries within our mount table. This is a quick way of verifying that there are no errors.

For a final test, we can now reboot our system and check that our filesystems have been correctly mounted:



Broadcast message from root@centos
	(/dev/pts/0) at 21:41 ...

The system is going down for reboot NOW!

Once the system is backup and we have logged in, we can check again with the "df -h" command:



[root@centos ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_centos-lv_root
                      1.6G  524M  950M  36% /
tmpfs                 504M     0  504M   0% /dev/shm
/dev/sda1             485M   38M  422M   9% /boot
/dev/mapper/vg01-lvex01
                      1.5G   35M  1.4G   3% /var/example01