Leo Zhang
菩提本无树,明镜亦非台!
 

一、基本概念:

逻辑卷管理(Logical Volume Manager):LVM将一个或多个硬盘的分区在逻辑上集合,相当于一个大硬盘来使用,
当硬盘的空间不够使用的时候,可以继续将其它的硬盘的分区加入其中,这样可以实现磁盘空间的动态管理。
物理存储介质(The Physical Media):这里指系统的存储设备:硬盘(如/dev/sda),是存储系统最低层的存储单元。
物理卷(Physical Volume):指硬盘分区或从逻辑上与磁盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块。
卷组(Volume Group):LVM卷组由一个或多个物理卷组成。可以在卷组上创建一个或多个逻辑卷。
逻辑卷(Logical Volume):类似于非LVM系统中的硬盘分区,在逻辑卷之上可以建立文件系统 (如/home),即为逻辑分区。
 
如下图所示PV、VG、LV三者关系:
 
 

二、常规操作及命令

1、实施LVM存储

# a.进入交互式界面后,创建新分区
fdisk /dev/sda
 
# b.创建物理卷(pv)
pvcreate /dev/sda4
 
### 可使用pvs、pvdisplay查看
 
# c.创建卷组(vg)
vgcreate vg4 /dev/sda4
 
### 可使用vgs、vgdisplay查看
 
# d.创建逻辑卷(lv)
lvcreate -n lv4 vg4 -L 1G
 
### 可使用lvs、lvdisplay查看
 
# e.建立文件系统(-t指定文件系统类型)
mkfs -t xfs /dev/vg4/lv4
 
# f.挂载文件目录
mount /dev/vg4/lv4 /data //临时挂载
vi /etc/fstab //永久挂载(在配置文件追加)
/dev/vg4/lv4 /data xfs defaults 0 0
 
# g.重新加载并查看效果
mount -a && df -Th

 

2、删除逻辑卷

# a.卸载挂载点
umount /data
 
### 若提示繁忙,可使用 fuser -m -k /data 结束掉
 
# b.删除逻辑卷
lvremove /dev/vg4/lv4
 
# c.删除卷组
vgremove vg4
 
# d.删除物理卷
pvremove /dev/sda4

 

3、扩展和缩减

# 假如vg容量不够,需要新建分区-创建物理卷-扩展卷组
fdisk /dev/sda
pvcreate /dev/sda4
vgextend vg4 /dev/sda4
 
# 若想缩减卷组需要缩减卷组-缩减物理卷
vgreduce vg4 /dev/vg1   
pvremove /dev/sda4
 
# 扩展逻辑卷后需根据文件系统类型进行扩展
lvextend /dev/vg4/lv1 -L 500M //将LV扩大至500M
xfs_growfs /dev/vg4/lv4     //xfs文件系统扩展方法
resize2fs /dev/vg4/lv4     //ext4文件系统扩展方法

 

 

三、实战演示:

1、前期准备

a)以虚拟机作为演示,先查看磁盘信息( fdisk -l )和系统磁盘大小(df -h ),当前均为40G。
 
b)关机虚拟机,扩展磁盘大小到50G。
 
c)启动虚拟机,再次查看磁盘信息,增加到50G(系统磁盘还是40G,是因为磁盘没有做分区)。
 

2、创建和删除分区

a)创建一个普通分区( fdisk /dev/sda )

注意:不同操作系统的磁盘命名方式不同,比如Ubuntu是/dev/vda,请根据磁盘信息首行显示Disk值。
[root@k8s-master ~]# fdisk -l
 
Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x000a4e32
 
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 616447 307200 83 Linux
/dev/sda2 616448 4812799 2098176 82 Linux swap / Solaris
/dev/sda3 4812800 83886079 39536640 83 Linux
[root@k8s-master ~]#
[root@k8s-master ~]# fdisk /dev/sda
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.
 
 
Command (m for help): m //查看帮助说明
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partitions system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
 
