Creating LVM Logical Volumes

LVM-Logical Volume Manager逻辑卷管理的一些基本概念:

用途:

在零停机前提下可以自如对文件系统的大小进行调整,可以方便实现文件系统跨越不同磁盘和分区。
当系统添加了新的磁盘,通过LVM机制,管理员就不必将磁盘的文件移动到新的磁盘上以充分利用新的存储空间,而是直接扩展文件系统跨越磁盘即可。

基本术语:

物理卷pv(Physical Volume)
物理卷就是指硬盘分区或从逻辑上与磁盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块,
但和基本的物理存储介质(如分区、磁盘等)比较,却包含有与LVM相关的管理参数.
●卷组vg(Volume Group)
卷组由物理卷组成,类似于非LVM系统中的物理硬盘,可以在卷组上创建一个或多个"LVM分区"(逻辑卷).
●逻辑卷lv(Logical Volume)
LVM的逻辑卷类似于非LVM系统中的硬盘分区,在逻辑卷之上可以建立文件系统(比如/home或者/usr等).
●PE(Physical Extent)
每一个物理卷被划分为称为PE(Physical Extents)的基本单元,具有唯一编号的PE是可以被LVM寻址的
最小单元.PE的大小是可配置的,默认为4MB.
●LE(Logical Extent)
逻辑卷也被划分为被称为LE(Logical Extents) 的可被寻址的基本单位.在同一个卷组中,LE的大小和
PE是相同的,并且一一对应.
和非LVM系统将包含分区信息的元数据保存在位于分区的起始位置的分区表中一样,逻辑卷以及卷 组相关的元数据也是保存在位于物理卷起始处的VGDA(卷组描述符区域)中.VGDA包括以下内容: PV描述符、VG描述符、LV描述符、和一些PE描述符 .
系统启动LVM时激活VG,并将VGDA加载至内存,来识别LV的实际物理存储位置.当系统进行I/O操作 时,就会根据VGDA建立的映射机制来访问实际的物理位置.

===========================

Creating LVM logical volumes involves creating the three layers in the LVM architecture. You first have to take care of the physical volume (PV), then you need to create the volume group (VG) and assign physical volumes to it. As the last step, the logical volume (LV) itself has to be created. In this section, you learn what is involved in creating these three layers. Different utilities exist for creating LVM. This chapter focuses on using the command-line utilities. They are relatively easy to use, and they are available in all environments (whether you are running a graphical interface or not). 顺序:PV--->VG--->LV

TIP You absolutely do not need to learn the commands discussed in this chapter by heart. All you really need to remember is pv , vg , lv . Open a command line, type pv and press the Tab key twice. This will show all commands that start with pv, which are all commands that are used for managing physical volumes. After you have found the command you need, run this command with the --help option. This shows a usage summary that lists everything that needs to be done to create the element you need.

一、Creating the Physical Volume --创建物理卷PV

In this exercise, you create a physical volume. To do this exercise, you need a hard disk that has free (unpartitioned) disk space available. The recommended method to make disk space available is by adding a new hard disk in your virtual machine environment. In this exercise, I use a clean /dev/vdb device to create the partition. You may have to change the device name to match your configuration. If you do not have a dedicated hard disk available to create this configuration, you might want to consider attaching a USB key to your machine. (我用的是Oracle VirtulBox虚拟机,添加一块虚拟磁盘用于测试)
1. Open a root shell and type fdisk /dev/sdc .

2. Type n to create a new partition. Select p to make it a primary partition, and use the partition number that is suggested as a default. If you are using a clean device, this will be partition number 1.

3. Press Enter when asked for the first sector and type +100M to accept the last sector.

4. Once you are back on the fdisk prompt, type t to change the partition type. Because there is one partition only, fdisk does not ask which partition to use this partition type on. You may have to select a partition if you are using a different configuration.

5. The partitioner asks for the partition type you want to use. Type 8e . Then, press w to write changes to disk and quit fdisk.

If you are getting a message that the partition table could not be updated while writing the changes to disk, reboot your system.

(首先,你得有一块未分区有磁盘,然后在这个磁盘上创建一个LVM类型的分区; 之后再为创建物理卷PV)

  • fdisk -l查看分区情况:可以看到此时的/dev/sdc是没有分区的。
