码迷,mamicode.com
首页 > 其他好文 > 详细

No.26 LVM逻辑卷管理

时间:2017-11-26 13:58:06      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:linux 运维 lvm 逻辑卷

整合小容量磁盘和实现分区的动态伸缩。

boot用来存放引导文件,不要基于LVM创建,开机的过程中不识别逻辑卷。

PVPhysical Volume,物理卷):物理分区,或整个物理磁盘,由PEPhysical Extent,基本单元)组成。

VGVolume Group,卷组):一个或多个物理卷组成的整体。

LVLogical Volume,逻辑卷):从卷组中分割出的一块逻辑存储空间,课通过格式化建立文件系统。

功能

物理卷

卷组

逻辑卷

scan(扫描)

pvscan

vgscan

lvscan

create(创建)

pvcreate

vgcreate

lvcreate

display(显示)

pvdisplay

vgdisplay

lvdisplay

remove(删除)

pvremove

vgremove

lvremove

extend(扩展)


vgextend

lvextend

reduce(减少)


vgreduce

lvreduce

图形管理程序:system-config-lvm

LVM创建过程:创建PV-->创建-->VG-->创建LV-->格式化LV-->挂载

LVM删除过程:卸载-->删除LV-->删除VG-->删除PV

pvcreate 设备路径

vgcreate 卷组名 设备路径

lvcreate -L 大小 -n 逻辑卷名 卷组名

创建LVM实验步骤:

添加一块硬盘20GB

[root@node0 ~]# lsblk   # 查看添加的硬盘

NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sr0     11:0    1  3.6G  0 rom  /dvd

sda      8:0    0   20G  0 disk

├─sda1   8:1    0  200M  0 part /boot

├─sda2   8:2    0 1000M  0 part [SWAP]

└─sda3   8:3    0 18.8G  0 part /

sdb      8:16   0   20G  0 disk

[root@node0 ~]# parted /dev/sdb  # 对硬盘进行分区

GNU Parted 2.1

Using /dev/sdb

Welcome to GNU Parted! Type 'help' to view a list of commands.

(parted) mklabel           # 创建分区表                                              

New disk label type? gpt       # 分区表类型为GPT                                           

(parted) mkpart                  # 创建分区                                        

Partition name?  []?                                                     

File system type?  [ext2]? ext4    # 写入文件系统的类型                                       

Start? 0                               # 开始位置                                 

End? 50%                                # 结束位置                                 

Warning: The resulting partition is not properly aligned for best performance.

Ignore/Cancel? i                      # 忽略                                   

(parted) p                             # 显示分区结果                                  

Model: ATA VMware Virtual S (scsi)

Disk /dev/sdb: 21.5GB

Sector size (logical/physical): 512B/512B

Partition Table: gpt

 

Number  Start   End     Size    File system  Name  Flags

 1      17.4kB  10.7GB  10.7GB

 

(parted) mkpart

Partition name?  []?                                                     

File system type?  [ext2]? ext4                                          

Start? 50%                                                               

End? 100%                                                                

(parted) p                                                                

Model: ATA VMware Virtual S (scsi)

Disk /dev/sdb: 21.5GB

Sector size (logical/physical): 512B/512B

Partition Table: gpt

 

Number  Start   End     Size    File system  Name  Flags

 1      17.4kB  10.7GB  10.7GB

 2      10.7GB  21.5GB  10.7GB

 

(parted) quit                         

[root@node0 ~]# pvscan # 扫描现有的PV

  No matching physical volumes found

[root@node0 ~]# pvcreate /dev/sdb1 /dev/sdb2   # 创建PV

  Physical volume "/dev/sdb1" successfully created

  Physical volume "/dev/sdb2" successfully created

[root@node0 ~]# vgcreate vg01 /dev/sdb1 /dev/sdb2

  Volume group "vg01" successfully created

[root@node0 ~]# vgscan # 扫描现有的VG

  Reading all physical volumes.  This may take a while...

  Found volume group "vg01" using metadata type lvm2

[root@node0 ~]# vgs

  VG   #PV #LV #SN Attr   VSize  VFree

  vg01   2   0   0 wz--n- 19.99g 19.99g

[root@node0 ~]# lvcreate -L 15GB -n lv01 vg01   # 创建LV

  Logical volume "lv01" created.

[root@node0 ~]# lvs 

  LV   VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert

  lv01 vg01 -wi-a----- 15.00g                                                   

[root@node0 ~]# lvscan  # 扫描现有的LV

  ACTIVE            '/dev/vg01/lv01' [15.00 GiB] inherit

