在CentOS6.8上创建过程如下:

#对需要创建lvm的卷进行磁盘分区
[root@www ~13:40:48]#fdisk /dev/sde Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0xe02dc36a. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) WARNING: DOS-compatible mode is deprecated. It's strongly recommended to switch off the mode (command 'c') and change display units to sectors (command 'u'). Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-2610, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +10G Command (m for help): t   #修改系统的ID Selected partition 1 Hex code (type L to list codes): L 0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris 1 FAT12 39 Plan 9 82 Linux swap / So c1 DRDOS/sec (FAT- 2 XENIX root 3c PartitionMagic 83 Linux c4 DRDOS/sec (FAT- 3 XENIX usr 40 Venix 80286 84 OS/2 hidden C: c6 DRDOS/sec (FAT- 4 FAT16 <32M 41 PPC PReP Boot 85 Linux extended c7 Syrinx 5 Extended 42 SFS 86 NTFS volume set da Non-FS data 6 FAT16 4d QNX4.x 87 NTFS volume set db CP/M / CTOS / . 7 HPFS/NTFS 4e QNX4.x 2nd part 88 Linux plaintext de Dell Utility 8 AIX 4f QNX4.x 3rd part 8e Linux LVM df BootIt 9 AIX bootable 50 OnTrack DM 93 Amoeba e1 DOS access a OS/2 Boot Manag 51 OnTrack DM6 Aux 94 Amoeba BBT e3 DOS R/O b W95 FAT32 52 CP/M 9f BSD/OS e4 SpeedStor c W95 FAT32 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi eb BeOS fs e W95 FAT16 (LBA) 54 OnTrackDM6 a5 FreeBSD ee GPT f W95 Ext'd (LBA) 55 EZ-Drive a6 OpenBSD ef EFI (FAT-12/16/ 10 OPUS 56 Golden Bow a7 NeXTSTEP f0 Linux/PA-RISC b 11 Hidden FAT12 5c Priam Edisk a8 Darwin UFS f1 SpeedStor 12 Compaq diagnost 61 SpeedStor a9 NetBSD f4 SpeedStor 14 Hidden FAT16 <3 63 GNU HURD or Sys ab Darwin boot f2 DOS secondary 16 Hidden FAT16 64 Novell Netware af HFS / HFS+ fb VMware VMFS 17 Hidden HPFS/NTF 65 Novell Netware b7 BSDI fs fc VMware VMKCORE 18 AST SmartSleep 70 DiskSecure Mult b8 BSDI swap fd Linux raid auto 1b Hidden W95 FAT3 75 PC/IX bb Boot Wizard hid fe LANstep 1c Hidden W95 FAT3 80 Old Minix be Solaris boot ff BBT 1e Hidden W95 FAT1 Hex code (type L to list codes): 8e #lvm的分区格式 Changed system type of partition 1 to 8e (Linux LVM) Command (m for help): w  #存盘退出 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.

#注:所有需要创建lvm的都需要将分区创建成"
8e Linux LVM ",但是分区的大小没有要求必须一样大

 

[root@www ~13:49:37]#pvs  #查看当前物理卷,但还没有创建过
[root@www ~13:50:04]#pvdisplay   #详细的查看当前物理卷的信息,但还没有创建过
 [root@www ~13:50:13]#pvcreate /dev/sdb2 /dev/sde1 /dev/sdf1     #将/dev/sdb2 /dev/sde1 /dev/sdf1三个分区创建成三个独立的物理卷
WARNING: swap signature detected on /dev/sdb2. Wipe it? [y/n]: n    #系统提示:/dev/sdb2是SWAP分区,要不要覆盖?选择否
Aborted wiping of swap signature. Aborting pvcreate on
/dev/sdb2.
Physical volume
"/dev/sde1" successfully created Physical volume "/dev/sdf1" successfully created  #/dev/sdb2创建失败,其余两个创建成功。

 

[root@www ~13:52:55]#pvs   #再次查看物理卷信息,有两个创建成功
  PV         VG   Fmt  Attr PSize  PFree 
  /dev/sde1       lvm2 ---- 10.00g 10.00g
  /dev/sdf1       lvm2 ---- 10.00g 10.00g
[root@www ~13:55:22]#pvdisplay 
  "/dev/sde1" is a new physical volume of "10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sde1  #物理卷名
  VG Name                      #还没有物理卷组名
  PV Size               10.00 GiB  
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               1KKk8Z-LMkr-XCXH-teRx-gz61-1zDo-1qJDfe
   
  "/dev/sdf1" is a new physical volume of "10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdf1
  VG Name               
  PV Size               10.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               r8PQTC-74Wr-fcKb-Vcmw-gbsa-982R-jhTbot

 

