Linux LVM的使用

尛樣兒發表於2011-11-25

        Linux平臺的LVM可以使用圖形化方式建立和管理,這裡主要討論使用命令的方式來建立、使用和管理Linux平臺的LVM。這裡使用的是Redhat Linux Enterprise 5.5 x86_64的作業系統。

一.Linux平臺LVM使用幫助
[root@rhel2 /]# lvm
lvm> help
  Available lvm commands:
  Use 'lvm help ' for more information
  
  dumpconfig      Dump active configuration
  formats         List available metadata formats
  help            Display help for commands
  lvchange        Change the attributes of logical volume(s)
  lvconvert       Change logical volume layout
  lvcreate        Create a logical volume
  lvdisplay       Display information about a logical volume
  lvextend        Add space to a logical volume
  lvmchange       With the device mapper, this is obsolete and does nothing.
  lvmdiskscan     List devices that may be used as physical volumes
  lvmsadc         Collect activity data
  lvmsar          Create activity report
  lvreduce        Reduce the size of a logical volume
  lvremove        Remove logical volume(s) from the system
  lvrename        Rename a logical volume
  lvresize        Resize a logical volume
  lvs             Display information about logical volumes
  lvscan          List all logical volumes in all volume groups
  pvchange        Change attributes of physical volume(s)
  pvresize        Resize physical volume(s)
  pvck            Check the consistency of physical volume(s)
  pvcreate        Initialize physical volume(s) for use by LVM
  pvdata          Display the on-disk metadata for physical volume(s)
  pvdisplay       Display various attributes of physical volume(s)
  pvmove          Move extents from one physical volume to another
  pvremove        Remove LVM label(s) from physical volume(s)
  pvs             Display information about physical volumes
  pvscan          List all physical volumes
  segtypes        List available segment types
  vgcfgbackup     Backup volume group configuration(s)
  vgcfgrestore    Restore volume group configuration
  vgchange        Change volume group attributes
  vgck            Check the consistency of volume group(s)
  vgconvert       Change volume group metadata format
  vgcreate        Create a volume group
  vgdisplay       Display volume group information
  vgexport        Unregister volume group(s) from the system
  vgextend        Add physical volumes to a volume group
  vgimport        Register exported volume group with system
  vgmerge         Merge volume groups
  vgmknodes       Create the special files for volume group devices in /dev
  vgreduce        Remove physical volume(s) from a volume group
  vgremove        Remove volume group(s)
  vgrename        Rename a volume group
  vgs             Display information about volume groups
  vgscan          Search for all volume groups
  vgsplit         Move physical volumes into a new or existing volume group
  version         Display software and driver version information

lvm> help pvcreate
  pvcreate: Initialize physical volume(s) for use by LVM

