天天學五分鐘 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 容量大小

 

 
完! 
相關文章
相關標籤/搜索