首页 > 其他分享 >LVM-详解

LVM-详解

时间:2023-05-22 20:11:35浏览次数:28  
标签:卷组 LvTest dev 详解 LVM Servere root VgTest

目录

LVM 定义

逻辑卷管理器(Logical Volume Manager,LVM)。
LVM允许用户对硬盘资源进行动态调整。

LVM是Linux系统用于对硬盘分区进行管理的一种机制,其是在硬盘分区和文件系统之间添加了一个逻辑层,它提供了一个抽象的卷组,可以把多块硬盘进行卷组合并。

image

image

  • 物理卷处于LVM中的最底层,可以将其理解为物理硬盘、硬盘分区或者RAID磁盘阵列。
  • 卷组建立在物理卷之上,一个卷组能够包含多个物理卷,而且在卷组创建之后也可以继续向其中添加新的物理卷。
  • 逻辑卷是用卷组中空闲的资源建立的,并且逻辑卷在建立后可以动态地扩展或缩小空间。这就是LVM的核心理念。

常用命令合集

功能/命令 物理卷管理 卷组管理 逻辑卷管理
扫描 pvscan vgscan lvscan
建立 pvcreate vgcreate lvcreate
显示 pvdisplay vgdisplay lvdispaly
删除 pvremove vgremove lvremove
扩展 添加磁盘 vgextend lvextend
缩小 删除磁盘 vgreduce lvreduce

命令

vgcreate
示例:vgcreate [vg.name] [/dev/sd*][可添加多个] [参数]

参数 说明
-L 指定块大小

LVM实验

实验目标

创建 VgTest 卷组。
创建 LvTest 逻辑卷。
使用 /dev/sdb/dev/sdc 两个物理卷【大小分别为10G】。
挂载到 /mnt/LvTest/ 目录下,并写入配置文件。
使用 /dev/sdd1LVTest 扩容【/dev/sdc1 大小为 3G】。
逆向缩容,使/dev/sd{a,b,c}三块磁盘置空。

创建LV

创建物理卷和卷组
  • 通过 lsblk 查看当前磁盘信息
[root@Servere ~]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   40G  0 disk
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0   39G  0 part
  ├─cs-root 253:0    0 36.9G  0 lvm  /
  └─cs-swap 253:1    0  2.1G  0 lvm  [SWAP]
sdb           8:16   0   10G  0 disk
sdc           8:32   0   10G  0 disk
sdd           8:48   0   10G  0 disk

使用 pvcreate & vgcreate 分别创建物理卷和卷组

[root@Servere ~]# pvcreate /dev/sdb /dev/sdc
  Physical volume "/dev/sdb" successfully created.
  Physical volume "/dev/sdc" successfully created.

[root@Servere ~]# vgcreate  VgTest /dev/sdb /dev/sdc
  Volume group "VgTest" successfully created

使用 pvdisplay & vgdisplay 查看创建好的物理卷和卷组

