LVM——Logical Volume Manager(邏輯卷管理)
LVM就是動態卷管理,可以將多個硬盤和硬盤分區做成一個邏輯卷,並把這個邏輯卷作為一個整體來統一管理,動態對分區進行擴縮空間大小,安全快捷方便管理。
LVM最大的特點就是可以對磁盤進行動態管理。因為邏輯卷的大小是可以動態調整的,而且不會丟失現有的數據。我們如果新增加了硬盤,其也不會改變現有上層的邏輯卷。作為一個動態磁盤管理機制,邏輯卷技術大大提高了磁盤管理的靈活性!
概念
①PE(Physical Extend) 物理拓展
②PV(Physical Volume) 物理卷
③VG(Volume Group) 卷組
④LV(Logical Volume) 邏輯卷
原理
(1)物理磁盤被格式化為PV,空間被划分為一個個的PE
(2)不同的PV加入到同一個VG中,不同PV的PE全部進入到了VG的PE池內
(3)LV基於PE創建,大小為PE的整數倍,組成LV的PE可能來自不同的物理磁盤
(4)LV現在就直接可以格式化后掛載使用了
(5)LV的擴充縮減實際上就是增加或減少組成該LV的PE數量,其過程不會丟失原始數據
PV(物理卷)操作
創建:pvcreate + 物理設備/分區/raid
[root@localhost ~]# pvcreate /dev/hda{6,7,8,9} Physical volume "/dev/hda6" successfully created Physical volume "/dev/hda7" successfully created Physical volume "/dev/hda8" successfully created Physical volume "/dev/hda9" successfully created
刪除pv:pvremove + 物理設備/分區/raid
[root@localhost ~]# pvremove /dev/sdb2 Labels on physical volume "/dev/sdb2" successfully wiped
掃描:pvscan
[root@localhost ~]# pvscan No matching physical volumes found #找不到任何的 PV 存在!
查看:pvdisplay
[root@localhost ~]# pvdisplay "/dev/hda6" is a new physical volume of "1.40 GB"
--- NEW Physical volume --- PV Name /dev/hda6 #實際的 partition 分區名稱 VG Name #因為尚未分配出去,所以空白! PV Size 1.40 GB #就是容量說明 Allocatable NO #是否已被分配,結果是 NO PE Size (KByte) 0 #在此 PV 內的 PE 大小 Total PE 0 #共分割出幾個 PE free PE 0 #沒被 LV 用掉的 PE Allocated PE 0 #尚可分配出去的 PE 數量 PV UUID Z13Jk5-RCls-UJ8B-HzDa-Gesn-atku-rf2biN
移動:pvmove(數據搬遷)
[root@redhat6 ~]# pvmove /dev/sdb/dev/sdb: Moved: 0.8%
/dev/sdb: Moved: 28.3%
/dev/sdb: Moved: 42.5%
/dev/sdb: Moved: 57.2%
/dev/sdb: Moved: 72.8%
/dev/sdb: Moved: 92.6%
/dev/sdb: Moved: 100.0%
列出所有pv:pvs
root@ubuntu:/# pvs /run/lvm/lvmetad.socket: connect failed: No such file or directory WARNING: Failed to connect to lvmetad. Falling back to internal scanning. PV VG Fmt Attr PSize PFree /dev/sda3 myVG lvm2 a-- 7.45g 7.45g /dev/sda4 myVG lvm2 a-- 4.66g 4.66g
VG(卷組)操作
創建:vgcreate + 卷組名(自定義) + 物理卷(pv)
root@ubuntu:/# vgcreate myVG /dev/sda{3,4}
/run/lvm/lvmetad.socket: connect failed: No such file or directory
WARNING: Failed to connect to lvmetad. Falling back to internal scanning.
Volume group "myVG" successfully created
刪除:vgremove
[root@www ~]# vgremove vg1000 #刪除卷組"vg1000" Volume group "vg1000" successfully removed
擴展:vgextend(新增一個pv到卷組)
root@ubuntu:/# vgextend myVG /dev/sda4 /run/lvm/lvmetad.socket: connect failed: No such file or directory WARNING: Failed to connect to lvmetad. Falling back to internal scanning. Volume group "myVG" successfully extended
縮小:vgreduce(將一個pv從卷組中拿走,在執行之前一定要執行pvmove將該pv數據挪到其他pv)
root@ubuntu:/# vgreduce myVG /dev/sda4 /run/lvm/lvmetad.socket: connect failed: No such file or directory WARNING: Failed to connect to lvmetad. Falling back to internal scanning. Removed "/dev/sda4" from volume group "myVG"
掃描:vgscan
顯示:vgdisplay
root@ubuntu:/# vgdisplay /run/lvm/lvmetad.socket: connect failed: No such file or directory WARNING: Failed to connect to lvmetad. Falling back to internal scanning. --- Volume group --- VG Name myVG 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 12.11 GiB PE Size 4.00 MiB //可以修改,在創建時加選項 -s(+單位) Total PE 3099 Alloc PE / Size 0 / 0 Free PE / Size 3099 / 12.11 GiB VG UUID LhGoo2-IZIb-330C-0MQu-HEz1-J97y-BlP7AE
列出所有vg:vgs
root@ubuntu:/# vgs /run/lvm/lvmetad.socket: connect failed: No such file or directory WARNING: Failed to connect to lvmetad. Falling back to internal scanning. VG #PV #LV #SN Attr VSize VFree myVG 2 0 0 wz--n- 12.11g 12.11g
LV(邏輯卷)操作
創建:lvcreate -L (大小) -n(指定名稱) VG組名
1. 同一個卷組上可以創建多個邏輯卷
root@ubuntu:/# lvcreate -L 50M -n myLV myVG /run/lvm/lvmetad.socket: connect failed: No such file or directory WARNING: Failed to connect to lvmetad. Falling back to internal scanning. Rounding up size to full physical extent 52.00 MiB Logical volume "myLV" created.
刪除:lvremove ( 刪除前需要先卸載 )
root@ubuntu:/# lvremove /dev/myVG/myLV /run/lvm/lvmetad.socket: connect failed: No such file or directory WARNING: Failed to connect to lvmetad. Falling back to internal scanning. Do you really want to remove and DISCARD active logical volume myLV? [y/n]: y Logical volume "myLV" successfully removed
擴展:lvextend
縮小:lvreduce
列出:lvs
root@ubuntu:/# lvs /run/lvm/lvmetad.socket: connect failed: No such file or directory WARNING: Failed to connect to lvmetad. Falling back to internal scanning. LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert myLV myVG -wi-a----- 52.00m
展示:lvdisplay
root@ubuntu:/# lvdisplay /run/lvm/lvmetad.socket: connect failed: No such file or directory WARNING: Failed to connect to lvmetad. Falling back to internal scanning. --- Logical volume --- LV Path /dev/myVG/myLV LV Name myLV VG Name myVG LV UUID arp8Y1-ThlO-oCas-5XGP-juPF-tB9x-5v2hyS LV Write Access read/write LV Creation host, time ubuntu, 2018-05-10 10:20:30 -0700 LV Status available # open 0 LV Size 52.00 MiB Current LE 13 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0
掛載
掛載前先創建文件系統
root@ubuntu:/# mke2fs -j /dev/myVG/myLV
mke2fs用於快速格式化,選項
‘-b’ 分區時設定每個數據區塊占用空間大小,目前支持1024, 2048 以及4096 bytes每個塊。
‘-i’ 設定inode的大小
‘-N’ 設定inode數量,有時使用默認的inode數不夠用,所以要自定設定inode數量。
‘-c’ 在格式化前先檢測一下磁盤是否有問題,加上這個選項后會非常慢
‘-L’ 預設該分區的標簽label
‘-j’ 建立ext3格式的分區,如果使用mkfs.ext3 就不用加這個選項了
‘-t’ 用來指定什么類型的文件系統,可以是ext2, ext3 也可以是 ext4.
resize2fs重新調整文件系統大小
-p 當前分區有多大則調整到多大
擴展邏輯卷
可以在線進行,不需要卸載
順序:擴展物理邊界 --> 擴展邏輯邊界
擴展物理邊界 lvextend -L [+]大小 /邏輯卷路徑
root@ubuntu:/# lvextend -L 100M /dev/myVG/myLV /run/lvm/lvmetad.socket: connect failed: No such file or directory WARNING: Failed to connect to lvmetad. Falling back to internal scanning. Size of logical volume myVG/myLV changed from 52.00 MiB (13 extents) to 100.00 MiB (25 extents). Logical volume myLV successfully resized.
擴展邏輯邊界 resize2fs /邏輯卷路徑 大小(擴展到多大)
root@ubuntu:/mnt# resize2fs -p /dev/myVG/myLV resize2fs 1.42.13 (17-May-2015) Filesystem at /dev/myVG/myLV is mounted on /mnt/test; on-line resizing required old_desc_blocks = 1, new_desc_blocks = 1 The filesystem on /dev/myVG/myLV is now 155648 (1k) blocks long.
縮小邏輯卷
順序:縮小邏輯邊界 --> 縮小物理邊界
注意
1. 不能在線縮減,需要先卸載
root@ubuntu:/mnt# df -h Filesystem Size Used Avail Use% Mounted on udev 481M 0 481M 0% /dev tmpfs 100M 6.5M 94M 7% /run /dev/sda1 19G 4.8G 13G 27% / tmpfs 500M 252K 500M 1% /dev/shm tmpfs 5.0M 4.0K 5.0M 1% /run/lock tmpfs 500M 0 500M 0% /sys/fs/cgroup tmpfs 100M 44K 100M 1% /run/user/1000 /dev/mapper/myVG-myLV 144M 1.6M 135M 2% /mnt/test
root@ubuntu:/# umount /mnt/test
2. 確保縮減后的空間依然能夠存下當前數據
3. 在縮減之前應該先強行檢查文件,確保文件系統處於一致性狀態
root@ubuntu:/# e2fsck -f #強行檢查
縮小邏輯邊界
resize2fs /邏輯卷路徑 大小(擴展到多大)
root@ubuntu:/# resize2fs /dev/myVG//myLV 100M resize2fs 1.42.13 (17-May-2015) Resizing the filesystem on /dev/myVG//myLV to 102400 (1k) blocks. The filesystem on /dev/myVG//myLV is now 102400 (1k) blocks long.
縮小物理邊界
lvreduce
root@ubuntu:/# lvreduce -L 100M /dev/myVG/myLV /run/lvm/lvmetad.socket: connect failed: No such file or directory WARNING: Failed to connect to lvmetad. Falling back to internal scanning. WARNING: Reducing active logical volume to 100.00 MiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce myLV? [y/n]: y Size of logical volume myVG/myLV changed from 152.00 MiB (38 extents) to 100.00 MiB (25 extents). Logical volume myLV successfully resized.
重新掛載 root@ubuntu:/# mount /dev/myVG/myLV /mnt/test
檢查磁盤情況 root@ubuntu:/# df -h Filesystem Size Used Avail Use% Mounted on udev 481M 0 481M 0% /dev tmpfs 100M 6.5M 94M 7% /run /dev/sda1 19G 4.8G 13G 27% / tmpfs 500M 252K 500M 1% /dev/shm tmpfs 5.0M 4.0K 5.0M 1% /run/lock tmpfs 500M 0 500M 0% /sys/fs/cgroup tmpfs 100M 44K 100M 1% /run/user/1000 /dev/mapper/myVG-myLV 93M 1.6M 87M 2% /mnt/test
現實快照卷
注意:
1. 聲明周期為整個數據時長,在這段時間內,增長數據不能超過快照卷大小
2. 快照卷是只讀
3. 和原卷在同一卷組內
eg:對 / dev / myVG / myLV 創建快照卷
root@ubuntu:/# lvcreate -L 50M -n myLV_snap -s -p r /dev/myVG/myLV /run/lvm/lvmetad.socket: connect failed: No such file or directory WARNING: Failed to connect to lvmetad. Falling back to internal scanning. Rounding up size to full physical extent 52.00 MiB Logical volume "myLV_snap" created.
然后掛載就可以使用
root@ubuntu:/# mount /dev/myVG/myLV_snap /mnt/test2
最后對test2目錄進行備份即可
#備份完成后進行卸載,然后刪除快照卷 root@ubuntu:/mnt# umount /mnt/test2 root@ubuntu:/mnt# lvremove /dev/myVG/myLV_snap
