1、邏輯卷簡要說明
將設備指定為物理卷
用一個或者多個物理卷來創建一個卷組
物理卷是用固定大小的物理區域(Physical Extent,PE)來定義的
在物理卷上創建的邏輯卷是由物理區域(PE)組成
可以在邏輯卷上創建文件系統
Paste_Image.png
2、創建邏輯卷
[root@centos7 ~]#pvcreate /dev/sdb /dev/sdc1 ---創建物理卷
Physical volume "/dev/sdb" successfully created.
Physical volume "/dev/sdc1" successfully created.
[root@centos7 ~]#pvs ---查看
PV VG Fmt Attr PSize PFree
/dev/sdb lvm2 --- 20.00g 20.00g
/dev/sdc1 lvm2 --- 15.00g 15.00g
[root@centos7 ~]#pvdisplay ---詳細查看
"/dev/sdb" is a new physical volume of "20.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb
VG Name
PV Size 20.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 72dJR7-GCfL-GOHH-Tg9a-IIbl-EgTD-YLrRGz
"/dev/sdc1" is a new physical volume of "15.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdc1
VG Name
PV Size 15.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID j8i7QQ-sSR2-bsJn-UlJj-a1jY-Ubum-KRYgWx
[root@centos7 ~]#vgcreate vg0 /dev/sd{b,c1} ----創建卷組,可以用-s選項指定PE的大小
Volume group "vg0" successfully created
[root@centos7 ~]#pvdisplay ---創建完卷組后再來看物理卷
--- Physical volume ---
PV Name /dev/sdb
VG Name vg0 ---卷組名
PV Size 20.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB ---PE大小
Total PE 5119 ---PE個數邏輯卷,分邏輯卷時是把PE分給
Free PE 5119
Allocated PE 0
PV UUID 72dJR7-GCfL-GOHH-Tg9a-IIbl-EgTD-YLrRGz
--- Physical volume ---
PV Name /dev/sdc1
VG Name vg0
PV Size 15.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 3839
Free PE 3839
Allocated PE 0
PV UUID j8i7QQ-sSR2-bsJn-UlJj-a1jY-Ubum-KRYgWx
[root@centos7 ~]#vgs ---查看卷組
VG #PV #LV #SN Attr VSize VFree
vg0 2 0 0 wz--n- 34.99g 34.99g
[root@centos7 ~]#vgdisplay ---詳細查看
--- Volume group ---
VG Name vg0
System ID
Format lvm2
Metadata Areas 2
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 2
Act PV 2
VG Size 34.99 GiB
PE Size 4.00 MiB
Total PE 8958
Alloc PE / Size 0 / 0
Free PE / Size 8958 / 34.99 GiB
VG UUID HMIGkc-kO3K-sd6j-YuPc-i6ez-IOqY-KIReKJ
[root@centos7 ~]#lvcreate -n lv0 -L 15G vg0 ---創建邏輯卷
Logical volume "lv0" created.
[root@centos7 ~]#lvs ---查看邏輯卷
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv0 vg0 -wi-a----- 15.00g
[root@centos7 ~]#lvdisplay ---詳細查看
--- Logical volume ---
LV Path /dev/vg0/lv0
LV Name lv0
VG Name vg0
LV UUID HH3DXN-bMUd-ebeu-89Vs-ifnw-XYE5-aEBotX
LV Write Access read/write
LV Creation host, time centos7.magedu.com, 2017-08-12 11:49:32 +0800
LV Status available
# open 0
LV Size 15.00 GiB
Current LE 3840
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
[root@centos7 ~]#ll /dev/vg0/lv0 ---可以看到邏輯卷的設備名為一個軟連接
lrwxrwxrwx. 1 root root 7 Aug 12 11:49 /dev/vg0/lv0 -> ../dm-0
[root@centos7 ~]#lvcreate -n lv1 -L 10G vg0 ---再創建一個邏輯卷
Logical volume "lv1" created.
[root@centos7 ~]#lvdisplay ---此時有兩個邏輯卷
--- Logical volume ---
LV Path /dev/vg0/lv0
LV Name lv0
VG Name vg0
LV UUID HH3DXN-bMUd-ebeu-89Vs-ifnw-XYE5-aEBotX
LV Write Access read/write
LV Creation host, time centos7.magedu.com, 2017-08-12 11:49:32 +0800
LV Status available
# open 0
LV Size 15.00 GiB
Current LE 3840
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
--- Logical volume ---
LV Path /dev/vg0/lv1
LV Name lv1
VG Name vg0
LV UUID RAIOfB-sJ1U-0wGj-FPjn-53SZ-rBeE-AvsvBp
LV Write Access read/write
LV Creation host, time centos7.magedu.com, 2017-08-12 12:00:14 +0800
LV Status available
# open 0
LV Size 10.00 GiB
Current LE 2560
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1
[root@centos7 ~]#mkfs.ext4 /dev/vg0/lv0 ---創建文件系統
[root@centos7 ~]#mkdir /mnt/lv0
[root@centos7 ~]#mount /dev/vg0/lv0 /mnt/lv0/ ---掛載
[root@centos7 ~]#df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 52403200 15338660 37064540 30% /
devtmpfs 485268 0 485268 0% /dev
tmpfs 499980 88 499892 1% /dev/shm
tmpfs 499980 13544 486436 3% /run
tmpfs 499980 0 499980 0% /sys/fs/cgroup
/dev/sda3 52403200 540228 51862972 2% /app
/dev/sda1 1038336 172284 866052 17% /boot
tmpfs 100000 16 99984 1% /run/user/0
/dev/sr0 8086368 8086368 0 100% /run/media/root/CentOS 7 x86_64
/dev/mapper/vg0-lv0 15350728 40984 14506928 1% /mnt/lv0
總結:創建邏輯卷的過程是在磁盤或者分區上先創建物理卷,如果是分區,創建分區時要指定id號為LVM,之后再創建卷組,通過卷組再創建邏輯卷,因為邏輯卷是通過卷組創建的,所以卷組中創建了PE
,默認PE大小為4MiB,用它來指定邏輯卷的最小塊的單位,在創建卷組時可以通過-s來指定PE的大小來指定邏輯卷中塊的大小,在沒有創建卷組時物理卷中的PE是沒有的,創建之后物理卷中才能看到。
3、擴展邏輯卷
[root@centos7 app]#vgdisplay ---首先要看卷組里是否有剩余空間
--- Volume group ---
VG Name vg0
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 1
Max PV 0
Cur PV 2
Act PV 2
VG Size 34.99 GiB
PE Size 4.00 MiB
Total PE 8958
Alloc PE / Size 6400 / 25.00 GiB
Free PE / Size 2558 / 9.99 GiB ----卷組剩下空間
VG UUID HMIGkc-kO3K-sd6j-YuPc-i6ez-IOqY-KIReKJ
①沒有剩余空間
[root@centos7 app]#pvcreate /dev/sdc2
WARNING: dos signature detected on /dev/sdc2 at offset 510. Wipe it? [y/n]: y
Wiping dos signature on /dev/sdc2.
Physical volume "/dev/sdc2" successfully created. ---創建物理卷
[root@centos7 app]#vgextend vg0 /dev/sdc2 ---擴展卷組,將物理卷加到卷組里
Volume group "vg0" successfully extended
[root@centos7 app]#vgdisplay
--- Volume group ---
VG Name vg0
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 1
Max PV 0
Cur PV 3
Act PV 3
VG Size 54.99 GiB
PE Size 4.00 MiB
Total PE 14077
Alloc PE / Size 6400 / 25.00 GiB
Free PE / Size 7677 / 29.99 GiB ---卷組的空間擴大
VG UUID HMIGkc-kO3K-sd6j-YuPc-i6ez-IOqY-KIReKJ
[root@centos7 app]#lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv0 vg0 -wi-ao---- 15.00g
lv1 vg0 -wi-a----- 10.00g
[root@centos7 app]#lvextend -L 20g /dev/vg0/lv0 ---擴展邏輯卷到20G
Size of logical volume vg0/lv0 changed from 15.00 GiB (3840 extents) to 20.00 GiB (5120 extents).
Logical volume vg0/lv0 successfully resized.
[root@centos7 app]#lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv0 vg0 -wi-ao---- 20.00g
lv1 vg0 -wi-a----- 10.00g
[root@centos7 app]#df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 50G 15G 36G 30% /
devtmpfs 474M 0 474M 0% /dev
tmpfs 489M 88K 489M 1% /dev/shm
tmpfs 489M 14M 476M 3% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda3 50G 1.1G 49G 3% /app
/dev/sda1 1014M 169M 846M 17% /boot
tmpfs 98M 16K 98M 1% /run/user/0
/dev/sr0 7.8G 7.8G 0 100% /run/media/root/CentOS 7 x86_64
/dev/mapper/vg0-lv0 15G 541M 14G 4% /mnt/lv0 ---此時文件系統顯示的大小還是15G,說明文件系統沒有同步
如果是xfs文件系統:xfs_growfs /mnt/lv0
[root@centos7 app]#resize2fs /dev/vg0/lv0 20G ---讓文件系統同步
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg0/lv0 is mounted on /mnt/lv0; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 3
The filesystem on /dev/vg0/lv0 is now 5242880 blocks long.
[root@centos7 app]#df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 xfs 50G 15G 36G 30% /
devtmpfs devtmpfs 474M 0 474M 0% /dev
tmpfs tmpfs 489M 88K 489M 1% /dev/shm
tmpfs tmpfs 489M 14M 476M 3% /run
tmpfs tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda3 xfs 50G 1.1G 49G 3% /app
/dev/sda1 xfs 1014M 169M 846M 17% /boot
tmpfs tmpfs 98M 16K 98M 1% /run/user/0
/dev/sr0 iso9660 7.8G 7.8G 0 100% /run/media/root/CentOS 7 x86_64
/dev/mapper/vg0-lv0 ext4 20G 544M 19G 3% /mnt/lv0 ---已經同步
②有剩余空間
[root@centos7 app]#mkfs.xfs /dev/vg0/lv1 ---對邏輯卷lv1創建文件系統
[root@centos7 app]#mount /dev/vg0/lv1 /mnt/lv1 ---掛載
[root@centos7 app]#df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 50G 15G 36G 30% /
devtmpfs 474M 0 474M 0% /dev
tmpfs 489M 88K 489M 1% /dev/shm
tmpfs 489M 14M 476M 3% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda3 50G 1.1G 49G 3% /app
/dev/sda1 1014M 169M 846M 17% /boot
tmpfs 98M 16K 98M 1% /run/user/0
/dev/sr0 7.8G 7.8G 0 100% /run/media/root/CentOS 7 x86_64
/dev/mapper/vg0-lv0 20G 544M 19G 3% /mnt/lv0
/dev/mapper/vg0-lv1 10G 33M 10G 1% /mnt/lv1
[root@centos7 app]#lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv0 vg0 -wi-ao---- 20.00g
lv1 vg0 -wi-ao---- 10.00g
[root@centos7 app]#lvextend -r -L +5G /dev/vg0/lv1 ---將lv1邏輯卷增加5G,-r選項可以實現自動同步文件系統。
Size of logical volume vg0/lv1 changed from 10.00 GiB (2560 extents) to 15.00 GiB (3840 extents).
Logical volume vg0/lv1 successfully resized.
meta-data=/dev/mapper/vg0-lv1 isize=512 agcount=4, agsize=655360 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=2621440, 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 2621440 to 3932160
[root@centos7 app]#lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv0 vg0 -wi-ao---- 20.00g
lv1 vg0 -wi-ao---- 15.00g ---已增加
[root@centos7 app]#df -h ---我們看到已經自動同步文件系統
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 50G 15G 36G 30% /
devtmpfs 474M 0 474M 0% /dev
tmpfs 489M 88K 489M 1% /dev/shm
tmpfs 489M 14M 476M 3% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda3 50G 1.1G 49G 3% /app
/dev/sda1 1014M 169M 846M 17% /boot
tmpfs 98M 16K 98M 1% /run/user/0
/dev/sr0 7.8G 7.8G 0 100% /run/media/root/CentOS 7 x86_64
/dev/mapper/vg0-lv0 20G 544M 19G 3% /mnt/lv0
/dev/mapper/vg0-lv1 15G 33M 15G 1% /mnt/lv1
4、縮減邏輯卷
只有邏輯卷是ext4文件系統的才能縮減邏輯卷,生產中一般不會縮減邏輯卷,有可能會破壞數據
[root@centos7 lv0]#df -T --這里我們縮減lv0邏輯卷
Filesystem Type 1K-blocks Used Available Use% Mounted on
/dev/sda2 xfs 52403200 15338912 37064288 30% /
devtmpfs devtmpfs 485268 0 485268 0% /dev
tmpfs tmpfs 499980 88 499892 1% /dev/shm
tmpfs tmpfs 499980 13548 486432 3% /run
tmpfs tmpfs 499980 0 499980 0% /sys/fs/cgroup
/dev/sda3 xfs 52403200 1052228 51350972 3% /app
/dev/sda1 xfs 1038336 172284 866052 17% /boot
tmpfs tmpfs 100000 16 99984 1% /run/user/0
/dev/sr0 iso9660 8086368 8086368 0 100% /run/media/root/CentOS 7 x86_64
/dev/mapper/vg0-lv0 ext4 20511312 557040 18931260 3% /mnt/lv0
/dev/mapper/vg0-lv1 xfs 15718400 71272 15647128 1% /mnt/lv1
[root@centos7 lv0]#umount /mnt/lv0 ---第一步取消掛載
[root@centos7 ~]#fsck -f /dev/vg0/lv0 ---第二步檢查文件系統
fsck from util-linux 2.23.2
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/mapper/vg0-lv0: 12/1310720 files (0.0% non-contiguous), 126323/5242880 blocks
[root@centos7 ~]#resize2fs /dev/vg0/lv0 18G ---第三步縮減文件系統到18G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg0/lv0 to 4718592 (4k) blocks.
The filesystem on /dev/vg0/lv0 is now 4718592 blocks long.
[root@centos7 ~]#lvreduce -L 18G /dev/vg0/lv0 ---第四步縮減邏輯卷到18G
WARNING: Reducing active logical volume to 18.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg0/lv0? [y/n]: y
Size of logical volume vg0/lv0 changed from 20.00 GiB (5120 extents) to 18.00 GiB (4608 extents).
Logical volume vg0/lv0 successfully resized.
[root@centos7 ~]#lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv0 vg0 -wi-ao---- 18.00g ---已經縮減
lv1 vg0 -wi-ao---- 15.00g
[root@centos7 ~]#mount -a
[root@centos7 ~]#df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 xfs 50G 15G 36G 30% /
devtmpfs devtmpfs 474M 0 474M 0% /dev
tmpfs tmpfs 489M 88K 489M 1% /dev/shm
tmpfs tmpfs 489M 20M 469M 4% /run
tmpfs tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda3 xfs 50G 1.1G 49G 3% /app
/dev/sda1 xfs 1014M 169M 846M 17% /boot
tmpfs tmpfs 98M 16K 98M 1% /run/user/0
/dev/sr0 iso9660 7.8G 7.8G 0 100% /run/media/root/CentOS 7 x86_64
/dev/mapper/vg0-lv1 xfs 15G 70M 15G 1% /mnt/lv1
/dev/mapper/vg0-lv0 ext4 18G 45M 17G 1% /mnt/lv0
總結:縮減邏輯卷的過程中是先取消掛載,這一步很重要,不要忘記,然后檢查文件系統,縮減文件系統,縮減邏輯卷,要按照這個過程一步步的來。
5、遷移卷組
[root@centos6 ~]#umount /mnt/lv0/ ---第一步取消掛載
[root@centos6 ~]#df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 50264772 4786024 42918748 11% /
tmpfs 502056 76 501980 1% /dev/shm
/dev/sda3 40185208 561020 37576188 2% /app
/dev/sda1 991512 34904 905408 4% /boot
/dev/sr0 6049912 6049912 0 100% /media/CDROM
[root@centos6 ~]#vgrename vg0 newvg0 ---第二步為了和其他卷組的名字沖突,重新定義卷組和邏輯卷的名字
Volume group "vg0" successfully renamed to "newvg0"
[root@centos6 ~]#lvrename /dev/newvg0/lv0 newlv0 ---邏輯卷的名字改不改都可以,卷組的名字變了,邏輯卷的名字也跟著變了
Renamed "lv0" to "newlv0" in volume group "newvg0"
[root@centos6 ~]#vgchange -an newvg0 ---第三步把卷組設為非活動狀態
0 logical volume(s) in volume group "newvg0" now active
[root@centos6 ~]#lvdisplay
--- Logical volume ---
LV Path /dev/newvg0/newlv0
LV Name newlv0
VG Name newvg0
LV UUID ua7BV0-2Zem-qtzq-Zeiv-DaM4-hA4K-2UKux0
LV Write Access read/write
LV Creation host, time centos6.magedu.com, 2017-08-09 16:46:39 +0800
LV Status NOT available ---查看已經處于不活動狀態
LV Size 8.00 GiB
Current LE 2048
Segments 2
Allocation inherit
Read ahead sectors auto
[root@centos6 ~]#vgexport newvg0
Volume group "newvg0" successfully exported ---第四步導出卷組
halt ---第五步關機,將硬盤裝入centos7
[root@centos7 ~]#echo '- - -'> /sys/class/scsi_host/host0/scan ---激活一下硬盤
[root@centos7 ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 50G 0 part /
├─sda3 8:3 0 50G 0 part /app
├─sda4 8:4 0 512B 0 part
└─sda5 8:5 0 2G 0 part [SWAP]
sdb 8:16 0 20G 0 disk
└─vg0-lv0 253:0 0 18G 0 lvm /mnt/lv0
sdc 8:32 0 150G 0 disk
├─sdc1 8:33 0 15G 0 part
│ └─vg0-lv1 253:1 0 15G 0 lvm /mnt/lv1
└─sdc2 8:34 0 20G 0 part
└─vg0-lv1 253:1 0 15G 0 lvm /mnt/lv1
sdd 8:48 0 120G 0 disk
sde 8:64 0 60G 0 disk ---可以看到這個磁盤了
├─sde1 8:65 0 5G 0 part
│ └─newvg0-newlv0 253:2 0 8G 0 lvm
└─sde2 8:66 0 5G 0 part
└─newvg0-newlv0 253:2 0 8G 0 lvm
sr0 11:0 1 7.7G 0 rom /run/media/root/CentOS 7 x86_64
[root@centos7 ~]#pvscan ---第六步可以看到新的卷組處于導出狀態
PV /dev/sdb VG vg0 lvm2 [20.00 GiB / 2.00 GiB free]
PV /dev/sdc1 VG vg0 lvm2 [15.00 GiB / 4.00 MiB free]
PV /dev/sdc2 VG vg0 lvm2 [20.00 GiB / 19.99 GiB free]
PV /dev/sde1 is in exported VG newvg0 [5.01 GiB / 0 free]
PV /dev/sde2 is in exported VG newvg0 [5.01 GiB / 2.02 GiB free]
Total: 5 [65.00 GiB] / in use: 5 [65.00 GiB] / in no VG: 0 [0 ]
root@centos7 ~]#vgimport newvg0 ---第七步導入卷組
Volume group "newvg0" successfully imported
root@centos7 ~]#vgchange -ay newvg0 ---第八步激活卷組
1 logical volume(s) in volume group "newvg0" now active
[root@centos7 ~]#mkdir /mnt/newlv0
[root@centos7 ~]#mount /dev/newvg0/newlv0 /mnt/newlv0/
[root@centos7 ~]#df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 52403200 15341732 37061468 30% /
devtmpfs 485268 0 485268 0% /dev
tmpfs 499980 128 499852 1% /dev/shm
tmpfs 499980 19816 480164 4% /run
tmpfs 499980 0 499980 0% /sys/fs/cgroup
/dev/sda3 52403200 1052228 51350972 3% /app
/dev/sda1 1038336 172284 866052 17% /boot
tmpfs 100000 20 99980 1% /run/user/0
/dev/sr0 8086368 8086368 0 100% /run/media/root/CentOS 7 x86_64
/dev/mapper/vg0-lv1 15718400 71272 15647128 1% /mnt/lv1
/dev/mapper/vg0-lv0 18447056 45084 17441872 1% /mnt/lv0
/dev/mapper/newvg0-newlv0 8125880 530424 7159644 7% /mnt/newlv0
[root@centos7 ~]#cd /mnt/newlv0/
[root@centos7 newlv0]#ls
f1 lost+found
可以看到文件沒有丟失
6、從邏輯卷中刪除正在使用的物理分區(硬盤)
[root@centos7 ~]#vgdisplay ---先看一下卷組
--- Volume group ---
VG Name vg0
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 10
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 1
Max PV 0
Cur PV 3
Act PV 3
VG Size 54.99 GiB
PE Size 4.00 MiB
Total PE 14077
Alloc PE / Size 8448 / 33.00 GiB
Free PE / Size 5629 / 21.99 GiB ---卷組的剩余空間是否滿足將硬盤或分期的空間轉移到其他物理卷
VG UUID HMIGkc-kO3K-sd6j-YuPc-i6ez-IOqY-KIReKJ
[root@centos7 ~]#pvdisplay ---這里我們可以把/dev/sdc2分區中已經分配出去的2個PE轉移到其他物理卷
--- Physical volume ---
PV Name /dev/sdb
VG Name vg0
PV Size 20.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 5119
Free PE 511
Allocated PE 4608
PV UUID 72dJR7-GCfL-GOHH-Tg9a-IIbl-EgTD-YLrRGz
--- Physical volume ---
PV Name /dev/sdc1
VG Name vg0
PV Size 15.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 3839
Free PE 1
Allocated PE 3838
PV UUID j8i7QQ-sSR2-bsJn-UlJj-a1jY-Ubum-KRYgWx
--- Physical volume ---
PV Name /dev/sdc2
VG Name vg0
PV Size 20.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 5119
Free PE 5117
Allocated PE 2
PV UUID DTnJdv-pV0n-p8Dz-Ku8b-gtQv-hNDI-A56LPK
[root@centos7 ~]#pvmove /dev/sdc2 ---移動/dev/sdc2中已經分配的PE空間給其他物理卷
/dev/sdc2: Moved: 100.00%
[root@centos7 ~]#pvdisplay
--- Physical volume ---
PV Name /dev/sdb
VG Name vg0
PV Size 20.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 5119
Free PE 509
Allocated PE 4610
PV UUID 72dJR7-GCfL-GOHH-Tg9a-IIbl-EgTD-YLrRGz
--- Physical volume ---
PV Name /dev/sdc1
VG Name vg0
PV Size 15.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 3839
Free PE 1
Allocated PE 3838
PV UUID j8i7QQ-sSR2-bsJn-UlJj-a1jY-Ubum-KRYgWx
--- Physical volume ---
PV Name /dev/sdc2
VG Name vg0
PV Size 20.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 5119
Free PE 5119
Allocated PE 0 ---此時/dev/sdc2分區一個PE也沒有分配給邏輯卷
PV UUID DTnJdv-pV0n-p8Dz-Ku8b-gtQv-hNDI-A56LPK
[root@centos7 ~]#vgreduce vg0 /dev/sdc2 ---縮減卷組
Removed "/dev/sdc2" from volume group "vg0"
[root@centos7 ~]#pvdisplay
--- Physical volume ---
PV Name /dev/sdb
VG Name vg0
PV Size 20.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 5119
Free PE 509
Allocated PE 4610
PV UUID 72dJR7-GCfL-GOHH-Tg9a-IIbl-EgTD-YLrRGz
--- Physical volume ---
PV Name /dev/sdc1
VG Name vg0
PV Size 15.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 3839
Free PE 1
Allocated PE 3838
PV UUID j8i7QQ-sSR2-bsJn-UlJj-a1jY-Ubum-KRYgWx
"/dev/sdc2" is a new physical volume of "20.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdc2
VG Name ---可以看到已經移除,不屬于任何卷組
PV Size 20.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID DTnJdv-pV0n-p8Dz-Ku8b-gtQv-hNDI-A56LPK
[root@centos7 ~]#pvremove /dev/sdc2 ---刪除物理卷
Labels on physical volume "/dev/sdc2" successfully wiped.
[root@centos7 ~]#pvdisplay
--- Physical volume ---
PV Name /dev/sdb
VG Name vg0
PV Size 20.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 5119
Free PE 509
Allocated PE 4610
PV UUID 72dJR7-GCfL-GOHH-Tg9a-IIbl-EgTD-YLrRGz
--- Physical volume ---
PV Name /dev/sdc1
VG Name vg0
PV Size 15.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 3839
Free PE 1
Allocated PE 3838
PV UUID j8i7QQ-sSR2-bsJn-UlJj-a1jY-Ubum-KRYgWx
之后就可以將這個分區刪除
7、邏輯卷快照管理
邏輯卷的快照就相當于在同一個卷組中再創建一個邏輯卷快照的空間,這個空間的大小要保證修改的文件能夠存入,剛開始時邏輯卷的快照里沒有數據,只是一個空間,當在邏輯卷中修改一個文件時,會把一個文件的原始狀態推送到這個快照空間中保存起來。
[root@centos7 lv0]#lvcreate -L 500M -s -n lv0-snapshot -p r /dev/vg0/lv0 ---對邏輯卷lv0創建一個500M的快照,-L指定大小,-s指定是快照,-n指定名字,-p指定是只讀
Using default stripesize 64.00 KiB.
Logical volume "lv0-snapshot" created.
[root@centos7 lv0]#mkdir /mnt/lv0-snapshot
[root@centos7 lv0]#mount /dev/vg0/lv0-snapshot /mnt/lv0-snapshot/ ---掛載快照
mount: /dev/mapper/vg0-lv0--snapshot is write-protected, mounting read-only
[root@centos7 lv0]#df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 52403200 15341232 37061968 30% /
devtmpfs 918932 0 918932 0% /dev
tmpfs 933644 88 933556 1% /dev/shm
tmpfs 933644 9260 924384 1% /run
tmpfs 933644 0 933644 0% /sys/fs/cgroup
/dev/sda3 52403200 1052228 51350972 3% /app
/dev/mapper/vg0-lv0 18447056 45084 17441872 1% /mnt/lv0
/dev/sda1 1038336 172284 866052 17% /boot
tmpfs 186732 12 186720 1% /run/user/0
/dev/sr0 8086368 8086368 0 100% /run/media/root/CentOS 7 x86_64
/dev/mapper/vg0-lv0--snapshot 18447056 45084 17441872 1% /mnt/lv0-snapshot
[root@centos7 lv0-snapshot]#cd /mnt/lv0 ---進入原來的lv0邏輯卷目錄
[root@centos7 lv0]#ls
fstab lost+found
[root@centos7 lv0]#vim fstab ---修改一下文件內容
1 UUID=e7e1738b-58ae-49d3-9f7e-c62988ead392 / xfs defaults
0 0
2 UUID=348930fe-f95e-40e6-90ff-83c66b3f2e9a /app xfs defaults
0 0
3 UUID=dfbab032-681e-4e34-a16e-2f5e1eda5f32 /boot xfs defaults
0 0
4 UUID=6145ae5c-6ebd-4655-a52f-e90f9b5a9e58 swap swap defaults
0 0
"fstab" 4L, 376C written
[root@centos7 lv0]#
[root@centos7 lv0]#
[root@centos7 lv0]#
[root@centos7 lv0]#
[root@centos7 lv0]#cat fstab ---修改之后的內容
UUID=e7e1738b-58ae-49d3-9f7e-c62988ead392 / xfs defaults 0 0
UUID=348930fe-f95e-40e6-90ff-83c66b3f2e9a /app xfs defaults 0 0
UUID=dfbab032-681e-4e34-a16e-2f5e1eda5f32 /boot xfs defaults 0 0
UUID=6145ae5c-6ebd-4655-a52f-e90f9b5a9e58 swap swap defaults 0 0
[root@centos7 lv0]#
0 0
[root@centos7 lv0]#cd /mnt/lv0-snapshot/ ---進入快照的目錄
[root@centos7 lv0-snapshot]#ls
fstab lost+found
[root@centos7 lv0-snapshot]#cat fstab ---可以看到已經將原始的內容保存到其中
#
# /etc/fstab
# Created by anaconda on Fri Jul 14 11:16:04 2017
#
# 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
#
UUID=e7e1738b-58ae-49d3-9f7e-c62988ead392 / xfs defaults 0 0
UUID=348930fe-f95e-40e6-90ff-83c66b3f2e9a /app xfs defaults 0 0
UUID=dfbab032-681e-4e34-a16e-2f5e1eda5f32 /boot xfs defaults 0 0
UUID=6145ae5c-6ebd-4655-a52f-e90f9b5a9e58 swap swap defaults 0 0
[root@centos6 ~]#lvconvert --merge /dev/vg0/lv0-snapshot ---恢復快照
Merging of volume vg0/lv0-snapshot started.
lv1: Merged: 100.00%
[root@centos6 lv1]#cd /mnt/lv0
[root@centos6 lv1]#ls
etc fstab
[root@centos6 lv0]#cat fstab ---可以看到文件又恢復到原來的狀態,沒有丟失
總結:在恢復快照之前一定要先取消掛載,包括邏輯卷的掛載和快照的掛載都要取消,否則無法恢復快照。這個是在練習中經常出現的錯誤。
#
# /etc/fstab
# Created by anaconda on Fri Jul 14 11:16:04 2017
#
# 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
#
UUID=e7e1738b-58ae-49d3-9f7e-c62988ead392 / xfs defaults 0 0
UUID=348930fe-f95e-40e6-90ff-83c66b3f2e9a /app xfs defaults 0 0
UUID=dfbab032-681e-4e34-a16e-2f5e1eda5f32 /boot xfs defaults 0 0
UUID=6145ae5c-6ebd-4655-a52f-e90f9b5a9e58 swap swap defaults 0 0
UUID=fca04a06-6e33-41bb-8902-cc89b908ae22 /mnt/lv0 ext4 defaults 0 0
總結:在修改邏輯卷上的文件時會將原文件數據推送至快照中,在生產中一般不會使用快照。因為這樣會影響服務器的性能。相當于在修改的時候會把原數據輸出到快照。
8、刪除邏輯卷的步驟
從上往下刪除,跟創建的時候相反
[root@centos7 lv1]#lvdisplay
--- Logical volume ---
LV Path /dev/vg0/lv1
LV Name lv1
VG Name vg0
LV UUID RAIOfB-sJ1U-0wGj-FPjn-53SZ-rBeE-AvsvBp
LV Write Access read/write
LV Creation host, time centos7.magedu.com, 2017-08-12 12:00:14 +0800
LV Status available
# open 1
LV Size 15.00 GiB
Current LE 3840
Segments 3
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
[root@centos7 ~]#umount /mnt/lv1 ---先取消掛載
[root@centos7 ~]#lvremove /dev/vg0/lv1 ---刪除邏輯卷
Do you really want to remove active logical volume vg0/lv1? [y/n]: y
Logical volume "lv1" successfully removed
[root@centos7 ~]#vgremove vg0
Volume group "vg0" successfully removed
[root@centos7 ~]#pvs
PV VG Fmt Attr PSize PFree
/dev/sdb lvm2 --- 20.00g 20.00g
/dev/sdc1 lvm2 --- 15.00g 15.00g
[root@centos7 ~]#pvremove /dev/sd{b,c1} ---刪除物理卷
Labels on physical volume "/dev/sdb" successfully wiped.
Labels on physical volume "/dev/sdc1" successfully wiped.
之后就可以刪除分區或者拔除硬盤了。