Rondo 博客

謝謝來到Rondo的博客。
個人資料
rondo (熱門博主)
  • 博客訪問:
文章分類
正文

Managing Physical & Logical Volumes

(2008-08-04 12:07:47) 下一個
The following article presents an overview of the commands used to manage physical and logical volumes for use with Logical Volume Manager (LVM) in Linux.

Before considering the various commands for LVM, lets first look at just what is meant by some of the terminology of LVM.

NOTE: Using Logical Volume Manager assumes that the disk or disks you will be using do not have a formatted partition. In Linux you would use the fdisk to remove the parition. KEEP IN MIND THAT THIS WILL REMOVE ALL DATA ON THAT DRIVE AND WILL NOT BE ABLE TO BE RECOVERED!!! As an example, lets remove the formatted partition on an external FireWire drive in Linux. Simply run the fdisk command and follow the screens to remove all partitions.

  # fdisk /dev/sda

A Logical Volume lives in a Volume Group that is made up of one or more Physical Volumes. All Volume Groups are part of the Logical Volume Manager. Here is a table that lists the three types of Logical Volume's.

Logical Volume Types
Logical VolumePhysical DeviceFile System
volume group one or more disksvgdisplay
physical volume groupphysical extents on a drivepvdisplay
logical volume groupmultiple physical volume groups, one or more diskslvdisplay

The following table provides an overview of some of the commands used in LVM and the functions they service.

LVM Commands
CommandLVM Function
vgcreateCreate a Volume Group. (Create a subset of the overall LVM.)
pvcreateCreate a Physical Volume, assign to Volume Group. (Specify a disk for inclusion in the overall LVM.)
vgextendAdd a new physical disk to a volume group.
lvcreateCreate a Logical Volume. (storage area for related files that is part of a Volume Group. A Volume Group consists of many logical volumes.)
lvextendIncrease the size of a Logical Volume.
lvreduceDecrease the size of a Logical Volume.
lvremoveRemoves a Logical Volume. (frees the storage area set aside for a logical volume.)
vgreduceReduce a Volume Group. (Reduces the number of disks in a Volume Group.)
vgremoveRemove a Volume Group. (Removes the designation of a group of disks as a Volume Group.)
vgdisplayVolume Group Display. (Displays information about one or more Volume Groups.)
pvdisplayPhysical Volume Display. (Displays information about one or more Volume Groups.)
lvdisplayLogical Volume Display. (Displays information about one or more Logical Volumes.)

Initializing Disks or Disk Partitions
Before you can use a disk or disk partition as a physical volume, you will have to initialize it. There are two ways to handle initializing a disk - as an entire disk or as a partition.
  • For entire disks:

    • Run the pvcreate command:
        # pvcreate /dev/sda
      This creates a volume group descripter at the start of the disk.

    • If you get an error that LVM cannot initialize a disk with a partition table on it, first make sure that the disk you are operating on is the correct one. If you are very sure that it is, run the following:

         The following commands will destroy the partition table on the disk being operated on. Be very sure it is the correct disk before continuing!

        # dd if=/dev/zero of=/dev/sda bs=1K count=1  # blockdev --rereadpt /dev/sda

  • For partitions:

    • Set the partition type to 0x8e (Linux LVM) using fdisk or some other similar program.

    • Run pvcreate on the partition:
        # pvcreate /dev/sda1
      This creates a volume group descripter at the start of the /dev/sda1 partition.
Create Physical / Logical Volumes
First, use fdisk to remove any formatted partitions on the disk.

The following set of commands perform the three steps required to create logical volumes:

  1. Use pvcreate to create a Physical Volume for use by the Logical Volume Manager (LVM).
  2. Use vgcreate to create a Volume Group for the drive or for the partition you want to use for RAW devices. Here we do the entire single drive. In our example (below), the command will allow 256 logical partitions and 256 physical partitions with a 128K extent size.
  3. Use lvcreate to create the Logical Volume(s) inside the volume group.
