Logical volume management is a widely-used technique for deploying logical storage rather than physical storage. With LVM, "logical" partitions can span across physical hard drives and can be resized (unlike traditional ext4/ext3 "raw" partitions).
Say, I have a 160GB hard disk and want to create LVM in this disk. Here is the way to go
LVM (Logical Volume Management) makes use of the device-mapper feature of the Linux kernel to provide a system of partitions that is independent of the underlying disk's layout.
So first load the device mapper module.
# modprobe dm-mod
# lsmod | grep dm_mod
Creating logical volume partitions involves 3 steps
- Creating Physical Volumes
- Forming Volume Group using the Physical Volumes
- Creating Logical Volume partitions on the Volume Group
Step 1 : Create a physical volume
Firstly, we need to create disk partitions of type LVM. Say, now I want to create 3 disk partitions of type LVM. I shall be using these 3 partitions like separate disks, for creating logical partitions on them. Since I have 125GB out of 160GB, free in my hard disk(not allocated to any partition earlier and no file system written on it), I use that space to create 3 partitions of size around 41GB each. fdisk comes to the aid of it and here is the way. Basically, fdisk creates raw partitions
[root@dhcppc1 ~]# fdisk /dev/sda
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): p
Disk /dev/sda: 160.0 GB, 160041885696 bytes
255 heads, 63 sectors/track, 19457 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: 0x000e6ea7
Device Boot Start End Blocks Id System
/dev/sda1 * 1 39 307200 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 39 2589 20480000 83 Linux
/dev/sda3 2589 3226 5120000 82 Linux swap / Solaris
/dev/sda4 3226 19457 130380128+ 5 Extended
Creating new partitions now
Command (m for help): n
First cylinder (3226-19457, default 3226):
Using default value 3226
Last cylinder, +cylinders or +size{K,M,G} (3226-19457, default 19457): +41G
Command (m for help): n
First cylinder (8579-19457, default 8579):
Using default value 8579
Last cylinder, +cylinders or +size{K,M,G} (8579-19457, default 19457): +41G
Command (m for help): n
First cylinder (13932-19457, default 13932):
Using default value 13932
Last cylinder, +cylinders or +size{K,M,G} (13932-19457, default 19457):
Using default value 19457
Command (m for help): p
Disk /dev/sda: 160.0 GB, 160041885696 bytes
255 heads, 63 sectors/track, 19457 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: 0x000e6ea7
Device Boot Start End Blocks Id System
/dev/sda1 * 1 39 307200 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 39 2589 20480000 83 Linux
/dev/sda3 2589 3226 5120000 82 Linux swap / Solaris
/dev/sda4 3226 19457 130380128+ 5 Extended
/dev/sda5 3226 8578 42994529+ 83 Linux
/dev/sda6 8579 13931 42997941 83 Linux
/dev/sda7 13932 19457 44387563+ 83 Linux
.
The partitions 5(/dev/sda5), 6(/dev/sda6), 7(/dev/sda7) are the newly created raw partitions
Need to convert the newly created partitions to lvm type
Command (m for help): t
Partition number (1-7): 5
Hex code (type L to list codes): 8e
Changed system type of partition 5 to 8e (Linux LVM)
Command (m for help): t
Partition number (1-7): 6
Hex code (type L to list codes): 8e
Changed system type of partition 6 to 8e (Linux LVM)
Command (m for help): t
Partition number (1-7): 7
Hex code (type L to list codes): 8e
Changed system type of partition 7 to 8e (Linux LVM)
Command (m for help): p
Disk /dev/sda: 160.0 GB, 160041885696 bytes
255 heads, 63 sectors/track, 19457 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: 0x000e6ea7
Device Boot Start End Blocks Id System
/dev/sda1 * 1 39 307200 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 39 2589 20480000 83 Linux
/dev/sda3 2589 3226 5120000 82 Linux swap / Solaris
/dev/sda4 3226 19457 130380128+ 5 Extended
/dev/sda5 3226 8578 42994529+ 8e Linux LVM
/dev/sda6 8579 13931 42997941 8e Linux LVM
/dev/sda7 13932 19457 44387563+ 8e Linux LVM
Finally press w write table to disk and exit and reboot
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
Now, reboot the system.
Creating physical volume using the newly created raw partitions
# pvcreate /dev/sda5
# pvcreate /dev/sda6
The pvdisplay command displays all physical volumes on your system.
# pvdisplay
[root@dhcppc1 ~]# pvdisplay
"/dev/sda5" is a new physical volume of "41.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sda5
VG Name
PV Size 41.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID V0ObJ7-y6E3-kJrv-Zee2-SSOe-46lB-303xuf
"/dev/sda6" is a new physical volume of "41.01 GiB"
--- NEW Physical volume ---
PV Name /dev/sda6
VG Name
PV Size 41.01 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 5cDz8n-C43V-juxj-0DTD-2yMD-u5R0-fI4hVB
Step 2 : Create a Volume Group - VolGroup
Using the two physical volumes created in previous step using pvcreate command, a volume group can be formed. Let us create a volume group named as, VolGroup (choose any name of your choice), for example.
[root@dhcppc1 ~]# vgcreate VolGroup /dev/sda5 /dev/sda6
Volume group "VolGroup" successfully created
Use vgdisplay command to display the details about the Volume Group created
[root@dhcppc1 ~]# vgdisplay
--- Volume group ---
VG Name VolGroup
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 82.00 GiB
PE Size 4.00 MiB
Total PE 20993
Alloc PE / Size 0 / 0
Free PE / Size 20993 / 82.00 GiB
VG UUID ZcdhvM-5Q5O-dffd-CA7e-JuQC-mIqM-SffOpx
Additional PVs can be added to this volume group using the vgextend command
# pvcreate /dev/sda7
Writing physical volume data to disk "/dev/sda7"
Physical volume "/dev/sda7" successfully created
# vgextend VolGroup /dev/sda7
Volume group "VolGroup" successfully extended
# vgdisplay
--- Volume group ---
VG Name VolGroup
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 2
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 124.33 GiB
PE Size 4.00 MiB
Total PE 31829
Alloc PE / Size 0 / 0
Free PE / Size 31829 / 124.33 GiB
VG UUID ZcdhvM-5Q5O-dffd-CA7e-JuQC-mIqM-SffOpx
The newly added PV(/dev/sda7) can be removed from volume_group_one by the vgreduce command
# vgreduce VolGroup /dev/sda7
Removed "/dev/sda7" from volume group "VolGroup"
# vgdisplay VolGroup
--- Volume group ---
VG Name VolGroup
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 3
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 82.00 GiB
PE Size 4.00 MiB
Total PE 20993
Alloc PE / Size 0 / 0
Free PE / Size 20993 / 82.00 GiB
VG UUID ZcdhvM-5Q5O-dffd-CA7e-JuQC-mIqM-SffOpx
Adding the Physical Volume(/dev/sda7) Back
[root@dhcppc1 ~]# vgextend VolGroup /dev/sda7
Volume group "VolGroup" successfully extended
[root@dhcppc1 ~]# vgdisplay VolGroup
--- Volume group ---
VG Name VolGroup
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 4
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 124.33 GiB
PE Size 4.00 MiB
Total PE 31829
Alloc PE / Size 0 / 0
Free PE / Size 31829 / 124.33 GiB
VG UUID ZcdhvM-5Q5O-dffd-CA7e-JuQC-mIqM-SffOpx
Step 3 : Creating Logical Volume(LV) partitions
On the Volume Group named VolGroup, now I want to create three logical partitions for home, music and opt. I create a
- 20GB linear LV named logical_volume_home
- 20GB linear LV named logical_volume_music and
- 5GB linear LV named logical_volume_opt
from volume group VolGroup using the lvcreate command as follows:
[root@dhcppc1 ~]# lvcreate -L20GB -n logical_volume_home VolGroup
Logical volume "logical_volume_home" created
[root@dhcppc1 ~]# lvcreate -L5GB -n logical_volume_opt VolGroup
Logical volume "logical_volume_opt" created
[root@dhcppc1 ~]# lvcreate -L20GB -n logical_volume_music VolGroup
Logical volume "logical_volume_music" created
Display status of Logical Volumes
[root@dhcppc1 ~]# lvdisplay
--- Logical volume ---
LV Name /dev/VolGroup/logical_volume_home
VG Name VolGroup
LV UUID mK0iA2-bYwU-iRFX-bFAN-Ghcg-Q0DO-f1HGVe
LV Write Access read/write
LV Status available
# open 0
LV Size 20.00 GiB
Current LE 5120
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
--- Logical volume ---
LV Name /dev/VolGroup/logical_volume_opt
VG Name VolGroup
LV UUID x9aPWe-yGEk-WybC-hJAE-ikKI-ftuc-e3pQRJ
LV Write Access read/write
LV Status available
# open 0
LV Size 5.00 GiB
Current LE 1280
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1
--- Logical volume ---
LV Name /dev/VolGroup/logical_volume_music
VG Name VolGroup
LV UUID 1meVhn-aa8m-RIBH-pWWk-QlXn-Thgl-atXFkN
LV Write Access read/write
LV Status available
# open 0
LV Size 20.00 GiB
Current LE 5120
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:2
Output a report of Logical Volumes
[root@dhcppc1 ~]# lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
logical_volume_home VolGroup -wi-a- 20.00g
logical_volume_music VolGroup -wi-a- 20.00g
logical_volume_opt VolGroup -wi-a- 5.00g
Create File system on logical volumes
mkfs.ext4 -m 0 /dev/VolGroup/logical_volume_vz
mkfs.ext4 -m 0 /dev/VolGroup/logical_volume_opt
mkfs.ext4 -m 0 /dev/VolGroup/logical_volume_music
the -m option specifies the percentage reserved for the super-user, set this to 0 if you wish not to waste any space, the default is 5%.
Edit /etc/fstab for mounting
Add an entry for your newly created logical volume into /etc/fstab
/dev/VolGroup/logical_volume_vz /vz ext4 defaults 0 2
/dev/VolGroup/logical_volume_music /music ext4 defaults 0 2
/dev/VolGroup/logical_volume_opt /opt ext4 defaults 0 2
Mount the partitions without rebooting
mkdir /vz /music /opt (Creating mount points)
mount -a
Displaying the newly mounted partitions
mount
df -h
Extending a logical volume partition
Say now we want to extend the /vz (/dev/VolGroup/logical_volume_vz) partition size by 5GB.
A LV can be extended by using the lvextend command. You can specify either an absolute size for the extended LV or how much additional storage you want to add to the LVM. For example:
# lvextend -L12G /dev/VolGroup/logical_volume_vz
will extend LV /dev/VolGroup/logical_volume_vz to 12 GB, while
# lvextend -L+5G /dev/VolGroup/logical_volume_vz (This is what Iam going to do now)
Extending logical volume logical_volume_vz to 25.00 GiB
Logical volume logical_volume_vz successfully resized
will extend LV /dev/VolGroup/logical_volume_vz by an additional 5 GB. Once a logical volume has been extended, the underlying file system has to be expanded to exploit the additional storage now available on the LV. To resize ext4fs, the following commands need to be run
umount /vz
e2fsck -f /dev/VolGroup/logical_volume_vz
resize2fs /dev/VolGroup/logical_volume_vz
This will extend the ext4 file system to completely fill the LV, /dev/VolGroup/logical_volume_vz, on which it resides.
Remounting the newly created partition
# mount -a
Removing a logical volume from Volume Group
lvdisplay
umount /dev/VolGroup/logical_volume_opt
lvremove /dev/VolGroup/logical_volume_opt
No comments:
Post a Comment