No.26 LVM邏輯卷管理
boot用來存放引導文件,不要基於LVM創建,開機的過程中不識別邏輯卷。
PV(Physical Volume,物理卷):物理分區,或整個物理磁盤,由PE(Physical Extent,基本單元)組成。
VG(Volume Group,卷組):一個或多個物理卷組成的整體。
LV(Logical Volume,邏輯卷):從卷組中分割出的一塊邏輯存儲空間,課通過格式化建立文件系統。
功能 | 物理卷 | 卷組 | 邏輯卷 |
scan(掃描) | pvscan | vgscan | lvscan |
create(創建) | pvcreate | vgcreate | lvcreate |
display(顯示) | pvdisplay | vgdisplay | lvdisplay |
remove(刪除) | pvremove | vgremove | lvremove |
extend(擴展) | vgextend | lvextend | |
reduce(減少) | vgreduce | lvreduce |
圖形管理程序:system-config-lvm
LVM創建過程:創建PV-->創建-->VG-->創建LV-->格式化LV-->掛載
LVM刪除過程:卸載-->刪除LV-->刪除VG-->刪除PV
pvcreate 設備路徑
vgcreate 卷組名 設備路徑
lvcreate -L 大小 -n 邏輯卷名 卷組名
創建LVM實驗步驟:
添加一塊硬盤20GB:
[root@node0 ~]# lsblk # 查看添加的硬盤
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 3.6G 0 rom /dvd
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 200M 0 part /boot
├─sda2 8:2 0 1000M 0 part [SWAP]
└─sda3 8:3 0 18.8G 0 part /
sdb 8:16 0 20G 0 disk
[root@node0 ~]# parted /dev/sdb # 對硬盤進行分區
GNU Parted 2.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel # 創建分區表
New disk label type? gpt # 分區表類型為GPT
(parted) mkpart # 創建分區
Partition name? []?
File system type? [ext2]? ext4 # 寫入文件系統的類型
Start? 0 # 開始位置
End? 50% # 結束位置
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? i # 忽略
(parted) p # 顯示分區結果
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number Start End Size File system Name Flags
1 17.4kB 10.7GB 10.7GB
(parted) mkpart
Partition name? []?
File system type? [ext2]? ext4
Start? 50%
End? 100%
(parted) p
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number Start End Size File system Name Flags
1 17.4kB 10.7GB 10.7GB
2 10.7GB 21.5GB 10.7GB
(parted) quit
[root@node0 ~]# pvscan # 掃描現有的PV
No matching physical volumes found
[root@node0 ~]# pvcreate /dev/sdb1 /dev/sdb2 # 創建PV
Physical volume "/dev/sdb1" successfully created
Physical volume "/dev/sdb2" successfully created
[root@node0 ~]# vgcreate vg01 /dev/sdb1 /dev/sdb2
Volume group "vg01" successfully created
[root@node0 ~]# vgscan # 掃描現有的VG
Reading all physical volumes. This may take a while...
Found volume group "vg01" using metadata type lvm2
[root@node0 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg01 2 0 0 wz--n- 19.99g 19.99g
[root@node0 ~]# lvcreate -L 15GB -n lv01 vg01 # 創建LV
Logical volume "lv01" created.
[root@node0 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv01 vg01 -wi-a----- 15.00g
[root@node0 ~]# lvscan # 掃描現有的LV
ACTIVE '/dev/vg01/lv01' [15.00 GiB] inherit
[root@node0 ~]# mkfs.ext4 /dev/vg01/lv01 # 格式化LV
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
983040 inodes, 3932160 blocks
196608 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4026531840
120 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 39 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@node0 ~]# lsblk # 查看設備
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 3.6G 0 rom /dvd
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 200M 0 part /boot
├─sda2 8:2 0 1000M 0 part [SWAP]
└─sda3 8:3 0 18.8G 0 part /
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 10G 0 part
│ └─vg01-lv01 (dm-0) 253:0 0 15G 0 lvm
└─sdb2 8:18 0 10G 0 part
└─vg01-lv01 (dm-0) 253:0 0 15G 0 lvm
擴展、縮減LV:
[root@node0 ~]# lvextend -L +2GB /dev/vg01/lv01 # 擴展LV
Size of logical volume vg01/lv01 changed from 15.00 GiB (3840 extents) to 17.00 GiB (4352 extents).
Logical volume lv01 successfully resized
[root@node0 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv01 vg01 -wi-a----- 17.00g
[root@node0 ~]# lvreduce -L -1GB /dev/vg01/lv01 # 減小LV
WARNING: Reducing active logical volume to 16.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv01? [y/n]: y
Size of logical volume vg01/lv01 changed from 17.00 GiB (4352 extents) to 16.00 GiB (4096 extents).
Logical volume lv01 successfully resized
擴展、縮減VG:
[root@node0 ~]# lsblk # 添加硬盤
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 3.6G 0 rom /dvd
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 200M 0 part /boot
├─sda2 8:2 0 1000M 0 part [SWAP]
└─sda3 8:3 0 18.8G 0 part /
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 10G 0 part
│ └─vg01-lv01 (dm-0) 253:0 0 16G 0 lvm
└─sdb2 8:18 0 10G 0 part
└─vg01-lv01 (dm-0) 253:0 0 16G 0 lvm
sdc 8:32 0 20G 0 disk
[root@node0 ~]# parted /dev/sdc # 為新添加的硬盤分區
GNU Parted 2.1
Using /dev/sdc
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mkl
New disk label type? Gpt # 創建分區表
(parted) mkpart # 創建分區
Partition name? []?
File system type? [ext2]? ext4
Start? 0%
End? 100%
(parted) p # 查看結果
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdc: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number Start End Size File system Name Flags
1 1049kB 21.5GB 21.5GB
(parted) quit # 退出parted分區工具
Information: You may need to update /etc/fstab.
[root@node0 ~]# lsblk # 查看結果
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 3.6G 0 rom /dvd
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 200M 0 part /boot
├─sda2 8:2 0 1000M 0 part [SWAP]
└─sda3 8:3 0 18.8G 0 part /
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 10G 0 part
│ └─vg01-lv01 (dm-0) 253:0 0 16G 0 lvm
└─sdb2 8:18 0 10G 0 part
└─vg01-lv01 (dm-0) 253:0 0 16G 0 lvm
sdc 8:32 0 20G 0 disk
└─sdc1 8:33 0 20G 0 part
[root@node0 ~]# pvcreate /dev/sdc1 # 創建PV
Physical volume "/dev/sdc1" successfully created
[root@node0 ~]# vgextend vg01 /dev/sdc1 # 擴展VG
Volume group "vg01" successfully extended
[root@node0 ~]# vgs # 查看擴展結果
VG #PV #LV #SN Attr VSize VFree
vg01 3 1 0 wz--n- 39.99g 23.99g
root@node0 ~]# lvextend -L +10GB -n /dev/vg01/lv01 # 擴展LV
Size of logical volume vg01/lv01 changed from 16.00 GiB (4096 extents) to 26.00 GiB (6656 extents).
Logical volume lv01 successfully resized
[root@node0 ~]# lvs # 查看擴展結果
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv01 vg01 -wi-a----- 26.00g
掛載LV:
[root@node0 ~]# mkdir /lv01_data # 創建用於掛載的目錄
[root@node0 ~]# ll /dev/vg01/lv01
lrwxrwxrwx. 1 root root 7 Nov 20 16:59 /dev/vg01/lv01 -> ../dm-0
[root@node0 ~]# vim /etc/fstab # 編輯/etc/fstab配置文件
/dev/vg01/lv01 /lv01_data ext4 defaults 0 0
[root@node0 ~]# mount -a # 刷新/etc/fstab
[root@node0 ~]# lsblk # 查看結果
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 3.6G 0 rom /dvd
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 200M 0 part /boot
├─sda2 8:2 0 1000M 0 part [SWAP]
└─sda3 8:3 0 18.8G 0 part /
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 10G 0 part
│ └─vg01-lv01 (dm-0) 253:0 0 26G 0 lvm /lv01_data
└─sdb2 8:18 0 10G 0 part
└─vg01-lv01 (dm-0) 253:0 0 26G 0 lvm /lv01_data
sdc 8:32 0 20G 0 disk
└─sdc1 8:33 0 20G 0 part
└─vg01-lv01 (dm-0) 253:0 0 26G 0 lvm /lv01_data
縮減文件系統:
[root@node0 ~]# resize2fs /dev/vg01/lv01 10G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg01/lv01 to 2621440 (4k) blocks.
The filesystem on /dev/vg01/lv01 is now 2621440 blocks long.
刪除LVM:
[root@node0 ~]# umount /lv01_data/ # 卸載文件系統
[root@node0 ~]# lvremove /dev/vg01/lv01 # 刪除LV
Do you really want to remove active logical volume lv01? [y/n]: y # 確認刪除
Logical volume "lv01" successfully removed
[root@node0 ~]# lvs # 查看結果
[root@node0 ~]# vgremove /dev/vg01 # 刪除VG
Volume group "vg01" successfully removed
[root@node0 ~]# vgs # 查看結果
[root@node0 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 3.6G 0 rom /dvd
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 200M 0 part /boot
├─sda2 8:2 0 1000M 0 part [SWAP]
└─sda3 8:3 0 18.8G 0 part /
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 10G 0 part
└─sdb2 8:18 0 10G 0 part
sdc 8:32 0 20G 0 disk
└─sdc1 8:33 0 20G 0 part
[root@node0 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 lvm2 --- 10.00g 10.00g
/dev/sdb2 lvm2 --- 10.00g 10.00g
/dev/sdc1 lvm2 --- 20.00g 20.00g
[root@node0 ~]# pvremove /dev/sdb1 /dev/sdb2 /dev/sdc1 # 刪除PV
Labels on physical volume "/dev/sdb1" successfully wiped
Labels on physical volume "/dev/sdb2" successfully wiped
Labels on physical volume "/dev/sdc1" successfully wiped
[root@node0 ~]# pvs # 查看結果
刪除/etc/fstab文件中的配置條目。
No.26 LVM邏輯卷管理