LVM(Logical Volume Manager)

打印 上一主题 下一主题

主题 890|帖子 890|积分 2670

一. LVM概述

1. 什么是 LVM

LVM(Logical Volume Manager,逻辑卷管理器)是 Linux 体系下的一种 存储管理 机制,能够灵活地管理磁盘分区。它提供了一种比传统分区管理(如fdisk、parted)更高级的存储管理方式,允许动态调整存储空间,方便扩展和缩减分区,而不会影响已有数据。
2. LVM 主要概念

LVM 的核心概念类似于 RAID 或虚拟存储池,其主要组成部分如下:

  • PV(Physical Volume,物理卷)

    • LVM 的最底层单位,可以是整个硬盘,也可以是一个分区。
    • 通过 pvcreate 将物理磁盘或分区初始化为 LVM 可用的 PV。

  • VG(Volume Group,卷组)

    • 由多个 PV 组成的存储池,全部的存储空间都可以在 VG 内部动态分配。
    • 通过 vgcreate 创建 VG。

  • LV(Logical Volume,逻辑卷)

    • 相称于传统的“分区”,可以格式化文件体系(如ext4、xfs),然后挂载利用。
    • 通过 lvcreate 创建 LV,大小可以随时扩展或缩小(前提是文件体系支持)。

  • PE(Physical Extents,物理分块)

    • LVM 内部的最小存储单元,默认大小 4MB(可调整)。
    • VG 内的空间被划分成若干个 PE,LV 的大小由 PE 数目决定。
    1. ┌───────────────────────────────┐
    2. │        物理磁盘(sdb, sdc)     │
    3. │  (未初始化)                     │
    4. └───────────────────────────────┘
    5.          ↓ pvcreate
    6. ┌───────────────────────────────┐
    7. │        物理卷(PV)             │
    8. │  /dev/sdb   /dev/sdc          │
    9. └───────────────────────────────┘
    10.          ↓ vgcreate
    11. ┌───────────────────────────────┐
    12. │        卷组(VG)               │
    13. │  my_vg = PV1 + PV2             │
    14. │  (存储池)                       │
    15. └───────────────────────────────┘
    16.          ↓ lvcreate
    17. ┌───────────────────────────────┐
    18. │        逻辑卷(LV)             │
    19. │  /dev/my_vg/my_lv (50G)       │
    20. │  (类似于分区,可格式化挂载)        │
    21. └───────────────────────────────┘
    复制代码
3. LVM 的优势


  • 动态扩展与缩减存储:可在不停机的情况下扩展 LV 或 VG,而无需重新分区。
  • 跨多块磁盘管理:可以将多块磁盘归并为一个存储池(VG),提高存储利用率。
  • 快照(Snapshot):支持创建数据快照,方便数据备份与恢复。
  • 条带化存储(Striping):类似 RAID 0,提高磁盘 I/O 性能。
4. LVM 基本操作
  1. # 1. 创建物理卷
  2. pvcreate /dev/sdb /dev/sdc
  3. # 2. 创建卷组
  4. vgcreate my_vg /dev/sdb /dev/sdc
  5. # 3. 创建逻辑卷(比如创建 10GB 的逻辑卷)
  6. lvcreate -L 10G -n my_lv my_vg
  7. # 4. 格式化并挂载
  8. mkfs.ext4 /dev/my_vg/my_lv
  9. mkdir /mnt/lvm_mount
  10. mount /dev/my_vg/my_lv /mnt/lvm_mount
  11. # 5. 扩展逻辑卷
  12. lvextend -L +5G /dev/my_vg/my_lv   # 增加 5GB
  13. resize2fs /dev/my_vg/my_lv         # 扩展文件系统(ext4)
复制代码
二. LVM 实战操作

1. 环境准备


2. 对磁盘分区

