创建LVM

LVM是Logical Volume Manager(逻辑卷管理)的简写,它是对磁盘分区进行管理的一种机制, LVM是建立在硬盘和分区之上的一个逻辑层,用来提高磁盘管理的灵活性.通过LVM可将若干个 磁盘分区连接为一个整块的卷组(Volume Group),形成一个存储池.可以在卷组上随意创建逻辑 卷(Logical Volumes),并进一步在逻辑卷上创建文件系统.

LVM作用:通过LVM可以方便的调整存储卷的大小,并且可以对磁盘存储按照组的方式进行命名、管理和 分配.当系统添加了新的磁盘,通过LVM可以直接扩展文件系统跨越该磁盘, 而不必将文件移动 到新的磁盘上

LVM四个概念:

pv:物理卷

vg:逻辑卷组,由物理卷组成

lv:逻辑卷

PE:最小存储单位


LVM创建四个步骤:

1、创建物理卷pv:pvcreate

2、创建物理卷组vg:vgcreat

3、创建逻辑卷lv:lvcreate

4、对逻辑卷进行文化系统格式化


查看PV,VG,LV信息:

1、查看PV:pvs

2、查看VG:vgs

3、查看LV:lvs



例:创建一个LV1

1、查看磁盘信息:

[root@localhost ~]# fdisk -l
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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

2、对磁盘sdb分区,分3个主分区:

[root@localhost ~]# 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 0x06a539c8.
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-2610, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +2G
以此类推,创建其它2个主分区,最后输入"w"保存分区创建。

Selected partition 4
First cylinder (787-2610, default 787):
Using default value 787
Last cylinder, +cylinders or +size{K,M,G} (787-2610, default 2610):
Using default value 2610

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

Calling ioctl() to re-read partition table.

分区创建后,输入命令:partprobe /dev/sdb 使分区在不重启系统的情况下生效。

[root@localhost ~]# ls /dev/sdb*
/dev/sdb  /dev/sdb1  /dev/sdb2  /dev/sdb3  /dev/sdb4

3、创建物理卷PV

[root@localhost ~]# pvcreate /dev/sdb{1,2}
  Physical volume "/dev/sdb1" successfully created
  Physical volume "/dev/sdb2" successfully created

将分区sdb1和sdb2转为物理卷,查看物理卷信息:

[root@localhost ~]# pvs
  PV                VG   Fmt      Attr      PSize      PFree
  /dev/sdb1           lvm2     a--       2.00g      2.00g
  /dev/sdb2           lvm2     a--       2.00g      2.00g

注:pv:物理卷名称  VG:所属逻辑卷组  PSize:物理卷容量  PFree:物理卷剩余容量


4、创建逻辑卷组VG1

[root@localhost ~]# vgcreate VG1 /dev/sdb{1,2}
  Volume group "VG1" successfully created
创建逻辑VG1,包含物理卷sdb1、sdb2,查看VG1信息:

[root@localhost ~]# vgs
  VG   #PV #LV #SN Attr   VSize VFree
  VG1    2   0   0 wz--n- 4.01g 4.01g
注:   VG:逻辑卷组名称    #PV:包含物理卷个数    #LV:包含逻辑卷个数   VSize:卷组容量

          VFree:卷组剩余容量


5、创建逻辑卷LV:

[root@localhost ~]# lvcreate -n LV1 -L 4G VG1
  Logical volume "LV1" created

注: -n 指定逻辑卷名称

        -L 指定逻辑卷容量大小

        VG1  逻辑卷LV1创建在逻辑卷组VG1上

查看逻辑卷VL1信息:

[root@localhost ~]# lvs
  LV   VG   Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  LV1  VG1  -wi-a----- 4.00g 


到此为止,创建LVM的前三个步骤已经完成。查看pv、vg、lv信息除了pvs、vgs、lvs外,还有pvdisplay、vgdisplay、lvdisplay命令查看,而且更加详细。