[root@Servere ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sdb
  VG Name               VgTest
  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               j2RqkR-l7uZ-E4zZ-306C-lVXK-L38M-LEge20
  --- Physical volume ---
  PV Name               /dev/sdc
  VG Name               VgTest
  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               Wjps5a-fp9w-cHcr-0xTm-2hTw-UsHR-6sgnpE
[root@Servere ~]# vgdisplay
  --- Volume group ---
  VG Name               VgTest
  System ID
  Format                lvm2
  Metadata Areas        2
  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                2
  Act PV                2
  VG Size               19.99 GiB
  PE Size               4.00 MiB
  Total PE              5118
  Alloc PE / Size       0 / 0
  Free  PE / Size       5118 / 19.99 GiB
  VG UUID               2SYOGi-73dK-wisb-X6sk-kDxg-hSOv-m8rGsn

使用 pvscan & vgscan & lvscan 扫描创建好的物理卷和卷组

[root@Servere ~]# pvscan
  PV /dev/sdb    VG VgTest          lvm2 [<10.00 GiB / <10.00 GiB free]
  PV /dev/sdc    VG VgTest          lvm2 [<10.00 GiB / <10.00 GiB free]
  PV /dev/sda2   VG cs              lvm2 [<39.00 GiB / 0    free]
  Total: 3 [<58.99 GiB] / in use: 3 [<58.99 GiB] / in no VG: 0 [0]

[root@Servere ~]# vgscan
  Found volume group "VgTest" using metadata type lvm2
  Found volume group "cs" using metadata type lvm2

[root@Servere ~]# lvscan
  ACTIVE            '/dev/cs/swap' [2.08 GiB] inherit
  ACTIVE            '/dev/cs/root' [36.91 GiB] inherit
创建逻辑卷

使用 lvcreate 创建逻辑卷

[root@Servere ~]# lvcreate -l 100%FREE -n LvTest /dev/VgTest
  Logical volume "LvTest" created.

使用 lvscan & lvdisplay扫描和查看创建好的逻辑卷信息

[root@Servere ~]# lvscan
  ACTIVE            '/dev/VgTest/LvTest' [19.99 GiB] inherit
  ACTIVE            '/dev/cs/swap' [2.08 GiB] inherit
  ACTIVE            '/dev/cs/root' [36.91 GiB] inherit
[root@Servere ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/VgTest/LvTest
  LV Name                LvTest
  VG Name                VgTest
  LV UUID                iFsJX5-xJEm-g8S3-43KJ-etcs-F4VR-5hYlkS
  LV Write Access        read/write
  LV Creation host, time Servere.lab.example.com, 2023-05-22 09:51:43 +0800
  LV Status              available
  # open                 0
  LV Size                19.99 GiB
  Current LE             5118
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2
挂载
[root@Servere ~]# mkdir /mnt/LvTest
[root@Servere ~]# mount /dev/VgTest/LvTest /mnt/LvTest/
[root@Servere ~]# echo "/dev/mapper/VgTest-LvTest /mnt/LvTest/ ext4 defaults 0 0" >> /etc/fstab
[root@Servere ~]# mount -a
[root@Servere ~]# df -Th
Filesystem                Type      Size  Used Avail Use% Mounted on
/dev/mapper/cs-root       xfs        37G  2.1G   35G   6% /
/dev/mapper/VgTest-LvTest ext4       20G   45M   19G   1% /mnt/LvTest

扩容LV

注意事项:

  • umount卸载挂载点
  • 重新设置逻辑卷大小(lvresize)
  • 检测磁盘错误(e2fsck -f)
  • 更新逻辑卷信息(resize2fs)
分配逻辑卷组

物理卷 > 卷组 > 逻辑卷

[root@Servere ~]# fdisk /dev/sdd

Welcome to fdisk (util-linux 2.32.1).
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.
Created a new DOS disklabel with disk identifier 0xa9dbbfa0.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p):

Using default response p.
Partition number (1-4, default 1):
First sector (2048-20971519, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519): +3G # 为sdd1分配3G大小的容量

Created a new partition 1 of type 'Linux' and of size 3 GiB.

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e # 使用`8e`格式的
Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): p
Disk /dev/sdd: 10 GiB, 10737418240 bytes, 20971520 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
Disklabel type: dos
Disk identifier: 0xa9dbbfa0

Device     Boot Start     End Sectors Size Id Type
/dev/sdd1        2048 6293503 6291456   3G 8e Linux LVM

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
扩容卷组
  • vgextend 【vg_name】 【device】
[root@Servere ~]# df -Th
Filesystem                Type      Size  Used Avail Use% Mounted on
/dev/mapper/cs-root       xfs        37G  2.1G   35G   6% /
/dev/mapper/VgTest-LvTest ext4       20G   45M   19G   1% /mnt/LvTest
[root@Servere ~]# vgextend VgTest /dev/sdd1
  Physical volume "/dev/sdd1" successfully created.
  Volume group "VgTest" successfully extended
