Linux LVM 分区管理

LVM可以创建和管理“逻辑”卷,而不是直接使用物理硬盘。可以让管理员弹性的管理逻辑卷的扩大缩小,操作简单,而不损坏已存储的数据。可以随意将新的硬盘添加到LVM,以直接扩展已经存在的逻辑卷。LVM并不需要重启就可以让内核知道分区的存在。

一. LVM基本命令

功能/命令 物理卷管理 卷组管理 逻辑卷管理
扫描 pvscan vgscan lvscan
建立 pvcreate vgcreate lvcreate
显示 pvdisplay/pvs vgdisplay/vgs lvdisplay/lvs
删除 pvremove vgremove lvremove
扩展   vgextend lvextend
缩小   vgreduce lvreduce

二. LVM基本概念

1. 物理磁盘被格式化为PV, 空间被分为一个个PE(PE默认大小是4M)

2. 不同的PV被加入同一个VG,不同PV的PE全部进入VG的存储池

3. LV是基于PE创建,大小是PE的整数倍,组成LV的PE可能来自不同的PV

4. LV可以格式化后挂载使用

5. LV的扩充和增减就是增减组成该LV的PE的数量.其过程不会丢失原始数据

三. LVM的创建,查看和删除

1. 查看物理磁盘fdisk -l 或lsblk

[vic@localhost ~]$ lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0    8G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0    7G  0 part 
  ├─centos-root 253:0    0  6.2G  0 lvm  /
  └─centos-swap 253:1    0  820M  0 lvm  [SWAP]
sdb               8:16   0    8G  0 disk 
sdc               8:32   0    8G  0 disk 
sdd               8:48   0    8G  0 disk 
sde               8:64   0    8G  0 disk 
sr0              11:0    1 1024M  0 rom  

 2. 将物理磁盘设备转换为物理卷

[vic@localhost ~]$ sudo pvcreate /dev/sdb /dev/sdc
  Physical volume "/dev/sdb" successfully created.
  Physical volume "/dev/sdc" successfully created.
[vic@localhost ~]$ sudo pvs
  PV         VG     Fmt  Attr PSize  PFree
  /dev/sda2  centos lvm2 a--  <7.00g    0 
  /dev/sdb          lvm2 ---   8.00g 8.00g
  /dev/sdc          lvm2 ---   8.00g 8.00g

3. 创建卷组,并将PV加入卷组中

[vic@localhost ~]$ sudo vgcreate linux-vli /dev/sdb /dev/sdc
  Volume group "linux-vli" successfully created
[vic@localhost ~]$ sudo vgs
  VG        #PV #LV #SN Attr   VSize  VFree 
  centos      1   2   0 wz--n- <7.00g     0 
  linux-vli   2   0   0 wz--n- 15.99g 15.99g

4. 创建逻辑卷

[vic@localhost ~]$ sudo lvcreate -n sharelv -L 4G linux-vli
  Logical volume "sharelv" created.
[vic@localhost ~]$ sudo lvs
  LV      VG        Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root    centos    -wi-ao----  <6.20g                                                    
  swap    centos    -wi-ao---- 820.00m                                                    
  sharelv linux-vli -wi-a-----   4.00g 

5. 格式化逻辑卷创建文件系统

[vic@localhost ~]$ sudo mkfs.ext4 /dev/linux-vli/sharelv

6. 将格式化的逻辑卷挂载使用

[vic@localhost ~]$ sudo mkdir /sharelv
[vic@localhost ~]$ sudo mount /dev/linux-vli/sharelv /sharelv/
[vic@localhost ~]$ df -h
Filesystem                      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root         6.2G  1.4G  4.9G  22% /
devtmpfs                        484M     0  484M   0% /dev
tmpfs                           496M     0  496M   0% /dev/shm
tmpfs                           496M  6.9M  490M   2% /run
tmpfs                           496M     0  496M   0% /sys/fs/cgroup
/dev/sda1                      1014M  132M  883M  14% /boot
tmpfs                           100M     0  100M   0% /run/user/0
tmpfs                           100M     0  100M   0% /run/user/1000
/dev/mapper/linux--vli-sharelv  3.9G   16M  3.6G   1% /sharelv

