LVM

来源:互联网 发布:fifa online mac 编辑:程序博客网 时间:2024/06/01 18:23

LVM

当前系统盘的分布:

[root@host ~]# fdisk -lDisk /dev/vda: 21.5 GB, 21474836480 bytes, 41943040 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x0009b56e   Device Boot      Start         End      Blocks   Id  System/dev/vda1   *        2048     1026047      512000   83  Linux/dev/vda2         1026048    41943039    20458496   8e  Linux LVMDisk /dev/mapper/centos-root: 18.8 GB, 18756927488 bytes, 36634624 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytes

一块系统盘/dev/vda被分成了两个分区/dev/vda1,/dev/vda2,然后查看挂载:

[root@host ~]# df -hFilesystem               Size  Used Avail Use% Mounted on/dev/mapper/centos-root   18G  1.6G   16G   9% /devtmpfs                 1.9G     0  1.9G   0% /devtmpfs                    1.9G     0  1.9G   0% /dev/shmtmpfs                    1.9G  8.3M  1.9G   1% /runtmpfs                    1.9G     0  1.9G   0% /sys/fs/cgroup/dev/vda1                497M  120M  378M  25% /boot

可以看到/dev/vda1分区被挂载到/boot下面,作为启动分区。这个分区的大小大概是512M,只用于启动分区。另外的一个分区/dev/vda2可以看到在df中没有显示出来,因为这个分区没有被直接使用,而是被当作LVM的PV来使用,可以查看:

[root@host ~]# pvdisplay  --- Physical volume ---  PV Name               /dev/vda2  VG Name               centos  PV Size               19.51 GiB / not usable 3.00 MiB  Allocatable           yes   PE Size               4.00 MiB  Total PE              4994  Free PE               10  Allocated PE          4984  PV UUID               2MR7wz-fh1t-6nlO-glse-MEBz-8Rjo-Y7gkhI

作为一个PV,它被划到了centos这个VG里面使用,再查看:

[root@host ~]# vgdisplay  --- Volume group ---  VG Name               centos  System ID               Format                lvm2  Metadata Areas        1  Metadata Sequence No  3  VG Access             read/write  VG Status             resizable  MAX LV                0  Cur LV                2  Open LV               2  Max PV                0  Cur PV                1  Act PV                1  VG Size               19.51 GiB  PE Size               4.00 MiB  Total PE              4994  Alloc PE / Size       4984 / 19.47 GiB  Free  PE / Size       10 / 40.00 MiB  VG UUID               h5CW2E-aYjt-AZLN-CTMe-7lZp-e2xk-yuDnLs

再查看这个VG被哪些LV使用了:

[root@host ~]# lvdisplay  --- Logical volume ---  LV Path                /dev/centos/swap  LV Name                swap  VG Name                centos  LV UUID                U0ANhF-afAc-pb0U-AYmN-EJH0-Wyj8-H2bvws  LV Write Access        read/write  LV Creation host, time localhost, 2015-12-01 21:35:58 +0800  LV Status              available  # open                 2  LV Size                2.00 GiB  Current LE             512  Segments               1  Allocation             inherit  Read ahead sectors     auto  - currently set to     8192  Block device           253:1  --- Logical volume ---  LV Path                /dev/centos/root  LV Name                root  VG Name                centos  LV UUID                sG5AuB-SsYW-ggN5-tl6l-csEg-14F7-6NNVnO  LV Write Access        read/write  LV Creation host, time localhost, 2015-12-01 21:35:59 +0800  LV Status              available  # open                 1  LV Size                17.47 GiB  Current LE             4472  Segments               1  Allocation             inherit  Read ahead sectors     auto  - currently set to     8192  Block device           253:0

可以看到一个VG被划成了两个LV所占用,分别是设备/dev/centos/swap和/dev/centos/root,再查看df命令里面的/dev/mapper/centos-root,可以发现,

[root@host ~]# ll /dev/centos/total 0lrwxrwxrwx 1 root root 7 Oct 10 17:46 root -> ../dm-0lrwxrwxrwx 1 root root 7 Oct 10 17:46 swap -> ../dm-1[root@host ~]# ll /dev/mapper/total 0lrwxrwxrwx 1 root root       7 Oct 10 17:46 centos-root -> ../dm-0lrwxrwxrwx 1 root root       7 Oct 10 17:46 centos-swap -> ../dm-1crw------- 1 root root 10, 236 Oct 10 17:46 control