Disk /dev/sdc: 5368 MB, 5368709120 bytes, 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0c16d904

   Device Boot      Start         End      Blocks   Id  System
  • 为/dev/sdc磁盘分出一个区sdc1,大小为100M,类型为LVM
[root@rhel7 ~]# fdisk /dev/sdc
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-10485759, default 2048):        
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759): +100M      
Partition 1 of type Linux and of size 100 MiB is set

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): L

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi eb  BeOS fs        
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT            
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1 80  Old Minix      
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@rhel7 ~]# 
  • fdisk -l 命令查看分区后的/dev/sdc
...略...
Disk /dev/sdc: 5368 MB, 5368709120 bytes, 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0c16d904

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048      206847      102400   8e  Linux LVM
.....略...

6. Now that the partition has been created, you need to flag it as an LVM physical volume.---分区创建完成后,可以使用命令partprobe使修改的分区生效,不用重启。

[root@rhel7 ~]# partprobe 

To do this, type  pvcreate /dev/sdc1 . You should now get this prompt: Physical volume “/dev/vbd1” successfully created.     

7.  Now type  pvs  to verify that the physical volume has been created successfully. The output may look like bellow  .

[root@rhel7 ~]# pvcreate /dev/sdc1
WARNING: ext4 signature detected on /dev/sdc1 at offset 1080. Wipe it? [y/n]: y
  Wiping ext4 signature on /dev/sdc1.
  Physical volume "/dev/sdc1" successfully created
[root@rhel7 ~]# pvs
  PV         VG   Fmt  Attr PSize   PFree  
  /dev/sda2  rhel lvm2 a--   19.51g  40.00m
  /dev/sdc1       lvm2 ---  100.00m 100.00m
[root@rhel7 ~]# 

As an alternative to the  pvs  command, which shows a summary of the physical volumes and their attributes, you can also use the  pvdisplay command to show some more details.

pvdisplay命令:

[root@rhel7 ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               rhel
  PV Size               19.51 GiB / not usable 3.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              4994
  Free PE               10
  Allocated PE          4984
  PV UUID               OjqvZk-KS1b-YegW-zb4b-uaNV-zGt7-npfsjU
   
  "/dev/sdc1" is a new physical volume of "100.00 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdc1
  VG Name               
  PV Size               100.00 MiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               mf29mQ-wx2f-L94K-AFWH-rwA1-23oj-YE4yoV
   

二、Creating the Volume Groups --创建卷组VG

   Now that the physical volume has been created, you can assign it to a volume group. It is possible to add a physical volume to an existing volume group , but you will now learn how to create a new volume group and add the physical volume to it.This is a simple one-command procedure. Just type  vgcreate  followed by the name of the volume group you want to create and the name of the physical device you want to add to it. So, if the physical volume name is /dev/sdc1, the complete command is   vgcreate vgdata /dev/sdc1 (语法:vgcreate 卷组名 设备名).  You are completely free in your choice of name for the volume group. I like to start all volume group names with vg, which makes it easy to find the volume groups if there are many, but you are free to choose anything you like. 

After creating the volume group, you can request details about the volume group using the  vgs  command for a short summary, or the vgdisplay  command to get more information.

[root@rhel7 ~]# vgcreate vgdate /dev/sdc1 
  Volume group "vgdate" successfully created
[root@rhel7 ~]# vgs
  VG     #PV #LV #SN Attr   VSize  VFree 
  rhel     1   2   0 wz--n- 19.51g 40.00m
  vgdate   1   0   0 wz--n- 96.00m 96.00m
[root@rhel7 ~]# vgdisplay
  --- Volume group ---
  VG Name               rhel
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               19.51 GiB
  PE Size               4.00 MiB
  Total PE              4994
  Alloc PE / Size       4984 / 19.47 GiB
  Free  PE / Size       10 / 40.00 MiB
  VG UUID               RisrnE-lJIv-XM6h-47zQ-rsiU-JVP6-9ZexZO
   
  --- Volume group ---
  VG Name               vgdate
  System ID             
  Format                lvm2
  Metadata Areas        1
  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                1
  Act PV                1
  VG Size               96.00 MiB
  PE Size               4.00 MiB
  Total PE              24
  Alloc PE / Size       0 / 0   
  Free  PE / Size       24 / 96.00 MiB
  VG UUID               hNTqaY-c0Hi-v5wk-p7lQ-TIbc-oSpm-TIOcHs
[root@rhel7 ~]# 

In this procedure, you learned how to create a volume group in a two-step procedure where first the physical volume is created with the  pvcreate  command, after which the volume group is added using the  vgcreate  command. You can do this in a one-step procedure as well (where using a separate  pvcreate  command will not be necessary). If you are adding a partition to the volume group, however, it must be marked as partition type 8e already.  

The one-step procedure is particularly useful for adding a complete disk device (which does not need to be marked as anything). If you want to add the disk / dev/ sdc, for instance, just type vgcreate vgdata /dev/sdc to create a volume group vgdata that contains the /dev/sdc device. When you are doing this to add a device that has not been marked as a physical volume yet, the vgcreate utility will automatically flag it as a physical volume. When creating volume groups, a physical extent size is used. The physical extent size defines the size of the building blocks used to create logical volumes. A logical volume always has a size that is a multiple of the physical extent size. If you need to create huge logical volumes, it is more efficient to use a big physical extent size. If you do not specify anything, a default extent size of 4.00 MiB is used. The physical extent size is always specified as a multiple of 2 MiB, with a maximum size of 128 MiB. Use the vgcreate -s option to specify the physical extent size you want to use.
NOTE When working with LVM, there is the physical extent size to consider. This is the size of the basic building blocks used in the LVM configuration. When working with an ext4 file system, logical extents are used. The extent size on LVM are in no way related to the extent sizes that are used on the file systems.

三、Creating the Logical Volumes and File Systems  创建逻辑卷和文件系统

Now that the volume group has been created, you can start creating logical volumes from it. This procedure is slightly more complicated than the creation of physical volumes or volume groups because there are more choices to be made. While creating the logical volume, you must specify a volume name and a size.

The volume size can be specified as an absolute value using the -L option. Use, for instance, -L 5G to create an LVM volume with a 5GB size. Alternatively, you can use relative sizes using the -l option. For instance, use -l 50%FREE to use half of all available disk space. You’ll further need to specify the name of the volume group that the logical volume is assigned to, and optionally (but highly recommended), you can use -n to specify the name of the logical volume. For instance, use lvcreate -n lvvol1 -L 100M vgdata to create a logical volume with the name lvvol1 and add that to the vgdata volume group.

[root@rhel7 ~]# vgs
  VG     #PV #LV #SN Attr   VSize  VFree 
  rhel     1   2   0 wz--n- 19.51g 40.00m
  vgdate   1   0   0 wz--n- 96.00m 96.00m
[root@rhel7 ~]# lvcreate -n lvvol1 -L 100M vgdate
  Volume group "vgdate" has insufficient free space (24 extents): 25 required.
[root@rhel7 ~]# lvcreate -n lvvol1 -L 90M vgdate
  Rounding up size to full physical extent 92.00 MiB
  Logical volume "lvvol1" created.
[root@rhel7 ~]# 

create file systems创建文件系统:

  • At this point, you are ready to create a file system on top of the logical volume. Type mkfs.xfs /dev/vgdata/lvdata to create the file system.
  • Type mkdir /lvmFiles to create a folder on which the volume can be mounted.
  • Add the following line to /etc/fstab:   ----开机自动挂载

        /dev/vgdata/lvdata /lvmFiles xfs defaults 1 2  

  • Type mount -a to verify that the mount works and mount the file system.
[root@rhel7 ~]# mkfs.xfs /dev/vgdate/lvvol1  
meta-data=/dev/vgdate/lvvol1     isize=256    agcount=4, agsize=5888 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=23552, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@rhel7 ~]# mkdir /lvmFiles
[root@rhel7 ~]# mount /dev/vgdate/lvvol1  /lvmFiles/  -手动挂载看看
[root@rhel7 ~]# df -h
Filesystem                 Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root       18G  4.5G   13G  26% /
devtmpfs                   911M     0  911M   0% /dev
tmpfs                      921M     0  921M   0% /dev/shm
tmpfs                      921M  8.4M  912M   1% /run
tmpfs                      921M     0  921M   0% /sys/fs/cgroup
/dev/sda1                  497M  124M  374M  25% /boot
tmpfs                      185M     0  185M   0% /run/user/0
/dev/mapper/vgdate-lvvol1   89M  4.8M   84M   6% /lvmFiles
[root@rhel7 ~]# 

