最近在搞linux lvm功能,为了方便记忆就将我实验中的一些配置过程记录下来,方便自己也方便他们能从中获取一些知识吧。废话不说了......干活。


使用fdisk进行分区并将分区标记为lvm分区格式
fdisk /dev/sda
[root@localhost ~]# fdisk /dev/sda

The number of cylinders for this disk is set to 109296.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
  (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): n
First cylinder (28982-109296, default 28982):
Using default value 28982
Last cylinder or +size or +sizeM or +sizeK (28982-109296, default 109296): +20G

Command (m for help): t
Partition number (1-14): 14
Hex code (type L to list codes): 8e
Changed system type of partition 14 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/sda: 898.9 GB, 898990342144 bytes
255 heads, 63 sectors/track, 109296 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

  Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        2611    20972826   83  Linux
/dev/sda2            2612        4651    16386300   82  Linux swap / Solaris
/dev/sda3            4652        7084    19543072+  83  Linux
/dev/sda4            7085      109296   821017890    5  Extended
/dev/sda5            7085        9517    19543041   83  Linux
/dev/sda6            9518       11950    19543041   83  Linux
/dev/sda7           11951       14383    19543041   83  Linux
/dev/sda8           14384       16816    19543041   83  Linux
/dev/sda9           16817       19249    19543041   8e  Linux LVM
/dev/sda10          19250       21682    19543041   8e  Linux LVM
/dev/sda11          21683       24115    19543041   8e  Linux LVM
/dev/sda12          24116       26548    19543041   8e  Linux LVM
/dev/sda13          26549       28981    19543041   8e  Linux LVM
/dev/sda14          28982       31414    19543041   8e  Linux LVM

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.


分区执行完毕后执行partprobe 来重新加载分区表

创建物理卷:
   pvcreate /dev/sda14  /*    创建的物理卷必须是linux lvm 分区 */

   [root@localhost ~]# pvcreate /dev/sda14
         Physical volume "/dev/sda14" successfully created

创建卷组:
   vgcreate wed_disk /dev/sda14 /*    后边可跟多个linux lvm物理卷 */
   [root@localhost ~]# vgcreate wed_disk /dev/sda14
       Volume group "wed_disk" successfully created
   不用重启操作系统使卷组生效的命令vgchange -ay “卷组”,用vgchange -an "卷组" 使卷组失效

创建逻辑卷:
   lvcreate -L|--size "磁盘大小 k、m、g" -n "lv名称" 卷组
   [root@localhost ~]# lvcreate --size 10G -n lgyfirstlv wed_disk
      Logical volume "lgyfirstlv" created

格式化逻辑卷:
   mkfs.ext3 "逻辑卷"
       [root@localhost ~]# mkfs.ext3 /dev/wed_disk/lgyfirstlv
           mke2fs 1.39 (29-May-2006)
           Filesystem label=
           OS type: Linux
           Block size=4096 (log=2)
           Fragment size=4096 (log=2)
           1310720 inodes, 2621440 blocks
           131072 blocks (5.00%) reserved for the super user
           First data block=0
           Maximum filesystem blocks=2684354560
           80 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, 1605632

           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.

挂载逻辑卷分区:
   mount "逻辑卷分区"  "目标"
   mount /dev/wed_disk/lgyfirstlv /usr/local/src/lvtest/
   挂载后就可以使用了。

查看一下分区:
   [root@localhost src]# df -h
       Filesystem            Size  Used Avail Use% Mounted on
       /dev/sda1              20G   12G  7.1G  62% /
       tmpfs                 3.9G     0  3.9G   0% /dev/shm
       /dev/sr0              3.6G  3.6G     0 100% /mnt
       /dev/mapper/wed_disk-lgyfirstlv
                                  9.9G  1.4G  8.1G  14% /usr/local/src/lvtest





深入一下:
vg扩展:
   vgextend "卷组名称"  "物理卷名"
   [root@localhost src]# vgextend wed_disk /dev/sda15
         Volume group "wed_disk" successfully extended