Command (m for help): n //创建分区
Partition type:
p primary (3 primary, 0 extended, 1 free) //选择主分区(最多4个)
e extended //扩展分区
Select (default e): p
Selected partition 4 //选择分区号
First sector (83886080-104857599, default 83886080): //分区起始位置(空格则使用默认值)
Using default value 83886080
Last sector, +sectors or +size{K,M,G} (83886080-104857599, default 104857599):
Using default value 104857599 //分区结束位置(或指定大小+10G)
Partition 4 of type Linux and of size 10 GiB is set
 
Command (m for help): p //查看分区情况
 
Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x000a4e32
 
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 616447 307200 83 Linux
/dev/sda2 616448 4812799 2098176 82 Linux swap / Solaris
/dev/sda3 4812800 83886079 39536640 83 Linux
/dev/sda4 83886080 104857599 10485760 83 Linux
 
Command (m for help): w //写入分区表
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
 
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@k8s-master ~]# partprobe //使分区表生效(无需重启)

 

b)创建一个LVM分区

[root@k8s-master ~]# fdisk /dev/sda
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.
 
Command (m for help): n
Partition type:
p primary (3 primary, 0 extended, 1 free)
e extended
Select (default e): p
Selected partition 4
First sector (83886080-104857599, default 83886080):
Using default value 83886080
Last sector, +sectors or +size{K,M,G} (83886080-104857599, default 104857599): +5G
Partition 4 of type Linux and of size 5 GiB is set //指定分区大小
 
Command (m for help): t //修改分区格式
Partition number (1-4, default 4): 4 //选择分区
Hex code (type L to list all codes): 8e //8e类型即为LVM
Changed type of partition 'Linux' to 'Linux LVM'
 
Command (m for help): p //查看当前分区
 
Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x000a4e32
 
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 616447 307200 83 Linux
/dev/sda2 616448 4812799 2098176 82 Linux swap / Solaris
/dev/sda3 4812800 83886079 39536640 83 Linux
/dev/sda4 83886080 94371839 5242880 8e Linux LVM

 

c)删除一个分区

[root@k8s-master ~]# fdisk /dev/sda
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.
 
 
Command (m for help): d
Partition number (1-4, default 4): 4
Partition 4 is deleted
 
Command (m for help): q
 

 

3、挂载磁盘到指定目录

[root@Server]# mkfs.xfs -f /dev/vdb1
meta-data=/dev/vdb1 isize=512 agcount=4, agsize=32767936 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=0
data = bsize=4096 blocks=131071744, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=63999, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
 
[root@Server /]# mkdir /data
[root@Server /]# mount /dev/vdb1 /data
[root@Server /]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 16G 0 16G 0% /dev
tmpfs tmpfs 16G 256K 16G 1% /dev/shm
tmpfs tmpfs 16G 22M 16G 1% /run
tmpfs tmpfs 16G 0 16G 0% /sys/fs/cgroup
/dev/vda3 xfs 14G 7.9G 6.0G 58% /
tmpfs tmpfs 16G 64K 16G 1% /tmp
/dev/vda2 xfs 1014M 222M 793M 22% /boot
/dev/vda1 vfat 200M 5.8M 195M 3% /boot/efi
tmpfs tmpfs 3.2G 576K 3.2G 1% /run/user/0
/dev/sr1 iso9660 478K 478K 0 100% /run/media/root/config-2
/dev/vdb1 xfs 500G 543M 500G 1% /data
[root@Server /]# echo "/dev/vdb1 /data xfs defaults 0 0" >> /etc/fstab
[root@Server-6822768a-984b-4aa9-afb4-d6e34b24e4bc /]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Tue Dec 1 10:40:10 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
UUID=f9d06f7f-0fe3-4dbf-8c30-2a8cd8f3ac3b / xfs defaults 0 0
UUID=c5f65c3b-114d-4c04-adf7-1e2e7351276e /boot xfs defaults 0 0
UUID=D071-A4FA /boot/efi vfat umask=0077,shortname=winnt 0 2
/dev/vdb1 /data xfs defaults 0 0
 
