ps: 紅字字體爲重要部分, 仔細看
bash
1、什麼是LVM.?ide
LVM是Logical Volume Manager(邏輯卷管理)的簡寫, 它是Linux環境下磁盤分區繼續管理的一種機制, 它由Heinz Mauelshagen在Linux2.4內核上實現;測試
2、爲何要用LVM.?字體
文件系統創建在邏輯捲上, 而邏輯卷可根據須要改變大小(在卷組容量範圍內)以知足需求;ui
使用LVM主要是方便管理、增長了系統的擴展性, 能夠隨時按要求增大,或根據使用狀況對各邏輯進行調整,當系統空間不足而加入新的磁盤時,沒必要把用戶的數據從原硬盤遷移到新硬盤, 而只需把新硬盤加入到卷組並擴充邏輯卷便可;spa
3、配置與管理3d
1. 測試環境;code
[root@Centos1 ~]# cat /etc/redhat-release && uname -r CentOS release 6.6 (Final) 2.6.32-504.12.2.el6.x86_64
2. 建立分區;blog
[root@Centos1 ~]# fdisk /dev/sdc ================================================================= Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0xb4007a2c. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) WARNING: DOS-compatible mode is deprecated. It's strongly recommended to switch off the mode (command 'c') and change display units to sectors (command 'u'). Command (m for help): n #新建一個分區; Command action e extended p primary partition (1-4) p #建立主分區 Partition number (1-4): 1 #分區編號 First cylinder (1-2610, default 1): #起始柱面 Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +8G #分區大小爲8G Command (m for help): n #新建一個分區; Command action e extended p primary partition (1-4) p #建立主分區 Partition number (1-4): 2 #分區編號 First cylinder (1046-2610, default 1046): #起始柱面 Using default value 1046 Last cylinder, +cylinders or +size{K,M,G} (1046-2610, default 2610): +8G #分區大小爲8G Command (m for help): n #新建一個分區; Command action e extended p primary partition (1-4) p #建立主分區 Partition number (1-4): 3 #分區編號 First cylinder (2091-2610, default 2091): #起始柱面 Using default value 2091 Last cylinder, +cylinders or +size{K,M,G} (2091-2610, default 2610): +8G #分區大小爲8G Value out of range. #提示硬盤所剩容量沒有8G. Last cylinder, +cylinders or +size{K,M,G} (2091-2610, default 2610): #結束柱面爲默認. 大概還剩4G. Using default value 2610 Command (m for help): p #查看分區 Disk /dev/sdc: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0xb4007a2c Device Boot Start End Blocks Id System /dev/sdc1 1 1045 8393931 83 Linux /dev/sdc2 1046 2090 8393962+ 83 Linux /dev/sdc3 2091 2610 4176900 83 Linux Command (m for help): t #更改分區id Partition number (1-4): 1 #分區編號 Hex code (type L to list codes): 8e #LVM的分區id爲8e Changed system type of partition 1 to 8e (Linux LVM) Command (m for help): t #更改分區id Partition number (1-4): 2 #分區編號 Hex code (type L to list codes): 8e #LVM的分區id爲8e Changed system type of partition 2 to 8e (Linux LVM) Command (m for help): t #更改分區id Partition number (1-4): 3 #分區編號 Hex code (type L to list codes): 8e #LVM的分區id爲8e Changed system type of partition 3 to 8e (Linux LVM) Command (m for help): p #查看分區 Disk /dev/sdc: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0xb4007a2c Device Boot Start End Blocks Id System /dev/sdc1 1 1045 8393931 8e Linux LVM /dev/sdc2 1046 2090 8393962+ 8e Linux LVM /dev/sdc3 2091 2610 4176900 8e Linux LVM Command (m for help): w #寫入分區表 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
=================================================================ip
3. 建立物理卷(PV);
[root@Centos1 ~]# pvcreate /dev/sdc{1,2,3} Physical volume "/dev/sdc1" successfully created Physical volume "/dev/sdc2" successfully created Physical volume "/dev/sdc3" successfully created [root@Centos1 ~]# pvdisplay #列出已經建立的物理卷;
4. 建立卷組VG01(VG);
[root@Centos1 ~]# vgcreate VG01 /dev/sdc{1,2,3} Volume group "VG01" successfully created [root@Centos1 ~]# vgremove VG01 #將卷組VG01移除; Volume group "VG01" successfully removed [root@Centos1 ~]# vgdisplay #列出已經建立的卷組; #默認PE大小爲4MB, PE是卷組的最小存儲的單元, 能夠經過-s參數修改大小;
5. 分割500M給新的邏輯卷LV01(LV);
[root@Centos1 ~]# lvcreate -L 500M -n LV01 VG01 Logical volume "LV01" created [root@Centos1 ~]# lvdisplay #列出已經建立的邏輯卷;
6. 格式化邏輯卷;
[root@Centos1 ~]# mkfs.ext4 /dev/VG01/LV01
7. 掛載邏輯卷;
[root@Centos1 ~]# mount /dev/VG01/LV01 /data [root@Centos1 ~]# df -h
4、擴展邏輯卷、卷組和減小邏輯卷大小
1. 邏輯卷LV01不夠用了, 咱們如何給它增長空間呢?
[root@Centos1 ~]# lvextend -L +500M /dev/VG01/LV01 [root@Centos1 ~]# resize2fs /dev/VG01/LV01 #同步文件系統 [root@Centos1 ~]# df -h
2. 擴展卷組;
[root@Centos1 ~]# vgextend VG01 /dev/sdd1 #注: 對sdd硬盤分區後必定要將分區id改成'8e' [root@Centos1 ~]# vgdisplay
3. 減小邏輯卷大小;
卸載 --> e2fsck檢測剩餘空間 --> resize2fs 同步文件系統 --> 使用lvreduce命令將邏輯卷減小.
注意: 文件系統大小和邏輯卷大小必定要保護一致才行. 若是邏輯卷大於文件系統,因爲部分區域未格式化成文件系統會形成空間的浪費.若是邏輯卷小於文件系統,那數據就出問題了.
[root@Centos1 ~]# umount /data/ #卸載/data目錄; [root@Centos1 ~]# e2fsck -f /dev/VG01/LV01 #檢測剩餘空間;
[root@Centos1 ~]# resize2fs /dev/VG01/LV01 600M #將文件系統系統減小到700M; [root@Centos1 ~]# lvreduce -L 600 /dev/VG01/LV01 #將邏輯卷減小到700M; [root@Centos1 ~]# mount /dev/VG01/LV01 /data ps: 筆者練習時, 邏輯卷小於系統,
4. 當磁盤或分區損壞時, 如何轉移數據;
pvmove 轉移空間數據 --> vgreduce命令將即將壞的磁盤或者分區從卷組VG01裏面移除 --> pvremove命令將即將壞的磁盤或者分區從系統中刪除掉 --> 拆除或修復磁盤.
[root@Centos1 ~]# pvmove /dev/sdc1 /dev/sdc1: Moved: 2.0% /dev/sdc1: Moved: 100.0% [root@Centos1 ~]# vgreduce VG01 /dev/sdc1 Removed "/dev/sdc1" from volume group "VG01" [root@Centos1 ~]# pvremove /dev/sdc1 Labels on physical volume "/dev/sdc1" successfully wiped
5. 刪除整個邏輯卷;
umount卸載邏輯卷 --> 修改配置文件/etc/fstab(如果以前更改過) --> lvremove 刪除邏輯卷LV01 --> vgremove 刪除卷組VG01 --> pvremove 將物理卷轉換成普通卷
[root@Centos1 ~]# umount /dev/VG01/LV01 [root@Centos1 ~]# lvremove /dev/VG01/LV01 Do you really want to remove active logical volume LV01? [y/n]: y Logical volume "LV01" successfully removed [root@Centos1 ~]# vgremove VG01 volume "VG01" successfully removed [root@Centos1 ~]# pvremove /dev/sdc1 No PV label found on /dev/sdc1. #以前已卸載 [root@Centos1 ~]# pvremove /dev/sdc2 Labels on physical volume "/dev/sdc2" successfully wiped [root@Centos1 ~]# pvremove /dev/sdc3 Labels on physical volume "/dev/sdc3" successfully wiped
PS: 刪除完了, 別忘記修改分區ID標識, 將'8e(LVM)'改成'83(Linux分區)';