LINUX LVM示例01

时间:2019-04-29 03:17:37  来源:igfitidea点击:

逻辑卷管理LVM示例01

在下面的示例中,我们将使用Virtual Box来演示逻辑卷管理。在Virtual Box下,我创建了一个CentOS 6.3操作系统,并添加了2个1GB虚拟磁盘。在此示例中,我们学习如何创建用于LVM的新分区。您将学习pvcreatepvdisplaypvsvgcreatevgdisplayvgsvgscanvgrenamevgremovelvcreatelvdisplay的基础知识,lvsmkfsmountumount命令。

显示当前磁盘摘要

[root@centos ~]# fdisk -l
Disk /dev/sda: 4294 MB, 4294967296 bytes
Disk /dev/sdb: 1073 MB, 1073741824 bytes
Disk /dev/sdc: 1073 MB, 1073741824 bytes

使用上面的 /dev/sdb和 /dev/sdc创建分区

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

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition\'s system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

从交互式菜单中选择以下选项:

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

在上面,为添加新分区选择了n,为主分区选择了p

定义分区标识类型,因为我们使用的是LVM,所以我们选择类型8e

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.

选择w将分区表写入磁盘。

/dev/sdc重复相同的过程

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

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

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.

确认磁盘情况

[root@centos ~]# fdisk -l

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         130     1044193+  8e  Linux LVM

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1         130     1044193+  8e  Linux LVM

为LVM准备分区

创建pv

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

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

为了演示,我们现在将使用pvremove命令删除这些分区:

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

再次创建分区:

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

查看物理卷

[root@centos ~]# pvdisplay

显示新PV:

[root@centos ~]# pvs
  PV         VG        Fmt  Attr PSize    PFree   
  /dev/sda2  vg_centos lvm2 a--     3.51g       0 
  /dev/sdb1            lvm2 a--  1019.72m 1019.72m
  /dev/sdc1            lvm2 a--  1019.72m 1019.72m

创建卷组(VG)

将pv与创建的卷组vg01关联。

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

查看新的卷组

[root@centos ~]# vgdisplay
  --- Volume group ---
  VG Name               vg01

[root@centos ~]# vgs
  VG        #PV #LV #SN Attr   VSize VFree
  vg01        2   0   0 wz--n- 1.98g 1.98g
  vg_centos   1   2   0 wz--n- 3.51g    0 

搜索卷组

[root@centos ~]# vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "vg01" using metadata type lvm2
  Found volume group "vg_centos" using metadata type lvm2

为了演示,我们现在使用vgrename命令将卷组重命名为testvg

[root@centos ~]# vgrename vg01 testvg
  Volume group "vg01" successfully renamed to "testvg"

[root@centos ~]# vgs
  VG        #PV #LV #SN Attr   VSize VFree
  testvg      2   0   0 wz--n- 1.98g 1.98g
  vg_centos   1   2   0 wz--n- 3.51g    0 

[root@centos ~]# vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "testvg" using metadata type lvm2
  Found volume group "vg_centos" using metadata type lvm2

为了演示,我们现在将使用vgremove命令删除卷组testvg

[root@centos ~]# vgremove testvg
  Volume group "testvg" successfully removed

重新创建卷组:

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

[root@centos ~]# vgs
  VG        #PV #LV #SN Attr   VSize VFree
  vg01        2   0   0 wz--n- 1.98g 1.98g
  vg_centos   1   2   0 wz--n- 3.51g    0

创建逻辑卷(lv)

[root@centos ~]# lvcreate --name lvex01 --size 1.5G vg01
  Logical volume "lvex01" created

我们在卷组vg01上创建了名称为lvex01和大小为1.5GB的逻辑卷,

查看新的逻辑卷:

[root@centos ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg01/lvex01
  LV Name                lvex01
  VG Name                vg01

[root@centos ~]# lvscan
  ACTIVE            '/dev/vg01/lvex01' [1.50 GiB] inherit
  ACTIVE            '/dev/vg_centos/lv_root' [1.54 GiB] inherit
  ACTIVE            '/dev/vg_centos/lv_swap' [1.97 GiB] inherit

[root@centos ~]# lvs
  LV      VG        Attr     LSize Pool Origin Data%  Move Log Copy%  Convert
  lvex01  vg01      -wi-a--- 1.50g                                           
  lv_root vg_centos -wi-ao-- 1.54g                                           
  lv_swap vg_centos -wi-ao-- 1.97g

添加文件系统-mkfs

在我们的示例中,我们将使用ext3文件系统类型

[root@centos ~]# mkfs.ext3 /dev/vg01/lvex01
mke2fs 1.41.12 (17-May-2010)
Filesystem label=

创建挂载点

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

使用mount命令来装载逻辑卷:

[root@centos ~]# mount /dev/vg01/lvex01 /var/example01

/etc/fstab中添加挂载项,以便系统启动时能自动挂载

/dev/vg01/lvex01                    /var/example01                   ext3    defaults        0 0

使用df -h命令查看文件系统

[root@centos ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_centos-lv_root
                      1.6G  524M  950M  36% /
tmpfs                 504M     0  504M   0% /dev/shm
/dev/sda1             485M   38M  422M   9% /boot
/dev/mapper/vg01-lvex01
                      1.5G   35M  1.4G   3% /var/example01