Linux Admin 简明教程

Linux Admin - Volume Management

Logical Volume Management (LVM) 是 Linux 用来跨不同物理硬盘管理存储卷的方法。不要将其与 RAID 混淆。但它可以被认为与 RAID 0 或 JBOD 类似的概念。利用 LVM,我们有可能(例如)拥有三个 1TB 的物理硬盘,然后一个大约 3TB 的逻辑卷,例如 /dev/sdb。甚至是两个 1.5TB 的逻辑卷,5 个 500GB 的卷,或者任何组合。单个硬盘还可用于逻辑卷的快照。

Logical Volume Management (LVM) is a method used by Linux to manage storage volumes across different physical hard disks. This is not to be confused with RAID. However, it can be thought of in a similar concept as RAID 0 or J-Bod. With LVM, it is possible to have (for example) three physical disks of 1TB each, then a logical volume of around 3TB such as /dev/sdb. Or even two logical volumes of 1.5TB, 5 volumes of 500GB, or any combination. One single disk can even be used for snapshots of Logical Volumes.

Note − 正确配置时,使用逻辑卷实际上会增加磁盘 I/O。这与在不同的硬盘中分配 RAID 0 条带数据的工作方式类似。

Note − Using Logical Volumes actually increases disk I/O when configured correctly. This works in a similar fashion to RAID 0 striping data across separate disks.

在通过 LVM 学习卷管理时,了解 LVM 中的每个组件会更容易。请学习下表,以牢固掌握每个组件。如有需要,请使用 Google 进行学习。了解逻辑卷的每个部分对于管理它们非常重要。

When learning about volume management with LVM, it is easier if we know what each component in LVM is. Please study the following table to get a firm grasp of each component. If you need to, use Google to study. Understanding each piece of a logical volume is important to manage them.

PV

Physical Volume

sda

PP

Physical Partition

sda1 , sda2

VG

Volume Group

Pooled physical resources

LV

Logical Volume

Seen as a storage facility to the operating system

physical volume 将被视为 /dev/sda、/dev/sdb;由 Linux 检测到的物理硬盘。

A physical volume will be seen as /dev/sda, /dev/sdb; a physical disk that is detected by Linux.

physical partition 将成为磁盘实用工具(比如 fdisk)分区的磁盘部分。记住,在最常见的 LVM 设置中不建议使用物理分区。示例:磁盘 /dev/sda 被分区为包括两个物理分区:/dev/sda1 和 /dev/sda2

A physical partition will be a section of the disk partitioned by a disk utility such as fdisk. Keep in mind, physical partition is not recommended in most common LVM setups. Example: disk /dev/sda is partitioned to include two physical partitions: /dev/sda1 and /dev/sda1

如果我们有两块各 1TB 的物理磁盘,我们可以在两者之间创建一个将近 2TB 的卷组。

If we have two physical disks of 1TB each, we can create a volume group of almost 2TB amongst the two.

从卷组中,我们可以创建三个任意大小的逻辑卷,不超过总卷组大小。

From the volume group, we can create three logical volumes each of any-size not exceeding the total volume group size.

Traditional Linux Disk Administration Tools

在了解 CentOS 7 中 LVM 管理最新最出色的工具之前,我们应该先探索用于 Linux 磁盘管理的更多传统工具。这些工具非常实用,现在仍可与高级 LVM 工具(如 System Storage Manager)配合使用:lsblk、parted 和 mkfs.xfs。

Before being acquainted with the latest and greatest featured tools for LVM Management in CentOS 7, we should first explore more traditional tools that have been used for Linux disk management. These tools will come handy and still have use with today’s advanced LVM tools such as the System Storage Manager: lsblk, parted, and mkfs.xfs.

现在,假设我们系统中又添加了一块或两块磁盘,我们需要列出 Linux 检测到的磁盘。我建议在执行被认为具有破坏性的操作前每次都列出磁盘。 lsblk 是获取磁盘信息的出色工具。让我们看看 CentOS 检测到哪些磁盘。

Now, assuming we have added another disk or two to our system, we need to enumerate disks detected by Linux. I’d always advise enumerating disks every time before performing operations considered as destructive. lsblk is a great tool for getting disk information. Let’s see what disks CentOS detects.

