LINUX磁盤-LVM |
作者:MP 歸檔:學習筆記 2017/11/25
|
格式說明: 藍色字體:註釋 ×××背景:重要 綠色背景:注意 |
目 錄
LVM
1)LVM原理圖
2)LVM相關名詞解釋
1.1 物理存儲介質:例如磁盤等
1.2 物理卷PV:磁盤分區、raid等
1.3 卷組VG:PV容量組件的池(pv的集合等)、類似於一塊待分區的“磁盤”
1.4 邏輯卷LV:VG的分區,再次上可以建立文件系統掛載等使用。類似於磁盤的分區可以被格式化、掛載使用
1.5 邏輯塊LE:邏輯捲上最小的容量單位,默認是4MB
演示環境:
第2章 創建LVM過程
2.1 使用fdisk初始化磁盤-選擇磁盤格式爲8e
2.1.1 初始化/dev/sdb
2.1.2 初始化/dev/sdc
2.2 創建PV
2.3 創建VG
2.4 從VG中劃分LV
2.5 格式化、掛載分區
第3章 擴容LVM
3.1 首先邏輯擴展(擴展LV容量)
3.2 其次文件系統的擴展
第4章 縮小LVM
4.1 卸載、檢查
4.2 縮減文件系統的容量
4.3 在縮減LV的容量
4.4 重新掛載
· LVM Logical Volume Manager(邏輯卷管理)的簡寫,它是Linux環境下對磁盤分區進行管理的一種機制。
LVM解決了分區大小分配磁盤擴展問題。
例如:
在創建的系統的時候講/date 分配掛載在/目錄下,後期由於業務需求需要獨立出來。重新掛載一個新的分區,期中之前的數據將無法使用,雖然可以使用cp等命令將數據遷移。但是若果新掛載的這個硬盤容量也滿了了?掛載一個新的更大容量的硬盤重新掛載?若是長期使用這種辦法也是也可以,但是資源浪費嚴重。
LVM就是打破物理存儲限制、可以隨意增加容量、縮減容量。
1)LVM原理圖
2)LVM相關名詞解釋
1.1 物理存儲介質:例如磁盤等
1.2 物理卷PV:磁盤分區、raid等
1.3 卷組VG:PV容量組件的池(pv的集合等)、類似於一塊待分區的“磁盤”
1.4 邏輯卷LV:VG的分區,再次上可以建立文件系統掛載等使用。類似於磁盤的分區可以被格式化、掛載使用
1.5 邏輯塊LE:邏輯捲上最小的容量單位,默認是4MB
演示環境:
演示環境:
VMware workstation 12.0.1 build-3160714
centos 6.9
掛載了兩塊新的虛擬磁盤容量分別爲1GB 2GB
[root@linux-edu ~]# fdisk -l |grep "/dev/sd[abc]"
Disk /dev/sda: 21.5 GB, 21474836480 bytes
/dev/sda1 * 1 26 204800 83 Linux
/dev/sda2 26 157 1048576 82 Linux swap / Solaris
/dev/sda3 157 2611 19717120 83 Linux
Disk /dev/sdb: 1073 MB, 1073741824 bytes
Disk /dev/sdc: 2147 MB, 2147483648 bytes
[root@linux-edu ~]#
LVM常用命令
階段 | 顯示 | 創建 | 刪除 | 增加 | 縮小 |
PV | pvdisplay | pvcreat | pvremove | ||
VG | vgdisplay | vgcreat | vgremove | vgextend | vgreduce |
LV | lvdisplay | lvcreat | lvremove | lvextend | lvreduce |
第2章 創建LVM過程
2.1 使用fdisk初始化磁盤-選擇磁盤格式爲8e
創建流程:新建主分區-編號爲1-容量所有-選擇格式爲LVM-保存
2.1.1 初始化/dev/sdb
[root@linux-edu ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x9396a336.
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-130, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-130, default 130):
Using default value 130
Command (m for help): p
Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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: 0x9396a336
Device Boot Start End Blocks Id System
/dev/sdb1 1 130 1044193+ 83 Linux
Command (m for help): t ###選擇格式
Selected partition 1
Hex code (type L to list codes): 8e ##格式設置爲8e
Changed system type of partition 1 to 8e (Linux LVM)
Command (m for help): p
Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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: 0x9396a336
Device Boot Start End Blocks Id System
/dev/sdb1 1 130 1044193+ 8e Linux LVM
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@linux-edu ~]#
2.1.2 初始化/dev/sdc
步驟如上
[root@linux-edu ~]# fdisk -l |grep "/dev/sd[abc]"
Disk /dev/sda: 21.5 GB, 21474836480 bytes
/dev/sda1 * 1 26 204800 83 Linux
/dev/sda2 26 157 1048576 82 Linux swap / Solaris
/dev/sda3 157 2611 19717120 83 Linux
Disk /dev/sdb: 1073 MB, 1073741824 bytes
/dev/sdb1 1 130 1044193+ 8e Linux LVM
Disk /dev/sdc: 2147 MB, 2147483648 bytes
/dev/sdc1 1 261 2096451 8e Linux LVM
[root@linux-edu ~]#
2.2 創建PV
#創建pv
[root@linux-edu ~]# pvcreate /dev/sd[bc]1
Physical volume "/dev/sdb1" successfully created
Physical volume "/dev/sdc1" successfully created
[root@linux-edu ~]#
#查看pv
[root@linux-edu ~]# pvdisplay
"/dev/sdb1" is a new physical volume of "1019.72 MiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 1019.72 MiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID GVfctN-vmJQ-3A6M-ogcd-fVbi-udQD-RRu03q
"/dev/sdc1" is a new physical volume of "2.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdc1
VG Name
PV Size 2.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 74mwlF-SBuT-U19r-JQZk-1BOj-NdMd-cdXIFg
[root@linux-edu ~]#
2.3 創建VG
[root@linux-edu ~]# vgcreate disk01 /dev/sd[bc]1 ###創建VG命名爲disk01
Volume group "disk01" successfully created
[root@linux-edu ~]# vgdisplay
--- Volume group ---
VG Name disk01 #VG名稱
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 2.99 GiB ##VG總容量
PE Size 4.00 MiB ##LE塊的大小
Total PE 765 ##LE塊的數量
Alloc PE / Size 0 / 0
Free PE / Size 765 / 2.99 GiB
VG UUID fj09w3-RSVt-oRxH-nETB-SsC3-C8i9-CxLA5j
[root@linux-edu ~]#
2.4 從VG中劃分LV
lvcreate 命令常用參數:
-L 設置指定大小
-n 設置指定名稱
[root@linux-edu ~]# lvcreate -L 100M -n LVM_date01 disk01
Logical volume "LVM_date01" created.
[root@linux-edu ~]# lvdisplay
--- Logical volume ---
LV Path /dev/disk01/LVM_date01
LV Name LVM_date01 #LV名稱
VG Name disk01 #所屬VG名稱
LV UUID g3BkWT-F79P-txPb-VvFv-FIsF-Ox1t-ZoTpye
LV Write Access read/write
LV Creation host, time linux-edu, 2017-11-25 20:05:04 +0800
LV Status available
# open 0
LV Size 100.00 MiB ##LV的總容量
Current LE 25 ##LE的個數
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
[root@linux-edu ~]#
2.5 格式化、掛載分區
[root@linux-edu ~]# mkfs.ext4 /dev/disk01/LVM_date01
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
......
[root@linux-edu /]# mount /dev/disk01/LVM_date01 /data/
[root@linux-edu /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 19G 1.5G 17G 9% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 190M 35M 146M 19% /boot
/dev/mapper/disk01-LVM_date01
93M 1.6M 87M 2% /data
[root@linux-edu /]#
至此LVM分區創建完畢
第3章 擴容LVM
3.1 首先邏輯擴展(擴展LV容量)
使用的命令:
lvextend 擴展LV容量
常用參數: -L 500M #將容量擴展到500M
-L +500M #在原有的基礎上加上500M
[root@linux-edu /]# lvextend -L +200M /dev/disk01/LVM_date01
Size of logical volume disk01/LVM_date01 changed from 100.00 MiB (25 extents) to 300.00 MiB (75 extents).
Logical volume LVM_date01 successfully resized.
[root@linux-edu /]# lvdisplay
--- Logical volume ---
LV Path /dev/disk01/LVM_date01
LV Name LVM_date01
VG Name disk01
LV UUID g3BkWT-F79P-txPb-VvFv-FIsF-Ox1t-ZoTpye
LV Write Access read/write
LV Creation host, time linux-edu, 2017-11-25 20:05:04 +0800
LV Status available
# open 1
LV Size 300.00 MiB
Current LE 75
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
[root@linux-edu /]#
3.2 其次文件系統的擴展
使用的命令是:resize2fs
[root@linux-edu /]# resize2fs -p /dev/disk01/LVM_date01
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/disk01/LVM_date01 is mounted on /data; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/disk01/LVM_date01 to 307200 (1k) blocks.
The filesystem on /dev/disk01/LVM_date01 is now 307200 blocks long.
[root@linux-edu /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 19G 1.5G 17G 9% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 190M 35M 146M 19% /boot
/dev/mapper/disk01-LVM_date01
287M 2.1M 270M 1% /data
[root@linux-edu /]#
至此擴容成功
第4章 縮小LVM
縮減容量是一件危險的操作;縮減必須在離線狀態下執行;並且必須先強制檢查文件系統錯誤,防止縮減過程損壞數據;
4.1 卸載、檢查
[root@linux-edu /]# umount /dev/disk01/LVM_date01
[root@linux-edu /]# e2fsck -f /dev/disk01/LVM_date01
e2fsck 1.41.12 (17-May-2010)
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/disk01/LVM_date01: 11/75088 files (9.1% non-contiguous), 15637/307200 blocks
[root@linux-edu /]#
4.2 縮減文件系統的容量
[root@linux-edu /]# resize2fs /dev/disk01/LVM_date01 100M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/disk01/LVM_date01 to 102400 (1k) blocks.
The filesystem on /dev/disk01/LVM_date01 is now 102400 blocks long.
[root@linux-edu /]#
4.3 在縮減LV的容量
[root@linux-edu /]# lvreduce -L 100M /dev/disk01/LVM_date01
WARNING: Reducing active logical volume to 100.00 MiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce disk01/LVM_date01? [y/n]: y
Size of logical volume disk01/LVM_date01 changed from 300.00 MiB (75 extents) to 100.00 MiB (25 extents).
Logical volume LVM_date01 successfully resized.
[root@linux-edu /]# lvdisplay
--- Logical volume ---
LV Path /dev/disk01/LVM_date01
LV Name LVM_date01
VG Name disk01
LV UUID g3BkWT-F79P-txPb-VvFv-FIsF-Ox1t-ZoTpye
LV Write Access read/write
LV Creation host, time linux-edu, 2017-11-25 20:05:04 +0800
LV Status available
# open 0
LV Size 100.00 MiB
Current LE 25
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
[root@linux-edu /]#
4.4 重新掛載
[root@linux-edu /]# mount /dev/disk01/LVM_date01 /data/
[root@linux-edu /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 19G 1.5G 17G 9% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 190M 35M 146M 19% /boot
/dev/mapper/disk01-LVM_date01
93M 1.6M 87M 2% /data
[root@linux-edu /]#
本文借鑑前輩、重新整理。