LVM的创建与使用

linux系统管理 struggling 1020次浏览 0个评论

LVM 的全名是 Logical Volume Manager,中文可以翻译作逻辑卷轴管理员。LVM 的作法是将几个实体的 partitions (或 disk) 透过软件组合成为一块看起来是独立的大磁盘 (VG) ,然后将这块大磁碟再经过分割成为可使用分割槽 (LV), 最终就能够挂载使用了。LVM由以下几个部分组成:

Physical Volume, PV, 物理卷
Volume Group, VG, 卷组
Physical Extend, PE, 盘区
Logical Volume, LV, 逻辑卷

关系图如下所示:

QQ图片20150620223457

建立方法大概如下:
先使用fdisk分割出分区,修改分区类型为8e—–>以分区建立PV—–>再以PV建立VG——>最后使用VG建立LV——>格式化挂载并使用

与建立LVM相关的命令有以下几个,用法都相似:

pv (Physical Volume):

<code>
pvcreate :将实体 partition 创建成为 PV ;
pvscan :搜寻目前系统里面任何具有 PV 的分区;
pvdisplay :显示出目前系统上面的 PV 状态;
pvremove :将 PV 属性移除,让该 partition 不具有 PV 属性。
</code>

vg (Volume Group):

<code>
vgcreate :就是主要创建 VG 的命令啦!他的参数比较多,等一下介绍。
vgscan :搜寻系统上面是否有 VG 存在
vgdisplay :显示目前系统上面的 VG 状态;
vgextend :在 VG 内添加额外的 PV ;
vgreduce :在 VG 内移除 PV;
vgchange :配置 VG 是否启动 (active);
vgremove :删除一个 VG 
</code>

lv (Logical Volume):

<code>
lvcreate :创建 LV !
lvscan :查询系统上面的 LV ;
lvdisplay :显示系统上面的 LV 状态!
lvextend :在 LV 里面添加容量!
lvreduce :在 LV 里面减少容量;
lvremove :删除一个 LV !
lvresize :对 LV 进行容量大小的调整
</code>

以下为创建LVM的过程:

创建一个10G的LVM,由3G和7G的组成

<code>
←#4#root@localhost ~  →fdisk -l  #首先创建好分区
Disk /dev/sdd: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 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: 0x536b182b

   Device Boot      Start         End      Blocks   Id  System
/dev/sdd1               1         393     3156741   83  Linux
/dev/sdd4             394        3005    20980890    5  Extended
/dev/sdd5             394         786     3156741   8e  Linux LVM
/dev/sdd6             787        1440     5253223+  8e  Linux LVM
/dev/sdd7            1441        2355     7349706   8e  Linux LVM

←#12#root@localhost ~  →pvcreate /dev/sdd{5,7}   #创建PV
  Physical volume "/dev/sdd5" successfully created
  Physical volume "/dev/sdd7" successfully created

←#13#root@localhost ~  →pvs   #查看PV信息
  PV         VG   Fmt  Attr PSize PFree
  /dev/sdd5       lvm2 ---  3.01g 3.01g
  /dev/sdd7       lvm2 ---  7.01g 7.01g

←#14#root@localhost ~  →pvdisplay  #显示PV详细信息   
  "/dev/sdd5" is a new physical volume of "3.01 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdd5       <==实际的 partition 装置名称
  VG Name                                      <==因为尚未分配出去,所以空白!
  PV Size               3.01 GiB              <==容量说明
  Allocatable           NO                   <==是否已被分配,结果是 NO
  PE Size               0                         <==在此 PV 内的 PE 大小,未加入vg所以为0. 
  Total PE              0                        <==共有几个 PE
  Free PE               0                        <==没被 LV 用掉的 PE
  Allocated PE          0                     <==尚可分配出去的 PE 数量
  PV UUID               KOwXE6-EFdL-wXx6-x2yw-bZQY-lD1U-3meNsH
   
  "/dev/sdd7" is a new physical volume of "7.01 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdd7
  VG Name               
  PV Size               7.01 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               Y6w53C-bg0n-FgDW-gVBd-lNzm-Pl8c-tADk3n

   