7. 删除LV --> 删除VG --> 删除物理卷

[vic@localhost ~]$ sudo umount /sharelv
[vic@localhost ~]$ df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root  6.2G  1.4G  4.9G  22% /
devtmpfs                 484M     0  484M   0% /dev
tmpfs                    496M     0  496M   0% /dev/shm
tmpfs                    496M  6.9M  490M   2% /run
tmpfs                    496M     0  496M   0% /sys/fs/cgroup
/dev/sda1               1014M  132M  883M  14% /boot
tmpfs                    100M     0  100M   0% /run/user/0
tmpfs                    100M     0  100M   0% /run/user/1000
[vic@localhost ~]$ sudo lvremove /dev/linux-vli/sharelv
Do you really want to remove active logical volume linux-vli/sharelv? [y/n]: y
  Logical volume "sharelv" successfully removed
[vic@localhost ~]$ sudo lvs
  LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos -wi-ao----  <6.20g                                                    
  swap centos -wi-ao---- 820.00m                                                    
[vic@localhost ~]$ sudo vgremove linux-vli
  Volume group "linux-vli" successfully removed
[vic@localhost ~]$ sudo vgs
  VG     #PV #LV #SN Attr   VSize  VFree
  centos   1   2   0 wz--n- <7.00g    0 
[vic@localhost ~]$ sudo pvremove /dev/sdb /dev/sdc
  Labels on physical volume "/dev/sdb" successfully wiped.
  Labels on physical volume "/dev/sdc" successfully wiped.
[vic@localhost ~]$ sudo pvs
  PV         VG     Fmt  Attr PSize  PFree
  /dev/sda2  centos lvm2 a--  <7.00g    0 

 四. LVM卷扩展

1. 查看VG的剩余空间

[vic@localhost ~]$ sudo vgdisplay 
  --- Volume group ---
  VG Name               linux-vli
  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               15.99 GiB
  PE Size               4.00 MiB
  Total PE              4094
  Alloc PE / Size       1024 / 4.00 GiB
  Free  PE / Size       3070 / 11.99 GiB
  VG UUID               0sJQDx-2Ijp-K0in-jdTs-1fvG-FW2n-n2dXND

2. 扩充逻辑卷

[vic@localhost ~]$ sudo lvextend -L +3G /dev/linux-vli/sharelv
  Size of logical volume linux-vli/sharelv changed from 4.00 GiB (1024 extents) to 7.00 GiB (1792 extents).
  Logical volume linux-vli/sharelv successfully resized.
[vic@localhost ~]$ sudo lvdisplay 
  --- Logical volume ---
  LV Path                /dev/linux-vli/sharelv
  LV Name                sharelv
  VG Name                linux-vli
  LV UUID                bS4J9e-ggqT-RKXG-m4Gg-lUDW-dFkg-Y85ChG
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2019-05-07 12:42:09 +0800
  LV Status              available
  # open                 1
  LV Size                7.00 GiB
  Current LE             1792
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2

3. 更新文件系统

[vic@localhost ~]$ sudo resize2fs /dev/linux-vli/sharelv 
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/linux-vli/sharelv is mounted on /sharelv; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/linux-vli/sharelv is now 1835008 blocks long.

[vic@localhost ~]$ df -h
Filesystem                      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root         6.2G  1.4G  4.9G  22% /
devtmpfs                        484M     0  484M   0% /dev
tmpfs                           496M     0  496M   0% /dev/shm
tmpfs                           496M  6.9M  490M   2% /run
tmpfs                           496M     0  496M   0% /sys/fs/cgroup
/dev/sda1                      1014M  132M  883M  14% /boot
tmpfs                           100M     0  100M   0% /run/user/0
tmpfs                           100M     0  100M   0% /run/user/1000
/dev/mapper/linux--vli-sharelv  6.8G   18M  6.5G   1% /sharelv

 五. LVM扩大卷组

