天天看點

lvm日常管理和維護

最近在搞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

   減少:[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

   [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