LVM: Create a new Logical Volume / Filesystem
Article Number: 162 | Rating: Unrated | Last Updated: Sat, Jun 2, 2018 10:13 PM
LVM: Create a new Logical Volume / Filesystem
# Tested on RHEL 5, 6 & 7
# Concatenated volume # ------------------------------------------------------------------------------------------ MNTPT=/testmntpt OWNER=testusr.testgrp VG=testvg LV=testlv SIZE=512 # Final size in MBytes FSTYPE=ext3 # ext4 can be created from RHEL 5 on # LV creation. Size given in Mbytes. To specify size in Logical Extents use '-l' lvcreate -L ${SIZE}M -n $LV $VG Logical volume "testlv" created. # Show new LV (note volume group is formed by three physical volumes but only one of them # has been used for the logical volume) pvs | egrep "PFree|$VG" PV VG Fmt Attr PSize PFree /dev/sde1 testvg lvm2 a-- 1020.00m 508.00m /dev/sde2 testvg lvm2 a-- 1020.00m 1020.00m /dev/sde3 testvg lvm2 a-- 1020.00m 1020.00m lvdisplay -m /dev/$VG/$LV --- Logical volume --- LV Path /dev/testvg/testlv LV Name testlv VG Name testvg LV UUID nBmMJZ-VlHJ-8JYS-WEQ7-bHxi-kock-31aGZS LV Write Access read/write LV Creation host, time myserver.localdomain, 2016-02-23 16:25:41 +0100 LV Status available # open 0 LV Size 512.00 MiB Current LE 128 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:8 --- Segments --- Logical extents 0 to 127: Type linear Physical volume /dev/sde1 Physical extents 0 to 127 # Filesystem creation mkfs -V -t $FSTYPE /dev/$VG/$LV mkdir -p $MNTPT mount /dev/$VG/$LV $MNTPT chown $OWNER $MNTPT # Check df -h $MNTPT ls -ld $MNTPT # Add output of following command to /etc/fstab echo "/dev/$VG/$LV $MNTPT $FSTYPE defaults 1 2" # Stripped over several physical volumes # ------------------------------------------------------------------------------------------ MNTPT=/testmntpt OWNER=testusr.testgrp VG=testvg LV=testlv SIZE=512 # Final size in MBytes FSTYPE=ext3 # ext4 can be created from RHEL 5 on STRIPES=2 # Number of stripes/PV DISKS="/dev/sde1 /dev/sde3" # LV creation. Size given in Mbytes. To specify size in Logical Extents use '-l' lvcreate -L ${SIZE}M -n $LV -i $STRIPES $VG $DISKS Logical volume "testlv" created. # Show new LV (note volume group is formed by three physical volumes but only two of them # have been used for the logical volume) pvs | egrep "PFree|$VG" PV VG Fmt Attr PSize PFree /dev/sde1 testvg lvm2 a-- 1020.00m 764.00m /dev/sde2 testvg lvm2 a-- 1020.00m 1020.00m /dev/sde3 testvg lvm2 a-- 1020.00m 764.00m lvdisplay -m /dev/$VG/$LV --- Logical volume --- LV Path /dev/testvg/testlv LV Name testlv VG Name testvg LV UUID Imu7dH-vBai-Nrxu-q2LZ-HKaH-ELja-xAS33r LV Write Access read/write LV Creation host, time myserver.localdomain, 2016-02-23 16:34:19 +0100 LV Status available # open 0 LV Size 512.00 MiB Current LE 128 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 512 Block device 253:8 --- Segments --- Logical extents 0 to 127: Type striped Stripes 2 Stripe size 64.00 KiB Stripe 0: Physical volume /dev/sde1 <--- Physical extents 0 to 63 Stripe 1: Physical volume /dev/sde3 <--- Physical extents 0 to 63 # Filesystem creation mkfs -V -t $FSTYPE /dev/$VG/$LV mkdir -p $MNTPT mount /dev/$VG/$LV $MNTPT chown $OWNER $MNTPT # Check df -h $MNTPT ls -ld $MNTPT # Add output of following command to /etc/fstab echo "/dev/$VG/$LV $MNTPT $FSTYPE defaults 1 2" # Mirrored # ------------------------------------------------------------------------------------------ # Every day it is less common to build mirrored logical volumes because of the use of HW # raids on local disks for OS, and SAN LUNs for data disks so we usually build concat # logical volumes on already mirrored physical volumes. Anyway here is how to build a # mirrored logical volume MNTPT=/testmntpt OWNER=testusr.testgrp VG=testvg LV=testlv SIZE=512 # Final size in MBytes FSTYPE=ext3 # ext4 can be created from RHEL 5 on MIRRORS=1 # Number of mirror copies, in this case a 2-way mirror. DISKS="/dev/sde1 /dev/sde2" # This is a simplified procedure for creating a mirrored logical volume as many options are # available for this type of volumes. # For instance, we could consider using '--mirrorlog' option that provides the ability of # specifying the type of log to be used. If this options is not used, it takes "disk" as # default value that is persistent and it will require a small amount of storage space, # usually on a separate device from the data being mirrored (as we took only two disks for # the mirror, the log is created in the mirrored volume itself; if we wanted the log to be # on a separate disk we should have set "DISKS" variable like this: # DISKS="/dev/sde1 /dev/sde2"; consider using a small disk for log). # # The segment type for the new implementation of mirroring is "raid1". For the earlier # implementation, the segment type is "mirror". On the new implementation, the default is # "raid1". To use the legacy "mirror" segment type use "--type mirror" option. # LV creation. Size given in Mbytes. To specify size in Logical Extents use '-l' lvcreate -L ${SIZE}M -n $LV -m $MIRRORS $VG $DISKS Logical volume "testlv" created. # Show LV # Old implementation lvdisplay -m /dev/$VG/$LV --- Logical volume --- LV Path /dev/testvg/testlv LV Name testlv VG Name testvg LV UUID FgqFKQ-kXEW-a9VF-PCX8-eqgL-lvi2-BBx3lb LV Write Access read/write LV Creation host, time myserver, 2016-02-23 17:20:12 +0100 LV Status available # open 0 LV Size 512.00 MiB Current LE 128 Mirrored volumes 2 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:11 --- Segments --- Logical extent 0 to 127: Type mirror Mirrors 2 Mirror size 128 Mirror log volume testlv_mlog Mirror region size 512.00 KiB Mirror original: Logical volume testlv_mimage_0 Logical extents 0 to 127 Mirror destinations: Logical volume testlv_mimage_1 Logical extents 0 to 127 pvs | egrep "PFree|$VG" PV VG Fmt Attr PSize PFree /dev/sde1 testvg lvm2 a-- 1.01g 520.00m /dev/sde2 testvg lvm2 a-- 1.01g 516.00m /dev/sde3 testvg lvm2 a-- 1.01g 1.01g lvs LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert testlv testvg mwi-a-m-- 512.00m testlv_mlog 100.00 # New implementation lvdisplay -m /dev/$VG/$LV --- Logical volume --- LV Path /dev/testvg/testlv LV Name testlv VG Name testvg LV UUID kLpej7-9Kpv-re5p-9rU2-vWbx-m4BE-GpTKOh LV Write Access read/write LV Creation host, time myserver.localdomain, 2016-02-23 17:20:10 +0100 LV Status available # open 0 LV Size 512.00 MiB Current LE 128 Mirrored volumes 2 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:12 --- Segments --- Logical extents 0 to 127: Type raid1 Monitoring monitored Raid Data LV 0 Logical volume testlv_rimage_0 Logical extents 0 to 127 Raid Data LV 1 Logical volume testlv_rimage_1 Logical extents 0 to 127 Raid Metadata LV 0 testlv_rmeta_0 Raid Metadata LV 1 testlv_rmeta_1 pvs | egrep "PFree|$VG" PV VG Fmt Attr PSize PFree /dev/sde1 testvg lvm2 a-- 1020.00m 504.00m /dev/sde2 testvg lvm2 a-- 1020.00m 504.00m /dev/sde3 testvg lvm2 a-- 1020.00m 1020.00m lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert [...] testlv testvg rwi-a-r--- 512.00m 100.00 # Filesystem creation mkfs -V -t $FSTYPE /dev/$VG/$LV mkdir -p $MNTPT mount /dev/$VG/$LV $MNTPT chown $OWNER $MNTPT # Check df -h $MNTPT ls -ld $MNTPT # Add output of following command to /etc/fstab echo "/dev/$VG/$LV $MNTPT $FSTYPE defaults 1 2" |