如何在 Linux 上创建 LVM 逻辑卷如何在 Linux 上创建 LVM 逻辑卷如何在 Linux 上创建 LVM 逻辑卷如何在 Linux 上创建 LVM 逻辑卷
  • 业务
  • 产品
  • 技术
  • 行业
  • 目标
  • 支持
  • 关于
  • 联系我们
  • 登录
✕

如何在 Linux 上创建 LVM 逻辑卷

发表 admin at 2025年2月28日
类别
  • Linux
标签

存储是服务器不可或缺的重要组件之一,因此无论如何都需要坚决关注。这是关于如何在 Linux 服务器或工作站上实施 LVM 的简要指南。感谢您的访问,我们希望这对您的任务/项目有所帮助。

“失败是给成功增添风味的调味品。 ”——杜鲁门·卡波特

在 Linux 上设置 LVM 存储

我敢打赌您以前听说过甚至更好地使用过 LVM。逻辑卷管理 (LVM) 技术简化了存储管理的方式。与旧的分区模式相比,LVM 实现存储虚拟化,并为系统管理员提供了更灵活的磁盘存储管理方式。逻辑卷管理的工作原理是将物理卷 (PV) 划分为物理盘区 (PE),然后将其映射到逻辑盘区 (LE)。

此后逻辑盘区被分组为卷组(VG)。正如您所猜测的,这些生成的卷组被组合成充当上述虚拟磁盘分区的逻辑卷(LV)。 LVM 使得在需要时调整存储卷大小和移动存储卷变得非常简单。

了解了这一点后,现在让我们深入了解 LVM 的设置。我将使用闪存驱动器,但任何其他驱动器或设备(硬盘驱动器等)的过程都是相同的。

步骤一:识别磁盘设备

使用 fdisk 列出可用的设备和分区。从输出中可以看到,有一个标记为 /dev/sdb 的物理设备。

$ sudo fdisk -l 
Disk /dev/sda: 119.2 GiB, 128035676160 bytes, 250069680 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: 0xa3bc85b8

Device     Boot    Start       End   Sectors  Size Id Type
/dev/sda1       62517248 250067789 187550542 89.4G 83 Linux
/dev/sda2           2048  62517247  62515200 29.8G 83 Linux

Partition table entries are not in disk order.

Disk /dev/sdb: 14.9 GiB, 15938355200 bytes, 31129600 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: 0x2a4e70c2

Device     Boot Start      End  Sectors  Size Id Type
/dev/sdb1        8192 31129599 31121408 14.9G  c W95 FAT32 (LBA)

步骤2:为LVM设备创建分区

通过 fdisk、parted 或 gdisk 格式化物理设备,准备物理设备。我们将使用fdisk。首先,我们将删除现有分区并创建新分区。

$ sudo fdisk /dev/sdb                                                                                  
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): n
Partition type
   p   primary (2 primary, 0 extended, 2 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-31129599, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-31129599, default 31129599): +7G

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

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 
First sector (14682112-31129599, default 14682112): 
Last sector, +sectors or +size{K,M,G,T,P} (14682112-31129599, default 31129599): 

Created a new partition 2 of type 'Linux' and of size 7.9 GiB.

Command (m for help): t
Partition number (1,2, default 2): 2
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.
Failed to remove partition 1 from system: Device or resource busy
Failed to add partition 1 to system: Device or resource busy
Failed to add partition 2 to system: Device or resource busy

The kernel still uses the old partitions. The new table will be used at the next reboot. 
Syncing disks.

通过输入 fdisk -l 确认 LVM 分区,如下所示:

$ sudo fdisk -l 
[sudo] password for penchant: 
Disk /dev/sda: 119.2 GiB, 128035676160 bytes, 250069680 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: 0xa3bc85b8

Device     Boot    Start       End   Sectors  Size Id Type
/dev/sda1       62517248 250067789 187550542 89.4G 83 Linux
/dev/sda2           2048  62517247  62515200 29.8G 83 Linux

Partition table entries are not in disk order.


Disk /dev/sdb: 14.9 GiB, 15938355200 bytes, 31129600 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: 0x2a4e70c2

Device     Boot Start      End  Sectors  Size Id Type
/dev/sdb1           2048 14682111 14680064    7G 8e Linux LVM
/dev/sdb2       14682112 31129599 16447488  7.9G 8e Linux LVM


Partition table entries are not in disk order.

步骤3:创建物理卷

使用 pvcreate 创建物理卷

$ sudo pvcreate /dev/sdb1 /dev/sdb2
  Physical volume "/dev/sdb1" successfully created.
  Physical volume "/dev/sdb2" successfully created.

使用 pvdisplay 命令确认物理卷

$ sudo pvdisplay
  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               tech
  PV Size               7.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              1791
  Free PE               214
  Allocated PE          1577
  PV UUID               cGnGfI-oVG7-9CcY-kdmK-aR4R-iZY9-O9gD0g
   
  --- Physical volume ---
  PV Name               /dev/sdb2
  VG Name               tech
  PV Size               7.84 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              2007
  Free PE               0
  Allocated PE          2007
  PV UUID               UvewNB-Z2d1-T3L1-c92C-rOLa-lcrg-19zuPk

步骤4:创建卷组

使用 vgcreate 创建卷组并使用您选择的名称。我将使用“技术”

$ sudo vgcreate tech /dev/sdb1 /dev/sdb2                                                                      
  Volume group "tech" successfully created

步骤5:创建逻辑卷

使用 lvcreate 以及一些选项和开关创建一个具有您选择的名称和大小的逻辑卷,如下所示。
选项 -n 用于指定逻辑卷的名称逻辑卷
选项-L指定大小。它可以以 MiB(兆字节)为单位,也可以以 GiB(千兆字节)为单位。

$ sudo lvcreate -n part1 -L 14GiB tech
  Logical volume "part1" created.

发出上述命令后,将创建一个名为 /dev/tech/part1 的设备。您可以通过调用 lvdisplay 命令来确认这一点。但是,该设备没有文件系统。

$ sudo lvdisplay
  --- Logical volume ---
  LV Path                /dev/tech/part1 
  LV Name                part1
  VG Name                tech
  LV UUID                O1qtcJ-dDAj-gPoL-nZn0-VUMs-rVwe-f31OHq
  LV Write Access        read/write
  LV Creation host, time computing-pc, 2018-10-14 00:39:25 +0300
  LV Status              available
  # open                 0
  LV Size                14.00 GiB
  Current LE             3584
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:0

您还可以使用 vgdisplay 查看卷组

步骤 6:在逻辑卷上创建文件系统

将您选择的文件系统加载到创建的逻辑卷中。让我们加载 xfs 文件系统。您可以在这里根据需要加载 ext3、ext4、brtfs 等。

$ sudo mkfs -t xfs /dev/tech/part1
meta-data=/dev/tech/part1        isize=512    agcount=4, agsize=917504 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=0
data     =                       bsize=4096   blocks=3670016, 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

完成后,我们已经成功创建了一个带有 xfs 文件系统的逻辑卷 /dev/tech/part1。干杯,感谢您访问我们的网站。请继续关注更多指南和教程。

更多指南:

  • 在 TrueNAS 上配置 FTP、SFTP 和 S3 存储服务
  • 安装和配置 OpenMediaVault NAS 存储服务器
  • 在 Docker 容器中运行 Filerun Storage Sync Server
©2015-2025 Norria Group support@norria.com