前言
环境:centos7.9 本篇来实际操作lvm逻辑卷管理,从新加硬盘开始讲解。
添加2块磁盘
[root@localhost ~]
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 12G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 11G 0 part
├─cl-root 253:0 0 9.8G 0 lvm /
└─cl-swap 253:1 0 1.2G 0 lvm [SWAP]
sr0 11:0 1 1024M 0 rom
[root@localhost ~]
[root@localhost ~]
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 12G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 11G 0 part
├─cl-root 253:0 0 9.8G 0 lvm /
└─cl-swap 253:1 0 1.2G 0 lvm [SWAP]
sdb 8:16 0 8G 0 disk
sdc 8:32 0 6G 0 disk
sr0 11:0 1 1024M 0 rom
创建PV(pvcreate 命令、pvs命令、pvdisplay命令、pvscan命令)
pvcreate:将一个或多个磁盘或分区转化为pv物理卷,其实内部就是将磁盘或分区转化为一个个PE,所以磁盘或分区最好是没有数据的,不然数据就会丢失。 pvs:查看pv的信息,信息简单明了。 pvdisplay:查看pv的信息,信息内容更加详细。 pvscan:查看pv的信息 创建PV有两种方式:基于磁盘创建、 基于分区创建。 1、基于磁盘的 ,就不需要划分分区 ,直接通过 pvcreate 来创建物理卷即可; 2、基于分区的 ,需要事先通过fdisk划分分区 ,并将分区格式转化为8e(8e表示的就是Linux lvm),分区不要mkfs格式化 ,然后再进行 pvcreate 来创建物理卷即可 。
[root@localhost ~]
Physical volume "/dev/sdb" successfully created.
Physical volume "/dev/sdc" successfully created.
[root@localhost ~]
PV VG Fmt Attr PSize PFree
/dev/sda2 cl lvm2 a-- 11.00g 4.00m
/dev/sdb lvm2 --- 8.00g 8.00g
/dev/sdc lvm2 --- 6.00g 6.00g
[root@localhost ~]
vgcreate创建卷组vg
[root@localhost ~]
Volume group "iflytek" successfully created
[root@localhost ~]
[root@localhost ~]
PV VG Fmt Attr PSize PFree
/dev/sda2 cl lvm2 a-- 11.00g 4.00m
/dev/sdb iflytek lvm2 a-- 8.00g 8.00g
/dev/sdc iflytek lvm2 a-- 6.00g 6.00g
[root@localhost ~]
lvcreate创建逻辑卷lv
[root@localhost ~]
VG
iflytek 2 1 0 wz--n- 13.99g 13.99g
[root@localhost ~]
[root@localhost ~]
Logical volume "lv_data" created.
[root@localhost ~]
[root@localhost ~]
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root cl -wi-ao---- 9.79g
swap cl -wi-ao---- 1.20g
lv_data iflytek -wi-a----- 6.00g
[root@localhost ~]
mkfs格式化逻辑卷并创建文件系统
[root@localhost ~]
meta-data=/dev/iflytek/lv_data isize=512 agcount=4, agsize=393216 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=1572864, 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
mount挂载
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/cl-root xfs 9.8G 1021M 8.8G 11% /
devtmpfs devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs tmpfs 1.9G 8.6M 1.9G 1% /run
tmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 139M 876M 14% /boot
tmpfs tmpfs 378M 0 378M 0% /run/user/0
/dev/mapper/iflytek-lv_data xfs 6.0G 33M 6.0G 1% /lv_data
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
小结
1、添加新硬盘(不需要分区)或者创建一个是8e的Linux lvm分区(不需要格式化);
2、pvcreate /dev/sdb
3、pvs 、pvscan、pvdisplay
4、vgcreate -n vg_iflytek /dev/sdb
5、vgs、vgscan、vgdisplay
6、lvcreate -n lv_data -L 6G vg_iflytek
7、lvs、lvdisplay、lvscan
8、mkfs.xfs /dev/vg_iflytek/lv_data
9、mkdir /data && mount /dev/vg_iflytek/lv_data /data
10、echo '/dev/iflytek/lv_data /lv_data xfs defaults 0 0' >> /etc/fsta
10、df -Th
lvextend 逻辑卷扩容
lvextend可以在线扩容逻辑卷,不需要关闭,也不需要停服务。
[root@localhost mapper]
[root@localhost mapper]
[root@localhost mapper]
[root@localhost mapper]
[root@localhost mapper]
[root@localhost mapper]
[root@localhost mapper]
vgextend 卷组扩容
卷组扩容其实就是新加一个磁盘或分区,转换为pv,然后把磁盘或分区加入卷组中即可。
[root@localhost ~]
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 12G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 11G 0 part
├─cl-root 253:0 0 9.8G 0 lvm /
└─cl-swap 253:1 0 1.2G 0 lvm [SWAP]
sdb 8:16 0 8G 0 disk
└─iflytek-lv_data 253:2 0 14G 0 lvm /lv_data
sdc 8:32 0 6G 0 disk
└─iflytek-lv_data 253:2 0 14G 0 lvm /lv_data
sdd 8:48 0 2G 0 disk
sr0 11:0 1 4.1G 0 rom
[root@localhost ~]
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 0xf521342a.
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-4194303, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-4194303, default 4194303):
Using default value 4194303
Partition 1 of type Linux and of size 2 GiB is set
Command (m for help): t
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/cl-root 9.8G 1021M 8.8G 11% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.6M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 1014M 139M 876M 14% /boot
/dev/mapper/iflytek-lv_data 14G 33M 14G 1% /lv_data
tmpfs 378M 0 378M 0% /run/user/0
[root@localhost ~]
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 12G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 11G 0 part
├─cl-root 253:0 0 9.8G 0 lvm /
└─cl-swap 253:1 0 1.2G 0 lvm [SWAP]
sdb 8:16 0 8G 0 disk
└─iflytek-lv_data 253:2 0 14G 0 lvm /lv_data
sdc 8:32 0 6G 0 disk
└─iflytek-lv_data 253:2 0 14G 0 lvm /lv_data
sdd 8:48 0 2G 0 disk
└─sdd1 8:49 0 2G 0 part
sr0 11:0 1 4.1G 0 rom
[root@localhost ~]
Physical volume "/dev/sdd1" successfully created.
[root@localhost ~]
PV VG Fmt Attr PSize PFree
/dev/sda2 cl lvm2 a-- 11.00g 4.00m
/dev/sdb iflytek lvm2 a-- 8.00g 0
/dev/sdc iflytek lvm2 a-- 6.00g 0
/dev/sdd1 lvm2 --- 2.00g 2.00g
[root@localhost ~]
Volume group "iflytek" successfully extended
[root@localhost ~]
PV VG Fmt Attr PSize PFree
/dev/sda2 cl lvm2 a-- 11.00g 4.00m
/dev/sdb iflytek lvm2 a-- 8.00g 0
/dev/sdc iflytek lvm2 a-- 6.00g 0
/dev/sdd1 iflytek lvm2 a-- 2.00g 2.00g
[root@localhost ~]
lvremove删除逻辑卷
[root@localhost ~]
[root@localhost ~]
Do you really want to remove active logical volume iflytek/lv_data? [y/n]: y
Logical volume "lv_data" successfully removed
[root@localhost ~]
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root cl -wi-ao---- 9.79g
swap cl -wi-ao---- 1.20g
[root@localhost ~]
vgreduce 删除卷组中的某个pv
[root@localhost ~]
Removed "/dev/sdd1" from volume group "iflytek"
[root@localhost ~]
PV VG Fmt Attr PSize PFree
/dev/sda2 cl lvm2 a-- 11.00g 4.00m
/dev/sdb iflytek lvm2 a-- 8.00g 0
/dev/sdc iflytek lvm2 a-- 6.00g 0
/dev/sdd1 lvm2 --- 2.00g 2.00g
[root@localhost ~]
vgremove 删除卷组
[root@localhost ~]
Do you really want to remove volume group "iflytek" containing 1 logical volumes? [y/n]: n
Volume group "iflytek" not removed
[root@localhost ~]
pvremove 删除物理卷
[root@localhost ~]
Labels on physical volume "/dev/sdd1" successfully wiped.
[root@localhost ~]
小结
1、 lvextend 命令逻辑卷扩容
[root@localhost mapper]
[root@localhost mapper]
[root@localhost mapper]
[root@localhost mapper]
[root@localhost mapper]
2、vgextend 卷组扩容
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
3、vgreduce 删除卷组中的某个pv
[root@localhost ~]
4、vgremove 删除卷组
[root@localhost ~]
5、pvremove 删除物理卷
[root@localhost ~]
|