扩容逻辑卷
  • lvextend -l +100%FREE 【/dev/mapper/...】
[root@Servere ~]# lvextend /dev/mapper/VgTest-LvTest /dev/sdd1 -L +2.99G
  Rounding size to boundary between physical extents: 2.99 GiB.
  Size of logical volume VgTest/LvTest changed from 19.99 GiB (5118 extents) to 22.98 GiB (5884 extents).
  Logical volume VgTest/LvTest successfully resized.
检测磁盘错误
  • 需要解除挂载目录
  • 仅适用于 ext4 文件系统
[root@Servere ~]# umount /mnt/LvTest 
[root@Servere ~]# e2fsck -f /dev/mapper/VgTest-LvTest
e2fsck 1.45.6 (20-Mar-2020)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/mapper/VgTest-LvTest: 11/1966080 files (0.0% non-contiguous), 167442/7858176 blocks
扩容文件系统

方法一:

  • resize2fs -f 【/dev/mapper/...】
[root@Servere ~]# resize2fs /dev/mapper/VgTest-LvTest -f
resize2fs 1.45.6 (20-Mar-2020)
Filesystem at /dev/mapper/VgTest-LvTest is mounted on /mnt/LvTest; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 3
The filesystem on /dev/mapper/VgTest-LvTest is now 6025216 (4k) blocks long.

方法二:

  • /dev/sdd剩余空间添加到/dev/mapper/VgTest-LvTest内。
  • 直接使用lvextend-r参数。
[root@Servere ~]# lvextend -L +6.99G /dev/mapper/VgTest-LvTest /dev/sdd2 -r
  Rounding size to boundary between physical extents: 6.99 GiB.
  Size of logical volume VgTest/LvTest changed from 22.98 GiB (5884 extents) to <29.98 GiB (7674 extents).
  Logical volume VgTest/LvTest successfully resized.
resize2fs 1.45.6 (20-Mar-2020)
Filesystem at /dev/mapper/VgTest-LvTest is mounted on /mnt/LvTest; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 4
The filesystem on /dev/mapper/VgTest-LvTest is now 7858176 (4k) blocks long.

缩容

注意事项:

  • 卸载(umount)
  • 检测(e2fsck -f)
  • 更新逻辑卷信息大小(resize2fs)
  • 重置逻辑卷大小(lvresize)
卸载
[root@Servere ~]# umount /mnt/LvTest 
检测磁盘错误
[root@Servere ~]# e2fsck -f /dev/mapper/VgTest-LvTest 
e2fsck 1.45.6 (20-Mar-2020)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/mapper/VgTest-LvTest: 11/1966080 files (0.0% non-contiguous), 167442/7858176 blocks
更新逻辑卷信息
[root@Servere ~]# resize2fs /dev/mapper/VgTest-LvTest
resize2fs 1.45.6 (20-Mar-2020)
The filesystem is already 7858176 (4k) blocks long.  Nothing to do!
重置逻辑卷大小

逻辑卷 > 卷组 > 物理卷

[root@Servere ~]# lvremove /dev/mapper/VgTest-LvTest
Do you really want to remove active logical volume VgTest/LvTest? [y/n]: y
  Logical volume "LvTest" successfully removed.
重置卷组
[root@Servere ~]# vgremove VgTest
  Volume group "VgTest" successfully removed
重置物理卷
  • 通过 pvremove 删除创建的物理卷
[root@Servere ~]# pvremove /dev/sdd{1,2}
  Labels on physical volume "/dev/sdd1" successfully wiped.
  Labels on physical volume "/dev/sdd2" successfully wiped.
  • 通过 fdisk 删除创建的物理卷
[root@Servere ~]# fdisk /dev/sdd
Welcome to fdisk (util-linux 2.32.1).
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,2, default 2):

Partition 2 has been deleted.

Command (m for help): d

Selected partition 1
Partition 1 has been deleted.

Command (m for help): p

