lvm机制的基本概念

  • PV (physicak volume 物理卷)
处于LVM最底层,可以是整个硬盘或使用fdisk等工具建立的普通分区
包括许多默认4MB大小的PE,PE是PV中可以用于分配的最小存储单元
  • VG(volume group 卷组)
是一个或多个物理卷组合而成的整体,建立在PV之上的。
同一VG中所以PV的PE大小应该相同,当VG被创建后,我们才能正常查看PE数量
  • LV(logical volume ,逻辑卷)
从卷组中分割出的一块空间,他是建立在VG之上,相当于原来分区的概念。不过大小可以动态改变。其实LV就是由PE组成的,不过在LV里面这些PE叫做LE
LE(Logical extents)是LV中可以用于分配的最小存储单元,与PE大小相同并一一对应

1. 对vdc进行lvm的分区

推荐步骤:添加硬盘——>分区8e——>PV——>VG——>LV——>格式化——>挂载使用文件系统
——>修改系统挂载文件,设置开机挂载

1.1 查看当前机器分区状态

$ df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        50G  1.2G   49G   3% /
devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                   tmpfs     1.9G  8.4M  1.9G   1% /run
tmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1               xfs      1014M  143M  872M  15% /boot
/dev/mapper/centos-home xfs        26G   33M   26G   1% /home
tmpfs                   tmpfs     380M     0  380M   0% /run/user/0
$ lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   80G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   79G  0 part 
  ├─centos-root 253:0    0   50G  0 lvm  /
  ├─centos-swap 253:1    0  3.9G  0 lvm  [SWAP]
  └─centos-home 253:2    0 25.1G  0 lvm  /home
sdb               8:16   0   10G  0 disk 
sr0              11:0    1 1024M  0 rom

1.2 进行分区

分一个主分区,将全部都分给主分区

$ fdisk /dev/sdb 
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x969372a9.

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
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   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
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-20971519, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): 
Using default value 20971519
Partition 1 of type Linux and of size 10 GiB is set

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

Calling ioctl() to re-read partition table.
Syncing disks.
$ lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   80G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   79G  0 part 
  ├─centos-root 253:0    0   50G  0 lvm  /
  ├─centos-swap 253:1    0  3.9G  0 lvm  [SWAP]
  └─centos-home 253:2    0 25.1G  0 lvm  /home
sdb               8:16   0   10G  0 disk 
└─sdb1            8:17   0   10G  0 part 
sr0              11:0    1 1024M  0 rom

1.3 创建物理卷

$ pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.

1.4 创建卷组vgdata

$ vgcreate vgdata /dev/sdb1
  Volume group "vgdata" successfully created

1.5 创建逻辑卷

$ lvcreate -n lvdata vgdata -l 100%VG
  Logical volume "lvdata" created.

1.6 初始化xfs类型的文件系统

$ mkfs.xfs /dev/vgdata/lvdata
meta-data=/dev/vgdata/lvdata     isize=512    agcount=4, agsize=655104 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=2620416, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

1.7 挂载文件系统到目录

$ mount /dev/vgdata/lvdata /pkl_test
$ lsblk
NAME              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                 8:0    0   80G  0 disk 
├─sda1              8:1    0    1G  0 part /boot
└─sda2              8:2    0   79G  0 part 
  ├─centos-root   253:0    0   50G  0 lvm  /
  ├─centos-swap   253:1    0  3.9G  0 lvm  [SWAP]
  └─centos-home   253:2    0 25.1G  0 lvm  /home
sdb                 8:16   0   10G  0 disk 
└─sdb1              8:17   0   10G  0 part 
  └─vgdata-lvdata 253:3    0   10G  0 lvm  /pkl_test
sr0                11:0    1 1024M  0 rom  
$ df -Th
Filesystem                Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   xfs        50G  1.2G   49G   3% /
devtmpfs                  devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                     tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                     tmpfs     1.9G  8.4M  1.9G   1% /run
tmpfs                     tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1                 xfs      1014M  143M  872M  15% /boot
/dev/mapper/centos-home   xfs        26G   33M   26G   1% /home
tmpfs                     tmpfs     380M     0  380M   0% /run/user/0
/dev/mapper/vgdata-lvdata xfs        10G   33M   10G   1% /pkl_test

1.8 将挂盘写进系统启动的初始化文件里面(/etc/fstab)

$ cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Mon Jul 4 19:58:25 2016
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/vda1 / xfs defaults 0 1
/swapfile none swap defaults 0 0
/dev/vdb /data xfs defaults,noatime 0 0
/dev/mapper/vgdata-lvdata /pkl_test xfs defaults 0 0

之后,再重启的时候,盘也会一直挂载系统上了。

2.在线扩容

不需要重启。

2.1 新加一个盘

$ lsblk
NAME              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                 8:0    0   80G  0 disk 
├─sda1              8:1    0    1G  0 part /boot
└─sda2              8:2    0   79G  0 part 
  ├─centos-root   253:0    0   50G  0 lvm  /
  ├─centos-swap   253:1    0  3.9G  0 lvm  [SWAP]
  └─centos-home   253:2    0 25.1G  0 lvm  /home
sdb                 8:16   0   10G  0 disk 
└─sdb1              8:17   0   10G  0 part 
  └─vgdata-lvdata 253:3    0   10G  0 lvm  /pkl_test
sdc                 8:32   0    8G  0 disk 
sr0                11:0    1 1024M  0 rom

2.2 创建为物理卷(pv)

$ pvcreate /dev/sdc
  Physical volume "/dev/sdc" successfully created.
$ lsblk
NAME              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                 8:0    0   80G  0 disk 
├─sda1              8:1    0    1G  0 part /boot
└─sda2              8:2    0   79G  0 part 
  ├─centos-root   253:0    0   50G  0 lvm  /
  ├─centos-swap   253:1    0  3.9G  0 lvm  [SWAP]
  └─centos-home   253:2    0 25.1G  0 lvm  /home
sdb                 8:16   0   10G  0 disk 
└─sdb1              8:17   0   10G  0 part 
  └─vgdata-lvdata 253:3    0   10G  0 lvm  /pkl_test
sdc                 8:32   0    8G  0 disk 
sr0                11:0    1 1024M  0 rom

2.3 当前系统的物理卷(pv)和卷组(vg)

$ pvs
  PV         VG     Fmt  Attr PSize   PFree
  /dev/sda2  centos lvm2 a--  <79.00g 4.00m
  /dev/sdb1  vgdata lvm2 a--  <10.00g    0 
  /dev/sdc          lvm2 ---    8.00g 8.00g
$ vgs
  VG     #PV #LV #SN Attr   VSize   VFree
  centos   1   3   0 wz--n- <79.00g 4.00m
  vgdata   1   1   0 wz--n- <10.00g    0

2.4 卷组vgdata扩容

将物理卷/dev/sdc扩展至vgdata卷组

$ vgextend vgdata /dev/sdc
  Volume group "vgdata" successfully extended

2.5 更改后vg和当前lg的情况

$ vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <79.00 GiB
  PE Size               4.00 MiB
  Total PE              20223
  Alloc PE / Size       20222 / 78.99 GiB
  Free  PE / Size       1 / 4.00 MiB
  VG UUID               33hOTD-jjk1-kb4b-sRdZ-uBmN-ujSt-7wQhbQ
   
  --- Volume group ---
  VG Name               vgdata
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               17.99 GiB
  PE Size               4.00 MiB
  Total PE              4606
  Alloc PE / Size       2559 / <10.00 GiB
  Free  PE / Size       2047 / <8.00 GiB
  VG UUID               iYEoGE-s2hq-fbpN-Oy7F-mHSu-uSjn-2TGJDt
   
$ lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                iLjbZC-4g7F-dkC8-eZyb-uCGx-aZo1-VMptq4
  LV Write Access        read/write
  LV Creation host, time localhost, 2021-04-28 19:50:06 +0800
  LV Status              available
  # open                 2
  LV Size                <3.88 GiB
  Current LE             992
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/centos/home
  LV Name                home
  VG Name                centos
  LV UUID                DKhvbr-gt8n-idlm-s9f2-YiYN-k3Vj-LYvpwU
  LV Write Access        read/write
  LV Creation host, time localhost, 2021-04-28 19:50:06 +0800
  LV Status              available
  # open                 1
  LV Size                <25.12 GiB
  Current LE             6430
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2
   
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                WzSvke-Oeci-UyGx-djsV-PO9P-R2VE-faF2lZ
  LV Write Access        read/write
  LV Creation host, time localhost, 2021-04-28 19:50:07 +0800
  LV Status              available
  # open                 1
  LV Size                50.00 GiB
  Current LE             12800
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/vgdata/lvdata
  LV Name                lvdata
  VG Name                vgdata
  LV UUID                1TjhlL-K5wH-rce3-cqyt-EN7S-TE0c-v0ry9r
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2021-05-12 11:10:15 +0800
  LV Status              available
  # open                 1
  LV Size                <10.00 GiB
  Current LE             2559
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:3