[root@www ~13:55:37]#blkid     #查看磁盘UUID
/dev/sda1: UUID="dcfcf26d-ae5e-4524-9326-c9cafffe7ecd" TYPE="ext4" 
/dev/sda2: UUID="98eefdc6-1f04-4784-8b31-876ee05e618f" TYPE="swap" 
/dev/sda3: UUID="f15c3ecc-b6e0-4bba-8918-36b6fcac540f" TYPE="ext4" 
/dev/sdc1: UUID="5137e1fa-bd81-4fd0-acbd-50473c277e29" TYPE="swap" 
/dev/sdb1: UUID="e4d3e8c6-0f8e-48ef-a80b-711a99639637" TYPE="ext4" 
/dev/sdb2: UUID="f54f914c-11ab-499d-9359-0947db5f24bd" TYPE="swap" 
/dev/sdd1: UUID="990eddeb-d162-4d6d-ac74-cdafa474e5c0" TYPE="ext4" 
/dev/sde1: UUID="1KKk8Z-LMkr-XCXH-teRx-gz61-1zDo-1qJDfe" TYPE="LVM2_member"   #lvm展示的信息是这样的
/dev/sdf1: UUID="r8PQTC-74Wr-fcKb-Vcmw-gbsa-982R-jhTbot" TYPE="LVM2_member" 

 

-s set:设定PE(物理区域)的大小
[root@www ~13:59:06]#vgs    #查看当前卷组 [root@www ~14:11:42]#vgdisplay   #查看当前卷组的详细信息 [root@www ~14:11:48]#vgcreate -s 16M vg0 /dev/sde1 /dev/sdf1 #创建卷组;vg0:是创建的卷组名;卷组中包含/dev/sde1和/dev/sdf1两个物理卷 Volume group "vg0" successfully created [root@www ~14:13:00]#vgs VG #PV #LV #SN Attr VSize VFree vg0 2 0 0 wz--n- 20.00g 20.00g [root@www ~14:13:09]# pvs PV VG Fmt Attr PSize PFree /dev/sde1 vg0 lvm2 a--u 10.00g 10.00g /dev/sdf1 vg0 lvm2 a--u 10.00g 10.00g [root@www ~14:13:35]#pvdisplay --- Physical volume --- PV Name /dev/sde1 VG Name vg0  #卷组创建成功 PV Size 10.00 GiB / not usable 4.54 MiB Allocatable yes PE Size 16.00 MiB Total PE 640 Free PE 640 Allocated PE 0 PV UUID 1KKk8Z-LMkr-XCXH-teRx-gz61-1zDo-1qJDfe --- Physical volume --- PV Name /dev/sdf1 VG Name vg0 PV Size 10.00 GiB / not usable 4.54 MiB Allocatable yes PE Size 16.00 MiB    #设定的PE大小 Total PE 640 Free PE 640 Allocated PE 0 PV UUID r8PQTC-74Wr-fcKb-Vcmw-gbsa-982R-jhTbot [root@www ~14:13:51]#vgdisplay --- Volume group --- VG Name vg0 System ID Format lvm2 Metadata Areas 2 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 2 Act PV 2 VG Size 20.00 GiB PE Size 16.00 MiB Total PE 1280 Alloc PE / Size 0 / 0 Free PE / Size 1280 / 20.00 GiB VG UUID 6whqdS-UkYN-rsT9-xv61-2WHF-byun-3RZw5q

 

-n name:创建逻辑卷的名称
-L:创建逻辑卷增加到多少
-l:创建逻辑卷增加了多少
[root@www ~14:14:05]#lvs  #查看当前逻辑卷 [root@www ~14:16:11]#lvdisplay   #查看当前逻辑卷的详细信息 [root@www ~14:16:15]#lvcreate -n lv0 -L 10G vg0   #创建逻辑卷 Logical volume "lv0" created. [root@www ~14:17:51]#lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv0 vg0 -wi-a----- 10.00g [root@www ~14:17:57]#lvdisplay --- Logical volume --- LV Path /dev/vg0/lv0 LV Name lv0 VG Name vg0 LV UUID g7Jr5N-pETn-RzgU-fVDy-qbNx-wq8x-7eV5ii LV Write Access read/write LV Creation host, time www, 2017-04-20 14:17:51 +0800 LV Status available # open 0 LV Size 10.00 GiB Current LE 640 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0 [root@www ~14:18:04]#vgdisplay --- Volume group --- VG Name vg0 System ID Format lvm2 Metadata Areas 2 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 2 Act PV 2 VG Size 20.00 GiB PE Size 16.00 MiB Total PE 1280 Alloc PE / Size 640 / 10.00 GiB Free PE / Size 640 / 10.00 GiB VG UUID 6whqdS-UkYN-rsT9-xv61-2WHF-byun-3RZw5q

 

