逻辑卷管理LVM命令示例02

在此示例中,我们将创建一个lookback设备以用作磁盘。为此,我们将使用losetup命令。Losetup用于将lookback设备与常规文件或块设备相关联。我们正在使用这种方法,因此,如果您不在使用可以添加磁盘的虚拟环境,则仍然可以尝试LVM命令。

创建物理卷(PV)

现在,我们将使用pvcreate命令来创建物理卷:

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

查看物理卷

[root@centos tmp]# pvs
  PV         VG          Fmt  Attr PSize PFree
  /dev/loop0             lvm2 a--  3.00g 3.00g
  /dev/sda2  vg_centos01 lvm2 a--  7.51g    0

上面的PV表示路径,VG表示我们的PV所属的卷组。PSize和PFree指示此PV可用的空间量。

可以使用pvdisplay命令获取更多信息:

[root@centos tmp]# pvdisplay

 "/dev/loop0" is a new physical volume of "3.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/loop0
  VG Name
  PV Size               3.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               QAQyHk-nXFh-QV1k-TAV0-WoW7-6kiS-OFxwac

创建一个挂载点

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

减少lvm文件系统空间

为了减少可用空间,我们需要使用lvreduce命令。

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

[root@centos var]# lvreduce /dev/vg01/lvdata01 -r -L 2G
fsck from util-linux-ng 2.17.2
/dev/mapper/vg01-lvdata01: 11/163840 files (0.0% non-contiguous), 27461/655360 blocks
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/mapper/vg01-lvdata01 to 524288 (4k) blocks.
The filesystem on /dev/mapper/vg01-lvdata01 is now 524288 blocks long.

  Reducing logical volume lvdata01 to 2.00 GiB
  Logical volume lvdata01 successfully resized

配置loopback 设备

要将我们的镜像文件与一个环回设备相关联,我们使用命令losetup

[root@centos tmp]# losetup /dev/loop0 /tmp/disk1.img
[root@centos tmp]# losetup --show /dev/loop0
/dev/loop0: [fd00]:266586 (/tmp/disk1.img)

添加另一个磁盘

向lookback设备/dev/loop1添加额外的1GB磁盘。

[root@centos tmp]# fallocate -l 1G /tmp/disk2.img

我们将该映像文件与/ dev/loop1关联:

[root@centos tmp]# losetup /dev/loop1 /tmp/disk2.img
[root@centos tmp]# losetup --show /dev/loop1
/dev/loop1: [fd00]:266761 (/tmp/disk2.img)

为新的环回设备创建PV:

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

[root@centos tmp]# pvs
  PV         VG          Fmt  Attr PSize PFree
  /dev/loop0 vg01        lvm2 a--  3.00g 1020.00m
  /dev/loop1             lvm2 a--  1.00g    1.00g
  /dev/sda2  vg_centos01 lvm2 a--  7.51g       0

将此设备添加到现有的卷组vg01中:

[root@centos tmp]# vgextend vg01 /dev/loop1
  Volume group "vg01" successfully extended
[root@centos tmp]# vgs
  VG          #PV #LV #SN Attr   VSize VFree
  vg01          2   1   0 wz--n- 3.99g 1.99g
  vg_centos01   1   2   0 wz--n- 7.51g    0

扩展逻辑卷

[root@centos tmp]# lvextend /dev/vg01/lvdata01 -r -L 3.5G

重新挂载

[root@centos tmp]# mount /dev/vg01/lvdata01 /var/myspace
[root@centos tmp]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_centos01-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
                      3.5G   69M  3.3G   3% /var/myspace


[root@centos tmp]# pvs
[root@centos tmp]# vgs

创建一个文件系统

[root@centos tmp]# mkfs.ext3 /dev/vg01/lvdata01

创建逻辑卷

[root@centos tmp]# lvcreate -n lvdata01 -L 2GB vg01
  Logical volume "lvdata01" created

[root@centos tmp]# lvs
  LV       VG          Attr     LSize Pool Oroirn Data%  Move Log Copy%  Convert
  lvdata01 vg01        -wi-a--- 2.00g

[root@centos tmp]# vgs
  VG          #PV #LV #SN Attr   VSize VFree
  vg01          1   1   0 wz--n- 3.00g 1020.00m

创建一个卷组

[root@centos tmp]# vgcreate vg01 /dev/loop0
  Volume group "vg01" successfully created

创建镜像文件

使用df -h命令检查系统可使用的空间:

[root@centos ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_centos01-lv_root
                      6.0G  633M  5.1G  11% /
tmpfs                 250M     0  250M   0% /dev/shm
/dev/sda1             485M   38M  422M   9% /boot

创建磁盘:

[root@centos tmp]# fallocate -l 3G disk1.img

挂载

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

将挂载配置添加到/etc/fstab ,这样开机启动时能自动挂载。

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

在逻辑卷中增加空间

。在初始分配中,我们创建了一个具有3GB空间的磁盘,但是我们仅分配了2GB的可用空间。如果要分配更多空间,可以使用lvextend命令。尽管许多文件系统支持在线调整大小,但对于我们的示例,我将首先卸载设备:

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

如果我们使用lvextend命令,除非提供-r选项,否则它不会自动调整文件系统的大小。让我们再次检查可用空间:

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

[root@centos var]# lvextend /dev/vg01/lvdata01 -L2.5G
  Extending logical volume lvdata01 to 2.50 GiB
  Logical volume lvdata01 successfully resized

现在我们可以看到我们的逻辑卷现在已更改为2.5GB,并且卷组vg01中的可用空间现已更改,

[root@centos var]# vgs
  VG          #PV #LV #SN Attr   VSize VFree
  vg01          1   1   0 wz--n- 3.00g 508.00m
  vg_centos01   1   2   0 wz--n- 7.51g      0

如前所述,除非应用了-r选项,否则扩展逻辑卷不会自动扩展文件系统。如果您不使用-r选项,那么我们将需要使用resize2fs命令。但是,我们需要首先运行e2fsck命令作为检查:

[root@centos var]# e2fsck -f /dev/vg01/lvdata01
e2fsck 1.41.12 (17-May-2010)
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/vg01/lvdata01: 11/131072 files (0.0% non-contiguous), 25405/524288 blocks

运行resize2fs命令:

[root@centos var]# resize2fs /dev/vg01/lvdata01
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg01/lvdata01 to 655360 (4k) blocks.
The filesystem on /dev/vg01/lvdata01 is now 655360 blocks long.

重新挂载

[root@centos var]# mount /dev/vg01/lvdata01 /var/myspace
[root@centos var]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_centos01-lv_root
                      6.0G  3.7G  2.1G  65% /
tmpfs                 250M     0  250M   0% /dev/shm
/dev/sda1             485M   38M  422M   9% /boot
/dev/mapper/vg01-lvdata01
                      2.5G   68M  2.3G   3% /var/myspace
LINUX LVM示例02
日期:2019-04-29 03:17:37 来源:oir作者:oir