[root@Server /]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 16G 0 16G 0% /dev
tmpfs 16G 256K 16G 1% /dev/shm
tmpfs 16G 22M 16G 1% /run
tmpfs 16G 0 16G 0% /sys/fs/cgroup
/dev/vda3 14G 7.9G 6.0G 58% /
tmpfs 16G 64K 16G 1% /tmp
/dev/vda2 1014M 222M 793M 22% /boot
/dev/vda1 200M 5.8M 195M 3% /boot/efi
tmpfs 3.2G 576K 3.2G 1% /run/user/0
/dev/sr1 478K 478K 0 100% /run/media/root/config-2
/dev/vdb1 500G 543M 500G 1% /data

 

 

4、创建和删除逻辑卷

a)创建一个逻辑卷并挂载

注意:这里/dev/sda4 分区名请根据自己实际情况修改。
[root@k8s-master ~]# pvcreate /dev/sda4
Physical volume "/dev/sda4" successfully created.
[root@k8s-master ~]# pvdisplay
"/dev/sda4" is a new physical volume of "10.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sda4
VG Name
PV Size 10.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID bp2axz-XfE1-m1Ca-6BX4-G22d-uliq-1q88Ky
 
[root@k8s-master ~]# vgcreate vg4 /dev/sda4
Volume group "vg4" successfully created
[root@k8s-master ~]# vgdisplay
--- Volume group ---
VG Name vg4
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 <10.00 GiB
PE Size 4.00 MiB
Total PE 2559
Alloc PE / Size 256 / 1.00 GiB
Free PE / Size 2303 / <9.00 GiB
VG UUID 0zg2zi-EcJx-d26s-cTwN-XSqD-oF4H-pmo8UP
 
[root@k8s-master ~]# lvcreate -n lv4 vg4 -L 1G
Logical volume "lv4" created.
[root@k8s-master ~]# lvdisplay
--- Logical volume ---
LV Path /dev/vg4/lv4
LV Name lv4
VG Name vg4
LV UUID FPWpmo-5cad-mRe1-LM3D-65ab-AcZW-kRxZpZ
LV Write Access read/write
LV Creation host, time k8s-master, 2021-06-11 02:56:36 -0700
LV Status available
# open 0
LV Size 1.00 GiB
Current LE 256
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
 
[root@k8s-master ~]# mkfs -t xfs /dev/vg4/lv4
meta-data=/dev/vg4/lv4 isize=512 agcount=4, agsize=65536 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=262144, 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@k8s-master ~]# mkdir /data
[root@k8s-master ~]# mount /dev/vg4/lv4 /data
[root@k8s-master ~]# df -Th | grep data
/dev/mapper/vg4-lv4 xfs 1014M 33M 982M 4% /data

 

b)删除一个逻辑卷

[root@k8s-master ~]# umount /data
[root@k8s-master ~]#
[root@k8s-master ~]# lvremove /dev/vg4/lv4
Do you really want to remove active logical volume vg4/lv4? [y/n]: y
Logical volume "lv4" successfully removed
[root@k8s-master ~]#
[root@k8s-master ~]# lvremove /dev/vg4/lv4
Failed to find logical volume "vg4/lv4"
[root@k8s-master ~]#
[root@k8s-master ~]# vgremove vg4
Volume group "vg4" successfully removed
[root@k8s-master ~]#
[root@k8s-master ~]# pvremove /dev/sda4
Labels on physical volume "/dev/sda4" successfully wiped.
[root@k8s-master ~]#
[root@k8s-master ~]# df -hT | grep data
[root@k8s-master ~]#

 

使用LVM对根分区进行扩容:https://blog.csdn.net/qq_29292203/article/details/106237908

 

 

posted on 2022-09-07 10:36  LeoZhanggg  阅读(890)  评论(0编辑  收藏  举报