[root@localhost ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               VG1
  PV Size               2.01 GiB / not usable 3.16 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              513
  Free PE               0
  Allocated PE          513
  PV UUID               dqBiSM-Gflt-IjH4-YMZH-vAF3-V2NQ-Zof2Nn
   
  --- Physical volume ---
  PV Name               /dev/sdb2
  VG Name               VG1
  PV Size               2.01 GiB / not usable 3.19 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              513
  Free PE               2
  Allocated PE          511
  PV UUID               jDlmm2-wIdW-pFlC-cMl0-0OWO-8XhT-JMJ73k

[root@localhost ~]# vgdisplay
  --- Volume group ---
  VG Name               VG1
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               4.01 GiB
  PE Size               4.00 MiB
  Total PE              1026
  Alloc PE / Size       1024 / 4.00 GiB
  Free  PE / Size       2 / 8.00 MiB
  VG UUID               HfIDZU-cPko-Lq4k-5vuB-5cAG-1soG-3oC8gM

[root@localhost ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/VG1/LV1
  LV Name                LV1
  VG Name                VG1
  LV UUID                EkGiw6-ltwY-UAv4-OOFK-EcYn-Ug8d-KDVqHw
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2016-01-04 22:08:18 +0800
  LV Status              available
  # open                 0
  LV Size                4.00 GiB
  Current LE             1024
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

5、最后对逻辑卷LV1进行文化系统格式化

[root@localhost ~]# mkfs -t ext4 /dev/VG1/LV1
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
262144 inodes, 1048576 blocks
52428 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=1073741824
32 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
    32768, 98304, 163840, 229376, 294912, 819200, 884736

正在写入inode表: 完成                            
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

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

到此创建LVM的4个步骤已完成。最后将创建的逻辑卷LV1挂载就可以存储文件了,手动挂载或写/etc/fstab文件中,让开机自动挂载。

查看LV1的UUID:

[root@localhost ~]# blkid
/dev/sda2: UUID="1a3ba114-02f9-491d-bebf-95d2231815ac" TYPE="ext4"
/dev/sda1: UUID="6f1089de-bb06-4ee3-8e9b-a07530e7d503" TYPE="ext4"
/dev/sda3: UUID="cd69336c-6e79-45cf-aacf-98a7d1138d2b" TYPE="ext4"
/dev/sda5: UUID="124418b0-fb6a-4c10-8299-f63d59cea547" TYPE="swap"
/dev/sr0: LABEL="CentOS_6.6_Final" TYPE="iso9660"
/dev/sdb1: UUID="dqBiSM-Gflt-IjH4-YMZH-vAF3-V2NQ-Zof2Nn" TYPE="LVM2_member"
/dev/sdb2: UUID="jDlmm2-wIdW-pFlC-cMl0-0OWO-8XhT-JMJ73k" TYPE="LVM2_member"
/dev/sdb3: UUID="E30ReV-Ofxk-GeHy-l5lu-zT6P-hSfC-oTP6uf" TYPE="LVM2_member"
/dev/mapper/VG1-LV1: UUID="1764dcfb-cf37-430b-a803-60f4e2837ec6" TYPE="ext4"

创建一个目录/LV1,将LV1挂载到此目录下

[root@localhost ~]# mkdir /LV1

编辑fstab

[root@localhost ~]# vim /etc/fstab

  9 UUID=1a3ba114-02f9-491d-bebf-95d2231815ac /                       ext4    defaults        1 1
 10 UUID=6f1089de-bb06-4ee3-8e9b-a07530e7d503 /boot                   ext4    defaults        1 2
 11 UUID=cd69336c-6e79-45cf-aacf-98a7d1138d2b /usr                    ext4    defaults        1 2
 12 UUID=124418b0-fb6a-4c10-8299-f63d59cea547 swap                    swap    defaults        0 0
 13 tmpfs                   /dev/shm                tmpfs   defaults        0 0
 14 devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
 15 sysfs                   /sys                    sysfs   defaults        0 0
 16 proc                    /proc                   proc    defaults        0 0
 17 /dev/sr0                /mnt                    iso9660 defaults 0 0
 18 UUID=1764dcfb-cf37-430b-a803-60f4e2837ec6       /LV1    ext4    defaults 0 0
重新启动自动挂载

[root@localhost ~]# mount -a
查看挂载信息:

[root@localhost ~]# mount -l
/dev/sda2 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
/dev/sda3 on /usr type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/sr0 on /mnt type iso9660 (ro) [CentOS_6.6_Final]
/dev/mapper/VG1-LV1 on /LV1 type ext4 (rw)



你可能感兴趣的:(lvm)