为新磁盘创建新的物理卷

要创建PV,我们将使用现在熟悉的pvcreate命令:

[root@centos /]# pvcreate /dev/sdc1
  Writing physical volume data to disk "/dev/sdc1"
  Physical volume "/dev/sdc1" successfully created

检查我们的PV和VG

我们可以通过执行pvsvgs命令来显示更改:

[root@centos ~]# pvs
  PV         VG          Fmt  Attr PSize PFree
  /dev/sda2  vg_centos lvm2 a--  7.51g    0
  /dev/sdb1  vg01        lvm2 a--  2.00g 2.00g
[root@centos ~]# vgs
  VG          #PV #LV #SN Attr   VSize VFree
  vg01          1   0   0 wz--n- 2.00g 2.00g
  vg_centos   1   2   0 wz--n- 7.51g    0

从上面我们可以看到,/ dev / sdb1现在与卷组vg01相关联。我们还可以看到,只有1PV与卷组关联。

创建物理卷(PV)

下一步是创建物理卷。为此,我们使用pvcreate命令。

[root@centos ~]# pvcreate /dev/sdb1
  Writing physical volume data to disk "/dev/sdb1"
  Physical volume "/dev/sdb1" successfully created

创建一个大文件

在本练习中,我们将创建一个大小为1GB的文件。创建大文件的最简单方法之一是使用dd命令:

[root@centos myspace]# dd if=/dev/zero of=/var/myspace/big_file.img bs=1G count=1

[root@centos myspace]# ls -lh big*
-rw-r--r--. 1 root root 1.0G Apr 12 08:50 big_file.img

现在,我们有了一个名为big_file.img的文件,其大小为1GB。创建此文件之后,现在我们可以从以下df -h命令中看到我们已经使用了56%的可用空间!

[root@centos myspace]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_centos-lv_root
                      6.0G  4.7G  1.1G  82% /
tmpfs                 250M     0  250M   0% /dev/shm
/dev/sda1             485M   38M  422M   9% /boot
/dev/mapper/vg01-lvdata01
                      2.0G  1.1G  850M  56% /var/myspace

添加另一个磁盘

在我们的练习中,我们现在将添加一个额外的磁盘以增加可用空间量。该过程将与第一个磁盘相同。在添加下一个磁盘之前,我们将使用umount命令卸载文件系统:

[root@centos myspace]# umount /var/myspace

我们要添加的磁盘是/ dev / sdc。我们可以从命令fdisk -l再次查看

Disk /dev/sdc: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

准备我们的新磁盘以用于LVM

[root@centos /]# fdisk /dev/sdc
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xdd63f450.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content wont be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. Its strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-522, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-522, default 522):
Using default value 522

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

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

创建具有所有可用空间的逻辑卷

用于创建逻辑卷的命令是lvcreate。在我们的示例中,将逻辑卷命名为lvdata01,并指定将使用卷组vg01中100%的可用空间。

[root@centos ~]# lvcreate -n lvdata01 -l 100%VG vg01
  Logical volume "lvdata01" created
逻辑卷管理器(LVM)示例

为我们的文件系统创建挂载点

在此示例中,我们将创建一个名为/ var / myspace的目录。这将成为我们的挂载点。用于创建目录的命令是mkdir。用于挂载文件系统的命令是mount

[root@centos ~]# mkdir /var/myspace

[root@centos ~]# mount /dev/vg01/lvdata01 /var/myspace

挂载文件系统后,我们可以通过执行命令df -h轻松地进行检查。

