Linux的LVM管理
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux的LVM管理相关的知识,希望对你有一定的参考价值。
参考技术A 一、磁盘管理步骤:
1. 物理磁盘被格式化为PV,空间被划分成一个个PE;
2. 不同的PV加入同一个VG中,不同PV的PE全部进入VG的PE池;
3. 在VG中基于PE创建LV,组成LV的PE可能来自于不同的物理磁盘;
4. LV直接格式化后挂在使用;
5. LV的增加或减少其实就是增加或减少该组LV的PE数量,其过程不会丢失原始数据。
操作步骤:
①查看待分配的磁盘
[root@cen7 ~]# fdisk -l
Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 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: 0x000a39f5
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 452607 225280 83 Linux
/dev/sda2 452608 415227903 207387648 83 Linux
/dev/sda3 415227904 419422207 2097152 82 Linux swap / Solaris
Disk /dev/sdb: 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: 0x99bab3b4
Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 8e Linux LVM
②创建PV
[root@cen7 ~]# pvcreate /dev/sdb1
WARNING: dos signature detected on /dev/sdb1 at offset 510. Wipe it? [y/n]: y
Wiping dos signature on /dev/sdb1.
Physical volume "/dev/sdb1" successfully created.
③查看PV
[root@cen7 ~]# pvdisplay /dev/sdb1
"/dev/sdb1" is a new physical volume of "5.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 5.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID yxWtAD-9Pye-9hVt-H7sZ-h44x-3MyP-NpadQ8
④创建VG
[root@cen7 ~]# vgcreate vg01 /dev/sdb1
Volume group "vg01" successfully created
⑤查看VG
[root@cen7 ~]# vgdisplay
--- Volume group ---
VG Name vg01
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 <5.00 GiB
PE Size 4.00 MiB
Total PE 1279
Alloc PE / Size 0 / 0
Free PE / Size 1279 / <5.00 GiB
VG UUID fVnLdP-mVtf-e1HI-33pR-MOcS-PLs5-l6nDck
⑥创建LV
[root@cen7 ~]# lvcreate -n lv01 -L 4.996G vg01#空间大小=1279*4M/1024
Rounding up size to full physical extent <5.00 GiB
Logical volume "lv01" created.
或者
[root@cen7 ~]# lvcreate -n lv01 -l 1279 vg01
Logical volume "lv01" created.
⑦查看LV
[root@cen7 ~]# lvdisplay
--- Logical volume ---
LV Path /dev/vg01/lv01
LV Name lv01
VG Name vg01
LV UUID dq0d77-sVP2-F3CX-baQC-5fvy-MeTU-YE710s
LV Write Access read/write
LV Creation host, time cen7, 2022-02-22 10:12:04 +0800
LV Status available
# open 0
LV Size <5.00 GiB
Current LE 1279
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
⑧格式化文件系统
[root@cen7 ~]# mkfs.xfs /dev/vg01/lv01
meta-data=/dev/vg01/lv01 isize=512 agcount=4, agsize=327424 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=1309696, 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 b
⑨挂载文件分区
[root@cen7 ~]# mkdir /mnt/lv01
[root@cen7 ~]# mount /dev/vg01/lv01 /mnt/lv01
⑩查看是否挂在成功
[root@cen7 ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs tmpfs 2.0G 13M 2.0G 1% /run
tmpfs tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/sda2 xfs 198G 27G 172G 14% /
/dev/sda1 xfs 217M 147M 71M 68% /boot
tmpfs tmpfs 394M 4.0K 394M 1% /run/user/42
tmpfs tmpfs 394M 28K 394M 1% /run/user/0
/dev/sr0 iso9660 4.4G 4.4G 0 100% /run/media/root/CentOS 7 x86_64
/dev/mapper/vg01-lv01 xfs 5.0G 33M 5.0G 1% /mnt/lv01
⑧移除LV
[root@cen7 ~]# lvremove /dev/vg01/lv01
Do you really want to remove active logical volume vg01/lv01? [y/n]: y
Logical volume "lv01" successfully removed
linux lvm的管理
LVM管理
lvm | 逻辑卷,可以让设备自由伸缩的一个设备管理软件 |
pv | 物理卷 |
vg | 物理卷组 |
pe | 物理拓展,lvm最小的存储单元 |
1、LVM的制作(针对xfs问价系统)
首先划分lvm所用到的设备,这些设备的功能id必须是linux LVM
1.1 lvm建立
1 2 3 4 5 6 | pvcreate /dev/vdb1 ##把物理分区做成物理卷 pvs|pvdisplay ##查看物理卷 vgcreate vg0 /dev/vdb1 ##用制作好的/dev/vdb1这个物理卷制作一个物理卷组vg0 vgs|vgdisplay ##查看物理卷组 lvcreate -L 200M-n lv0 vg0 ##在vg0这个组中建立出lv0设备 -L 指定设备大小 -n 指定设备名字 lvs|lvdisplay ##对逻辑卷设备进行查看 |
1.2 lvm设备的扩大
1 2 3 4 5 6 | mkfs.xfs /dev/lv0/vg0 #格式化 mount /dev/lv0/vg0 /mnt #挂载 #如果/mnt/目录下的存储容量不够使用则可以扩展vg0 vgextend -L 500M /dev/vg0/lv0 #将lv0扩展到500M #这时应扩大xfs文件系统到设备大小 xfs_growfs /dev/vg0/lv0 |
2、LVM的制作(针对xfs问价系统)
2.1 lv 扩大
1 2 3 4 5 | umount /mnt #将刚才的目录卸载 mkfs.ext4 /dev/vg0/lv0 #将其文件系统格式化成ext4文件系统 mount /dev/vg0/lv0 /mnt #重新挂载 lvextend -L 800M /dev/vg0/lv0 resize2fs /dev/vg0/lv0 |
2.2 lv缩小
1 2 3 4 5 | umount /mnt e2fsck -f /dev/vg0/lv0 resizefs /dev/vg0/lv0 200M mount /dev/vg0/lvo /mnt lvreduce -L 200 M /dev/vg0/lv0 |
2.3 vg扩大
1 2 | pvcreate /dev/vdb3 ##在pv中增加一块设备/dev/vdb3 vgextend vg0 /dev/vdb3 ##把新建立的/dev/vdb2添加到vg0中 |
2.4 vg缩减
1 2 3 4 | #在vg缩减时只能缩减没有被占用的设备 pvmove 设备1 设备2 ##把设备1上的所有数据移动到设备2上 vgreduce vg0 /dev/vdb1 ##把vg0中的/dev/vdb1删除 pvremove /dev/vdb1 ##移除设备/dev/vdb1 |
3、删除lvm
1 2 3 4 | umount /mnt lvremove /dev/vg0/lv0 vgremove vg0 pvremove /dev/vdb2 |
4.lvm的快照
快照作用:保护原始数据不被破坏
>>lvcreate -L 100M -n lv0backup -s /dev/vg0/lv0
实操:
1)在/westos下建立文件
注:此时/westos下挂载的是设备/dev/vg0/lv0
touch /westos/file{1..4}
ls /westos
2)取消/westos挂载
umount /westos
3)新建快照设备删除/westos下文件
lvcreate -L 100M -n lv0backup -s /dev/vg0/lv0##同时亦快照了文件系统,故不用对快照的逻辑卷格式化就可以进行挂载
mount /dev/vg0/lv0backup /westos
ls /westos
rm -rf file*
4)取消快照设备挂载
umount /westos
5)移除快照设备
lvremove /dev/vg0/lv0backup
6)新建快照设备lvcreate -L 100M -n lv0backup -s /dev/vg0/lv0
mount /dev/vg0/lv0backup /westos
7)查看/westos下文件
ls /westos
5. lvm的删除
lvremove /dev/vg0/lv0
vgremove vg0
pvremove /dev/vdb2
fdisk /dev/vdb
#当lvm的分区被误删除时#
当出现以下错误时:
1)
删除分区 /dev/vdb1 后,同步结果如下:[[email protected] ~]# partprobeError: Partition(s) 1 on /dev/vdb have been written, but we have been unable to inform the kernel of the change, probably because it/they are in use. As a result, the old partition(s) will remain in use. You should reboot now before making further changes. [[email protected] ~]# fdisk -l Device Boot Start End Blocks Id System /dev/vdb2 1026048 2050047 512000 8e Linux LVM [[email protected] ~]# blkid/dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs" /dev/vdb1: UUID="1vNDy0-kQyO-GMPX-OaQv-R5Ip-YPaG-kxdtUH" TYPE="LVM2_member" /dev/vdb2: UUID="8XOl5c-DuVJ-SCzS-NSND-Zf6h-Op0V-CIKcB7" TYPE="LVM2_member" [[email protected] ~]# pvremove /dev/vdb1 PV /dev/vdb1 belongs to Volume Group vg0 so please use vgreduce first. (If you are certain you need pvremove, then confirm by using --force twice.)说明分区 /dev/vdb1 正在被使用时,被删除了。
解决办法:
[[email protected] ~]# pvmove /dev/vdb1 /dev/vdb2 /dev/vdb1: Moved: 6.0% /dev/vdb1: Moved: 100.0%
2)
[[email protected] ~]# pvs PV 1vNDy0-kQyO-GMPX-OaQv-R5Ip-YPaG-kxdtUH not recognised. Is the device missing? PV 1vNDy0-kQyO-GMPX-OaQv-R5Ip-YPaG-kxdtUH not recognised. Is the device missing? PV 1vNDy0-kQyO-GMPX-OaQv-R5Ip-YPaG-kxdtUH not recognised. Is the device missing? PV VG Fmt Attr PSize PFree /dev/vdb2 vg0 lvm2 a-- 496.00m 296.00m unknown device vg0 lvm2 a-m 496.00m 496.00m [[email protected] ~]# vgs PV 1vNDy0-kQyO-GMPX-OaQv-R5Ip-YPaG-kxdtUH not recognised. Is the device missing? PV 1vNDy0-kQyO-GMPX-OaQv-R5Ip-YPaG-kxdtUH not recognised. Is the device missing? VG #PV #LV #SN Attr VSize VFree vg0 2 1 0 wz-pn- 992.00m 792.00m [[email protected] ~]# partprobe
本文出自 “13402758” 博客,请务必保留此出处http://13412758.blog.51cto.com/13402758/1980156
以上是关于Linux的LVM管理的主要内容,如果未能解决你的问题,请参考以下文章