[root@node0 ~]# mkfs.ext4 /dev/vg01/lv01   # 格式化LV

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

983040 inodes, 3932160 blocks

196608 blocks (5.00%) reserved for the super user

First data block=0

Maximum filesystem blocks=4026531840

120 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, 2654208

 

Writing inode tables: done                           

Creating journal (32768 blocks): done

Writing superblocks and filesystem accounting information: done

 

This filesystem will be automatically checked every 39 mounts or

180 days, whichever comes first.  Use tune2fs -c or -i to override.

[root@node0 ~]# lsblk  # 查看设备

NAME                 MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sr0                   11:0    1  3.6G  0 rom  /dvd

sda                    8:0    0   20G  0 disk

├─sda1                 8:1    0  200M  0 part /boot

├─sda2                 8:2    0 1000M  0 part [SWAP]

└─sda3                 8:3    0 18.8G  0 part /

sdb                    8:16   0   20G  0 disk

├─sdb1                 8:17   0   10G  0 part

└─vg01-lv01 (dm-0) 253:0    0   15G  0 lvm 

└─sdb2                 8:18   0   10G  0 part

  └─vg01-lv01 (dm-0) 253:0    0   15G  0 lvm

扩展、缩减LV

[root@node0 ~]# lvextend -L +2GB /dev/vg01/lv01  # 扩展LV

  Size of logical volume vg01/lv01 changed from 15.00 GiB (3840 extents) to 17.00 GiB (4352 extents).

  Logical volume lv01 successfully resized

[root@node0 ~]# lvs

  LV   VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert

  lv01 vg01 -wi-a----- 17.00g         

[root@node0 ~]# lvreduce -L -1GB /dev/vg01/lv01  # 减小LV

  WARNING: Reducing active logical volume to 16.00 GiB

  THIS MAY DESTROY YOUR DATA (filesystem etc.)

Do you really want to reduce lv01? [y/n]: y

  Size of logical volume vg01/lv01 changed from 17.00 GiB (4352 extents) to 16.00 GiB (4096 extents).

  Logical volume lv01 successfully resized

扩展、缩减VG

[root@node0 ~]# lsblk   # 添加硬盘

NAME                 MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sr0                   11:0    1  3.6G  0 rom  /dvd

sda                    8:0    0   20G  0 disk

├─sda1                 8:1    0  200M  0 part /boot

├─sda2                 8:2    0 1000M  0 part [SWAP]

└─sda3                 8:3    0 18.8G  0 part /

sdb                    8:16   0   20G  0 disk

├─sdb1                 8:17   0   10G  0 part

└─vg01-lv01 (dm-0) 253:0    0   16G  0 lvm 

└─sdb2                 8:18   0   10G  0 part

  └─vg01-lv01 (dm-0) 253:0    0   16G  0 lvm 

sdc                    8:32   0   20G  0 disk

[root@node0 ~]# parted /dev/sdc  # 为新添加的硬盘分区

GNU Parted 2.1

Using /dev/sdc

Welcome to GNU Parted! Type 'help' to view a list of commands.

(parted) mkl                                                             

New disk label type? Gpt  # 创建分区表                                                                              

(parted) mkpart             # 创建分区                                              

Partition name?  []?                                                     

File system type?  [ext2]? ext4                                          

Start? 0%                                                                

End? 100%                                                                

(parted) p                    # 查看结果                                           

Model: ATA VMware Virtual S (scsi)

Disk /dev/sdc: 21.5GB

Sector size (logical/physical): 512B/512B

Partition Table: gpt

 

Number  Start   End     Size    File system  Name  Flags

 1      1049kB  21.5GB  21.5GB

 

(parted) quit               # 退出parted分区工具                                             

Information: You may need to update /etc/fstab.      

[root@node0 ~]# lsblk     # 查看结果

NAME                 MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sr0                   11:0    1  3.6G  0 rom  /dvd

sda                    8:0    0   20G  0 disk

├─sda1                 8:1    0  200M  0 part /boot

├─sda2                 8:2    0 1000M  0 part [SWAP]

└─sda3                 8:3    0 18.8G  0 part /

sdb                    8:16   0   20G  0 disk

├─sdb1                 8:17   0   10G  0 part

└─vg01-lv01 (dm-0) 253:0    0   16G  0 lvm 

└─sdb2                 8:18   0   10G  0 part

  └─vg01-lv01 (dm-0) 253:0    0   16G  0 lvm 

sdc                    8:32   0   20G  0 disk

└─sdc1                 8:33   0   20G  0 part

