每天学五分钟 Liunx 101 | 存储篇:LVM


LVM

LVM(Logical Volume Manager),逻辑卷管理器。一种高级文件系统管理方式,它可以动态扩展文件系统。
 
LVM 的示意图如下所示:
 
partition:磁盘分区。
PV:Physical Volume,实体卷,由磁盘分区生成。
VG:Volume Group,卷组,由多个 PV 组成。
LV:Logical Volume,逻辑卷,从 VG 中划分出来。LV 可以制成文件系统再挂载,从而为用户所用。
 
之所以可以动态扩展文件系统,主要是有 PE 这个东西,PE(Physical Extend,实体扩展块)是 LVM 最小的存储单位,类似于文件系统的 block:
 
扩展 LV 的时候将 VG 中不用的 PE 加入到 LV 中。同样的,缩小 LV 的时候将 LV 中不用的 PE 给移出去。
 
制作 LV 流程:
1. 由于测试机空间不够,分区分不出来,这里用 dd 命令将文件挂载到 loop 设备以实现文件的分区,效果是一样一样的。
[root@test:/home/robot/test]
# dd if=/dev/zero of=/home/robot/test/lvm1 bs=1M count=512
512+0 records in
512+0 records out
536870912 bytes (537 MB, 512 MiB) copied, 0.474638 s, 1.1 GB/s
[root@test:/home/robot/test]
# dd if=/dev/zero of=/home/robot/test/lvm2 bs=1M count=512
512+0 records in
512+0 records out
536870912 bytes (537 MB, 512 MiB) copied, 0.49283 s, 1.1 GB/s
[root@test:/home/robot/test]
# dd if=/dev/zero of=/home/robot/test/lvm3 bs=1M count=512
512+0 records in
512+0 records out
536870912 bytes (537 MB, 512 MiB) copied, 0.48691 s, 1.1 GB/s
[root@test:/home/robot/test]
#
[root@test:/home/robot/test]
# losetup /dev/loop1 /home/robot/test/lvm1
[root@test:/home/robot/test]
# losetup /dev/loop2 /home/robot/test/lvm2
[root@test:/home/robot/test]
# losetup /dev/loop3 /home/robot/test/lvm3
[root@test:/home/robot/test]
# ll
total 1572876
-rw-r--r-- 1 root root 536870912 Mar 16 12:28 lvm1
-rw-r--r-- 1 root root 536870912 Mar 16 12:28 lvm2
-rw-r--r-- 1 root root 536870912 Mar 16 12:28 lvm3
三个分区 /dev/loop1 /dev/loop2 和 /dev/loop3 制作完毕!
 
2. pvcreate 命令将分区转成 PV。
[root@test:/home/robot/test]
# pvdisplay
[root@test:/home/robot/test]
# pvcreate /dev/loop1
  Physical volume "/dev/loop1" successfully created.
[root@test:/home/robot/test]
# pvcreate /dev/loop2
  Physical volume "/dev/loop2" successfully created.
[root@test:/home/robot/test]
# pvcreate /dev/loop3
  Physical volume "/dev/loop3" successfully created.
[root@test:/home/robot/test]
# pvdisplay
  "/dev/loop3" is a new physical volume of "512.00 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/loop3
  VG Name
  PV Size               512.00 MiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               PpWfyJ-1lSY-iNZY-MU0K-wQfz-zrh0-n9YnCF
 
  "/dev/loop1" is a new physical volume of "512.00 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/loop1
  VG Name
  PV Size               512.00 MiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               9cUDS8-9VGN-cvnS-8tCF-prIy-eRYS-Y83es9
 
  "/dev/loop2" is a new physical volume of "512.00 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/loop2
  VG Name
  PV Size               512.00 MiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               fi632i-W46C-dvfw-9YCt-Z08g-rUZB-Hmakox

 

3. vgcreate 命令将 PV 转成 VG。 
[root@test:/home/robot/test]
# vgdisplay
[root@test:/home/robot/test]
# vgcreate -s 4M lianhuasheng /dev/loop{1,2,3}
  Volume group "lianhuasheng" successfully created