2.1 利用 fdisk 对 /dev/sdc 举行分区
  1. # 1.进入 fdisk 交互模式
  2. [root@lvm:~]# fdisk /dev/sdc
  3. Welcome to fdisk (util-linux 2.37.2).
  4. Changes will remain in memory only, until you decide to write them.
  5. Be careful before using the write command.
  6. Device does not contain a recognized partition table.
  7. Created a new DOS disklabel with disk identifier 0x0313bb2e.
  8. # 2.查看磁盘信息
  9. Command (m for help): p
  10. Disk /dev/sdc: 300 GiB, 322122547200 bytes, 629145600 sectors
  11. Disk model: VMware Virtual S
  12. Units: sectors of 1 * 512 = 512 bytes
  13. Sector size (logical/physical): 512 bytes / 512 bytes
  14. I/O size (minimum/optimal): 512 bytes / 512 bytes
  15. Disklabel type: dos
  16. Disk identifier: 0x0313bb2e
  17. # 发现磁盘 /dev/sdc 没有有效的分区表,默认是 dos 分区表。
  18. # 3.创建 GPT 分区表
  19. Command (m for help): g
  20. Created a new GPT disklabel (GUID: F96B7C68-81FB-D14C-A17D-C686B732B937).
  21. # 4.创建第一个 100G 分区
  22. Command (m for help): n
  23. Partition number (1-128, default 1):        # 分区编号(默认 1)
  24. First sector (2048-629145566, default 2048):        # 起始扇区(默认 2048)
  25. Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-629145566, default 629145566): +100G
  26. Created a new partition 1 of type 'Linux filesystem' and of size 100 GiB.
  27. # 成功创建 /dev/sdc1 分区,大小 100G
  28. # 5.创建第二个 200G 分区
  29. Command (m for help): n
  30. Partition number (2-128, default 2):        # 分区编号(默认 2)
  31. First sector (209717248-629145566, default 209717248):        # 起始扇区(默认 209717248)
  32. Last sector, +/-sectors or +/-size{K,M,G,T,P} (209717248-629145566, default 629145566):
  33. Created a new partition 2 of type 'Linux filesystem' and of size 200 GiB.
  34. # 成功创建 /dev/sdc2 分区,大小 200G
  35. # 6.查看分区情况
  36. Command (m for help): p
  37. Disk /dev/sdc: 300 GiB, 322122547200 bytes, 629145600 sectors
  38. Disk model: VMware Virtual S
  39. Units: sectors of 1 * 512 = 512 bytes
  40. Sector size (logical/physical): 512 bytes / 512 bytes
  41. I/O size (minimum/optimal): 512 bytes / 512 bytes
  42. Disklabel type: gpt
  43. Disk identifier: F96B7C68-81FB-D14C-A17D-C686B732B937
  44. Device         Start       End   Sectors  Size Type
  45. /dev/sdc1       2048 209717247 209715200  100G Linux filesystem
  46. /dev/sdc2  209717248 629145566 419428319  200G Linux filesystem
  47. # 7.保存分区表并退出
  48. Command (m for help): w
  49. The partition table has been altered.
  50. Calling ioctl() to re-read partition table.
  51. Syncing disks.
  52. # 8.验证分区是否生效
  53. [root@lvm:~]# fdisk -l /dev/sdc
  54. Disk /dev/sdc: 300 GiB, 322122547200 bytes, 629145600 sectors
  55. Disk model: VMware Virtual S
  56. Units: sectors of 1 * 512 = 512 bytes
  57. Sector size (logical/physical): 512 bytes / 512 bytes
  58. I/O size (minimum/optimal): 512 bytes / 512 bytes
  59. Disklabel type: gpt
  60. Disk identifier: F96B7C68-81FB-D14C-A17D-C686B732B937
  61. Device         Start       End   Sectors  Size Type
  62. /dev/sdc1       2048 209717247 209715200  100G Linux filesystem
  63. /dev/sdc2  209717248 629145566 419428319  200G Linux filesystem
复制代码
2.2 对/dev/sde举行分区
  1. [root@lvm:~]# fdisk -l /dev/sde
  2. Disk /dev/sde: 1 TiB, 1099511627776 bytes, 2147483648 sectors
  3. Disk model: VMware Virtual S
  4. Units: sectors of 1 * 512 = 512 bytes
  5. Sector size (logical/physical): 512 bytes / 512 bytes
  6. I/O size (minimum/optimal): 512 bytes / 512 bytes
  7. [root@lvm:~]# fdisk /dev/sde
  8. Welcome to fdisk (util-linux 2.37.2).
  9. Changes will remain in memory only, until you decide to write them.
  10. Be careful before using the write command.
  11. Device does not contain a recognized partition table.
  12. Created a new DOS disklabel with disk identifier 0xf8cc9c96.
  13. Command (m for help): p
  14. Disk /dev/sde: 1 TiB, 1099511627776 bytes, 2147483648 sectors
  15. Disk model: VMware Virtual S
  16. Units: sectors of 1 * 512 = 512 bytes
  17. Sector size (logical/physical): 512 bytes / 512 bytes
  18. I/O size (minimum/optimal): 512 bytes / 512 bytes
  19. Disklabel type: dos
  20. Disk identifier: 0xf8cc9c96
  21. Command (m for help): g
  22. Created a new GPT disklabel (GUID: 7B0A9C2B-F3B1-3A42-9665-8640DFC613DC).
  23. Command (m for help): n
  24. Partition number (1-128, default 1):
  25. First sector (2048-2147483614, default 2048):
  26. Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-2147483614, default 2147483614): +500G
  27. Created a new partition 1 of type 'Linux filesystem' and of size 500 GiB.
  28. Command (m for help): n
  29. Partition number (2-128, default 2):
  30. First sector (1048578048-2147483614, default 1048578048):
  31. Last sector, +/-sectors or +/-size{K,M,G,T,P} (1048578048-2147483614, default 2147483614):
  32. Created a new partition 2 of type 'Linux filesystem' and of size 524 GiB.
  33. Command (m for help): w
  34. The partition table has been altered.
  35. Calling ioctl() to re-read partition table.
  36. Syncing disks.
  37. [root@lvm:~]# fdisk -l /dev/sde
  38. Disk /dev/sde: 1 TiB, 1099511627776 bytes, 2147483648 sectors
  39. Disk model: VMware Virtual S
  40. Units: sectors of 1 * 512 = 512 bytes
  41. Sector size (logical/physical): 512 bytes / 512 bytes
  42. I/O size (minimum/optimal): 512 bytes / 512 bytes
  43. Disklabel type: gpt
  44. Disk identifier: 7B0A9C2B-F3B1-3A42-9665-8640DFC613DC
  45. Device          Start        End    Sectors  Size Type
  46. /dev/sde1        2048 1048578047 1048576000  500G Linux filesystem
  47. /dev/sde2  1048578048 2147483614 1098905567  524G Linux filesystem
复制代码
2.3 检查磁盘分区情况


3. 创建pv

3.1 检察现有的pv列表

[code][root@lvm:~]# pvs  PV         VG        Fmt  Attr PSize   PFree  /dev/sda3  ubuntu-vg lvm2 a--

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

乌市泽哥

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表