所有上面的那些*display命令都可以使用简化版本的

[root@host ~]# pvs  PV         VG     Fmt  Attr PSize  PFree   /dev/vda2  centos lvm2 a--  19.51g 40.00m[root@host ~]# vgs  VG     #PV #LV #SN Attr   VSize  VFree   centos   1   2   0 wz--n- 19.51g 40.00m[root@host ~]# lvs  LV   VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert  root centos -wi-ao---- 17.47g                                                      swap centos -wi-ao----  2.00g            

也就是说无论是/dev/mapper/还是/dev/centos/下面的文件都是直接链接到/dev/dm-上面,前者是管理层次上的一个文件,后者才是lv的操作系统里面的具体的设备(分区)。所以对于fdisk -l命令看到的结果,由于它无法区分/dev/centos/与/dev/vda之间的关系,所以都显示了出来。

[root@host ~]# ll /dev | grep dmcrw-------  1 root root     10,  61 Oct 10 17:46 cpu_dma_latencybrw-rw----  1 root disk    253,   0 Oct 10 17:46 dm-0brw-rw----  1 root disk    253,   1 Oct 10 17:46 dm-1crw-------  1 root root      1,  12 Oct 10 17:46 oldmem

/dev/vda1被挂载在/boot上面,作为启动分区
/dev/mapper/root分区被挂在根目录上,作为根分区(即磁盘/dev/vda的第二个分区虚拟化出的一块磁盘再分出的第一个区)
/dev/mapper/swap分区不用挂载,直接作为交换分区(即磁盘/dev/vda的第二个分区虚拟化出的一块磁盘再分出的第二个区

LVM扩容

新挂载了一块磁盘之后,可以看到:

Disk /dev/vdb: 128.8 GB, 128849018880 bytes, 251658240 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytes

使用命令将这个磁盘设置为PV,设置完成之后,这块PV暂时还没有人使用,不属于任何VG

[root@host ~]# pvcreate /dev/vdb  Physical volume "/dev/vdb" successfully created[root@host ~]# pvs  PV         VG     Fmt  Attr PSize   PFree    /dev/vda2  centos lvm2 a--   19.51g  40.00m  /dev/vdb          lvm2 ---  120.00g 120.00g

再使用vgextend将将的PV加入到VG中,

[root@host ~]# vgextend centos /dev/vdb  Volume group "centos" successfully extended[root@host ~]# pvs  PV         VG     Fmt  Attr PSize   PFree    /dev/vda2  centos lvm2 a--   19.51g  40.00m  /dev/vdb   centos lvm2 a--  120.00g 120.00g[root@host ~]# vgs  VG     #PV #LV #SN Attr   VSize   VFree    centos   2   2   0 wz--n- 139.50g 120.04g

最后再扩容原来的root的LV。lvextend中的扩容参数也可以使用+版本,这样就不用计算总大小。

[root@host ~]# lvextend -L 120G centos/root  Size of logical volume centos/root changed from 17.47 GiB (4472 extents) to 120.00 GiB (30720 extents).  Logical volume root successfully resized[root@host ~]# lvs  LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert  root centos -wi-ao---- 120.00g                                                      swap centos -wi-ao----   2.00g                           

但此时发现df -h查看时,根目录仍然是原来的大小,因为lvextend并不识别任何文件系统,只负责将分区后面添加了可用的blocks。此时需要根据不同的文件系统使用文件系统的工具resize2fs或者xfs_growfs。对于普通的文件系统分区,可以直接使用resize2fs,但如果是xfs分区,需要使用xfs_growfs,否则会提示无法识别super block。完成之后再查看df可以发现已经完成了。

[root@host ~]# xfs_growfs /dev/centos/root meta-data=/dev/mapper/centos-root isize=256    agcount=4, agsize=1144832 blks         =                       sectsz=512   attr=2, projid32bit=1         =                       crc=0        finobt=0data     =                       bsize=4096   blocks=4579328, imaxpct=25         =                       sunit=0      swidth=0 blksnaming   =version 2              bsize=4096   ascii-ci=0 ftype=0log      =internal               bsize=4096   blocks=2560, version=2         =                       sectsz=512   sunit=0 blks, lazy-count=1realtime =none                   extsz=4096   blocks=0, rtextents=0data blocks changed from 4579328 to 31457280

下面是所有设置完成之后的状态

[root@host ~]# pvs  PV         VG     Fmt  Attr PSize   PFree   /dev/vda2  centos lvm2 a--   19.51g     0   /dev/vdb   centos lvm2 a--  120.00g 17.50g[root@host ~]# vgs  VG     #PV #LV #SN Attr   VSize   VFree   centos   2   2   0 wz--n- 139.50g 17.50g[root@host ~]# lvs  LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert  root centos -wi-ao---- 120.00g                                                      swap centos -wi-ao----   2.00g                                                    [root@host ~]# df -hFilesystem               Size  Used Avail Use% Mounted on/dev/mapper/centos-root  120G  1.6G  119G   2% /devtmpfs                 1.9G     0  1.9G   0% /devtmpfs                    1.9G     0  1.9G   0% /dev/shmtmpfs                    1.9G  8.3M  1.9G   1% /runtmpfs                    1.9G     0  1.9G   0% /sys/fs/cgroup/dev/vda1                497M  120M  378M  25% /boot

镜像启动的虚拟机系统盘扩容

制作虚拟机镜像的时候,会指定根磁盘的大小,比如20G,那样的话,使用制作出来的镜像文件来启动新的虚拟机的话,根盘也会占据这么大。这样看来,镜像文件里面包含了磁盘的分区表信息(注意是磁盘,原镜像里面所有的分区信息都会带着,包含创建的LVM信息),也就说,这个镜像文件原封不动地展开到了新的根磁盘里面。这样的话,如果新分配的根磁盘的大小大于原来的创建时的大小,那么,多余的大小不会被使用。 如下图所示,虽然给根磁盘分配了80多G的空间,但根镜像只使用了其中的20G。如果想要利用这些多余的空间,除了可以把它们创建一个新的分区之外,如果需要更大的根磁盘空间,需要利用LVM的特性来扩容。

Disk /dev/vda: 85.9 GB, 85899345920 bytes, 167772160 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x0009b56e   Device Boot      Start         End      Blocks   Id  System/dev/vda1   *        2048     1026047      512000   83  Linux/dev/vda2         1026048    41943039    20458496   8e  Linux LVM如下使用的情况[root@host ~]# df -hFilesystem               Size  Used Avail Use% Mounted on/dev/mapper/centos-root   18G  789M   17G   5% /devtmpfs                 7.8G     0  7.8G   0% /devtmpfs                    7.8G     0  7.8G   0% /dev/shmtmpfs                    7.8G  8.3M  7.8G   1% /runtmpfs                    7.8G     0  7.8G   0% /sys/fs/cgroup/dev/vda1                497M  120M  378M  25% /boot

这个时候需要利用fdisk工具来对磁盘进行新分区的建立,而且必须建立成8e格式的分区

fdisk /dev/vda

进入交互式的界面,可以选择m来查看帮助,使用p来打印当前的分区表
使用n创建新分区,但在创建新分区的时候,无法指定分区的Id号,需要在创建完成之后输入t进行分区Id的修改,然后输入8e即可以转成Linux LVM类型的分区。
最后输入w即可将分区表写入到磁盘。(有时候会提示设备忙,写入失败,需要重新启动。这时候重新启动即生效)。

Disk /dev/vda: 85.9 GB, 85899345920 bytes, 167772160 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x0009b56e   Device Boot      Start         End      Blocks   Id  System/dev/vda1   *        2048     1026047      512000   83  Linux/dev/vda2         1026048    41943039    20458496   8e  Linux LVM/dev/vda3        41943040   167772159    62914560   8e  Linux LVM

由上面的操作可以看到,如果原来制作镜像的时候,使用的分区方式不是使用LVM类型的话,那么在使用大容量根磁盘的时候,就可能没有办法无损对根磁盘进行扩容(但有可能有其它的工具可以无损扩容,但必然会花费大量的时间来修改文件系统)。

0 0
原创粉丝点击