歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

LVM常用操作

一、LVM是什麼   LVM是 Logical Volume Manager(邏輯卷管理)的簡寫,簡單的說LVM可以跨分區存儲數據,並且LVM可以在無需停機的狀態下動態調整分區的大小。   二、基本術語   *物理存儲介質(PhysicalStorageMedia)   指系統的物理存儲設備:磁盤,如:/dev/hda、/dev/sda等,是存儲系統最底層的存儲單元。   *物理卷(Physical Volume,PV)   指磁盤分區或從邏輯上與磁盤分區具有同樣功能的設備(如RAID),是LVM的基本存儲邏輯塊,但和基本的物理存儲介質(如分區、磁盤等)比較,卻包含有與LVM相關的管理參數。   *卷組(Volume Group,VG)   類似於非LVM系統中的物理磁盤,其由一個或多個物理卷PV組成。可以在卷組上創建一個或多個LV(邏輯卷)。   *邏輯卷(Logical Volume,LV)   類似於非LVM系統中的磁盤分區,邏輯卷建立在卷組VG之上。在邏輯卷LV之上可以建立文件系統(比如/home或者/usr等)。   *物理塊(Physical Extent,PE)   每一個物理卷PV被劃分為稱為PE(Physical Extents)的基本單元,具有唯一編號的PE是可以被LVM尋址的最小單元。PE的大小是可配置的,默認為4MB。所以物理卷(PV)由大小等同的基本單元PE組成。   *邏輯塊(Logical Extent,LE)   每一個物理卷PV被劃分為稱為PE(Physical Extents)的基本單元,具有唯一編號的PE是可以被LVM尋址的最小單元。PE的大小是可配置的,默認為4MB。所以物理卷(PV)由大小等同的基本單元PE組成。   邏輯卷LV也被劃分為可被尋址的基本單位,稱為LE。在同一個卷組中,LE的大小和PE是相同的,並且一一對應。如圖(1) 三、創建管理   常用管理命令如下:   管理lv: pv: pvcreate, pvdisplay, pvs, pvscan, pvremove, pvmove vg: vgcreate, vgdisplay, vgs, vgscan, vgremove, vgextend, vgreduce lv: lvcreate, lvdisplay, lvs, lvresize, lvextend, lvreduce, lvremove   1、PV相關操作   創建pv   1.1、如圖(2) 使用fdisk創建物理磁盤分區   [root@www ~]# fdisk -l /dev/sdb Disk /dev/sdb: 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: 0xd9180e6e    Device Boot      Start         End      Blocks   Id  System /dev/sdb1               1         262     2104483+  8e  Linux LVM /dev/sdb2             263         655     3156772+  8e  Linux LVM [root@www ~]#       創建物理卷 pvcreate  
[root@www ~]# pvcreate /dev/sdb1 /dev/sdb2//創建PV 
  Physical volume "/dev/sdb1" successfully created
  Physical volume "/dev/sdb2" successfully created

 

  查看PV  
[root@www ~]# pvs//查看PV
  PV         VG   Fmt  Attr PSize  PFree
  /dev/sda2  vg0  lvm2 a--  59.99g 7.99g
  /dev/sdb1       lvm2 ---   2.01g 2.01g
  /dev/sdb2       lvm2 ---   3.01g 3.01g
[root@www ~]# pvdisplay /dev/sdb[0-9]//可以看到詳細信息
  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               myvg
  PV Size               2.01 GiB / not usable 3.16 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              513
  Free PE               513
  Allocated PE          0
  PV UUID               rA5Vdy-j2rr-b9Vf-zo8I-sjFD-1z23-VhHAgI
    
  --- Physical volume ---
  PV Name               /dev/sdb2
  VG Name               myvg
  PV Size               3.01 GiB / not usable 2.79 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              770
  Free PE               770
  Allocated PE          0
  PV UUID               GVtLyK-Ugd1-DKHo-3wHS-oia1-lbpz-PA4oma
    