[root@node0 ~]# pvcreate /dev/sdc1  # 创建PV

  Physical volume "/dev/sdc1" successfully created

[root@node0 ~]# vgextend vg01 /dev/sdc1 # 扩展VG

  Volume group "vg01" successfully extended

[root@node0 ~]# vgs   # 查看扩展结果

  VG   #PV #LV #SN Attr   VSize  VFree

  vg01   3   1   0 wz--n- 39.99g 23.99g

root@node0 ~]# lvextend -L +10GB -n /dev/vg01/lv01 # 扩展LV

  Size of logical volume vg01/lv01 changed from 16.00 GiB (4096 extents) to 26.00 GiB (6656 extents).

  Logical volume lv01 successfully resized

[root@node0 ~]# lvs   # 查看扩展结果

  LV   VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert

  lv01 vg01 -wi-a----- 26.00g                                                   

挂载LV

[root@node0 ~]# mkdir /lv01_data   # 创建用于挂载的目录

[root@node0 ~]# ll /dev/vg01/lv01

lrwxrwxrwx. 1 root root 7 Nov 20 16:59 /dev/vg01/lv01 -> ../dm-0

[root@node0 ~]# vim /etc/fstab     # 编辑/etc/fstab配置文件

/dev/vg01/lv01          /lv01_data              ext4    defaults        0 0

[root@node0 ~]# mount -a            # 刷新/etc/fstab

[root@node0 ~]# lsblk               # 查看结果

NAME                 MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sr0                   11:0    1  3.6G  0 rom  /dvd

sda                    8:0    0   20G  0 disk

├─sda1                 8:1    0  200M  0 part /boot

├─sda2                 8:2    0 1000M  0 part [SWAP]

└─sda3                 8:3    0 18.8G  0 part /

sdb                    8:16   0   20G  0 disk

├─sdb1                 8:17   0   10G  0 part

└─vg01-lv01 (dm-0) 253:0    0   26G  0 lvm  /lv01_data

└─sdb2                 8:18   0   10G  0 part

  └─vg01-lv01 (dm-0) 253:0    0   26G  0 lvm  /lv01_data

sdc                    8:32   0   20G  0 disk

└─sdc1                 8:33   0   20G  0 part

  └─vg01-lv01 (dm-0) 253:0    0   26G  0 lvm  /lv01_data

缩减文件系统:

[root@node0 ~]# resize2fs /dev/vg01/lv01 10G

resize2fs 1.41.12 (17-May-2010)

Resizing the filesystem on /dev/vg01/lv01 to 2621440 (4k) blocks.

The filesystem on /dev/vg01/lv01 is now 2621440 blocks long.

删除LVM

[root@node0 ~]# umount /lv01_data/   # 卸载文件系统

[root@node0 ~]# lvremove /dev/vg01/lv01  # 删除LV

Do you really want to remove active logical volume lv01? [y/n]: y # 确认删除

  Logical volume "lv01" successfully removed

[root@node0 ~]# lvs   # 查看结果

[root@node0 ~]# vgremove /dev/vg01   # 删除VG

  Volume group "vg01" successfully removed

[root@node0 ~]# vgs # 查看结果

[root@node0 ~]# lsblk

NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sr0     11:0    1  3.6G  0 rom  /dvd

sda      8:0    0   20G  0 disk

├─sda1   8:1    0  200M  0 part /boot

├─sda2   8:2    0 1000M  0 part [SWAP]

└─sda3   8:3    0 18.8G  0 part /

sdb      8:16   0   20G  0 disk

├─sdb1   8:17   0   10G  0 part

└─sdb2   8:18   0   10G  0 part

sdc      8:32   0   20G  0 disk

└─sdc1   8:33   0   20G  0 part

[root@node0 ~]# pvs

  PV         VG   Fmt  Attr PSize  PFree

  /dev/sdb1       lvm2 ---  10.00g 10.00g

  /dev/sdb2       lvm2 ---  10.00g 10.00g

  /dev/sdc1       lvm2 ---  20.00g 20.00g

[root@node0 ~]# pvremove /dev/sdb1 /dev/sdb2 /dev/sdc1    # 删除PV

  Labels on physical volume "/dev/sdb1" successfully wiped

  Labels on physical volume "/dev/sdb2" successfully wiped

  Labels on physical volume "/dev/sdc1" successfully wiped

[root@node0 ~]# pvs  # 查看结果

删除/etc/fstab文件中的配置条目。


No.26 LVM逻辑卷管理

标签:linux 运维 lvm 逻辑卷

原文地址:http://blog.51cto.com/xvjunjie/2044320

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!