网上找的lvm结构图
对磁盘创建分区,也可以不创建分区,直接将整个盘变成pv,但是不建议这么做
查看当前系统识别到的磁盘
[root@datacentercm ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 60G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 59G 0 part
├─centos-root 253:0 0 55.1G 0 lvm /
└─centos-swap 253:1 0 3.9G 0 lvm [SWAP]
sdb 8:16 0 3G 0 disk
sr0 11:0 1 1024M 0 rom
[root@datacentercm ~]#
对/dev/sdb操作,首先创建lvm格式分区
[root@datacentercm ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x69155380.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-6291455, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-6291455, default 6291455):
Using default value 6291455
Partition 1 of type Linux and of size 3 GiB is set
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): p
Disk /dev/sdb: 3221 MB, 3221225472 bytes, 6291456 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x69155380
Device Boot Start End Blocks Id System
/dev/sdb1 2048 6291455 3144704 8e Linux LVM
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@datacentercm ~]# partprobe
[root@datacentercm ~]#
[root@datacentercm ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 60G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 59G 0 part
├─centos-root 253:0 0 55.1G 0 lvm /
└─centos-swap 253:1 0 3.9G 0 lvm [SWAP]
sdb 8:16 0 3G 0 disk
└─sdb1 8:17 0 3G 0 part
sr0 11:0 1 1024M 0 rom
创建pv
[root@datacentercm ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created.
查看pv
[root@datacentercm ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <59.00g 4.00m
/dev/sdb1 lvm2 --- <3.00g <3.00g
[root@datacentercm ~]# pvdisplay
"/dev/sdb1" is a new physical volume of "<3.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size <3.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID qYMYKd-cJj7-8ojC-y1Eq-sd8t-m4Vt-bK22Zt
创建vg
#data是vg名称
[root@datacentercm ~]# vgcreate data /dev/sdb1
Volume group "data" successfully created
[root@datacentercm ~]#
查看vg
[root@datacentercm ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <59.00g 4.00m
data 1 1 0 wz--n- <3.00g 0
[root@datacentercm ~]# vgdisplay
--- Volume group ---
VG Name data
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 <3.00 GiB
PE Size 4.00 MiB
Total PE 767
Alloc PE / Size 0 / 0
Free PE / Size 767 / <3.00 GiB
VG UUID xDideV-T67z-ZE6S-fUH3-pcAA-5lQV-nMvTD1
--- 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 <59.00 GiB
PE Size 4.00 MiB
Total PE 15103
Alloc PE / Size 15102 / 58.99 GiB
Free PE / Size 1 / 4.00 MiB
VG UUID abmb7G-hw6p-Vo6o-IH4p-Ghea-yU1X-uNVaiI
[root@datacentercm ~]#
创建lvm
#使用全部空间创建lvm,-n指定lvm名称
[root@datacentercm ~]# lvcreate -n data -l 100%FREE data
Logical volume "data" created.
#也可以指定大小的方式创建lvm,-L指定容量,单位G和M
[root@datacentercm ~]# lvcreate -n data -L 3G data
查看lvm
[root@datacentercm ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- <55.12g
swap centos -wi-ao---- <3.88g
data data -wi-ao---- <3.00g
[root@datacentercm ~]# lvdisplay
--- Logical volume ---
LV Path /dev/data/data
LV Name data
VG Name data
LV UUID Z5Uze9-kRBC-BqE7-7eZN-b35o-eqDY-rofsKK
LV Write Access read/write
LV Creation host, time datacentercm.com, 2023-08-08 16:26:56 +0800
LV Status available
# open 0
LV Size <3.00 GiB
Current LE 767
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
[root@datacentercm ~]#
格式化lvm并挂载
[root@datacentercm ~]# mkfs.ext4 /dev/data/data
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
196608 inodes, 785408 blocks
39270 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=805306368
24 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912
Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
[root@datacentercm ~]# mount /dev/data/data /test/
[root@datacentercm ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 3.8G 0 3.8G 0% /dev
tmpfs 3.9G 28K 3.9G 1% /dev/shm
tmpfs 3.9G 20M 3.8G 1% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 56G 12G 45G 21% /
/dev/sda1 1014M 192M 823M 19% /boot
cm_processes 3.9G 104K 3.9G 1% /run/cloudera-scm-agent/process
tmpfs 781M 0 781M 0% /run/user/0
/dev/mapper/data-data 2.9G 9.0M 2.8G 1% /test
[root@datacentercm ~]#
2、扩容
当前lvm使用的是一块3G的盘,再加一块4G的盘,将lvm扩容到7G
[root@datacentercm ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 60G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 59G 0 part
├─centos-root 253:0 0 55.1G 0 lvm /
└─centos-swap 253:1 0 3.9G 0 lvm [SWAP]
sdb 8:16 0 3G 0 disk
└─sdb1 8:17 0 3G 0 part
└─data-data 253:2 0 3G 0 lvm /test
sdc 8:32 0 4G 0 disk
sr0 11:0 1 1024M 0 rom
[root@datacentercm ~]#
/dev/sdc盘已经加上了,容量4G,首先创建lvm分区
#这步可以参照上面的操作,n>p>t>8e>w
[root@datacentercm ~]# fdisk /dev/sdc
[root@datacentercm ~]# pvcreate /dev/sdc1
Physical volume "/dev/sdc1" successfully created.
[root@datacentercm ~]#
接下来要先扩展vg,而不是创建
#扩容vg [root@datacentercm ~]# vgextend data /dev/sdc1 Volume group "data" successfully extended [root@datacentercm ~]# vgs VG #PV #LV #SN Attr VSize VFree centos 1 2 0 wz--n- <59.00g 4.00m data 2 1 0 wz--n- 6.99g <4.00g [root@datacentercm ~]#
扩容lvm
#使用剩余全部空间扩容
[root@datacentercm ~]# lvextend -l +100%FREE /dev/data/data
Size of logical volume data/data changed from <3.00 GiB (767 extents) to 6.99 GiB (1790 extents).
Logical volume data/data successfully resized.
[root@datacentercm ~]#
#也可以指定扩容大小
[root@datacentercm ~]# lvextend -L +4G /dev/data/data
[root@datacentercm ~]# lvdisplay
--- Logical volume ---
LV Path /dev/data/data
LV Name data
VG Name data
LV UUID Z5Uze9-kRBC-BqE7-7eZN-b35o-eqDY-rofsKK
LV Write Access read/write
LV Creation host, time datacentercm.com, 2023-08-08 16:26:56 +0800
LV Status available
# open 1
LV Size 6.99 GiB
Current LE 1790
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
此时lvm容量虽然扩大了,但是文件系统显示还是原来大小
[root@datacentercm ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 3.8G 0 3.8G 0% /dev
tmpfs 3.9G 28K 3.9G 1% /dev/shm
tmpfs 3.9G 20M 3.8G 1% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 56G 12G 45G 21% /
/dev/sda1 1014M 192M 823M 19% /boot
cm_processes 3.9G 104K 3.9G 1% /run/cloudera-scm-agent/process
tmpfs 781M 0 781M 0% /run/user/0
/dev/mapper/data-data 2.9G 9.0M 2.8G 1% /test
[root@datacentercm ~]#
扩容分区
#ext系列分区
[root@datacentercm ~]# resize2fs /dev/data/data
#xfs分区
[root@datacentercm ~]# xfs_growfs /dev/data/data
[root@datacentercm ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 3.8G 0 3.8G 0% /dev
tmpfs 3.9G 28K 3.9G 1% /dev/shm
tmpfs 3.9G 20M 3.8G 1% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 56G 12G 45G 21% /
/dev/sda1 1014M 192M 823M 19% /boot
cm_processes 3.9G 104K 3.9G 1% /run/cloudera-scm-agent/process
tmpfs 781M 0 781M 0% /run/user/0
/dev/mapper/data-data 6.9G 14M 6.5G 1% /test
[root@datacentercm ~]#
再此查看文件系统容量已经更新,扩容完成
3、常用命令
pv物理卷
pvcreate 将物理分区建立成独立的pv; pvscan 查找目前系统里面任何具有pv的磁盘; pvdisplay 显示出目前系统上面的pv状态; pvremove 将pv属性删除,让该分区不具有pv属性。
vg卷组
vgcreate 主要建立VG的命令,主要参数如下: -l:卷组上允许创建的最大逻辑卷数; -p:卷组中允许添加的最大物理卷数; -s:卷组上的物理卷的PE大小。 vgscan 查找系统上面是否有VG存在; vgdisplay 显示系统上面的VG状态; vgextend 在VG内增加额外的PV; vgreduce 在VG内删除PV; vgchange 设置VG是否启动; vgremove 删除一个vg。
lvm逻辑卷
lvcreat 建立LV; lvscan 查询系统上面的LV; lvdisplay 显示系统上面的LV状态; lvextend 在LV里面增加容量; lvreduce 在LV里面减少容量; lvremove 删除一个LV; lvresize 对LV进行容量大小的调整。