[root@www ~]#

 

刪除pv  
[root@www ~]# pvcreate /dev/sdb3   //創建pv
  Physical volume "/dev/sdb3" successfully created
[root@www ~]# pvs   //查看pv
  PV         VG   Fmt  Attr PSize  PFree
  /dev/sda2  vg0  lvm2 a--  59.99g 7.99g
  /dev/sdb1  myvg lvm2 a--   2.00g    0 
  /dev/sdb2  myvg lvm2 a--   3.01g 2.01g
  /dev/sdb3       lvm2 ---   3.01g 3.01g
[root@www ~]# pvremove /dev/sdb3   //刪除pv /dev/sdb3
  Labels on physical volume "/dev/sdb3" successfully wiped
[root@www ~]# pvs
  PV         VG   Fmt  Attr PSize  PFree
  /dev/sda2  vg0  lvm2 a--  59.99g 7.99g
  /dev/sdb1  myvg lvm2 a--   2.00g    0 
  /dev/sdb2  myvg lvm2 a--   3.01g 2.01g

 

  2、VG相關操作   創建VG vgcreate   # vgcreate VGNAME /dev/PV1... [-s|--physicalextentsize PhysicalExtentSize[bBsSkKmMgGtTpPeE]]   //指定PE大小       [-t|--test] //測試,不真正創建        [-v|--verbose]//顯示創建過程  
[root@www ~]# vgcreate myvg /dev/sdb{1,2}
  Volume group "myvg" successfully created

 

  查看VG   
[root@www ~]# vgs
  VG   #PV #LV #SN Attr   VSize  VFree
  myvg   2   0   0 wz--n-  5.01g 5.01g
  vg0    1   4   0 wz--n- 59.99g 7.99g
[root@www ~]# vgdisplay 
  --- 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               5.01 GiB
  PE Size               4.00 MiB  //PE 塊大小
  Total PE              1283
  Alloc PE / Size       0 / 0   
  Free  PE / Size       1283 / 5.01 GiB
  VG UUID               llSGpO-Bk51-KVBB-llAZ-AA9E-aBYq-sUrnPL

 

刪除vg  
# vgremove VGNAME...

[root@www ~]# vgs
  VG   #PV #LV #SN Attr   VSize  VFree
  myvg   2   0   0 wz--n-  5.01g 5.01g
  vg0    1   4   0 wz--n- 59.99g 7.99g
[root@www ~]# vgremove myvg//刪除vg
  Volume group "myvg" successfully removed
[root@www ~]# vgs
  VG   #PV #LV #SN Attr   VSize  VFree
  vg0    1   4   0 wz--n- 59.99g 7.99g
[root@www ~]#

 

  縮減VG:通過從vg中移除pv實現   注意:計劃移除的pv不能包含已經存儲數據的pe;如果有需要先使用pvmove移動到同vg內其它pv上;
[root@www ~]# vgcreate myvg /dev/sdb{1,2}
  Volume group "myvg" successfully created
[root@www ~]# vgdisplay 
  --- 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               5.01 GiB//大小為5G
  PE Size               4.00 MiB
  Total PE              1283
  Alloc PE / Size       0 / 0   
  Free  PE / Size       1283 / 5.01 GiB
  VG UUID               Iz9bc0-Sw38-H916-TAg3-abNh-iUJW-hjuRoB

------------------------------------------------------------------------------

 

  我們要在myvg中移除/dev/sdb2   【縮減vg要保證縮減後的vg大小能夠存儲原來的數據】  
[root@www ~]# pvmove /dev/sdb2  // 將/sdb2中的數據移動到其他pv上
  No data to move for myvg
[root@www ~]# vgreduce myvg /dev/sdb2//移除/dev/sdb2
  Removed "/dev/sdb2" from volume group "myvg"