[root@centos ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_centos-lv_root
                      6.0G  4.7G  1.1G  82% /
tmpfs                 250M     0  250M   0% /dev/shm
/dev/sda1             485M   38M  422M   9% /boot
/dev/mapper/vg01-lvdata01
                      2.0G   36M  1.9G   2% /var/myspace

现在,从上面的输出中我们可以看到我们的文件系统已安装并且具有1.9GB的可用空间。

从卷组中删除/ dev / sdb1

本练习的下一步是使用vgreduce命令从卷组vg01中删除/ dev / sdb1

[root@centos /]# vgreduce vg01 /dev/sdb1
  Removed "/dev/sdb1" from volume group "vg01"

将条目添加到装载表

为了使文件系统在引导时自动挂载,它需要具有一个在挂载表中定义的条目。为了使文件系统自动挂载,我们需要在文件/ etc/fstab中添加以下行:

/dev/vg01/lvdata01      /var/myspace            ext3    defaults        0 0

添加该行后,您可以通过执行命令mount -a来测试语法。/ etc/fstab中的所有条目都将被挂载。

pvmove命令-将数据从一个磁盘移动到另一个磁盘

现在,我们将使用pvmove命令将数据从/ dev / sdb1移至更大的区域/ dev / sdc1 :

[root@centos /]# pvmove /dev/sdb1 /dev/sdc1
  /dev/sdb1: Moved: 0.4%
  /dev/sdb1: Moved: 16.8%
  /dev/sdb1: Moved: 34.6%
  /dev/sdb1: Moved: 51.1%
  /dev/sdb1: Moved: 75.0%
  /dev/sdb1: Moved: 100.0%

删除物理卷

下一步是使用` pvremove命令从LVM中删除/ dev / sdb1 :

[root@centos /]# pvremove /dev/sdb1
  Labels on physical volume "/dev/sdb1" successfully wiped

将新磁盘添加到现有卷组

现在,我们执行vgextend命令,将/ dev / sdc1添加到我们现有的卷组vg01

[root@centos /]# vgextend vg01 /dev/sdc1
  Volume group "vg01" successfully extended

创建一个文件系统

要创建文件系统,我们将使用mkfs.ext3。这将创建类型为ext3的文件系统。

[root@centos ~]# mkfs.ext3 /dev/vg01/lvdata01
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
130816 inodes, 523264 blocks
26163 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8176 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912

Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 33 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override. 

检查PV和LV

如果现在执行命令lvsvgs,我们可以检查配置的当前状态:

[root@centos ~]# lvs
  LV       VG          Attr     LSize Pool Oroirn Data%  Move Log Copy%  Convert
  lvdata01 vg01        -wi-a--- 2.00g
  lv_root  vg_centos -wi-ao-- 6.04g
  lv_swap  vg_centos -wi-ao-- 1.47g
[root@centos ~]# vgs
  VG          #PV #LV #SN Attr   VSize VFree
  vg01          1   1   0 wz--n- 2.00g    0
  vg_centos   1   2   0 wz--n- 7.51g    0

从上面我们可以看到我们的逻辑卷的大小为2GB,并且与卷组vg01相关联

使用fdisk创建分区

首先,我们需要创建一个用于LVM的分区。要启动交互式对话,我们执行命令fdisk / dev / sdb

然后,我们为新分区选择n,为主分区选择p。接下来我们接受默认分区号和默认大小值。我们选择t来设置我们的系统分区。选择选项8e,因为它适用于LVM。完成后,我们通过选择w来编写更改。

[root@centos ~]# fdisk /dev/sdb

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x55112c08.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won\'t be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It\'s strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-261, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-261, default 261):
Using default value 261

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

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

创建卷组(VG)

要创建卷组,我们使用命令vgcreate

[root@centos ~]# vgcreate vg01 /dev/sdb1
  Volume group "vg01" successfully created

使用LVM添加和删除磁盘-示例03

在此示例中,我们将介绍向LVM添加磁盘的基本过程。然后,我们将添加第二个较大的磁盘,并将数据从较小的磁盘移至较大的磁盘。两个磁盘已添加到我们的虚拟环境中,可以从下面的fdisk -l命令中看到。

[root@centos ~]# fdisk -l

Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/sdc: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

最初,我们只使用较小的磁盘/ dev / sdb(2147 MB​​)。

扩展逻辑卷和文件系统

要使用新添加的空间,我们需要扩展逻辑卷/ dev / vg01 / lvdata01和文件系统。该任务的命令是` lvextend。注意,我们将使用-r选项自动调整文件系统的大小:

[root@centos /]# lvextend /dev/vg01/lvdata01 /dev/sdc1 -r
fsck from util-linux-ng 2.17.2
/dev/mapper/vg01-lvdata01: 12/130816 files (0.0% non-contiguous), 279590/523264 blocks
  Extending logical volume lvdata01 to 4.00 GiB
  Logical volume lvdata01 successfully resized
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/mapper/vg01-lvdata01 to 1047552 (4k) blocks.
The filesystem on /dev/mapper/vg01-lvdata01 is now 1047552 blocks long.

最后...。再次挂载我们的文件系统

[root@centos /]# mount /dev/vg01/lvdata01 /var/myspace
[root@centos /]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_centos-lv_root
                      6.0G  4.7G  1.1G  82% /
tmpfs                 250M     0  250M   0% /dev/shm
/dev/sda1             485M   38M  422M   9% /boot
/dev/mapper/vg01-lvdata01
                      4.0G  1.1G  2.7G  28% /var/myspace

现在这一次,当我们执行df -h命令时,我们可以看到我们现在仅利用了可用空间的28%。现在,我们的卷组的大小为4GB。我们有big_file.img

[root@centos myspace]# ls -l /var/myspace
total 1049620
-rw-r--r--. 1 root root 1073741824 Apr 12 08:50 big_file.img
日期:2019-04-29 03:17:37 来源:oir作者:oir