Linux磁盤(pán)管理-邏輯卷管理lvm

一、什么是lvm

是對(duì)磁盤(pán)分區(qū)進(jìn)行管理的一種機(jī)制。通過(guò)在硬盤(pán)和分區(qū)之上建立一個(gè)邏輯層,提高了磁盤(pán)分區(qū)管理的靈活性,在線(xiàn)調(diào)整磁盤(pán)各個(gè)分區(qū)的大小,動(dòng)態(tài)調(diào)整磁盤(pán)容量。

PV --- 物理卷,由分區(qū)轉(zhuǎn)化
VG --- 卷組,建立在pv之上,有一個(gè)或多個(gè)組成
LV --- 邏輯卷,建立在vg之上,是一個(gè)動(dòng)態(tài)改變大小的分區(qū)
PE --- 物理區(qū)域,是lvm的最小尋址單位,大小為4MB

二、lvm相關(guān)常用命令

命令 含義
pvcreate,vgcreate,lvcreate 創(chuàng)建
pvremove,vgremove,lvremove 移除
pvdisplay,vgdisplay,lvdisplay 查看屬性
pvscan,vgscan,lvscan 掃描
pvs,vgs,lvs 列出所有
pvmove 數(shù)據(jù)遷移
lvextend 擴(kuò)容
lvreduce 縮容
lvresize 調(diào)整大小

三、創(chuàng)建lvm

1.安裝軟件包

[root@work1 ~]# yum install -y lvm2

2.創(chuàng)建物理卷PV

[root@work1 ~]# pvcreate /dev/sdb
WARNING: dos signature detected on /dev/sdb at offset 510. Wipe it? [y/n]: y
  Wiping dos signature on /dev/sdb.
  Physical volume "/dev/sdb" successfully created.
[root@work1 ~]# pvs
  PV         VG Fmt  Attr PSize PFree
  /dev/sdb      lvm2 ---  1.00g 1.00g

3.創(chuàng)建卷組VG

[root@work1 ~]# vgcreate vg_test01 /dev/sdb 
  Volume group "vg_test01" successfully created
[root@work1 ~]# vgs
  VG        #PV #LV #SN Attr   VSize    VFree   
  vg_test01   1   0   0 wz--n- 1020.00m 1020.00m

4.創(chuàng)建邏輯卷lv

[root@work1 ~]# lvcreate -n lv_test01 -L 100MB vg_test01
WARNING: xfs signature detected on /dev/vg_test01/lv_test01 at offset 0. Wipe it? [y/n]: y
  Wiping xfs signature on /dev/vg_test01/lv_test01.
  Logical volume "lv_test01" created.
[root@work1 ~]# lvs
  LV        VG        Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv_test01 vg_test01 -wi-a----- 100.00m

5.查看磁盤(pán)分區(qū)信息

[root@work1 ~]# lsblk
NAME                  MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                     8:0    0   50G  0 disk 
├─sda1                  8:1    0    2G  0 part [SWAP]
└─sda2                  8:2    0   48G  0 part /
sdb                     8:16   0    1G  0 disk 
└─vg_test01-lv_test01 253:0    0  100M  0 lvm  
sr0                    11:0    1 1024M  0 rom 

6.掛載使用

#xfs文件系統(tǒng),ext4文件系統(tǒng)使用mkfs.ext4
[root@work1 ~]# mkfs.xfs /dev/vg_test01/lv_test01 
[root@work1 parted]# mount /dev/vg_test01/lv_test01 data1

7.邏輯卷擴(kuò)容
查看卷組容量,如果有空閑,則直接使用 lvextend擴(kuò)容邏輯卷lv;如果沒(méi)有空閑,則添加磁盤(pán)-->轉(zhuǎn)化為pv-->加入卷組vg-->lvextend擴(kuò)容邏輯卷lv。

[root@work1 ~]# vgs
  VG        #PV #LV #SN Attr   VSize    VFree  
  vg_test01   1   1   0 wz--n- 1020.00m 920.00m

#vg容量充足,直接使用lvextend,縮容是lvreduce
[root@work1 ~]# lvextend -L 200MB /dev/vg_test01/lv_test01    # -L 是擴(kuò)容到
  Size of logical volume vg_test01/lv_test01 changed from 100.00 MiB (25 extents) to 200.00 MiB (50 extents).
  Logical volume vg_test01/lv_test01 successfully resized.

[root@work1 ~]# lvs
  LV        VG        Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv_test01 vg_test01 -wi-a----- 200.00m 

#如果已經(jīng)掛載使用,擴(kuò)容后,系統(tǒng)是還沒(méi)有識(shí)別的,需要用命令重讀文件系統(tǒng)
xfs文件系統(tǒng)類(lèi)型使用命令   xfs_growfs    擴(kuò)容邏輯卷的路徑
ext4文件系統(tǒng)類(lèi)型使用命令   resize2fs    擴(kuò)容邏輯卷的路徑
[root@work1 parted]# xfs_growfs /dev/vg_test01/lv_test01