[root@localhost rdc]# lsblk
NAME         MAJ:MIN    RM    SIZE    RO    TYPE MOUNTPOINT
sda            8:0       0     20G     0        disk
├─sda1         8:1       0      1G     0     part /boot
└─sda2         8:2       0     19G     0        part
  ├─cl-root  253:0       0     17G     0      lvm  /
  └─cl-swap  253:1       0      2G     0      lvm  [SWAP]
    sdb       8:16       0      6G     0       disk
    sdc       8:32       0      4G     0       disk
    sr0       11:0       1   1024M     0       rom

如你所见,此系统上有三块磁盘:sda、sdb 和 sdc。

As you can see, we have three disks on this system: sda, sdb, and sdc.

磁盘 sda 包含我们正在运行的 CentOS 安装,所以我们不希望随意更改 sda。sdb 和 sdc 在本教程中都添加到了系统中。让我们让 CentOS 能够使用这些磁盘。

Disk sda contains our working CentOS installation, so we do not want to toy around with sda. Both sdb and sdc were added to the system for this tutorial. Let’s make these disks usable to CentOS.

Create a Disk Label

[root@localhost rdc]# parted /dev/sdb mklabel GPT
Warning: The existing disk label on /dev/sdb will be destroyed and all data on this
   disk will be lost. Do you want to continue?
Yes/No? Yes
[root@localhost rdc]#

我们现在有一个带有标签的磁盘。只需按照相同的方式在 sdc 上运行 parted 命令。

We now have one disk labeled. Simply run the parted command in the same manner on sdc.

Create the Partitions on the Disk

我们仅在每个磁盘上创建一个分区。要创建分区,再次使用 parted 命令。

We will only create a single partition on each disk. To create partitions, the parted command is used again.

[root@localhost rdc]# parted -a opt /dev/sdb mkpart primary ext4 0% 100%

Warning − 您从 0.00B 到 6442MB(扇区 0..12582911)请求一个分区。

Warning − You requested a partition from 0.00B to 6442MB (sectors 0..12582911).

我们能够管理的最接近位置是从 17.4kB 到 1048kB(扇区 34..2047)。

The closest location we can manage is 17.4kB to 1048kB (sectors 34..2047).

您是否仍可接受?

Is this still acceptable to you?

是/否?否

Yes/No? NO

[root@localhost rdc]# parted -a opt /dev/sdc mkpart primary ext4 0% 100%

Information − 您可能需要更新 /etc/fstab。

Information − You may need to update /etc/fstab.

[root@localhost rdc]# lsblk
NAME        MAJ:MIN   RM    SIZE    RO    TYPE MOUNTPOINT
sda           8:0      0     20G     0        disk
├─sda1        8:1      0      1G     0      part / boot
└─sda2        8:2      0     19G     0        part
 ├─cl-root  253:0      0     17G     0       lvm  /
 └─cl-swap  253:1      0      2G     0       lvm  [SWAP]
sdb          8:16      0      6G     0        disk
└─sdb1       8:17      0      6G     0        part
 sdc         8:32      0      4G     0        disk
└─sdc1       8:33      0      4G     0        part
sr0          11:0      1   1024M     0        rom

[root@localhost rdc]#

正如从 lsblk 输出中看到的那样,我们现在有两个分区,分别在 sdb 和 sdc 上。

As you can see from lsblk output, we now have two partitions, each on sdb and sdc.

Make the File System

最后,在挂载和使用任何卷之前,我们需要添加文件系统。我们将使用 XFS 文件系统。

Finally, before mounting and using any volume we need to add a file system. We will be using the XFS file system.

root@localhost rdc]# mkfs.xfs -f /dev/sdb1
meta-data = /dev/sdb1               isize = 512    agcount = 4, agsize = 393088 blks
            =                      sectsz = 512    attr = 2, projid32bit = 1
            =                         crc = 1      finobt = 0, sparse = 0
data        =                       bsize = 4096   blocks = 1572352, 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@localhost rdc]# mkfs.xfs -f /dev/sdc1
meta-data   = /dev/sdc1             isize = 512    agcount = 4, agsize = 262016 blks
            =                      sectsz = 512    attr = 2, projid32bit = 1
            =                         crc = 1      finobt = 0, sparse = 0
data        =                       bsize = 4096   blocks = 1048064, 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@localhost rdc]#

让我们检查一下确保每个分区都有一个可用的文件系统。

Let’s check to make sure each have a usable file system.

[root@localhost rdc]# lsblk -o NAME,FSTYPE
NAME           FSTYPE
sda
├─sda1         xfs
└─sda2         LVM2_member
 ├─cl-root     xfs
 └─cl-swap     swap