[root@test:/home/robot/test]
# vgdisplay
  --- Volume group ---
  VG Name               lianhuasheng
  System ID
  Format                lvm2
  Metadata Areas        3
  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                3
  Act PV                3
  VG Size               <1.49 GiB
  PE Size               4.00 MiB
  Total PE              381
  Alloc PE / Size       0 / 0
  Free  PE / Size       381 / <1.49 GiB
  VG UUID               Y4l3Ad-bdIJ-7V5A-3FI1-kR74-QwDg-DbRdJ0
 
4. lvreate 命令将 VG 转成 LV。
[root@test:/home/robot/test]
# lvcreate -l 50 -n lv1 lianhuasheng
  Logical volume "lv1" created.
[root@test:/home/robot/test]
# lvdisplay
  --- Logical volume ---
  LV Path                /dev/lianhuasheng/lv1
  LV Name                lv1
  VG Name                lianhuasheng
  LV UUID                jQU1SV-tIzt-SRet-L5XK-rk1K-YC3h-o7YT7Q
  LV Write Access        read/write
  LV Creation host, time test, 2020-03-16 22:27:14 +0800
  LV Status              available
  # open                 0
  LV Size                200.00 MiB
  Current LE             50
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:8
每个 PE 的大小是 4M,lv1 有 50 个 PE,所以 LV Size 是 200M。
 
5. 将 lv1 制成文件系统,挂载:
[root@test:/home/robot/test]
# mkfs -t ext4 /dev/lianhuasheng/lv1
mke2fs 1.45.5 (07-Jan-2020)
Discarding device blocks: done
Creating filesystem with 204800 1k blocks and 51200 inodes
Filesystem UUID: 39a25411-b81c-42c0-b1d4-cb7dd1119e87
Superblock backups stored on blocks:
        8193, 24577, 40961, 57345, 73729
 
Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
 
[root@test:/home/robot/test]
# ls
lvm1  lvm2  lvm3
[root@test:/home/robot/test]
# mkdir mountPoint
[root@test:/home/robot/test]
# mount /dev/lianhuasheng/lv1 /home/robot/test/mountPoint/
[root@test:/home/robot/test]
# df -hT
Filesystem                                                 Type      Size  Used Avail Use% Mounted on
/dev/vda1                                                  ext4       40G  8.9G   29G  24% /
/dev/mapper/lianhuasheng-lv1                               ext4      190M  1.6M  175M   1% /home/robot/test/mountPoint
注意 lv 挂载之后的文件系统名字变成了 /dev/mapper/<vg_name>-<lv_name> 的形式。并且,可以发现文件系统的 Size 成了 190M,而分区的 Size 是 200M,说明文件系统自身也需要占用磁盘空间。
 
即然 LVM 是动态可扩展的(LVM 最大的优点),那么现在想给 lv 在增加 200M 空间怎么做呢?
可以通过 lvresize 命令扩展 lv:
[root@test:/home/robot/test/mountPoint]
# lvdisplay
  --- Logical volume ---
  LV Path                /dev/lianhuasheng/lv1
  LV Name                lv1
  VG Name                lianhuasheng
  LV UUID                jQU1SV-tIzt-SRet-L5XK-rk1K-YC3h-o7YT7Q
  LV Write Access        read/write
  LV Creation host, time test, 2020-03-16 22:27:14 +0800
  LV Status              available
  # open                 1
  LV Size                200.00 MiB
  Current LE             50
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:8
[root@test:/home/robot/test/mountPoint]
# df -hT
Filesystem                                                 Type      Size  Used Avail Use% Mounted on
/dev/vda1                                                  ext4       40G  8.9G   29G  24% /
/dev/mapper/lianhuasheng-lv1                               ext4      190M  1.6M  175M   1% /home/robot/test/mountPoint
[root@test:/home/robot/test/mountPoint]
# touch test.log
[root@test:/home/robot/test/mountPoint]
# ll -hi
total 12K
11 drwx------ 2 root root 12K Mar 16 22:31 lost+found
12 -rw-r--r-- 1 root root   0 Mar 16 22:43 test.log
 
[root@test:/home/robot/test/mountPoint]
# lvresize -l +50 /dev/lianhuasheng/lv1
 