[root@www ~]# vgdisplay myvg
  --- Volume group ---
  VG Name               myvg
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               2.00 GiB// 大小變為2G
  PE Size               4.00 MiB
  Total PE              513
  Alloc PE / Size       0 / 0   
  Free  PE / Size       513 / 2.00 GiB
  VG UUID               Iz9bc0-Sw38-H916-TAg3-abNh-iUJW-hjuRoB

 

----------------------- 擴展VG: 向VG中添加更多的PV來實現   vgextend  
[root@www ~]# vgs
  VG   #PV #LV #SN Attr   VSize  VFree
  myvg   1   0   0 wz--n-  2.00g 2.00g//擴展前大小為2G
  vg0    1   4   0 wz--n- 59.99g 7.99g
[root@www ~]# vgextend myvg /dev/sdb2//向myvg中添加/dev/sdb2
  Volume group "myvg" successfully extended
[root@www ~]# vgs
  VG   #PV #LV #SN Attr   VSize  VFree
  myvg   2   0   0 wz--n-  5.01g 5.01g//大小變為5G
  vg0    1   4   0 wz--n- 59.99g 7.99g

 

  3、LV相關操作   創建LV Logical Volume     創建lv:     lvcreate -L #[bBsSkKmMgGtTpPeE] [-n|--name  LogicalVolume{Name|Path}]  VG_NAME         LV設備路徑:     (1) /dev/VGNAME/LVNAME     (2) /dev/mapper/VGNAME-LVNAME       實際路徑:/dev/dm-#   注意:lv的大小必須為pe的整數倍  
[root@www ~]# lvcreate -L 4G -n mylv myvg//創建大小為4G的LV 名字為mylv
  Logical volume "mylv" created
[root@www ~]# lvs//查看LV
  LV   VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  mylv myvg -wi-a-----  4.00g           //大小為4G                                         
  root vg0  -wi-ao---- 20.00g                                                    
  swap vg0  -wi-ao----  2.00g                                                    
  usr  vg0  -wi-ao---- 10.00g                                                    
  var  vg0  -wi-ao---- 20.00g                                                    