←#15#root@localhost ~  →pvdisplay  /dev/sdd5   #也可以显示单个PV的信息
  "/dev/sdd5" is a new physical volume of "3.01 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdd5
  VG Name               
  PV Size               3.01 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               KOwXE6-EFdL-wXx6-x2yw-bZQY-lD1U-3meNsH

←#16#root@localhost ~  →pvscan  #扫描PV信息
  PV /dev/sdd5             lvm2 [3.01 GiB]
  PV /dev/sdd7             lvm2 [7.01 GiB]
  Total: 3 [18.02 GiB] / in use: 1 [8.00 GiB] / in no VG: 2 [10.02 GiB]  
</code>

# 这就分别显示每个 PV 的资讯与系统所有 PV 的信息。尤其最后一行,显示的是:
# 整体 PV 的量 / 已经被使用到 VG 的 PV 量 / 剩余的 PV 量

<code>
 ←#22#root@localhost ~  →vgcreate myvg1 /dev/sdd{5,7}      #创建名为myvg1的vg
  Volume group "myvg1" successfully created

←#23#root@localhost ~  →vgs      #查看vg
  VG    #PV #LV #SN Attr   VSize  VFree 
  myvg1   2   0   0 wz--n- 10.02g 10.02g 

  ←#26#root@localhost ~  →vgdisplay myvg1     #显示vg详细信息
  --- Volume group ---
  VG Name               myvg1
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               10.02 GiB      <==整体的 VG 容量有这么大
  PE Size               4.00 MiB        <==内部每个 PE 的大小
  Total PE              2564              <==总共的 PE 数量
  Alloc PE / Size       0 / 0   
  Free  PE / Size       2564 / 10.02 GiB
  VG UUID               pXgVCo-NaoH-MIqH-8qbs-Oxiz-njXP-lfJu7u
   
←#27#root@localhost ~  →pvdisplay  /dev/sdd5
  --- Physical volume ---
  PV Name               /dev/sdd5
  VG Name               myvg1
  PV Size               3.01 GiB / not usable 2.75 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              770      ---》将PV加入到VG后就有了PE
  Free PE               770
  Allocated PE          0
  PV UUID               KOwXE6-EFdL-wXx6-x2yw-bZQY-lD1U-3meNsH
  
 ←#30#root@localhost ~  →vgremove myvg1  #移除vg
  Volume group "myvg1" successfully removed

←#36#root@localhost ~  →vgcreate -s 8M  myvg1 /dev/sdd{5,7}    #重新创建并设置PE大小为8G
  Volume group "myvg1" successfully created

  ←#38#root@localhost ~  →vgdisplay  myvg1
  --- Volume group ---
  VG Name               myvg1
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               10.02 GiB
  PE Size               8.00 MiB         ------》8M
  Total PE              1282
  Alloc PE / Size       0 / 0   
  Free  PE / Size       1282 / 10.02 GiB
  VG UUID               QNfPZD-tljN-TtJD-xh8z-cMwq-IkK6-SgU23Y
  
 ←#40#root@localhost ~  →vgreduce myvg1 /dev/sdd5  #在myvg1中移除vg
  Removed "/dev/sdd5" from volume group "myvg1"
←#41#root@localhost ~  →vgs
  VG    #PV #LV #SN Attr   VSize VFree
  myvg1   1   0   0 wz--n- 7.01g 7.01g
←#42#root@localhost ~  →pvs
  PV         VG    Fmt  Attr PSize PFree
  /dev/sdd5        lvm2 ---  3.01g 3.01g
  /dev/sdd7  myvg1 lvm2 a--  7.01g 7.01g

←#43#root@localhost ~  →pvremove /dev/sdd5   #移除pv
  Labels on physical volume "/dev/sdd5" successfully wiped
←#44#root@localhost ~  →pvs
  PV         VG    Fmt  Attr PSize PFree
  /dev/sdb5  myvg  lvm2 a--  8.00g 3.00g
  /dev/sdd7  myvg1 lvm2 a--  7.01g 7.01g  

←#45#root@localhost ~  →pvcreate  /dev/sdd6
  Physical volume "/dev/sdd6" successfully created