pvcreate -d /dev/sdavgcreate -l 256 -p 256 -s 128k /dev/pv1 /dev/sdalvcreate -L 500m /dev/pv1lvcreate -L 500m /dev/pv1lvcreate -L 300m /dev/pv1lvcreate -L 100m /dev/pv1lvcreate -L 100m /dev/pv1lvcreate -L 100m /dev/pv1lvcreate -L 100m /dev/pv1lvcreate -L 100m /dev/pv1lvcreate -L 100m /dev/pv1lvcreate -L 100m /dev/pv1lvcreate -L 100m /dev/pv1lvcreate -L 100m /dev/pv1lvcreate -L 200m /dev/pv1lvcreate -L 200m /dev/pv1lvcreate -L 200m /dev/pv1lvcreate -L 200m /dev/pv1lvcreate -L 200m /dev/pv1lvcreate -L 150m /dev/pv1lvcreate -L 150m /dev/pv1lvcreate -L 150m /dev/pv1lvcreate -L 150m /dev/pv1lvcreate -L 150m /dev/pv1lvcreate -L 150m /dev/pv1lvcreate -L 50m /dev/pv1lvcreate -L 50m /dev/pv1lvcreate -L 500m /dev/pv1
Click here to see the output from the above commands.
Now lets scan the LVM:
  # lvscan  lvscan -- no volume groups found
View Physical / Logical Volumes
  • Use pvdisplay to show details about a Physical Volume. If you are unsure of the name of the physical volume you want to view, use pvscan:
      # pvscan  pvscan -- reading all physical volumes (this may take a while...)  pvscan -- ACTIVE   PV "/dev/sda" of VG "pv1" [74.55 GB / 69.96 GB free]  pvscan -- total: 1 [74.55 GB] / in use: 1 [74.55 GB] / in no VG: 0 [0]
      # pvdisplay /dev/sda  --- Physical volume ---  PV Name               /dev/sda  VG Name               pv1  PV Size               74.55 GB [156340476 secs] / NOT usable 2.62 MB [LVM: 2.45 MB]  PV#                   1  PV Status             available  Allocatable           yes  Cur LV                26  PE Size (KByte)       128  Total PE              610683  Free PE               573083  Allocated PE          37600  PV UUID               pbYmt7-5nq6-SOAJ-1Y3n-cCqw-mP0P-bAePUG

  • Use vgdisplay to show details about a Volume Group. If you are unsure of the name of the volume group you want to view, use vgscan:
      # vgscan  vgscan -- reading all physical volumes (this may take a while...)  vgscan -- found active volume group "pv1"  vgscan -- "/etc/lvmtab" and "/etc/lvmtab.d" successfully created  vgscan -- WARNING: This program does not do a VGDA backup of your volume group
      # vgdisplay /dev/pv1  --- Volume group ---  VG Name               pv1  VG Access             read/write  VG Status             available/resizable  VG #                  0  MAX LV                256  Cur LV                26  Open LV               0  MAX LV Size           8 GB  Max PV                256  Cur PV                1  Act PV                1  VG Size               74.55 GB  PE Size               128 KB  Total PE              610683  Alloc PE / Size       37600 / 4.59 GB  Free  PE / Size       573083 / 69.96 GB  VG UUID               DfKOWb-HwLj-kzSf-XpdJ-aLyJ-q9Ym-x6DyVh

  • Use lvdisplay to show details about a Logical Volume. If you are unsure of the name of the logical volume you want to view, use lvscan:
      # lvscan  lvscan -- ACTIVE            "/dev/pv1/lvol1" [500 MB]  lvscan -- ACTIVE            "/dev/pv1/lvol2" [500 MB]  lvscan -- ACTIVE            "/dev/pv1/lvol3" [300 MB]  lvscan -- ACTIVE            "/dev/pv1/lvol4" [100 MB]  lvscan -- ACTIVE            "/dev/pv1/lvol5" [100 MB]  lvscan -- ACTIVE            "/dev/pv1/lvol6" [100 MB]  lvscan -- ACTIVE            "/dev/pv1/lvol7" [100 MB]  lvscan -- ACTIVE            "/dev/pv1/lvol8" [100 MB]  lvscan -- ACTIVE            "/dev/pv1/lvol9" [100 MB]  lvscan -- ACTIVE            "/dev/pv1/lvol10" [100 MB]  lvscan -- ACTIVE            "/dev/pv1/lvol11" [100 MB]  lvscan -- ACTIVE            "/dev/pv1/lvol12" [100 MB]  lvscan -- ACTIVE            "/dev/pv1/lvol13" [200 MB]  lvscan -- ACTIVE            "/dev/pv1/lvol14" [200 MB]  lvscan -- ACTIVE            "/dev/pv1/lvol15" [200 MB]  lvscan -- ACTIVE            "/dev/pv1/lvol16" [200 MB]  lvscan -- ACTIVE            "/dev/pv1/lvol17" [200 MB]  lvscan -- ACTIVE            "/dev/pv1/lvol18" [150 MB]  lvscan -- ACTIVE            "/dev/pv1/lvol19" [150 MB]  lvscan -- ACTIVE            "/dev/pv1/lvol20" [150 MB]  lvscan -- ACTIVE            "/dev/pv1/lvol21" [150 MB]  lvscan -- ACTIVE            "/dev/pv1/lvol22" [150 MB]  lvscan -- ACTIVE            "/dev/pv1/lvol23" [150 MB]  lvscan -- ACTIVE            "/dev/pv1/lvol24" [50 MB]  lvscan -- ACTIVE            "/dev/pv1/lvol25" [50 MB]  lvscan -- ACTIVE            "/dev/pv1/lvol26" [500 MB]  lvscan -- 26 logical volumes with 4.59 GB total in 1 volume group  lvscan -- 26 active logical volumes
      # lvdisplay /dev/pv1/lvol15  --- Logical volume ---  LV Name                /dev/pv1/lvol15  VG Name                pv1  LV Write Access        read/write  LV Status              available  LV #                   15  # open                 0  LV Size                200 MB  Current LE             1600  Allocated LE           1600  Allocation             next free  Read ahead sectors     1024  Block device           58:14