Disk /dev/sdd: 10 GiB, 10737418240 bytes, 20971520 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
Disklabel type: dos
Disk identifier: 0xa9dbbfa0

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

标签:卷组,LvTest,dev,详解,LVM,Servere,root,VgTest
From: https://www.cnblogs.com/Anzi-0524/p/17421617.html

相关文章

  • idea中jdk11用maven编译失败 Fatal error compiling: tools.jar not found: XX\Java
    ideamaven编译需要用到jdk的lib包里面的tools.jar文件,但是jdk1.8之后就没有tools.jar了。我这里用的是graalvm的jdk11,编译一直报错,网上也查不到。解决办法: 根据对应路径创建一个lib包,并把jdk1.8的lib下面的tools.jar复制一个放到这个包下面,让这个路径有这个包就行了。我......
  • iOS base64 编码详解
    iOSbase64编码详解iOS中将NSData转为base64编码时有NSDataBase64EncodingOptionsNSDataBase64EncodingOptions有四个选项/****************Base64Options****************/typedefNS_OPTIONS(NSUInteger,NSDataBase64EncodingOptions){//Usezerooron......
  • 信号量和互斥锁详解 非MarkDown版
    [参考链接1](http://blog.chinaunix.net/uid-24612247-id-2305050.html)详细说明:```信号量强调的是线程(或进程)间的同步:“信号量用在多线程多任务同步的,一个线程完成了某一个动作就通过信号量告诉别的线程,别的线程再进行某些动作(大家都在sem_wait的时候......
  • iOS 内购详解
    iOSApp内购SKProductsRequestDelegate不调用原因及解决办法之前代码是这样写的://此时request没有强指针指向它这样会导致SKProductsRequestDelegate不调用但不是每次都不调用,只是偶尔出现不调用的情况这个问题不好发现SKProductsRequest*request=[[SKProductsReq......
  • 信号量和互斥锁详解 MarkDown版
    信号量和互斥锁详解参考链接1参考链接2参考链接3参考链接4详细说明:信号量强调的是线程(或进程)间的同步:“信号量用在多线程多任务同步的,一个线程完成了某一个动作就通过信号量告诉别的线程,别的线程再进行某些动作(大家都在sem_wait的时候,就阻塞在那里)。当信号量为单值信号量是,也......
  • 线程条件变量pthread_cond_t和线程条件锁详解
    线程条件变量pthread_cond_t和线程条件锁详解参考文章条件变量常与互斥锁同时使用,达到线程同步的目的:条件变量通过允许线程阻塞和等待另一个线程发送信号的方法弥补了互斥锁的不足。APUE上,关于条件锁。其中有2条总结:1.使用条件锁前必须先锁住对应的互斥锁。2.条件锁进入阻塞(pthrea......
  • iOS ijkplayer 播放器 消息循环 详解
    iOSijkplayer 消息循环_mediaPlayer=ijkmp_ios_create(media_player_msg_loop);//开始消息循环intmedia_player_msg_loop(void*arg){@autoreleasepool{IjkMediaPlayer*mp=(IjkMediaPlayer*)arg;__weakIJKFFMoviePlayerController*ffpControll......
  • iOS Apple Development Document 详解
    iOS官方开发文档https://developer.apple.com/library/prerelease/ios/navigation/点击打开链接再次标记一下。......
  • ServletContext接口详解
    1. ServletContext是什么?  14 *  ServletContext是接口,是Servlet规范中的一员。2. ServletContext是谁实现的?14 *  Tomcat服务器(WEB服务器)实现了ServletContext接口。 *  public class org.apache.catalina.core.ApplicationContextFacade implements ServletCont......
  • Android ContentProvider详解
    一.Android四大组件Android四大组件是Activity,Service,ContentProvider,BroadcastReceiver。Activity作为程序界面,直接与用户交互Service运行在后台,没有界面,完成特定的功能ContentProvider维护应用数据,方便应用本身或其它应用访问BroadcastReceiver提供异步广播消息接收机制,......