←#46#root@localhost ~  →vgextend myvg1  /dev/sdd6  #扩展myvg1
  Volume group "myvg1" successfully extended
←#47#root@localhost ~  →vgs

  VG    #PV #LV #SN Attr   VSize  VFree 
  myvg    1   1   0 wz--n-  8.00g  3.00g
  myvg1   2   0   0 wz--n- 12.02g 12.02g
</code>

[root@www ~]# lvcreate [-L N[mgt]] [-n LV名称] VG名称
[root@www ~]# lvcreate [-l N] [-n LV名称] VG名称
选项与参数:
-L :后面接容量,容量的单位可以是 M,G,T 等,要注意的是,最小单位为 PE,
因此这个数量必须要是 PE 的倍数,若不相符,系统会自行计算最相近的容量。
-l :后面可以接 PE 的『个数』,而不是数量。若要这么做,得要自行计算 PE 数。
-n :后面接的就是 LV 的名称啦!

<code>
←#54#root@localhost ~  →lvcreate -L 50M -n  testlv myvg1  #创建LV,大小50M,名字为testlv
  Rounding up size to full physical extent 56.00 MiB
  Logical volume "testlv" created

←#55#root@localhost ~  →lvs
  LV     VG    Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  mydata myvg  -wi-ao----  5.00g                                                    
  testlv myvg1 -wi-a----- 56.00m         
                                           
←#56#root@localhost ~  →lvdisplay
  --- Logical volume ---
  LV Path                /dev/myvg1/testlv   <==这个才是 LV 的全名!
  LV Name                testlv
  VG Name                myvg1
  LV UUID                tOA30I-UbE2-MkJs-EMxz-nwD4-oByB-KGtAE2
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2015-06-20 05:09:40 -0700
  LV Status              available
  # open                 0
  LV Size                56.00 MiB                  <==这个 LV 的容量
  Current LE             7
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1
   
←#57#root@localhost ~  →mke2fs -j  /dev/myvg1/testlv  #格式化
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
14336 inodes, 57344 blocks
2867 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=58720256
7 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks: 
	8193, 24577, 40961

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

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

←#58#root@localhost ~  →mount /dev/myvg1/testlv /mnt/  #挂载

←#59#root@localhost ~  →ls /mnt/
lost+found

←#60#root@localhost ~  →mount
/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/mapper/myvg-mydata on /mydata type ext3 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
vmware-vmblock on /var/run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,default_permissions,allow_other)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
nfsd on /proc/fs/nfsd type nfsd (rw)
/dev/mapper/myvg1-testlv on /mnt type ext3 (rw)    -----》注意此处,名字不同

←#61#root@localhost ~  →ls /dev/mapper/
control  myvg1-testlv  myvg-mydata

←#63#root@localhost ~  →ls -l /dev/mapper/
total 0
crw-rw----. 1 root root 10, 58 Jun 20 04:17 control
lrwxrwxrwx. 1 root root      7 Jun 20 05:10 myvg1-testlv -> ../dm-1
lrwxrwxrwx. 1 root root      7 Jun 20 04:28 myvg-mydata -> ../dm-0
←#64#root@localhost ~  →ls -l  /dev/myvg1  #其实只是个链接
total 0
lrwxrwxrwx. 1 root root 7 Jun 20 05:10 testlv -> ../dm-1

←#67#root@localhost ~  →lvremove  /dev/mapper/myvg1-testlv  #必须卸载才能移除 
  Logical volume myvg1/testlv contains a filesystem in use.

←#68#root@localhost ~  →umount /mnt/

←#69#root@localhost ~  →lvremove  /dev/mapper/myvg1-testlv   #移除lv
Do you really want to remove active logical volume testlv? [y/n]: y
  Logical volume "testlv" successfully removed
</code>

其实 LV 的名称建置成为 /dev/vbirdvg/testlv 是为了让使用者直觉式的找到我们所需要的数据, 实际上 LVM 使用的装置是放置到 /dev/mapper/ 目录下的!


DevOps-田飞雨 》》转载请注明源地址
喜欢 (2)or分享 (0)
发表我的评论
取消评论
*

表情 贴图 加粗 链接 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址