查看卷组信息:
   vgdisplay "卷组名称"
   [root@localhost src]# vgdisplay wed_disk
         --- Volume group ---
          VG Name               wed_disk
          System ID
         Format                lvm2
         Metadata Areas        2
         Metadata Sequence No  3
         VG Access             read/write
         VG Status             resizable
         MAX LV                0
         Cur LV                1
           Open LV               1
         Max PV                0
         Cur PV                2
         Act PV                2
         VG Size               37.27 GB
         PE Size               4.00 MB
         Total PE              9542
         Alloc PE / Size       2560 / 10.00 GB
         Free  PE / Size       6982 / 27.27 GB
         VG UUID               c1cIin-A75c-04yR-GXhM-UT2f-hLT1-d0CPxe

查看物理卷信息:
   pvdisplay "磁盘卷"
   [root@localhost src]# pvdisplay
         --- Physical volume ---
         PV Name               /dev/sda14
         VG Name               wed_disk
         PV Size               18.64 GB / not usable 1.00 MB
         Allocatable           yes
         PE Size (KByte)       4096
         Total PE              4771
         Free PE               2211
         Allocated PE          2560
         PV UUID               lw29T7-aLGb-a8zk-OPTB-7EJE-iQ7C-3HkAfa

查看逻辑卷信息:
   lvdisplay "逻辑卷"
   [root@localhost ~]# lvdisplay
         --- Logical volume ---
         LV Name                /dev/wed_disk/lgyfirstlv
         VG Name                wed_disk
         LV UUID                3nKULI-0Tue-kg02-A0Ky-Qzd1-IQju-Z5BMA2
         LV Write Access        read/write
         LV Status              available
         # open                 1
         LV Size                10.00 GB
         Current LE             2560
         Segments               1
         Allocation             inherit
         Read ahead sectors     auto
         - currently set to     256
         Block device           253:0

调整逻辑卷大小:
   lvresize -L +|- [kKmMgGtT] "调整的逻辑卷"
   增加: [root@localhost src]# lvresize -L +1G /dev/mapper/wed_disk-lgyfirstlv
     Extending logical volume lgyfirstlv to 11.00 GB
     Logical volume lgyfirstlv successfully resized
   减少:[root@localhost src]# lvresize -L -1G /dev/mapper/wed_disk-lgyfirstlv
         WARNING: Reducing active and open logical volume to 10.00 GB
         THIS MAY DESTROY YOUR DATA (filesystem etc.)
         Do you really want to reduce lgyfirstlv? [y/n]: y
         Reducing logical volume lgyfirstlv to 10.00 GB
         Logical volume lgyfirstlv successfully resized
   较少磁盘空间时可能会删除原有数据



调整卷组大小:
   vgextend "卷组名称" "物理卷"
   扩展:[root@localhost src]# vgextend wed_disk /dev/sda8
         Volume group "wed_disk" successfully extended
   减少:[root@localhost src]# vgreduce web_document /dev/sda9
         Removed "/dev/sda9" from volume group "web_document"

删除物理卷:
   pvremove "物理卷"
       [root@localhost src]# pvremove /dev/sda9
         Labels on physical volume "/dev/sda9" successfully wiped

删除逻辑卷:
   lvremove "逻辑卷名称" /* 被删除的逻辑卷在删除之前必须处于未挂载状态 */
   [root@localhost ~]# lvremove /dev/wed_disk/lgyfirstlv
       Do you really want to remove active logical volume lgyfirstlv? [y/n]: y
         Logical volume "lgyfirstlv" successfully removed
删除逻辑卷组:
   vgremove "逻辑卷组" /* 删除逻辑卷组之前确保没有逻辑卷在使用 */
   [root@localhost ~]# vgremove wed_disk
         Volume group "wed_disk" successfully removed
删除物理卷:
   pvremove "物理卷"
   [root@localhost ~]# pvremove /dev/sda8
         Labels on physical volume "/dev/sda8" successfully wiped



开机自动挂载设备:
   在/etc/fstab 中添加

   /dev/wed_disk/lgyfirstlv  /usr/local/src/lv ext3 defaults 0 0