目录
概念:Linux 下对硬盘分区的一种管理机制,适合于管理大存储设备,并允许用户动态调整文件系统的大小。
优点:相比较分区而言逻辑卷可以动态调整文件系统的大小。
注意事项:①/boot分区用于存放引导文件所以不能建立逻辑卷
? ? ? ? ? ? ? ? ? ②创建逻辑卷=完成磁盘使用的分区步骤,依然需要将逻辑卷格式化挂载才能使用
①pvcreate:创建物理卷 ??
基本格式:pvcreate 设备名1 设备名2... (如果是分区格式必须是8e,磁盘无要求)
②vgcreate:创建卷组
基本格式:vgcreate 卷组名 物理卷名1 物理卷2...
③lvcreate:创建逻辑卷
基本格式:lvcreate -L 容量大小10G等.. -n 逻辑卷名 卷组名,从卷组取PE
④vgextend:扩展卷组
基本格式:vgextend /要扩容的卷组 /dev/新建的物理卷
⑤lvextend:扩展逻辑卷
基本格式:lvextend ? ? -L[扩展大小选项] ? ? +20G(扩展具体大小)或者写扩展后的总容量,不写代表把此逻辑卷组变为20G, ? ? ?逻辑卷组名 ? ? ? ? ?-r(表示添加完毕直接刷新显示) 注意+号。
⑥pvscan:查看物理卷信息。 pvdisplay:查看详细的物理卷信息
? ?vgscan: ? ?查看卷组信息。 ? ?vgdisplay:查看卷组的具体信息
? ? lvscan:查看逻辑卷信息 ? ? ?lvdisplay:查看逻辑卷具体信息
[root@localhost ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 60G 0 disk
├─sda1 8:1 0 5G 0 part /boot
└─sda2 8:2 0 54G 0 part
├─centos-root 253:0 0 50G 0 lvm /
└─centos-swap 253:1 0 4G 0 lvm [SWAP]
sr0 11:0 1 4.2G 0 rom /run/media/root/CentOS 7 x86_64
[root@localhost ~]#fdisk /dev/sdb
fdisk: 打不开 /dev/sdb: 没有那个文件或目录
[root@localhost ~]#scan
[root@localhost ~]#fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
Device does not contain a recognized partition table
使用磁盘标识符 0x54299b1c 创建新的 DOS 磁盘标签。
命令(输入 m 获取帮助):n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
分区号 (1-4,默认 1):
起始 扇区 (2048-41943039,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-41943039,默认为 41943039):+10G
分区 1 已设置为 Linux 类型,大小设为 10 GiB
命令(输入 m 获取帮助):p
磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x54299b1c
设备 Boot Start End Blocks Id System
/dev/sdb1 2048 20973567 10485760 83 Linux
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
正在同步磁盘。
[root@localhost ~]#pvcreate /dev/sdb1 /dev/sdc
#设置物理卷
Physical volume "/dev/sdb1" successfully created.
Physical volume "/dev/sdc" successfully created.
[root@localhost ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 60G 0 disk
├─sda1 8:1 0 5G 0 part /boot
└─sda2 8:2 0 54G 0 part
├─centos-root 253:0 0 50G 0 lvm /
└─centos-swap 253:1 0 4G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 10G 0 part
sdc 8:32 0 20G 0 disk
sr0 11:0 1 4.2G 0 rom /run/media/root/CentOS 7 x86_64
[root@localhost ~]#vgcreate zk /dev/sdb1 /dev/sdc
#设置卷组
Volume group "zk" successfully created
[root@localhost ~]#lvcreate -n mysql -L 10G ghd
Logical volume "mysql" created.
#建立逻辑卷-n 指定逻辑卷名字mysql -L指定大小为10G(PE是ghd卷组中来的)
[root@localhost ~]#ll /dev/mapper/zk-mysql
lrwxrwxrwx 1 root root 7 12月 28 15:02 /dev/mapper/ghd-mysql -> ../dm-2
[root@localhost ~]#ll dev/ghd/mysql
lrwxrwxrwx 1 root root 7 12月 28 15:02 /dev/ghd-mysql -> ../dm-2
#逻辑卷有两个名字
安装文件系统
[root@localhost ~]#mkfs.xfs /dev/zk/mysql
meta-data=/dev/ghd/mysql isize=512 agcount=4, agsize=655360 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=2621440, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@localhost ~]#mount /dev/zk/mysql /mnt/
[root@localhost ~]#df
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/mapper/centos-root 52403200 4763800 47639400 10% /
devtmpfs 917604 0 917604 0% /dev
tmpfs 933524 0 933524 0% /dev/shm
tmpfs 933524 9308 924216 1% /run
tmpfs 933524 0 933524 0% /sys/fs/cgroup
/dev/sda1 5232640 182368 5050272 4% /boot
tmpfs 186708 4 186704 1% /run/user/42
tmpfs 186708 28 186680 1% /run/user/0
/dev/sr0 4414592 4414592 0 100% /run/media/root/CentOS 7 x86_64
/dev/mapper/ghd-mysql 10475520 32944 10442576 1% /mnt
[root@localhost ~]#scan
[root@localhost ~]#df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 50G 4.6G 46G 10% /
devtmpfs 897M 0 897M 0% /dev
tmpfs 912M 0 912M 0% /dev/shm
tmpfs 912M 9.1M 903M 1% /run
tmpfs 912M 0 912M 0% /sys/fs/cgroup
/dev/sda1 5.0G 179M 4.9G 4% /boot
tmpfs 183M 4.0K 183M 1% /run/user/42
tmpfs 183M 28K 183M 1% /run/user/0
/dev/sr0 4.3G 4.3G 0 100% /run/media/root/CentOS 7 x86_64
/dev/mapper/ghd-mysql 10G 33M 10G 1% /mnt
scan? display都可以查看当前物理卷或者卷组的详细信息
[root@localhost ~]#pvscan
PV /dev/sdb1 VG ghd lvm2 [<10.00 GiB / <10.00 GiB free]
PV /dev/sdc VG ghd lvm2 [<20.00 GiB / <10.00 GiB free]
PV /dev/sda2 VG centos lvm2 [54.00 GiB / 4.00 MiB free]
Total: 3 [<84.00 GiB] / in use: 3 [<84.00 GiB] / in no VG: 0 [0 ]
[root@localhost ~]#pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- 54.00g 4.00m
/dev/sdb1 ghd lvm2 a-- <10.00g <10.00g
/dev/sdc ghd lvm2 a-- <20.00g <10.00g
[root@localhost ~]#pvdisplay
--- Physical volume ---
PV Name /dev/sdb1
VG Name ghd
PV Size 10.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 2559
Free PE 2559
Allocated PE 0
PV UUID XWgeNp-JDpF-VqB6-8gF9-23Wh-TOUS-gBSJlC
--- Physical volume ---
PV Name /dev/sdc
VG Name ghd
PV Size 20.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 5119
Free PE 2559
Allocated PE 2560
PV UUID 897008-qwNC-HabQ-cPcY-BA3c-IWTK-hlcqDl
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size <54.01 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 13825
Free PE 1
Allocated PE 13824
PV UUID UjOeXH-jU4j-jhcP-JNEN-D0XC-nHtB-SURY2m
卷组空间不够,要使用扩容
vgextend? 卷组名? 设备名
vgextend? vg1? /dev/sdd
[root@localhost ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 60G 0 disk
├─sda1 8:1 0 5G 0 part /boot
└─sda2 8:2 0 54G 0 part
├─centos-root 253:0 0 50G 0 lvm /
└─centos-swap 253:1 0 4G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 10G 0 part
sdc 8:32 0 20G 0 disk
└─ghd-mysql 253:2 0 10G 0 lvm /mnt
sr0 11:0 1 4.2G 0 rom /run/media/root/CentOS 7 x86_64
[root@localhost ~]#fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
命令(输入 m 获取帮助):n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
分区号 (2-4,默认 2):
起始 扇区 (20973568-41943039,默认为 20973568):
将使用默认值 20973568
Last 扇区, +扇区 or +size{K,M,G} (20973568-41943039,默认为 41943039):+4G
分区 2 已设置为 Linux 类型,大小设为 4 GiB
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
正在同步磁盘。
[root@localhost ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 60G 0 disk
├─sda1 8:1 0 5G 0 part /boot
└─sda2 8:2 0 54G 0 part
├─centos-root 253:0 0 50G 0 lvm /
└─centos-swap 253:1 0 4G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 10G 0 part
└─sdb2 8:18 0 4G 0 part
sdc 8:32 0 20G 0 disk
└─ghd-mysql 253:2 0 10G 0 lvm /mnt
sr0 11:0 1 4.2G 0 rom /run/media/root/CentOS 7 x86_64
[root@localhost ~]#vgextend ghd /dev/sdb2
Physical volume "/dev/sdb2" successfully created.
Volume group "ghd" successfully extended
##扩容先扩卷组
lvextend? -L? 指定大小? ?逻辑卷名字
lxextend? -L? +5G? ?/dev/ghd/mysql
(一定要+5G,不然是缩容,也就是指定该逻辑卷的大小为5G)
[root@localhost ~]#lvextend -L +5G /dev/ghd/mysql
Size of logical volume ghd/mysql changed from 10.00 GiB (2560 extents) to 15.00 GiB (3840 extents).
Logical volume ghd/mysql successfully resized.
#扩展逻辑卷 指定扩展大小扩5G(一定要“+”)
tips:lvextend -L? +5G? /dev/ghd/mysql? -r(-r代表立马同步文件系统)
[root@localhost ~]#xfs_growfs /mnt
#同步文件系统
meta-data=/dev/mapper/ghd-mysql isize=512 agcount=4, agsize=655360 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=2621440, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 2621440 to 3932160
[root@localhost ~]#df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 50G 4.6G 46G 10% /
devtmpfs 897M 0 897M 0% /dev
tmpfs 912M 0 912M 0% /dev/shm
tmpfs 912M 9.1M 903M 1% /run
tmpfs 912M 0 912M 0% /sys/fs/cgroup
/dev/sda1 5.0G 179M 4.9G 4% /boot
tmpfs 183M 4.0K 183M 1% /run/user/42
tmpfs 183M 28K 183M 1% /run/user/0
/dev/sr0 4.3G 4.3G 0 100% /run/media/root/CentOS 7 x86_64
/dev/mapper/ghd-mysql 15G 33M 15G 1% /mnt
[root@localhost ~]#
只有ext4可以缩减容量
vgdisplay 查看根的卷组
vgextend centos /dev/sdc 先扩卷组
lvexten -L +10G /dev/centos/root -r 再扩根的逻辑卷
[root@localhost ~]#vgdisplay
--- Volume group ---
VG Name ghd
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 0
Max PV 0
Cur PV 3
Act PV 3
VG Size <33.99 GiB
PE Size 4.00 MiB
Total PE 8701
Alloc PE / Size 3840 / 15.00 GiB
Free PE / Size 4861 / <18.99 GiB
VG UUID qRhIUi-zKfd-NS7S-9HZX-r8NT-GWhn-7AT8PD
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 54.00 GiB
PE Size 4.00 MiB
Total PE 13825
Alloc PE / Size 13824 / 54.00 GiB
Free PE / Size 1 / 4.00 MiB
VG UUID 1D0yqi-om1I-c0gi-Xlhy-XEJ3-XF9S-dQvqNB
[root@localhost ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 60G 0 disk
├─sda1 8:1 0 5G 0 part /boot
└─sda2 8:2 0 54G 0 part
├─centos-root 253:0 0 50G 0 lvm /
└─centos-swap 253:1 0 4G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 10G 0 part
└─sdb2 8:18 0 4G 0 part
sdc 8:32 0 20G 0 disk
└─ghd-mysql 253:2 0 15G 0 lvm
sr0 11:0 1 4.2G 0 rom /run/media/root/CentOS 7 x86_64
[root@localhost ~]#scan
[root@localhost ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 60G 0 disk
├─sda1 8:1 0 5G 0 part /boot
└─sda2 8:2 0 54G 0 part
├─centos-root 253:0 0 50G 0 lvm /
└─centos-swap 253:1 0 4G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 10G 0 part
└─sdb2 8:18 0 4G 0 part
sdc 8:32 0 20G 0 disk
└─ghd-mysql 253:2 0 15G 0 lvm
sdd 8:48 0 20G 0 disk
sr0 11:0 1 4.2G 0 rom /run/media/root/CentOS 7 x86_64
[root@localhost ~]#vgextend centos /dev/sdd
Physical volume "/dev/sdd" successfully created.
Volume group "centos" successfully extended
#先扩卷组
[root@localhost ~]#lvextend -L +10G /dev/centos/root -r
#再扩根的逻辑卷
Size of logical volume centos/root changed from 50.00 GiB (12800 extents) to 60.00 GiB (15360 extents).
Logical volume centos/root successfully resized.
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=3276800 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=13107200, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=6400, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 13107200 to 15728640
[root@localhost ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 60G 0 disk
├─sda1 8:1 0 5G 0 part /boot
└─sda2 8:2 0 54G 0 part
├─centos-root 253:0 0 60G 0 lvm /
└─centos-swap 253:1 0 4G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 10G 0 part
└─sdb2 8:18 0 4G 0 part
sdc 8:32 0 20G 0 disk
└─ghd-mysql 253:2 0 15G 0 lvm
sdd 8:48 0 20G 0 disk
└─centos-root 253:0 0 60G 0 lvm /
sr0 11:0 1 4.2G 0 rom /run/media/root/CentOS 7 x86_64