sdb
└─sdb1         xfs
sdc
└─sdc1         xfs
sr0

[root@localhost rdc]#

每个分区现在都在使用 XFS 文件系统。让我们将它们挂载、检查挂载并复制一个文件到每个分区。

Each is now using the XFS file system. Let’s mount them, check the mount, and copy a file to each.

[root@localhost rdc]# mount -o defaults /dev/sdb1 /mnt/sdb
[root@localhost rdc]# mount -o defaults /dev/sdc1 /mnt/sdc

[root@localhost ~]# touch /mnt/sdb/myFile /mnt/sdc/myFile
[root@localhost ~]# ls /mnt/sdb /mnt/sdc
 /mnt/sdb:
  myFile

 /mnt/sdc:
  myFile

我们在此时有两个可用的磁盘。但是,只有在手动挂载它们时它们才可用。若要在启动时挂载它们,我们必须编辑 fstab 文件。此外,必须为需要访问新磁盘的组设置权限。

We have two usable disks at this point. However, they will only be usable when we mount them manually. To mount each on boot, we must edit the fstab file. Also, permissions must be set for groups needing access to the new disks.

Create Volume Groups and Logical Volumes

CentOS 7 中最重要的新增功能之一是实用程序 System Storage Manager 或 ssm。 System Storage Manager 大大简化了 Linux 上的 LVM 池和存储卷的管理过程。

One of the greatest addition to CentOS 7 was the inclusion of a utility called System Storage Manager or ssm. System Storage Manager greatly simplifies the process of managing LVM pools and storage volumes on Linux.

我们来一步步了解在 CentOS 中创建简单卷池和逻辑卷的过程。第一步是安装 System Storage Manager。

We will go through the process of creating a simple volume pool and logical volumes in CentOS. The first step is installing the System Storage Manager.

[root@localhost rdc]# yum  install system-storage-manager

让我们使用 ssm list 命令查看我们的磁盘。

Let’s look at our disks using the ssm list command.

ssm list command

如上所示,系统上共安装了三块磁盘。

As seen above, a total of three disks are installed on the system.

  1. /sdba1 − Hosts our CentOS installation

  2. /sdb1 − Mounted at /mnt/sdb

  3. /sdc1 − Mounted at /mnt/sdc

我们要做的便是使用两块磁盘 (sdb 和 sdc) 创建卷组。然后为系统提供三个 3GB 逻辑卷。

What we want to do is make a Volume Group using two disks (sdb and sdc). Then make three 3GB Logical Volumes available to the system.

让我们创建我们的卷组。

Let’s create our Volume Group.

[root@localhost rdc]# ssm create -p NEW_POOL /dev/sdb1 /dev/sdc1

默认情况下,ssm 将创建一个单个逻辑卷,扩展整个 10GB 的池。我们不希望这样,所以让我们删除此内容。

By default, ssm will create a single logical volume extending the entire 10GB of the pool. We don’t want this, so let’s remove this.

[root@localhost rdc]# ssm remove /dev/NEW_POOL/lvol001
 Do you really want to remove active logical volume NEW_POOL/lvol001? [y/n]: y
 Logical volume "lvol001" successfully removed
[root@localhost rdc]#

最后,我们来创建三个逻辑卷。

Finally, let’s create the three Logical Volumes.

[root@localhost rdc]# ssm create -n disk001 --fs xfs -s 3GB -p NEW_POOL
[root@localhost rdc]# ssm create -n disk002 --fs xfs -s 3GB -p NEW_POOL
[root@localhost rdc]# ssm create -n disk003 --fs xfs -s 3GB -p NEW_POOL

现在,让我们检查我们的新卷。

Now, let’s check our new volumes.

volumes

现在我们有了跨越两个物理磁盘分区的三块独立逻辑卷。

We now have three separate logical volumes spanned across two physical disk partitions.

逻辑卷是一项现在内置于 CentOS Linux 中的强大功能。我们已经接触到了这些管理内容的皮毛。精通池和逻辑卷需要反复练习和教程中心的扩展学习。现在,你已经了解 CentOS 中 LVM 管理的基础知识,并具备在单一主机上创建基本条带逻辑卷的能力。

Logical volumes are a powerful feature now built into CentOS Linux. We have touched the surface on managing these. Mastering pools and logical volumes come with practice and extended learning from Tutorials Point. For now, you have learned the basics of LVM management in CentOS and possess the ability to create basic striped Logical Volumes on a single host.