[root@www ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/myvg/mylv
  LV Name                mylv
  VG Name                myvg
  LV UUID                i3tDAL-GdB4-TVIf-Efun-vVmg-Ig00-cSCdKk
  LV Write Access        read/write
  LV Creation host, time www.w1.com, 2015-08-16 05:05:40 +0800
  LV Status              available
  # open                 0
  LV Size                4.00 GiB
  Current LE             1024
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:4

 

移除LV    lvremove LV_PATH  
[root@www ~]# lvs
  LV   VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  mylv myvg -wi-a-----  4.00g          //查看lv                                          
  root vg0  -wi-ao---- 20.00g                                                    
  swap vg0  -wi-ao----  2.00g                                                    
  usr  vg0  -wi-ao---- 10.00g                                                    
  var  vg0  -wi-ao---- 20.00g                                                    
[root@www ~]# lvremove /dev/myvg/mylv//移除mylv
Do you really want to remove active logical volume mylv? [y/n]: y
  Logical volume "mylv" successfully removed
[root@www ~]# lvs
  LV   VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root vg0  -wi-ao---- 20.00g             //已經不存在mylv                                       
  swap vg0  -wi-ao----  2.00g                                                    
  usr  vg0  -wi-ao---- 10.00g                                                    
  var  vg0  -wi-ao---- 20.00g                                                    
[root@www ~]#

 

  擴展LV        存儲設備有:物理邊界     文件系統有:邏輯邊界       擴展:先擴展物理邊界,而後拉伸邏輯邊界     lvextend, lvresize     resize2fs   擴展LV步驟     1、先確定目標大小:要確保此lv所在vg中有足夠的空閒可用; 2G --> 4G             4G             +2G 2、擴展物理邊界         lvextend -L [+]#[bBsSkKmMgGtTpPeE] /dev/VGNAME/LVNAME 3、拉伸邏輯邊界         resize2fs /dev/VGNAME/LVNAME      例:先創建大小為2G的LV然後在擴展為4G  
[root@www ~]# lvcreate -L 2G -n mylv myvg
  Logical volume "mylv" created
[root@www ~]# lvdisplay /dev/myvg/mylv
  --- Logical volume ---
  LV Path                /dev/myvg/mylv
  LV Name                mylv
  VG Name                myvg
  LV UUID                yZE2SK-G9J9-dFdH-uJvK-Lln6-AS2G-UqHMjU
  LV Write Access        read/write
  LV Creation host, time www.w1.com, 2015-08-16 05:12:04 +0800
  LV Status              available
  # open                 0
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:4

 

  格式化/myvg/mylv  
[root@www ~]# mke2fs -t ext4 /dev/myvg/mylv
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131072 inodes, 524288 blocks
26214 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
32768, 98304, 163840, 229376, 294912
Writing inode tables: done                            
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 38 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

 

 
[root@www ~]# mkdir /nyist//創建掛載點
[root@www ~]# mount /dev/myvg/mylv /nyist//掛載
[root@www ~]# df -h//查看大小
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg0-root   20G   18G  908M  96% /
tmpfs                 565M     0  565M   0% /dev/shm
/dev/sda1             190M   33M  148M  18% /boot
/dev/mapper/vg0-usr   9.8G  2.4G  6.9G  26% /usr
/dev/mapper/vg0-var    20G  294M   19G   2% /var
/dev/sda3              59G  4.7G   52G   9% /usr/soc
/dev/mapper/myvg-mylv
                      2.0G  3.0M  1.9G   1% /nyist

 

  [root@www ~]# cp /etc/fstab /nyist   查看文件  
[root@www ~]# cat /nyist/fstab 
#
# /etc/fstab
# Created by anaconda on Thu Mar 26 20:01:38 2015
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/vg0-root    /                       ext4    defaults        1 1
UUID=0d0698ff-1645-4ff8-af27-fc6e73573941 /boot                   ext4    defaults        1 2
/dev/mapper/vg0-usr     /usr                    ext4    defaults        1 2
/dev/mapper/vg0-var     /var                    ext4    defaults        1 2
/dev/mapper/vg0-swap    swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/sda3           /usr/soc                ext4    defaults        0 0
[root@www ~]#

 

  查看VG剩余空間:  
[root@www ~]# vgs
  VG   #PV #LV #SN Attr   VSize  VFree
  myvg   2   1   0 wz--n-  5.01g 3.01g//還有3G 我們要擴展到4G 容量足夠
  vg0    1   4   0 wz--n- 59.99g 7.99g

 

  擴展物理邊界  
[root@www ~]# lvs /dev/myvg/mylv
  LV   VG   Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  mylv myvg -wi-a----- 2.00g          //目前大小為2G                                          
[root@www ~]# lvextend -L 4G  /dev/myvg/mylv  //擴展LV 也可以寫為: lvextend -L +2G /vev/myvg/mylv
  Size of logical volume myvg/mylv changed from 2.00 GiB (512 extents) to 4.00 GiB (1024 extents).
  Logical volume mylv successfully resized
[root@www ~]# lvs /dev/myvg/mylv查看mylv大小
  LV   VG   Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  mylv myvg -wi-a----- 4.00g              //擴展為4G                                      
[root@www ~]# 
[root@www ~]# lvdisplay /dev/myvg/mylv
  --- Logical volume ---
  LV Path                /dev/myvg/mylv
  LV Name                mylv
  VG Name                myvg
  LV UUID                yZE2SK-G9J9-dFdH-uJvK-Lln6-AS2G-UqHMjU
  LV Write Access        read/write
  LV Creation host, time www.w1.com, 2015-08-16 05:12:04 +0800
  LV Status              available
  # open                 0
  LV Size                4.00 GiB  //已經擴展為4G
  Current LE             1024
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:4

 

  查看邏輯空間大小  
[root@www ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
......
/dev/sda3              59G  4.7G   52G   9% /usr/soc
/dev/mapper/myvg-mylv
                      2.0G  3.1M  1.9G   1% /nyist//依然為2G
[root@www ~]#

 

  拉伸邏輯邊界  
[root@www ~]# resize2fs /dev/myvg/mylv//擴展邏輯邊界
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/myvg/mylv is mounted on /nyist; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/myvg/mylv to 1048576 (4k) blocks.
The filesystem on /dev/myvg/mylv is now 1048576 blocks long.
[root@www ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg0-root   20G   18G  908M  96% /
tmpfs                 565M     0  565M   0% /dev/shm
/dev/sda1             190M   33M  148M  18% /boot
/dev/mapper/vg0-usr   9.8G  2.4G  6.9G  26% /usr
/dev/mapper/vg0-var    20G  294M   19G   2% /var
/dev/sda3              59G  4.7G   52G   9% /usr/soc
/dev/mapper/myvg-mylv
                      3.9G  4.0M  3.7G   1% /nyist//大小已經為3.9G 成功擴展
[root@www ~]#

 

  ============================================================ 縮減LV 注意: 1、縮減LV必須要離線 2、必須先縮減文件系統,然後再縮減LV大小   過程:       1、先確定縮減後的目標大小:要確保縮減後的大小能夠容納現在的所有數據;     2、卸載邏輯卷,並強制做文件系統一致性檢查;     e2fsck -f /dev/VGNAME/LVNAME     3、縮減邏輯邊界     resize2fs /dev/VGNAME/LVNAME #[bBsSkKmMgGtTpPeE] // resize2fs /dev/myvg/mylv 3G     4、縮減物理邊界     lvreduce -L [-]#[bBsSkKmMgGtTpPeE] /dev/VGNAME/LVNAME //lvreduce -L 3G /dev/myvg/mylv    //縮減到3G   eg:  
[root@www ~]# umount /nyist
[root@www ~]# e2fsck -f /dev/myvg/mylv
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/myvg/mylv: 12/262144 files (0.0% non-contiguous), 33871/1048576 blocks
[root@www ~]# resize2fs /dev/myvg/mylv 3G//縮減邏輯邊界
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/myvg/mylv to 786432 (4k) blocks.
The filesystem on /dev/myvg/mylv is now 786432 blocks long.
[root@www ~]# lvreduce -L 3G /dev/myvg/mylv//縮減物理邊界
  WARNING: Reducing active logical volume to 3.00 GiB
  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 4.00 GiB (1024 extents) to 3.00 GiB (768 extents).
  Logical volume mylv successfully resized
[root@www ~]#

 

  驗證:  
[root@www ~]# lvs
  LV   VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  mylv myvg -wi-a-----  3.00g          //大小3G                                          
  root vg0  -wi-ao---- 20.00g                                                    
  swap vg0  -wi-ao----  2.00g                                                    
  usr  vg0  -wi-ao---- 10.00g                                                    
  var  vg0  -wi-ao---- 20.00g                                                    
[root@www ~]# mount /dev/myvg/mylv /nyist
[root@www ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg0-root   20G   18G  908M  96% /
tmpfs                 565M     0  565M   0% /dev/shm
/dev/sda1             190M   33M  148M  18% /boot
/dev/mapper/vg0-usr   9.8G  2.4G  6.9G  26% /usr
/dev/mapper/vg0-var    20G  294M   19G   2% /var
/dev/sda3              59G  4.7G   52G   9% /usr/soc
/dev/mapper/myvg-mylv
                      2.9G  3.1M  2.8G   1% /nyist//大小2.9G   
[root@www ~]#

 


Copyright © Linux教程網 All Rights Reserved