Delete Physical / Logical Volumes
Before going into the details of how to drop all of the physical and logical volumes created in this article, it is very common to only want to remove a Logical Volume from a Volume Group. Let's say we no longer needed lvol15. We can remove it and place its PE's back in the empty pool for the Volume Group. First, unmounting its filesystem (if it is mounted). Next, deactive it with lvchange and finally delete it with lvremove. Here is an example that removes the lvol15:
  # lvchange -a n /dev/pv1/lvol15  # lvremove /dev/pv1/lvol15  lvremove -- do you really want to remove "/dev/pv1/lvol15"? [y/n]: y  lvremove -- doing automatic backup of volume group "pv1"  lvremove -- logical volume "/dev/pv1/lvol15" successfully removed

Now, it you would like to delete all physical and logical volumes created in this article. In this example, we no longer need the /dev/pv1 Physical Volume.

lvremove -f /dev/pv1/lvol1lvremove -f /dev/pv1/lvol2lvremove -f /dev/pv1/lvol3lvremove -f /dev/pv1/lvol4lvremove -f /dev/pv1/lvol5lvremove -f /dev/pv1/lvol6lvremove -f /dev/pv1/lvol7lvremove -f /dev/pv1/lvol8lvremove -f /dev/pv1/lvol9lvremove -f /dev/pv1/lvol10lvremove -f /dev/pv1/lvol11lvremove -f /dev/pv1/lvol12lvremove -f /dev/pv1/lvol13lvremove -f /dev/pv1/lvol14lvremove -f /dev/pv1/lvol15lvremove -f /dev/pv1/lvol16lvremove -f /dev/pv1/lvol17lvremove -f /dev/pv1/lvol18lvremove -f /dev/pv1/lvol19lvremove -f /dev/pv1/lvol20lvremove -f /dev/pv1/lvol21lvremove -f /dev/pv1/lvol22lvremove -f /dev/pv1/lvol23lvremove -f /dev/pv1/lvol24lvremove -f /dev/pv1/lvol25lvremove -f /dev/pv1/lvol26vgchange -a n /dev/pv1vgremove /dev/pv1
[ 打印 ]
閱讀 ()評論 (0)
評論
目前還沒有任何評論
登錄後才可評論.