[root@www ~14:18:16]#mkfs.ext4 /dev/vg0/lv0  #格式化逻辑卷,定义逻辑卷的文件系统
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655360 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
8192 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 22 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@www ~14:21:15]#mkdir /mnt/lv0    #为挂载逻辑卷创建目录
[root@www ~14:21:42]#mount /dev/vg0/lv0 /mnt/lv0  #挂载逻辑卷
[root@www ~14:22:14]#df -h
Filesystem           Size  Used Avail Use% Mounted on
/dev/sda3            6.6G  1.8G  4.5G  29% /
tmpfs                497M     0  497M   0% /dev/shm
/dev/sda1            190M   58M  122M  33% /boot
/dev/sdb1            2.0G  3.1M  1.9G   1% /newFS
/dev/sdd1             50G   52M   47G   1% /home
/dev/mapper/vg0-lv0  9.8G   23M  9.2G   1% /mnt/lv0

 

-L:扩展到多大
-l:扩展了多大
[root@www ~14:22:19]#lvextend -L 20G /dev/vg0lv0 #扩展逻辑卷的容量 Path required for Logical Volume "vg0lv0". Please provide a volume group name Run `lvextend --help' for more information. [root@www ~14:26:09]#lvextend -L 20G /dev/vg0/lv0 Size of logical volume vg0/lv0 changed from 10.00 GiB (640 extents) to 20.00 GiB (1280 extents). Logical volume lv0 successfully resized.

 

[root@www ~14:26:22]#df -h  #扩展之后容量并没有变大
Filesystem           Size  Used Avail Use% Mounted on
/dev/sda3            6.6G  1.8G  4.5G  29% /
tmpfs                497M     0  497M   0% /dev/shm
/dev/sda1            190M   58M  122M  33% /boot
/dev/sdb1            2.0G  3.1M  1.9G   1% /newFS
/dev/sdd1             50G   52M   47G   1% /home
/dev/mapper/vg0-lv0  9.8G   23M  9.2G   1% /mnt/lv0
[root@www ~14:27:18]#resize2fs /dev/vg0/lv0    #扩展后,要将文件系统同步,此命令仅用于CentOS6.8
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg0/lv0 is mounted on /mnt/lv0; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/vg0/lv0 to 5242880 (4k) blocks.
The filesystem on /dev/vg0/lv0 is now 5242880 blocks long.

[root@www ~14:27:57]#df -h
Filesystem           Size  Used Avail Use% Mounted on
/dev/sda3            6.6G  1.8G  4.5G  29% /
tmpfs                497M     0  497M   0% /dev/shm
/dev/sda1            190M   58M  122M  33% /boot
/dev/sdb1            2.0G  3.1M  1.9G   1% /newFS
/dev/sdd1             50G   52M   47G   1% /home
/dev/mapper/vg0-lv0   20G   28M   19G   1% /mnt/lv0

 