添加到/etc/fstab,加下如一行,使其开机自动挂载:

/dev/vgdate/lvvol1  /lvmFiles/  xfs     defaults        1 2

===============

LVM一些常用的命令:

步骤总结:

1.fdisk --磁盘分区为lvm类型(-t修改分区格式为8e、w写入分区表)
2.partprobe --让kernel重新读取分区表,使分区生效,无需重启系统
3.pvcreate /dev/sda1 ----创建物理卷PV
4.vgcreate VGname /dev/sda1 ---创建卷组VG,可以使用已经存在的VG名,把上一步创建的物理卷加入到这个VG里;一个VG可以包括多个PV.
5.lvcreate -n LVname -L 200MB VGname ---创建逻辑卷LV,要指定逻辑卷的名字LVname、大小以及从哪个卷组VG划分
6.mkfs.xfs /dev/VGname/LVname ---格式化逻辑卷LVname
7.mount /dev/VGname/LVname /lvmFiles ---mount挂载后就可以使用了
8.vi /etc/fstab --加入开机自动挂载即可

==============================================================

测试:这是在安装系统设置分区的过程中,创建PV、VG及LV的情况:

disk size:20GB

image

image

image

image

[root@rhel7Oracle ~]# pvs ---整个disk,共20GB作为一个PV
  PV         VG        Fmt  Attr PSize  PFree
  /dev/sda1  lxjtestVG lvm2 a--  20.00g    0 
[root@rhel7Oracle ~]# vgs  ----创建了一个卷组VG:lxjtestVG
  VG        #PV #LV #SN Attr   VSize  VFree
  lxjtestVG   1   3   0 wz--n- 20.00g    0 
[root@rhel7Oracle ~]# lvs  ----创建了三个逻辑卷:home,lxjtestName,lxjtestswap
  LV          VG        Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home        lxjtestVG -wi-ao----  9.00g                                                    
  lxjtestName lxjtestVG -wi-ao---- 10.00g                                                    
  lxjtestswap lxjtestVG -wi-ao----  1.00g                                                    
[root@rhel7Oracle ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda1
  VG Name               lxjtestVG
  PV Size               20.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              5119
  Free PE               0
  Allocated PE          5119
  PV UUID               UIy2Bp-54uT-WUaz-fDo1-ljqd-u1uD-dlSpGn
   
[root@rhel7Oracle ~]# vgdisplay
  --- Volume group ---
  VG Name               lxjtestVG
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               20.00 GiB
  PE Size               4.00 MiB
  Total PE              5119
  Alloc PE / Size       5119 / 20.00 GiB
  Free  PE / Size       0 / 0   
  VG UUID               2TbtUe-e34q-p36b-u9XA-OkfJ-EDvS-0Oel35
   
[root@rhel7Oracle ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/lxjtestVG/home
  LV Name                home
  VG Name                lxjtestVG
  LV UUID                sOHWsQ-dsya-p1cM-p4no-pVPf-KX6g-E9SmsA
  LV Write Access        read/write
  LV Creation host, time localhost, 2016-08-09 16:42:37 +0800
  LV Status              available
  # open                 1
  LV Size                9.00 GiB
  Current LE             2303
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2
   
  --- Logical volume ---
  LV Path                /dev/lxjtestVG/lxjtestswap
  LV Name                lxjtestswap
  VG Name                lxjtestVG
  LV UUID                OUroIn-GQih-o2zB-dHUO-qH1W-ahWb-bCJuQB
  LV Write Access        read/write
  LV Creation host, time localhost, 2016-08-09 16:42:38 +0800
  LV Status              available
  # open                 2
  LV Size                1.00 GiB
  Current LE             256
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/lxjtestVG/lxjtestName
  LV Name                lxjtestName
  VG Name                lxjtestVG
  LV UUID                2zpdZg-GFCo-NI6B-Kmfo-LjC5-jwYf-RZtcMA
  LV Write Access        read/write
  LV Creation host, time localhost, 2016-08-09 16:42:38 +0800
  LV Status              available
  # open                 1
  LV Size                10.00 GiB
  Current LE             2560
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
[root@rhel7Oracle ~]# 
原文地址:https://www.cnblogs.com/rusking/p/5742125.html