一、简介
LVM是逻辑盘卷管理(Logical Volume Manager)的简称,它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵活性。
LVM的工作原理其实很简单,它就是通过将底层的物理硬盘抽象的封装起来,然后以逻辑卷的方式呈现给上层应用。在传统的磁盘管理机制中,我们的上层应用是直接访问文件系统,从而对底层的物理硬盘进行读取,而在LVM中,其通过对底层的硬盘进行封装,当我们对底层的物理硬盘进行操作时,其不再是针对于分区进行操作,而是通过一个叫做逻辑卷的东西来对其进行底层的磁盘管理操作。比如说我增加一个物理硬盘,这个时候上层的服务是感觉不到的,因为呈现给上层服务的是以逻辑卷的方式。
LVM最大的特点就是可以对磁盘进行动态管理。因为逻辑卷的大小是可以动态调整的,而且不会丢失现有的数据。如果我们新增加了硬盘,其也不会改变现有上层的逻辑卷。作为一个动态磁盘管理机制,逻辑卷技术大大提高了磁盘管理的灵活性。
基本的逻辑卷管理概念:
PV(Physical Volume)- 物理卷
物理卷在逻辑卷管理中处于最底层,它可以是实际物理硬盘上的分区,也可以是整个物理硬盘,也可以是raid设备。
VG(Volumne Group)- 卷组
卷组建立在物理卷之上,一个卷组中至少要包括一个物理卷,在卷组建立之后可动态添加物理卷到卷组中。一个逻辑卷管理系统工程中可以只有一个卷组,也可以拥有多个卷组。
LV(Logical Volume)- 逻辑卷
逻辑卷建立在卷组之上,卷组中的未分配空间可以用于建立新的逻辑卷,逻辑卷建立后可以动态地扩展和缩小空间。系统中的多个逻辑卷可以属于同一个卷组,也可以属于不同的多个卷组。
PE(Physical Extent)- 物理块
LVM 默认使用4MB的PE区块,而LVM的LV最多仅能含有65534个PE(lvm1的格式),因此默认的LVM的LV最大容量为4M*65534/(1024M/G)=256G。PE是整个LVM最小的储存区块,也就是说,其实我们的资料都是由写入PE来处理的。简单的说,这个PE就有点像文件系统里面的block大小。所以调整PE会影响到LVM的最大容量!不过,在CentOS 6.x以后,由于直接使用lvm2的各项格式功能,因此这个限制已经不存在了。
二、安装LVM管理工具
2.1 检查系统中是否安装了LVM管理工具
# rpm -qa|grep lvm
2.2 如果未安装,则使用yum方式安装
# yum install lvm*
# rpm -qa|grep lvm
三、新建一个分区
[root@oracle ~]# fdisk -l
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: 0x0000832f
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 548 4194304 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3 548 15666 121428992 83 Linux
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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
[root@oracle ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x1a091f57.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
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
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-652, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652):
Using default value 652
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@oracle ~]# fdisk -l
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: 0x0000832f
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 548 4194304 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3 548 15666 121428992 83 Linux
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0x1a091f57
Device Boot Start End Blocks Id System
/dev/sdb1 1 652 5237158+ 83 Linux
四、创建PV
[root@oracle ~]# pvcreate /dev/sdb1
dev_is_mpath: failed to get device for 8:17
Physical volume "/dev/sdb1" successfully created
[root@oracle ~]# pvdisplay
"/dev/sdb1" is a new physical volume of "4.99 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 4.99 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID jqnJu3-zJIc-tLDU-RNhZ-Ixcf-5bXd-uAI0fK
还可以使用命令pv和pvscan查看简略信息
五、创建VG
[root@oracle ~]# vgdisplay
No volume groups found
[root@oracle ~]# vgcreate VolGroup00 /dev/sdb1
Volume group "VolGroup00" successfully created
[root@oracle ~]# vgdisplay
--- Volume group ---
VG Name VolGroup00
System ID
Format lvm2
Metadata Areas 1
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 1
Act PV 1
VG Size 4.99 GiB
PE Size 4.00 MiB
Total PE 1278
Alloc PE / Size 0 / 0
Free PE / Size 1278 / 4.99 GiB
VG UUID kyafXU-PWQU-FCam-pxkl-jNZX-qQbH-A2cRMn
还可以使用命令vgs和vgscan查看简略信息
[root@oracle ~]# vgextend VolGroup00 /dev/sdb1
Physical volume '/dev/sdb1' is already in volume group 'VolGroup00'
Unable to add physical volume '/dev/sdb1' to volume group 'VolGroup00'.
[root@oracle ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sdb1
VG Name VolGroup00
PV Size 4.99 GiB / not usable 2.41 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 1278
Free PE 1278
Allocated PE 0
PV UUID jqnJu3-zJIc-tLDU-RNhZ-Ixcf-5bXd-uAI0fK
六、创建LV
[root@oracle ~]# lvcreate -L 2G -n LogVol00 VolGroup00
Logical volume "LogVol00" created
[root@oracle ~]# lvdisplay
--- Logical volume ---
LV Path /dev/VolGroup00/LogVol00
LV Name LogVol00
VG Name VolGroup00
LV UUID LkyfTA-1uzY-ZLNx-106T-ZPEx-Yp6k-6yCm4n
LV Write Access read/write
LV Creation host, time oracle, 2017-02-03 10:50:14 +0800
LV Status available
# open 0
LV Size 2.00 GiB
Current LE 512
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
[root@oracle ~]# vgdisplay
--- Volume group ---
VG Name VolGroup00
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size 4.99 GiB
PE Size 4.00 MiB
Total PE 1278
Alloc PE / Size 512 / 2.00 GiB
Free PE / Size 766 / 2.99 GiB
VG UUID kyafXU-PWQU-FCam-pxkl-jNZX-qQbH-A2cRMn
[root@oracle ~]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
LogVol00 VolGroup00 -wi-a----- 2.00g
七、格式化LV
7.1报错
[root@oracle ~]# resize2fs /dev/VolGroup00/LogVol00
resize2fs 1.41.12 (17-May-2010)
resize2fs: Bad magic number in super-block while trying to open /dev/VolGroup00/LogVol00
Couldn't find valid filesystem superblock.
[root@oracle ~]# mkdir /sun
[root@oracle ~]# mount /dev/VolGroup00/LogVol00 /sun
mount: you must specify the filesystem type
7.2使用mkfs.ext4格式化
[root@oracle ~]# mkfs.ext4 /dev/VolGroup00/LogVol00
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
131072 inodes, 524288 blocks
26214 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 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 (16384 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 23 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@oracle ~]# resize2fs /dev/VolGroup00/LogVol00
resize2fs 1.41.12 (17-May-2010)
The filesystem is already 524288 blocks long. Nothing to do!
[root@oracle ~]# fdisk -l
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: 0x0000832f
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 548 4194304 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3 548 15666 121428992 83 Linux
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0x1a091f57
Device Boot Start End Blocks Id System
/dev/sdb1 1 652 5237158+ 83 Linux
Disk /dev/mapper/VolGroup00-LogVol00: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 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
八、挂载使用
[root@oracle ~]# mount /dev/VolGroup00/LogVol00 /sun
[root@oracle ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 114G 8.5G 100G 8% /
tmpfs 939M 0 939M 0% /dev/shm
/dev/sda1 194M 34M 151M 19% /boot
/dev/mapper/VolGroup00-LogVol00 2.0G 67M 1.9G 4% /sun
[root@oracle sun]# touch /sun/a.txt
[root@oracle sun]# cat /sun/a.txt
1234
[root@oracle ~]# vgs
VG #PV #LV #SN Attr VSize VFree
VolGroup00 1 1 0 wz--n- 4.99g 2.99g
九、继续扩展2G分配给LVM
[root@oracle ~]# lvextend -L +2G -n /dev/VolGroup00/LogVol00
Extending logical volume LogVol00 to 4.00 GiB
Logical volume LogVol00 successfully resized
[root@oracle ~]# vgs
VG #PV #LV #SN Attr VSize VFree
VolGroup00 1 1 0 wz--n- 4.99g 1016.00m
[root@oracle ~]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
LogVol00 VolGroup00 -wi-ao---- 4.00g
[root@oracle ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 114G 8.5G 100G 8% /
tmpfs 939M 0 939M 0% /dev/shm
/dev/sda1 194M 34M 151M 19% /boot
/dev/mapper/VolGroup00-LogVol00 2.0G 67M 1.9G 4% /sun
[root@oracle sun]# resize2fs /dev/VolGroup00/LogVol00
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/VolGroup00/LogVol00 is mounted on /sun; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/VolGroup00/LogVol00 to 1048576 (4k) blocks.
The filesystem on /dev/VolGroup00/LogVol00 is now 1048576 blocks long.
[root@oracle sun]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 114G 8.5G 100G 8% /
tmpfs 939M 0 939M 0% /dev/shm
/dev/sda1 194M 34M 151M 19% /boot
/dev/mapper/VolGroup00-LogVol00 4.0G 68M 3.7G 2% /sun
[root@oracle sun]# cat a.txt
1234
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29812844/viewspace-2133023/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/29812844/viewspace-2133023/