pvcreate
 [--restorefile file]
 [-d|--debug]
 [-f[f]|--force [--force]]
 [-h|-?|--help]
 [--labelsector sector]
 [-M|--metadatatype 1|2]
 [--pvmetadatacopies #copies]
 [--metadatasize MetadataSize[bBsSkKmMgGtTpPeE]]
 [--dataalignment Alignment[bBsSkKmMgGtTpPeE]]
 [--dataalignmentoffset AlignmentOffset[bBsSkKmMgGtTpPeE]]
 [--setphysicalvolumesize PhysicalVolumeSize[bBsSkKmMgGtTpPeE]
 [-t|--test]
 [-u|--uuid uuid]
 [-v|--verbose]
 [-y|--yes]
 [-Z|--zero {y|n}]
 [--version]
 PhysicalVolume [PhysicalVolume...]

也可以直接執行lvm help pvcreate命令獲取pvcreate的幫助資訊。

二.基本使用
[root@rhel2 dev]# fdisk -l /dev/sdb

Disk /dev/sdb: 53.6 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        6527    52428096    5  Extended
/dev/sdb5               1          13      104359+  83  Linux
/dev/sdb6              14          26      104391   83  Linux
/dev/sdb7              27          39      104391   83  Linux
/dev/sdb8              40          64      200781   83  Linux
/dev/sdb9              65          89      200781   83  Linux
/dev/sdb10             90         114      200781   83  Linux
/dev/sdb11            115         737     5004216   83  Linux
/dev/sdb12            738        1360     5004216   83  Linux
/dev/sdb13           1361        1983     5004216   83  Linux
/dev/sdb14           1984        2606     5004216   83  Linux
/dev/sdb15           2607        3229     5004216   83  Linux

[root@rhel2 dev]# pvcreate /dev/sdb11
  Physical volume "/dev/sdb11" successfully created
[root@rhel2 dev]# pvcreate /dev/sdb12
  Physical volume "/dev/sdb12" successfully created
[root@rhel2 dev]# pvcreate /dev/sdb13
  Physical volume "/dev/sdb13" successfully created

[root@rhel2 /]# pvs
  PV         VG   Fmt  Attr PSize   PFree 
  /dev/sdb11      lvm2 --     4.77G   4.77G
  /dev/sdb12      lvm2 --     4.77G   4.77G
  /dev/sdb13      lvm2 --     4.77G   4.77G
  /dev/sdb5       lvm2 --   101.91M 101.91M
  /dev/sdb6       lvm2 --   101.94M 101.94M
  /dev/sdb7       lvm2 --   101.94M 101.94M

[root@rhel2 /]# pvdisplay /dev/sdb11
  "/dev/sdb11" is a new physical volume of "4.77 GB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb11
  VG Name              
  PV Size               4.77 GB
  Allocatable           NO
  PE Size (KByte)       0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               zsIwLB-3olS-NNi1-4pDN-E4f7-7ZkI-LBK3p5

[root@rhel2 dev]# vgcreate -s 256M oravg /dev/sdb11 /dev/sdb12
  Volume group "oravg" successfully created
[root@rhel2 dev]# vgextend oravg /dev/sdb13
  Volume group "oravg" successfully extended
[root@rhel2 dev]# vgchange -a y oravg
  0 logical volume(s) in volume group "oravg" now active
[root@rhel2 dev]# vgdisplay oravg
  --- Volume group ---
  VG Name               oravg
  System ID            
  Format                lvm2
  Metadata Areas        3
  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                3
  Act PV                3
  VG Size               14.25 GB
  PE Size               256.00 MB
  Total PE              57
  Alloc PE / Size       0 / 0  
  Free  PE / Size       57 / 14.25 GB
  VG UUID               3MiH2E-gBSN-0oLx-0kZN-GF0t-pWid-nr0TLW
  
[root@rhel2 dev]# lvcreate -L 5g -n lvora1 oravg
  Logical volume "lvora1" created
[root@rhel2 dev]# vgdisplay -v oravg
    Using volume group(s) on command line
    Finding volume group "oravg"
  --- Volume group ---
  VG Name               oravg
  System ID            
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               14.25 GB
  PE Size               256.00 MB
  Total PE              57
  Alloc PE / Size       20 / 5.00 GB
  Free  PE / Size       37 / 9.25 GB
  VG UUID               3MiH2E-gBSN-0oLx-0kZN-GF0t-pWid-nr0TLW
  
  --- Logical volume ---
  LV Name                /dev/oravg/lvora1
  VG Name                oravg
  LV UUID                glJVda-fCtd-fmEZ-nL7h-wjT4-0KcN-Kdg5wc
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                5.00 GB
  Current LE             20
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
  
  --- Physical volumes ---
  PV Name               /dev/sdb11    
  PV UUID               zsIwLB-3olS-NNi1-4pDN-E4f7-7ZkI-LBK3p5
  PV Status             allocatable
  Total PE / Free PE    19 / 0
  
  PV Name               /dev/sdb12    
  PV UUID               rLlY4W-nc3n-Xdns-lNg5-Xg3f-02Tx-0cUGah
  PV Status             allocatable
  Total PE / Free PE    19 / 18
  
  PV Name               /dev/sdb13    
  PV UUID               zNyDs2-5KKN-AwxJ-TZXP-d1If-9nGZ-CcVh36
  PV Status             allocatable
  Total PE / Free PE    19 / 19
  
[root@rhel2 dev]# lvdisplay /dev/oravg/lvora1
  --- Logical volume ---
  LV Name                /dev/oravg/lvora1
  VG Name                oravg
  LV UUID                glJVda-fCtd-fmEZ-nL7h-wjT4-0KcN-Kdg5wc
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                5.00 GB
  Current LE             20
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
  
[root@rhel2 dev]# mkfs.ext3 /dev/oravg/lvora1
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
655360 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
 32768, 98304, 163840, 229376, 294912, 819200, 884736

Writing inode tables: done                           
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 20 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@rhel2 dev]# cd /
[root@rhel2 /]# mkdir /oracle
[root@rhel2 /]# cp /etc/fstab /etc/fstab.bak
[root@rhel2 /]# echo "/dev/oravg/lvora1         /oracle ext3 defaults        0 0" >> /etc/fstab
[root@rhel2 /]# mount /oracle
[root@rhel2 /]# mount
/dev/sda3 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/mapper/oravg-lvora1 on /oracle type ext3 (rw)
[root@rhel2 /]# ls -al /dev/oravg/lvora1
lrwxrwxrwx 1 root root 24 Nov 22 03:45 /dev/oravg/lvora1 -> /dev/mapper/oravg-lvora1
[root@rhel2 /]# df -ahT
Filesystem    Type    Size  Used Avail Use% Mounted on
/dev/sda3     ext3     44G   16G   26G  38% /
proc          proc       0     0     0   -  /proc
sysfs        sysfs       0     0     0   -  /sys
devpts      devpts       0     0     0   -  /dev/pts
/dev/sda1     ext3     99M   12M   83M  13% /boot
tmpfs        tmpfs    636M     0  636M   0% /dev/shm
none   binfmt_misc       0     0     0   -  /proc/sys/fs/binfmt_misc
sunrpc  rpc_pipefs       0     0     0   -  /var/lib/nfs/rpc_pipefs
/dev/mapper/oravg-lvora1
              ext3    5.0G  139M  4.6G   3% /oracle
[root@rhel2 /]# lvextend -L +5g /dev/oravg/lvora1
  Extending logical volume lvora1 to 10.00 GB
  Logical volume lvora1 successfully resized
[root@rhel2 /]# df -ahT
Filesystem    Type    Size  Used Avail Use% Mounted on
/dev/sda3     ext3     44G   16G   26G  38% /
proc          proc       0     0     0   -  /proc
sysfs        sysfs       0     0     0   -  /sys
devpts      devpts       0     0     0   -  /dev/pts
/dev/sda1     ext3     99M   12M   83M  13% /boot
tmpfs        tmpfs    636M     0  636M   0% /dev/shm
none   binfmt_misc       0     0     0   -  /proc/sys/fs/binfmt_misc
sunrpc  rpc_pipefs       0     0     0   -  /var/lib/nfs/rpc_pipefs
/dev/mapper/oravg-lvora1
              ext3    5.0G  139M  4.6G   3% /oracle

[root@rhel2 /]# umount /oracle
[root@rhel2 /]#
lvremove -f /dev/oravg/lvora1
  Logical volume "lvora1" successfully removed
[root@rhel2 /]#
vgreduce oravg /dev/sdb13
  Removed "/dev/sdb13" from volume group "oravg"
[root@rhel2 /]#
vgremove -f oravg
  Volume group "oravg" successfully removed


三.VG的恢復,匯出和匯入
       
下面討論兩個節點掛載相同的儲存,在節點間如何實現VG的匯出和匯入。

節點1:
[root@cddcdb1 /]#
df -ahT
Filesystem    Type    Size  Used Avail Use% Mounted on
/dev/sda3     ext3    122G   14G  103G  12% /
proc          proc       0     0     0   -  /proc
sysfs        sysfs       0     0     0   -  /sys
devpts      devpts       0     0     0   -  /dev/pts
/dev/sda1     ext3     99M   12M   82M  13% /boot
tmpfs        tmpfs    7.9G     0  7.9G   0% /dev/shm
none   binfmt_misc       0     0     0   -  /proc/sys/fs/binfmt_misc
sunrpc  rpc_pipefs       0     0     0   -  /var/lib/nfs/rpc_pipefs
/opt/RHEL_5.4 x86_64.iso
           iso9660    3.4G  3.4G     0 100% /mnt
/dev/mapper/bakvg-lvbak1
              ext3    788G  809M  747G   1% /dbbak
1).解除安裝檔案系統:
[root@cddcdb1 /]# umount /dbbak
2).檢視vg詳細資訊:
[root@cddcdb1 /]# vgdisplay -v bakvg
    Using volume group(s) on command line
    Finding volume group "bakvg"
  --- Volume group ---
  VG Name               bakvg
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               805.00 GB
  PE Size               256.00 MB
  Total PE              3220
  Alloc PE / Size       3200 / 800.00 GB
  Free  PE / Size       20 / 5.00 GB
  VG UUID               LnuziM-1tlI-EYZQ-LAdV-8mFR-TfKJ-xAqDeb

  --- Logical volume ---
  LV Name                /dev/bakvg/lvbak1
  VG Name                bakvg
  LV UUID                zvpK4u-1T99-SAke-mToV-XtfG-euHM-uJTA8v
  LV Write Access        read/write
  LV Status              NOT available
  LV Size                800.00 GB
  Current LE             3200
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto

  --- Physical volumes ---
  PV Name               /dev/sdg
  PV UUID               66QUV4-f8g6-UKZ1-SfNV-oOwX-stuI-omJAyn
  PV Status             allocatable
  Total PE / Free PE    3220 / 20