[root@test:/home/robot/test/mountPoint]
# lvdisplay
  --- Logical volume ---
  LV Path                /dev/lianhuasheng/lv1
  LV Name                lv1
  VG Name                lianhuasheng
  LV UUID                jQU1SV-tIzt-SRet-L5XK-rk1K-YC3h-o7YT7Q
  LV Write Access        read/write
  LV Creation host, time test, 2020-03-16 22:27:14 +0800
  LV Status              available
  # open                 1
  LV Size                400.00 MiB
  Current LE             100
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:8
[root@test:/home/robot/test/mountPoint]
# df -hT
Filesystem                                                 Type      Size  Used Avail Use% Mounted on
/dev/vda1                                                  ext4       40G  8.9G   29G  24% /
/dev/mapper/lianhuasheng-lv1                               ext4      190M  1.6M  175M   1% /home/robot/test/mountPoint
[root@test:
/home/robot/test/mountPoint] # resize2fs /dev/mapper/lianhuasheng-lv1 resize2fs 1.45.5 (07-Jan-2020) Filesystem at /dev/mapper/lianhuasheng-lv1 is mounted on /home/robot/test/mountPoint; on-line resizing required old_desc_blocks = 2, new_desc_blocks = 4 The filesystem on /dev/mapper/lianhuasheng-lv1 is now 409600 (1k) blocks long. [root@test:/home/robot/test/mountPoint] # df -hT Filesystem Type Size Used Avail Use% Mounted on /dev/vda1 ext4 40G 8.9G 29G 24% / /dev/mapper/lianhuasheng-lv1 ext4 384M 2.3M 360M 1% /home/robot/test/mountPoint [root@test:/home/robot/test/mountPoint] # ll -hi total 12K 11 drwx------ 2 root root 12K Mar 16 22:31 lost+found 12 -rw-r--r-- 1 root root 0 Mar 16 22:43 test.log
通过 lvextend 将文件系统在线扩展,不需要 umount (需要用 resieze2fs 命令更新文件系统),并且不影响原始文件。
 

卸载 LVM

1. umount 文件系统;
2. lvremove 删除 lv;
3. vgremove 删除 VG;
4. pvremove 删除 PV;
[root@test:/home/robot/test/mountPoint]
# df -hT
Filesystem                                                 Type      Size  Used Avail Use% Mounted on
/dev/vda1                                                  ext4       40G  8.9G   29G  24% /
/dev/mapper/lianhuasheng-lv1                               ext4      384M  2.3M  360M   1% /home/robot/test/mountPoint
[root@test:/home/robot/test/mountPoint]
# cd ..
[root@test:/home/robot/test]
# umount /home/robot/test/mountPoint/
[root@test:/home/robot/test]
# lvremove /dev/lianhuasheng/lv1
Do you really want to remove active logical volume lianhuasheng/lv1? [y/n]: y
  Logical volume "lv1" successfully removed
[root@test:/home/robot/test]
# vgremove lianhuasheng
  Volume group "lianhuasheng" successfully removed
[root@test:/home/robot/test]
# pvremove /dev/loop{1,2,3}
  Labels on physical volume "/dev/loop1" successfully wiped.
  Labels on physical volume "/dev/loop2" successfully wiped.
  Labels on physical volume "/dev/loop3" successfully wiped.
[root@test:/home/robot/test]
# ll -hi
total 1.4G
656227 -rw-r--r-- 1 root root 512M Mar 16 22:58 lvm1
656229 -rw-r--r-- 1 root root 512M Mar 16 22:58 lvm2
656230 -rw-r--r-- 1 root root 512M Mar 16 22:58 lvm3
[root@test:/home/robot/test]
# rm -rf lvm{1,2,3}
[root@test:/home/robot/test]
# ll -hi
 

LVM 命令

PV
pvcreate                创建实体 partition 为 PV
pvscan                  搜寻系统磁盘是否有 PV
pvdisplay               显示 PV 状态
pvremove                移除 PV 移除,partition 不具有 PV 属性
 
VG
vgcreate               创建 VG 
vgscan                 搜寻系统是否有 VG
vgdisplay              显示 VG 状态
vgextend               在 VG 内添加额外的 PV
vgreduce               在 VG 内移除 PV
vgchange               配置 VG 是否启动 (active)
vgremove               移除 VG 
 
LV
lvcreate              创建 LV
lvscan                查询系统是否有 LV
lvdisplay             显示 LV 状态
lvextend              扩展 LV 容量
lvreduce              缩小 LV 容量
lvremove              删除 LV
lvresize              调整 LV 容量大小

 

 
(完)
 
posted @ 2020-03-16 23:27  hxia043  阅读(559)  评论(0编辑  收藏  举报