2.6 lvdata扩容

如果直接+10G上限会报错,最好加的少点+9.8G就可以,演示的例子做了很多测试,正式可以直接加。

$ lvextend -L +10G /dev/vgdata/lvdata   # 报错
  Insufficient free space: 2560 extents needed, but only 1407 available
$ lvextend -L +5.5G /dev/vgdata/lvdata
  Insufficient free space: 1408 extents needed, but only 1407 available
$ lvextend -L +5G /dev/vgdata/lvdata
  Size of logical volume vgdata/lvdata changed from <12.50 GiB (3199 extents) to <17.50 GiB (4479 extents).
  Logical volume vgdata/lvdata successfully resized.
$ lvextend -L +0.4G /dev/vgdata/lvdata
  Rounding size to boundary between physical extents: 412.00 MiB.
  Size of logical volume vgdata/lvdata changed from <17.50 GiB (4479 extents) to <17.90 GiB (4582 extents).
  Logical volume vgdata/lvdata successfully resized.

查看当前分区情况

$ lsblk
NAME              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                 8:0    0   80G  0 disk 
├─sda1              8:1    0    1G  0 part /boot
└─sda2              8:2    0   79G  0 part 
  ├─centos-root   253:0    0   50G  0 lvm  /
  ├─centos-swap   253:1    0  3.9G  0 lvm  [SWAP]
  └─centos-home   253:2    0 25.1G  0 lvm  /home
sdb                 8:16   0   10G  0 disk 
└─sdb1              8:17   0   10G  0 part 
  └─vgdata-lvdata 253:3    0 17.9G  0 lvm  /pkl_test
sdc                 8:32   0    8G  0 disk 
└─vgdata-lvdata   253:3    0 17.9G  0 lvm  /pkl_test
sr0                11:0    1 1024M  0 rom

2.7 空间合并

$ xfs_growfs /dev/vgdata/lvdata
meta-data=/dev/mapper/vgdata-lvdata isize=512    agcount=4, agsize=655104 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=2620416, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 2620416 to 4691968

ext4文件系统需要用这个命令扩盘:

$ resize2fs /dev/centos/root

2.8 lvm动态扩容是否成功

$ df -Th
Filesystem                Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   xfs        50G  1.2G   49G   3% /
devtmpfs                  devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                     tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                     tmpfs     1.9G  8.4M  1.9G   1% /run
tmpfs                     tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1                 xfs      1014M  143M  872M  15% /boot
/dev/mapper/centos-home   xfs        26G   33M   26G   1% /home
/dev/mapper/vgdata-lvdata xfs        18G   33M   18G   1% /pkl_test
tmpfs                     tmpfs     380M     0  380M   0% /run/user/0
$ lsblk
NAME              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                 8:0    0   80G  0 disk 
├─sda1              8:1    0    1G  0 part /boot
└─sda2              8:2    0   79G  0 part 
  ├─centos-root   253:0    0   50G  0 lvm  /
  ├─centos-swap   253:1    0  3.9G  0 lvm  [SWAP]
  └─centos-home   253:2    0 25.1G  0 lvm  /home
sdb                 8:16   0   10G  0 disk 
└─sdb1              8:17   0   10G  0 part 
  └─vgdata-lvdata 253:3    0 17.9G  0 lvm  /pkl_test
sdc                 8:32   0    8G  0 disk 
└─vgdata-lvdata   253:3    0 17.9G  0 lvm  /pkl_test
sr0                11:0    1 1024M  0 rom

按照以前文档的说法,系统的重启后读取的初始化文件可不动。即,不用修改/etc/fstab,重启也是生效的。

3. 删除lvm分区

lvm分区 (千万不要在没有关闭LVM的情况下删除分区)

  1. umount卸载lvm文件系统
umount /dev/mapper/vgdata-lvdata
  1. lvremove删除逻辑卷
lvremove vgdata
  1. vgremove删除卷组
vgremove vgdata
  1. pvremove删除物理卷
pvremove /dev/sdb1
  1. 清楚/etc/fstab中的lvm挂载记录
  2. fdisk修改分区ID