1. 添加新的硬盘并格式化为PV

[vic@localhost ~]$ sudo pvcreate /dev/sdd
 Physical volume "/dev/sdd" successfully created.
[vic@localhost ~]$ sudo pvs
  PV         VG        Fmt  Attr PSize  PFree   
  /dev/sda2  centos    lvm2 a--  <7.00g       0 
  /dev/sdb   linux-vli lvm2 a--  <8.00g 1020.00m
  /dev/sdc   linux-vli lvm2 a--  <8.00g   <8.00g
  /dev/sdd             lvm2 ---   8.00g    8.00g
[vic@localhost ~]$ 

2. 将新的PV添加到指定卷组中

[vic@localhost ~]$ sudo vgextend linux-vli /dev/sdd
  Volume group "linux-vli" successfully extended
[vic@localhost ~]$ sudo vgs
  VG        #PV #LV #SN Attr   VSize   VFree  
  centos      1   2   0 wz--n-  <7.00g      0 
  linux-vli   3   1   0 wz--n- <23.99g <16.99g

六. LVM缩小逻辑卷

1. 卸载已经挂载的逻辑卷

[vic@localhost ~]$ sudo umount /sharelv/

2. 缩小文件系统

[vic@localhost ~]$ sudo e2fsck -f /dev/linux-vli/sharelv 
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/linux-vli/sharelv: 12/458752 files (0.0% non-contiguous), 66176/1835008 blocks
[vic@localhost ~]$ sudo resize2fs /dev/linux-vli/sharelv 3G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/linux-vli/sharelv to 786432 (4k) blocks.
The filesystem on /dev/linux-vli/sharelv is now 786432 blocks long.

3. 缩小LV

[vic@localhost ~]$ sudo lvreduce -L -3G /dev/linux-vli/sharelv
  WARNING: Reducing active logical volume to 4.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce linux-vli/sharelv? [y/n]: y
  Size of logical volume linux-vli/sharelv changed from 7.00 GiB (1792 extents) to 4.00 GiB (1024 extents).
  Logical volume linux-vli/sharelv successfully resized.
[vic@localhost ~]$ sudo lvs
  LV      VG        Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root    centos    -wi-ao----  <6.20g                                                    
  swap    centos    -wi-ao---- 820.00m                                                    
  sharelv linux-vli -wi-a-----   4.00g                                                    
[vic@localhost ~]$ sudo mount /dev/linux-vli/sharelv /sharelv/
[vic@localhost ~]$ cat /sharelv/linuxlv 
i am linuxlv
[vic@localhost ~]$ df -h
Filesystem                      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root         6.2G  1.4G  4.9G  22% /
devtmpfs                        484M     0  484M   0% /dev
tmpfs                           496M     0  496M   0% /dev/shm
tmpfs                           496M  6.9M  490M   2% /run
tmpfs                           496M     0  496M   0% /sys/fs/cgroup
/dev/sda1                      1014M  132M  883M  14% /boot
tmpfs                           100M     0  100M   0% /run/user/0
tmpfs                           100M     0  100M   0% /run/user/1000
/dev/mapper/linux--vli-sharelv  2.9G   13M  2.7G   1% /sharelv

七. 缩小卷组

1. 将PV从卷组中移除

[vic@localhost ~]$ sudo vgreduce linux-vli /dev/sdd
  Removed "/dev/sdd" from volume group "linux-vli"
[vic@localhost ~]$ sudo vgdisplay 
  --- Volume group ---
  VG Name               linux-vli
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  6
  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               15.99 GiB
  PE Size               4.00 MiB
  Total PE              4094
  Alloc PE / Size       1024 / 4.00 GiB
  Free  PE / Size       3070 / 11.99 GiB
  VG UUID               0sJQDx-2Ijp-K0in-jdTs-1fvG-FW2n-n2dXND

 

posted on 2019-05-07 11:55  okay半个  阅读(331)  评论(0编辑  收藏  举报

导航