3).執行lvchange命令使vg上面的所有lv都變得不活動:
[root@cddcdb1 bakvg]# lvchange -a n /dev/bakvg/lvbak1
4).匯出bakvg:

[root@cddcdb1 bakvg]# vgexport bakvg
 
Volume group "bakvg" successfully exported


節點2
1).掃描pv:

[root@cddcdb2 /]# vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "bakvg" using metadata type lvm2
2).顯示vg資訊:
[root@cddcdb2 /]# vgdisplay
  --- Volume group ---
  VG Name               bakvg
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               805.00 GB
  PE Size               256.00 MB
  Total PE              3220
  Alloc PE / Size       3200 / 800.00 GB
  Free  PE / Size       20 / 5.00 GB
  VG UUID               LnuziM-1tlI-EYZQ-LAdV-8mFR-TfKJ-xAqDeb

3).匯入vg:
[root@cddcdb2 /]# vgimport bakvg
  Volume group "bakvg" successfully imported
4).啟用vg:
[root@cddcdb2 /]# vgchange -a y bakvg
  1 logical volume(s) in volume group "bakvg" now active
[root@cddcdb2 /]# cd /dev/bakvg
[root@cddcdb2 bakvg]# ls
lvbak1
[root@cddcdb2 bakvg]# cat /etc/fstab
LABEL=/                 /                       ext3    defaults        1 1
LABEL=/boot             /boot                   ext3    defaults        1 2
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
LABEL=SWAP-sda2         swap                    swap    defaults        0 0
/dev/bakvg/lvbak1               /dbbak                  ext3    defaults        0 0
5).啟用vg上面的所有lv:
[root@cddcdb2 bakvg]# lvchange -a y /dev/bakvg/lvbak1
[root@cddcdb2 bakvg]# cd /
6).建立掛載目錄,掛載檔案系統:
[root@cddcdb2 /]# mkdir dbbak
[root@cddcdb2 /]# mount /dbbak

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/23135684/viewspace-712121/,如需轉載,請註明出處,否則將追究法律責任。

相關文章