#迁移PE到新磁盘上:假设逻辑卷空间不够,需要新增空间
[root@www ~00:47:26]#pvcreate /dev/sdf2 #创建物理卷 Physical volume "/dev/sdf2" successfully created [root@www ~00:51:13]#vgextend vg0 /dev/sdf2    #扩展卷组vg0 Volume group "vg0" successfully extended [root@www ~00:51:34]#vgs      #查看当前卷组 VG #PV #LV #SN Attr VSize VFree vg0 3 1 0 wz--n- 30.00g 10.00g [root@www ~00:51:42]#vgdisplay --- Volume group --- VG Name vg0 System ID Format lvm2 Metadata Areas 3 Metadata Sequence No 4 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 1 Max PV 0 Cur PV 3 Act PV 3 VG Size 30.00 GiB PE Size 16.00 MiB Total PE 1920 Alloc PE / Size 1280 / 20.00 GiB Free PE / Size 640 / 10.00 GiB VG UUID 6whqdS-UkYN-rsT9-xv61-2WHF-byun-3RZw5q [root@www ~00:52:09]#lvdisplay    #查看卷组详细信息 --- Logical volume --- LV Path /dev/vg0/lv0 LV Name lv0 VG Name vg0 LV UUID g7Jr5N-pETn-RzgU-fVDy-qbNx-wq8x-7eV5ii LV Write Access read/write LV Creation host, time www, 2017-04-20 14:17:51 +0800 LV Status available # open 1 LV Size 20.00 GiB Current LE 1280 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0 [root@www ~00:53:21]#pvdisplay #查看物理卷详细信息 --- Physical volume --- PV Name /dev/sde1 VG Name vg0 PV Size 10.00 GiB / not usable 4.54 MiB Allocatable yes (but full) PE Size 16.00 MiB Total PE 640 Free PE 0 Allocated PE 640 PV UUID 1KKk8Z-LMkr-XCXH-teRx-gz61-1zDo-1qJDfe --- Physical volume --- PV Name /dev/sdf1    #假设此卷组发生警报,需要更换 VG Name vg0 PV Size 10.00 GiB / not usable 4.54 MiB Allocatable yes (but full) PE Size 16.00 MiB Total PE 640 Free PE 0 Allocated PE 640 PV UUID r8PQTC-74Wr-fcKb-Vcmw-gbsa-982R-jhTbot --- Physical volume --- PV Name /dev/sdf2    #在卷组vg0中正好有此设备是空闲的,而且容量不小于快要损坏的/dev/sdf1,故可移走损坏的 VG Name vg0 PV Size 10.00 GiB / not usable 4.58 MiB Allocatable yes PE Size 16.00 MiB Total PE 640 Free PE 640 Allocated PE 0 PV UUID 5zCxP8-62mX-9VFI-q8Zq-EwZn-3CsV-UsLNL2 [root@www ~00:53:44]#pvmove /dev/sdf1   #移走损坏的/dev/sdf1 /dev/sdf1: Moved: 0.0% /dev/sdf1: Moved: 10.5% /dev/sdf1: Moved: 19.8% /dev/sdf1: Moved: 27.7% /dev/sdf1: Moved: 35.3% /dev/sdf1: Moved: 42.3% /dev/sdf1: Moved: 49.8% /dev/sdf1: Moved: 57.3% /dev/sdf1: Moved: 67.2% /dev/sdf1: Moved: 75.5% /dev/sdf1: Moved: 83.0% /dev/sdf1: Moved: 95.8% /dev/sdf1: Moved: 100.0%

[root@www ~01:01:47]#vgreduce vg0 /dev/sdf1    #先从卷组中减少设备/dev/sdf1
Removed "/dev/sdf1" from volume group "vg0"


[root@www ~01:10:09]#pvremove /dev/sdf1       #然后删除物理卷
Labels on physical volume "/dev/sdf1" successfully wiped

 

#缩减逻辑卷
[root@www mnt01:18:08]#umount /mnt/lv0  #首先取消挂载 [root@www mnt01:20:24]#e2fsck -f /dev/vg0/lv0    #检查逻辑卷完整性
e2fsck 1.41.12 (17-May-2010) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information /dev/vg0/lv0: 11/1310720 files (0.0% non-contiguous), 122065/5242880 blocks [root@www mnt01:21:06]#resize2fs /dev/vg0/lv0 10G    #同步逻辑卷的大小到指定缩减的大小 resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/vg0/lv0 to 2621440 (4k) blocks. The filesystem on /dev/vg0/lv0 is now 2621440 blocks long. [root@www mnt01:22:27]#lvreduce -L 10G /dev/vg0/lv0    #缩减逻辑卷的大小到指定的大小 WARNING: Reducing active logical volume to 10.00 GiB. THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce vg0/lv0? [y/n]: Y       #缩减必须考虑当前的系统数据做好备份,或者缩减后剩余的空间足够容纳当前数据  Size of logical volume vg0/lv0 changed from 20.00 GiB (1280 extents) to 10.00 GiB (640 extents). Logical volume lv0 successfully resized. [root@www mnt01:22:49]#mount /dev/vg0/lv0 /mnt/lvo mount: mount point /mnt/lvo does not exist [root@www mnt01:23:39]#mount /dev/vg0/lv0 /mnt/lv0 [root@www mnt01:23:57]#lvs    #查看缩减后的逻辑卷信息 LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv0 vg0 -wi-ao---- 10.00g [root@www mnt01:24:09]#