8.邏輯卷縮容
ext4文件系統(tǒng)支持縮容,但不建議進(jìn)行縮容操作,縮容會(huì)損壞文件系統(tǒng),導(dǎo)致數(shù)據(jù)丟失。
xfs文件系統(tǒng)不支持縮容,需要轉(zhuǎn)換成ext4支持縮容的文件系統(tǒng),再進(jìn)行縮容。

1.查看磁盤(pán)掛載情況
[root@work1 parted]# df -Th
Filesystem                      Type      Size  Used Avail Use% Mounted on
/dev/sda2                       xfs        48G  3.5G   45G   8% /
devtmpfs                        devtmpfs  476M     0  476M   0% /dev
tmpfs                           tmpfs     487M   12K  487M   1% /dev/shm
tmpfs                           tmpfs     487M  7.7M  479M   2% /run
tmpfs                           tmpfs     487M     0  487M   0% /sys/fs/cgroup
tmpfs                           tmpfs      98M     0   98M   0% /run/user/0
/dev/mapper/vg_test01-lv_test01 xfs       149M  5.4M  144M   4% /root/study/parted/data1
/dev/mapper/vg_test02-lv_test02 ext4      6.8M   77K  6.2M   2% /root/study/parted/data2

2.卸載磁盤(pán)
[root@work1 ~]# umount /dev/vg_test02/lv_test02

3.檢查磁盤(pán)情況
[root@work1 parted]# e2fsck -f /dev/vg_test02/lv_test02
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/vg_test02/lv_test02: 11/2048 files (9.1% non-contiguous), 1362/8192 blocks

4.文件系統(tǒng)容量更新
[root@work1 parted]# resize2fs /dev/vg_test02/lv_test02
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg_test02/lv_test02 to 53248 (1k) blocks.
The filesystem on /dev/vg_test02/lv_test02 is now 53248 blocks long.

5.執(zhí)行邏輯卷縮容命令,進(jìn)行縮容,還可以用 lvresize命令
[root@work1 parted]# lvreduce -L 30MB /dev/vg_test02/lv_test02
  Rounding size to boundary between physical extents: 32.00 MiB.
  WARNING: Reducing active logical volume to 32.00 MiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg_test02/lv_test02? [y/n]: y
  Size of logical volume vg_test02/lv_test02 changed from 52.00 MiB (13 extents) to 32.00 MiB (8 extents).
  Logical volume vg_test02/lv_test02 successfully resized.

6.查看邏輯卷信息,縮容成功
[root@work1 parted]# lvs
  LV        VG        Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv_test01 vg_test01 -wi-ao---- 152.00m                                                    
  lv_test02 vg_test02 -wi-a-----  32.00m 

7.掛載使用,掛載失敗,文件系統(tǒng)損壞,數(shù)據(jù)丟失
[root@work1 parted]# mount /dev/vg_test02/lv_test02 data2
mount: wrong fs type, bad option, bad superblock on /dev/mapper/vg_test02-lv_test02,
       missing codepage or helper program, or other error

       In some cases useful info is found in syslog - try
       dmesg | tail or so.

[root@work1 parted]# dmesg | tail
[15316.816635] e1000: eth2 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[15317.381916] e1000: eth0 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[16731.617369] bond0: Releasing backup interface eth2
[16835.339320] e1000: eth1 NIC Link is Down
[16836.329593] e1000: eth0 NIC Link is Down
[16842.622359] e1000: eth1 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[16842.624127] e1000: eth0 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[19352.794060] EXT4-fs (dm-1): mounted filesystem with ordered data mode. Opts: (null)
[19998.112564] EXT4-fs (dm-1): mounted filesystem with ordered data mode. Opts: (null)
[20238.769157] EXT4-fs (dm-1): bad geometry: block count 53248 exceeds size of device (32768 blocks)

[root@work1 parted]# e2fsck -f /dev/vg_test02/lv_test02
e2fsck 1.42.9 (28-Dec-2013)
The filesystem size (according to the superblock) is 53248 blocks
The physical size of the device is 32768 blocks
Either the superblock or the partition table is likely to be corrupt!
Abort<y>? yes

9.移除邏輯卷,恢復(fù)磁盤(pán)

1.卸載硬盤(pán)
[root@work1 parted]# umount data1

2.移除邏輯卷lv    lvremove
[root@work1 parted]# lvs
  LV        VG        Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv_test01 vg_test01 -wi-a----- 300.00m                                                    
[root@work1 parted]# lvremove /dev/vg_test01/lv_test01 
Do you really want to remove active logical volume vg_test01/lv_test01? [y/n]: y
  Logical volume "lv_test01" successfully removed
[root@work1 parted]# lvs

3.移除卷組vg    vgremove
[root@work1 parted]# vgs
  VG        #PV #LV #SN Attr   VSize    VFree   
  vg_test01   1   0   0 wz--n- 1020.00m 1020.00m
[root@work1 parted]# vgremove vg_test01 
  Volume group "vg_test01" successfully removed
[root@work1 parted]# vgs

4.移除物理卷pv    pvremove
[root@work1 parted]# pvs
  PV         VG Fmt  Attr PSize PFree
  /dev/sdb      lvm2 ---  1.00g 1.00g
[root@work1 parted]# pvremove /dev/sdb
  Labels on physical volume "/dev/sdb" successfully wiped.
[root@work1 parted]# pvs

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容