1.LVM的基本簡介
LVM:logical Volume Manager,邏輯卷管理,其目的在于可以彈性調整檔案系統容量。LVM建立在硬碟和分區之上的一個邏輯層,來提高磁盤分區管理的靈活性,它可以整合多個實體分區在一起,讓這些分區看起來像一個磁盤,而且,還可以将其他實體分區添加LVM中或從這個LVM管理的磁盤中删除。
2.LVM基本術語
PV:Physical Volume實體卷,就是指硬碟分區或從邏輯上與磁盤分區具有同樣功能的裝置(如RAID),是LVM的基本存儲邏輯塊,但和基本的實體存儲媒體(如分區、磁盤等)比較,卻包含有與LVM相關的管理參數。注意:需要将分區辨別符改為8e(LVM的辨別符),然後用pvcreate
指令建立pv。
VG:Volume Group卷組類似于非LVM系統中的實體硬碟,其由實體卷組成。可以在卷組上建立一個或多個“LVM分區”(邏輯卷),LVM卷組由一個或多個實體卷組成。
PE:每一個實體卷被劃分為稱為PE(Physical Extents)的基本單元,具有唯一編号的PE是可以被LVM尋址的最小單元。PE的大小是可配置的,預設為4MB。
LV:從VG中劃分的邏輯分區。

3.LVM的建立與管理
建立分區:使用fdisk指令建立分區,調整分區辨別符為8e。
[root@magedu ~]# 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): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (7859-15665, default 7859):
Using default value 7859
Last cylinder, +cylinders or +size{K,M,G} (7859-15665, default 15665): +5G
First cylinder (8513-15665, default 8513):
Using default value 8513
Last cylinder, +cylinders or +size{K,M,G} (8513-15665, default 15665): +5G
Command (m for help): P
Disk /dev/sda: 128.8 GB, 128849018880 bytes
255 heads, 63 sectors/track, 15665 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: 0x00051a06
Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 26 7859 62914560 8e Linux LVM
/dev/sda3 7859 15665 62708728+ 5 Extended
/dev/sda5 7859 8512 5252224+ 83 Linux
/dev/sda6 8513 9166 5253223+ 83 Linux
Command (m for help): t
Partition number (1-6): 5
Hex code (type L to list codes): 8e
Changed system type of partition 5 to 8e (Linux LVM)
Partition number (1-6): 6
Changed system type of partition 6 to 8e (Linux LVM)
Command (m for help): p
/dev/sda5 7859 8512 5252224+ 8e Linux LVM
/dev/sda6 8513 9166 5253223+ 8e Linux LVM
建立PV:pvcreate /dev/devices,使用pvdisplay檢視pv詳細資訊
[root@magedu ~]# pvcreate /dev/sda5 /dev/sda6
Physical volume "/dev/sda5" successfully created
Physical volume "/dev/sda6" successfully created
[root@magedu ~]# pvdisplay /dev/sda5 /dev/sda6
"/dev/sda5" is a new physical volume of "5.01 GiB"
--- NEW Physical volume ---
PV Name /dev/sda5
VG Name
PV Size 5.01 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID L8WHe3-EdsZ-aC6K-RJUW-8kkL-D8wj-RiwkUv
"/dev/sda6" is a new physical volume of "5.01 GiB"
PV Name /dev/sda6
PV UUID EcvE1Y-5wXl-t5Kc-M48F-Nrqw-R2hL-xi0hch
建立VG:vgcreate VolumeGroupName PhysicalDevicePath
[root@magedu ~]# vgcreate myvg /dev/sda5 /dev/sda6
Volume group "myvg" successfully created
[root@magedu ~]# vgdisplay myvg
--- Volume group ---
VG Name myvg
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 10.02 GiB
PE Size 4.00 MiB
Total PE 2564
Alloc PE / Size 0 / 0
Free PE / Size 2564 / 10.02 GiB
VG UUID mDpNg3-cUa0-AQ2F-doM2-9wmj-fc6t-Q2XqiH
建立LV:lvcreate -L #[mMgGtT] -n NAME VolumeGroup
[root@magedu ~]# lvcreate -L 10 -n mylv myvg
Rounding up size to full physical extent 12.00 MiB
Logical volume "mylv" created
[root@magedu ~]# lvdisplay /dev/myvg/mylv
--- Logical volume ---
LV Path /dev/myvg/mylv
LV Name mylv
VG Name myvg
LV UUID yfJqxR-ao9l-uR9p-OcEb-Xdi4-pJ2d-7bYO5A
LV Write Access read/write
LV Creation host, time magedu, 2015-08-18 13:21:49 +0800
LV Status available
# open 0
LV Size 12.00 MiB
Current LE 3
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:4
LV格式化及挂載:lv需要先格式化才能存儲
[root@magedu ~]# mke2fs -t ext4 /dev/myvg/mylv
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
3072 inodes, 12288 blocks
614 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=12582912
2 block groups
8192 blocks per group, 8192 fragments per group
1536 inodes per group
Superblock backups stored on blocks:
8193
Writing inode tables: done
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 32 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@magedu ~]# mkdir /tmp/lvtest
[root@magedu ~]# mount /dev/myvg/mylv /tmp/lvtest/
[root@magedu ~]# mount
/dev/mapper/vg0-root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
/dev/mapper/vg0-usr on /usr type ext4 (rw)
/dev/mapper/vg0-var on /var type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/mapper/myvg-mylv on /tmp/lvtest type ext4 (rw)
擴充邏輯卷:
lvextend -L [+]#[mMgGtT] /dev/VG_NAME/LV_NAME
resize2fs /dev/VG_NAME/LV_NAME
縮減邏輯卷:
umount /dev/VG_NAME/LV_NAME
e2fsck -f /dev/VG_NAME/LV_NAME
resize2fs /dev/VG_NAME/LV_NAME #[mMgGtT]
lvreduce -L [-]#[mMgGtT] /dev/VG_NAME/LV_NAME