磁盤存儲和文件系統(七)
磁盤存儲和文件系統(七)
邏輯卷管理器(LVM)
- 容許對捲進行方便操做的抽象層,包括從新設定文件系統的大小
- 容許在多個物理設備間從新組織文件系統
- 將設備指定爲物理卷
- 用一個或者多個物理捲來建立一個卷組
- 物理卷是用固定大小的物理區域(Physical Extent,PE)來定義的
- 在物理捲上建立的邏輯卷是由物理區域(PE)組成
- 能夠在邏輯捲上建立文件系統
![](http://static.javashuo.com/static/loading.gif)
LVM介紹
- LVM:Logical Volume Manager,Version 2
- dm:device mapper,將一個或多個底層塊設備組織成一個邏輯設備的模塊
- 設備名:/dev/dm-#
- 軟連接:
- /dev/mapper/VG_NAME-LV_NAME
- /dev/VG_NAME/LV_NAME
LVM更改文件系統的容量
- LVM能夠彈性的更改LVM的容量
- 經過交換PE來進行資料的轉換,將原來LV的PE轉移到其餘的設備中以下降LVr容量,或將其餘設備中的PE加到LV中以加大容量
![](http://static.javashuo.com/static/loading.gif)
PV管理工具
vg 管理工具
- 顯示卷組
- 建立卷組
- vgcreate [-s #[kKmMgGtTpPeE]] VolumeGRoupName PhysicalDevicePath [PhysicalDevicePath...]
- 管理卷組
- vgextend VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...]
- vgreduce VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...]
- 刪除卷組
LV管理工具
- 顯示邏輯卷
- 建立邏輯卷
- lvcreate -L #[mMgGtT] -n NAME VolumeGroup
- lvcreate -L 60%VG -n mylv testvg
- lvcreate -l 100%FREE -n yourlv testvg
- 刪除邏輯卷
- lvremove /dev/VG_NAME/LV_NAME
- 重設文件系統大小
- fsadm [options] resize device [new_size[BKMGTEP]]
- resize2fs [-f] [-F] [-M] [-P] device [new_size]
- xts_growfs /mountpoint
擴展和縮減邏輯卷
- 擴展邏輯卷:
- lvextend -L [+]#[mMgGtT] /dev/VG_NAME/LV_NAME
- resize2fs /dev/VG_NAME/LV_NAME
- lvresize -r -l +100%FREE /dev/VG_NAME/LV_NAME
- 縮減邏輯卷:
- umount /dev/VG_NAME/LV_NAME
- e2fsck -f /dev/VG_NAME/LV_NAME
- resize2fs /dev/VG_NAME/LV_NAME #[mMgGtT]
- lvreduce -L [-]#[mMgGtT] /dev/VG_NAME/LV_NAME
- mount
建立邏輯卷示例
- 建立物理卷
- 爲卷組分配物理卷
- vgcreate vg0 /dev/sda3
- 從卷組建立邏輯卷
- lvcreate -L 256M -n data vg0
- mkfs.xfs /dev/vg0/data
- 掛載
- mount /dev/vg0/data /mnt/data
跨主機遷移卷組
- 源計算機上
- 在舊系統中,umount全部卷組上的邏輯卷
- 禁用卷組
- vgchange –a n vg0
- lvdisplay
- 導出卷組
- vgexport vg0
- pvscan
- vgdisplay
- 拆下舊硬盤
- 在目標計算機上
- 在新系統中安裝舊硬盤,並導入卷組:vgimport vg0
- vgchange –ay vg0 啓用
- mount全部卷組上的邏輯卷
邏輯卷管理器快照
- 快照是特殊的邏輯卷,它是在生成快照時存在的邏輯卷的準確拷貝
- 對於須要備份或者複製的現有數據臨時拷貝以及其它操做來講,快照是最合適
的選擇
- 快照只有在它們和原來的邏輯卷不一樣時纔會消耗空間
- 在生成快照時會分配給它必定的空間,但只有在原來的邏輯卷或者快照有所
改變纔會使用這些空間
- 當原來的邏輯卷中有所改變時,會將舊的數據複製到快照中
- 快照中只含有原來的邏輯卷中更改的數據或者自生成快照後的快照中更改的
數據
- 創建快照的卷大小小於等於原始邏輯卷,也可使用lvextend擴展快照
邏輯卷管理器快照
- 快照就是將當時的系統信息記錄下來,就好像照相通常,若未來有任何數據
改動了,則原始數據會被移動到快照區,沒有改動的區域則由快照區和文件
系統共享
![](http://static.javashuo.com/static/loading.gif)
- 因爲快照區與本來的LV共用不少PE的區塊,所以快照與被快照的LV必須在同一個VG中.系統恢復的時候的文件數量不能高於快照區的實際容量
使用LVM快照
- 爲現有邏輯卷建立快照
- lvcreate -l 64 -s -n data-snapshot -p r /dev/vg0/data
- 掛載快照
- mkdir -p /mnt/snap
- mount -o ro /dev/vg0/data-snapshot /mnt/snap
- 恢復快照
- umount /dev/vg0/data-snapshot
- umount /dev/vg0/data
- lvconvert --merge /dev/vg0/data-snapshot
- 刪除快照
- umount /mnt/databackup
- lvremove /dev/vg0/databackup
邏輯卷例子
- 準備一個硬盤 /dev/sdb,分區 /dev/sda6
- fdisk /dev/sda
- partprobe
- sdb 8:16 0 20G 0 disk
- 建立物理卷
- yum install lvm2 -y;pvcreate /dev/sda6 /dev/sdb
- pvs
- 建立卷組
- vgcreate vg0 /dev/sda6 /dev/sdb
- 建立邏輯卷
- lvcreate -n mysql -L 8G vg0
- 建立文件系統
- mkfs.xfs /dev/vg0/mysql
- 掛載
- mkdir /mnt/mysql
- mount /dev/vg0/mysql /mnt/mysql
- vim /etc/fstab
-
![](http://static.javashuo.com/static/loading.gif)
- 查看
- mount |grep /dev/mapper/vg0-mysql
-
![](http://static.javashuo.com/static/loading.gif)
- 單個硬盤與邏輯卷性能比較
-
![](http://static.javashuo.com/static/loading.gif)
- 擴展邏輯卷(文件系統爲xfs)
- lvextend -l +100%free /dev/vg0/mysql
- df -hP /dev/vg0/mysql
-
![](http://static.javashuo.com/static/loading.gif)
- xfs_growfs /mnt/mysql/
- df -hP /dev/vg0/mysql
-
- 卷組、物理卷的空間都沒有了,邏輯卷的空間仍是不夠用咋辦?
- pvcreate /dev/sdc
- vgextend vg0 /dev/sdc
- vgdisplay|egrep "vg0|PE"
- 再建立一個邏輯卷,建立文件系統爲ext4
- lvcreate -n binlog -L 10G vg0
- mkfs.ext4 /dev/vg0/binlog
- blkid /dev/vg0/binlog
-
![](http://static.javashuo.com/static/loading.gif)
- 擴展邏輯卷( 文件系統爲ext4 )
- vgdisplay |grep Free
-
![](http://static.javashuo.com/static/loading.gif)
- mkdir /mnt/binlog
- mount /dev/vg0/binlog /mnt/binlog
- lvextend -l +1000 /dev/vg0/binlog
- resize2fs /dev/vg0/binlog
- 擴展邏輯卷( ext4 與 xfs 文件系統都適用)
- lvextexted -r -l +100%free /dev/vg0/mysql
- 縮減邏輯卷(只適用於ext4文件系統)
- umount /mnt/binlog/
- e2fsck -f /dev/vg0/binlog
- resize2fs /dev/vg0/binlog 10G
- lvreduce -L 10G /dev/vg0/binlog
- mount /dev/vg0/binlog /mnt/binlog/
- df -hP /mnt/binlog/
跨主機遷移卷組實例
- 在 centos6 上建立邏輯卷,建立文件系統,掛載,寫入數據,卷組更名
- yum install lvm2 -y
- pvcreate /dev/sdb
- vgcreate -s 16M vg0 /dev/sdb
- lvcreate -n mysql -L 5G vg0
- mkfs.ext4 /dev/vg0/mysql
- mount /dev/vg0/mysql /mnt/mysql
- cp -av /etc/. /mnt/mysql
- vgrename vg0 vg1
- 在centos6機器上卸載全部邏輯卷,禁用卷組、導出卷組
- umount /mnt/mysql/
- vgchange -an vg1
![](http://static.javashuo.com/static/loading.gif)
- vgexport vg1
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
- init 0
- 查看全部邏輯卷對應的全部虛擬磁盤文件
![](http://static.javashuo.com/static/loading.gif)
- 在另外一臺機器上添加硬盤,導入 centos6對應的虛擬文件
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
- 識別新添加的硬盤
![](http://static.javashuo.com/static/loading.gif)
- 查看系統中全部的卷組,導入卷組,啓用卷組,掛載,查看數據
- [root@Centos7 ~]# yum install lvm2 -y
![](http://static.javashuo.com/static/loading.gif)
- [root@Centos7 ~]# vgimport vg1
- [root@Centos7 ~]# vgchange -ay vg1
![](http://static.javashuo.com/static/loading.gif)
硬盤( 邏輯卷的組成部分 )快壞了,搬家
- 查看快壞的硬盤空間大小
-
![](http://static.javashuo.com/static/loading.gif)
- 查看 物理卷 /dev/sdb所屬的 vg0 所剩空間的大小
![](http://static.javashuo.com/static/loading.gif)
- 將 /dev/sdb 搬家到它所屬的vg0的剩餘空間,發現剩餘空間過小了
![](http://static.javashuo.com/static/loading.gif)
- 建立一個物理卷,擴展卷組 vg0,將/dev/sdb搬家
- pvcreate /dev/sdd
- vgextend vg0 /dev/sdd
![](http://static.javashuo.com/static/loading.gif)
- 從卷組中移除 /dev/sdb,
![](http://static.javashuo.com/static/loading.gif)
- 查看
![](http://static.javashuo.com/static/loading.gif)
- 刪除物理卷
![](http://static.javashuo.com/static/loading.gif)
- 這時 /dev/sdb就是一個普通硬盤了,把即將壞的硬盤拆走。
邏輯卷還原成普通硬盤
- umount /mnt/mysql
![](http://static.javashuo.com/static/loading.gif)
- 刪除 vg1
![](http://static.javashuo.com/static/loading.gif)
- 查看
![](http://static.javashuo.com/static/loading.gif)
- 物理卷的刪除
![](http://static.javashuo.com/static/loading.gif)
- 這就成了一個獨立硬盤就能夠拔走了
邏輯卷快照舉例
- 建立 2 個邏輯卷 /dev/vg0/lv-mysql,/dev/vg0/lv-binlog,vg0有剩餘空間
- yum install lvm2 -y
- pvcreate /dev/sd{b,c}
- vgcreate vg0 /dev/sd{b,c}
- lvcreate -L 23G -n lv-mysql vg0
- lvcreate -l +50%free -n lv-binlog vg0
- 建立文件系統,掛載,查看
- mkdir /mnt/mysql
- mkdir /mnt/binlog
- mkfs.xfs /dev/vg0/lv-mysql
- mkfs.ext4 /dev/vg0/lv-binlog
- mount /dev/vg0/lv-mysql /mnt/mysql/
- mount /dev/vg0/lv-binlog /mnt/binlog/
![](http://static.javashuo.com/static/loading.gif)
- 向邏輯卷寫入數據
- 向 /dev/vg0/lvl-mysql 寫入數據
![](http://static.javashuo.com/static/loading.gif)
- 向 /dev/vg0/lv-binlog 寫入數據
![](http://static.javashuo.com/static/loading.gif)
- 建立邏輯卷快照
- lvcreate -s -L 1G -n lv-mysql-snapshot /dev/vg0/lv-mysql // xfs
- lvcreate -s -L 1G -n lv-binlog-snapshot -p r /dev/vg0/lv-binlog //ext4
- 掛載邏輯卷快照
- mkdir /mnt/mysql-snapshot;mkdir /mnt/binlog-snapshot
- mount -o ro,nouuid /dev/vg0/lv-mysql-snapshot /mnt/mysql-snapshot/ //xfs
- mount -o ro /dev/vg0/lv-binlog-snapshot /mnt/binlog-snapshot/ //ext4
- 查看
![](http://static.javashuo.com/static/loading.gif)
- 修改邏輯卷的原數據
![](http://static.javashuo.com/static/loading.gif)
- 利用邏輯卷快照進行恢復
- umount /mnt/{mysql,mysql-snapshot,binlog,binlog-snapshot}
- lvconvert --merge /dev/vg0/lv-mysql-snapshot
- lvconvert --merge /dev/vg0/lv-binlog-snapshot
- mount /dev/vg0/lv-mysql /mnt/mysql
- mount /dev/vg0/lv-binlog /mnt/binlog
![](http://static.javashuo.com/static/loading.gif)
- 那麼邏輯卷快照去哪兒了呢?
![](http://static.javashuo.com/static/loading.gif)
歡迎關注本站公眾號,獲取更多信息