一、什么是LVM?

LVM是 Logical Volume Manager(逻辑卷管理)的简写。
它是Linux环境下对磁盘分区进行管理的一种机制,它由HeinzMauelshagen在Linux 2.4内核上实现,目前最新版本为:稳定版1.0.5,开发版 1.1.0-rc2,以及LVM2开发版。Linux用户安装Linux操作系统时遇到的一个常见的难以决定的问题就是如何正确地评估各分区大小,以分配合适的硬盘空间。普通的磁盘分区管理方式在逻辑分区划分好之后就无法改变其大小,当一个逻辑分区存放不下某个文件时,这个文件因为受上层文件系统的限制,也不能跨越多个分区来存放,所以也不能同时放到别的磁盘上。而遇到出现某个分区空间耗尽时,解决的方法通常是使用符号链接,或者使用调整分区大小的工具,但这只是暂时解决办法,没有从根本上解决问题。随着Linux的逻辑卷管理功能的出现,这些问题都迎刃而解,用户在无需停机的情况下可以方便地调整各个分区大小。

二、LVM的建立。

首先,先给大家普及以下Linux中lvm中的一些常见符号及意义。

pv       ##物理卷:被lv命令处理过的物理分区vg        ##物理卷组:被组装到一起的物理卷pe      ##实体延伸区块:PE是LVM最小的存储单位,一般为4MB大小,它是构成VG的基本单位lvm     ##逻辑卷:直接使用的设备,可以增大缩减并保持原有数据不变

1、xfs文件系统的LVM管理

1)分区时定分区标签为8e。

[root@localhost ~]# fdisk /dev/vdb
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.Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x9ee14f08.Command (m for help): n
Partition type:p   primary (0 primary, 0 extended, 4 free)e   extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
First sector (2048-20971519, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +100M
Partition 1 of type Linux and of size 100 MiB is setCommand (m for help): n
Partition type:p   primary (1 primary, 0 extended, 3 free)e   extended
Select (default p):
Using default response p
Partition number (2-4, default 2):
First sector (206848-20971519, default 206848):
Using default value 206848
Last sector, +sectors or +size{K,M,G} (206848-20971519, default 20971519): +100M
Partition 2 of type Linux and of size 100 MiB is setCommand (m for help): n
Partition type:p   primary (2 primary, 0 extended, 2 free)e   extended
Select (default p):
Using default response p
Partition number (3,4, default 3):
First sector (411648-20971519, default 411648):
Using default value 411648
Last sector, +sectors or +size{K,M,G} (411648-20971519, default 20971519): +100M
Partition 3 of type Linux and of size 100 MiB is setCommand (m for help): n
Partition type:p   primary (3 primary, 0 extended, 1 free)e   extended
Select (default e):
Using default response e
Selected partition 4
First sector (616448-20971519, default 616448):
Using default value 616448
Last sector, +sectors or +size{K,M,G} (616448-20971519, default 20971519):
Using default value 20971519
Partition 4 of type Extended and of size 9.7 GiB is setCommand (m for help): wq
The partition table has been altered!Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]#
[root@localhost ~]# fdisk /dev/vdb
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
All primary partitions are in use
Adding logical partition 5
First sector (618496-20971519, default 618496):
Using default value 618496
Last sector, +sectors or +size{K,M,G} (618496-20971519, default 20971519):
Using default value 20971519
Partition 5 of type Linux and of size 9.7 GiB is setCommand (m for help): d
Partition number (1-5, default 5): 5
Partition 5 is deletedCommand (m for help): n
All primary partitions are in use
Adding logical partition 5
First sector (618496-20971519, default 618496):
Using default value 618496
Last sector, +sectors or +size{K,M,G} (618496-20971519, default 20971519): +500M
Partition 5 of type Linux and of size 500 MiB is setCommand (m for help): n
All primary partitions are in use
Adding logical partition 6
First sector (1644544-20971519, default 1644544):
Using default value 1644544
Last sector, +sectors or +size{K,M,G} (1644544-20971519, default 20971519): +500M
Partition 6 of type Linux and of size 500 MiB is setCommand (m for help): t
Partition number (1-6, default 6): 5
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'Command (m for help): t
Partition number (1-6, default 6): 6
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'Command (m for help): pDisk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x9ee14f08Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux
/dev/vdb2          206848      411647      102400   83  Linux
/dev/vdb3          411648      616447      102400   83  Linux
/dev/vdb4          616448    20971519    10177536    5  Extended
/dev/vdb5          618496     1642495      512000   8e  Linux LVM
/dev/vdb6         1644544     2668543      512000   8e  Linux LVMCommand (m for help): wq
The partition table has been altered!Calling ioctl() to re-read partition table.

2)创建物理卷

pvcreate /dev/vdb5       ##创建物理卷
pvcreate /dev/vdb6

示例:
采用以下命令监视物理卷、物理卷组、逻辑卷、以及逻辑卷挂载点的有关数据。

watch -n 1 "pvs;echo ===;vgs;echo ===;lvs;echo ===;df -h /mnt"

3)创建物理卷组

vgcreate vg0 /dev/vdb5       ##创建物理卷组
vgextend vg0 /dev/vdb6


分析:把第一块物理卷加入物理卷组时用vgcreate,之后增添物理卷组都用vgextend。

4)创建逻辑卷及挂载逻辑卷

lvcreate -L 20M -n lv0 vg0   ##创建逻辑卷
mkfs.xfs /dev/vg0/lv0       ##格式化为xfs的文件系统
mount /dev/vg0/lv0 /mnt     ##挂载到mnt


测试:

[root@localhost ~]# df -H /mnt
Filesystem           Size  Used Avail Use% Mounted on
/dev/mapper/vg0-lv0   18M  1.2M   17M   7% /mnt

5)如何增添逻辑卷

pvcreate /dev/vdb7               ##创建物理卷标签为8e
vgextend vg0 /dev/vdb9          ##增添物理卷组
lvextend -L 1500M /dev/vg0/lv0  ##拉伸逻辑卷为1500M
xfs_growfs /dev/vg0/lv0         ##拉伸文件系统使1500M的逻辑卷可用

示例:

[root@localhost ~]# vgcreate vg0 /dev/vdb5Volume group "vg0" successfully created
[root@localhost ~]# vgextend vg0 /dev/vdb6Volume group "vg0" successfully extended
[root@localhost ~]# lvcreate -L 20M -n lv0 vg0Logical volume "lv0" created
[root@localhost ~]# mkfs.xfs /dev/vg0/lv0
meta-data=/dev/vg0/lv0           isize=256    agcount=1, agsize=5120 blks=                       sectsz=512   attr=2, projid32bit=1=                       crc=0
data     =                       bsize=4096   blocks=5120, 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@localhost ~]# mount /dev/vg0/lv0 /mnt
[root@localhost ~]# df -H /mnt
Filesystem           Size  Used Avail Use% Mounted on
/dev/mapper/vg0-lv0   18M  1.2M   17M   7% /mnt
[root@localhost ~]# fdisk /dev/vdb
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
All primary partitions are in use
Adding logical partition 7
First sector (2670592-20971519, default 2670592):
Using default value 2670592
Last sector, +sectors or +size{K,M,G} (2670592-20971519, default 20971519): +1G
Partition 7 of type Linux and of size 1 GiB is setCommand (m for help): t
Partition number (1-7, default 7):
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'Command (m for help): wq
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 or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@localhost ~]# partprobe
[root@localhost ~]# pvcreate /dev/vdb7Physical volume "/dev/vdb7" successfully created
[root@localhost ~]# vgextend vg0 /dev/vdb7Volume group "vg0" successfully extended
[root@localhost ~]# lvextend -L 1500M /dev/vg0/lv0 Extending logical volume lv0 to 1.46 GiBLogical volume lv0 successfully resized
[root@localhost ~]# xfs_growfs /dev/vg0/lv0
meta-data=/dev/mapper/vg0-lv0    isize=256    agcount=1, agsize=5120 blks=                       sectsz=512   attr=2, projid32bit=1=                       crc=0
data     =                       bsize=4096   blocks=5120, imaxpct=25=                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=853, version=2=                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 5120 to 384000

2、ext4文件系统的硬盘缩减及拉伸

前几步与xfs文件系统的相同就不重复介绍了。

1、创建逻辑卷,将逻辑卷格式化为ext4的文件系统。

mkfs.ext4 /dev/vg0/lv0       ##lv0格式化为ext4文件系统

2、挂载ext4文件系统的lv0到/mnt,及拉伸逻辑卷、拉伸文件系统。

mount /dev/vg0/lv0 /mnt/     ##挂载
lvextend -L 1800 /dev/vg0/lv0   ##拉伸逻辑卷
resize2fs /dev/vg0/lv0          ##拉伸ext4文件系统

3、缩减逻辑卷及文件系统

umount /mnt/                 ##卸载
e2fsck -f /dev/vg0/lv0          ##检查lv0数据
resize2fs /dev/vg0/lv0 1000M    ##缩减ext4文件系统
mount /dev/vg0/lv0 /mnt/        ##挂载
lvreduce -L 1000M /dev/vg0/lv0  ##缩减逻辑卷
pvmove /dev/vdb6 /dev/vdb7      ##把vdb6的数据移动到vdb7
vgreduce vg0 /dev/vdb6          ##把vdb6移除vg0物理卷组
pvremove /dev/vdb6              ##把vdb6移除物理卷

示例:

[root@localhost ~]# umount /mnt/
[root@localhost ~]# e2fsck -f /dev/vg0/lv0
e2fsck 1.42.9 (28-Dec-2013)
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/120000 files (9.1% non-contiguous), 16862/460800 blocks
[root@localhost ~]# resize2fs /dev/vg0/lv0 1000M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg0/lv0 to 256000 (4k) blocks.
The filesystem on /dev/vg0/lv0 is now 256000 blocks long.[root@localhost ~]# mount /dev/vg0/lv0 /mnt/
[root@localhost ~]# lvreduce -L 1000M /dev/vg0/lv0 WARNING: Reducing active and open logical volume to 1000.00 MiBTHIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv0? [y/n]: yReducing logical volume lv0 to 1000.00 MiBLogical volume lv0 successfully resized
[root@localhost ~]# pvmove /dev/vdb6 /dev/vdb7No data to move for vg0
[root@localhost ~]# vgreduce vg0 /dev/vdb6Removed "/dev/vdb6" from volume group "vg0"
[root@localhost ~]# pvremove /dev/vdb6Labels on physical volume "/dev/vdb6" successfully wiped
[root@localhost ~]# 


三、创建逻辑卷快照。

lvcreate -L 50M -n /dev/vg0/lv0_snap -s /dev/vg0/lv0 ##创建逻辑卷快照
mount /dev/vg0/lv0_snap /mnt/                           ##挂载快照


小测试:模拟逻辑卷快照的损坏及重新拍摄快照。

[root@localhost ~]# umount /mnt/
[root@localhost ~]# mount /dev/vg0/lv0 /mnt/
[root@localhost ~]# touch /mnt/file{1..9}
[root@localhost ~]# umount /mnt/
[root@localhost ~]# lvcreate -L 50M -n /dev/vg0/lv0_snap -s /dev/vg0/lv0Rounding up size to full physical extent 52.00 MiBLogical volume "lv0_snap" created
[root@localhost ~]# mount /dev/vg0/lv0_snap /mnt/
[root@localhost ~]# ls /mnt/*
/mnt/file1  /mnt/file3  /mnt/file5  /mnt/file7  /mnt/file9
/mnt/file2  /mnt/file4  /mnt/file6  /mnt/file8/mnt/lost+found:
[root@localhost ~]# rm -rf /mnt/*
[root@localhost ~]# ls /mnt/*
ls: cannot access /mnt/*: No such file or directory
[root@localhost ~]# umount /mnt/
[root@localhost ~]# lvremove /dev/vg0/lv0_snap
Do you really want to remove active logical volume lv0_snap? [y/n]: yLogical volume "lv0_snap" successfully removed
[root@localhost ~]# lvcreate -L 50M -n /dev/vg0/lv0_snap -s /dev/vg0/lv0Rounding up size to full physical extent 52.00 MiBLogical volume "lv0_snap" created
[root@localhost ~]# mount /dev/vg0/lv0_snap /mnt/
[root@localhost ~]# ls /mnt/*
/mnt/file1  /mnt/file3  /mnt/file5  /mnt/file7  /mnt/file9
/mnt/file2  /mnt/file4  /mnt/file6  /mnt/file8/mnt/lost+found:
[root@localhost ~]#


四、删除LVM

lvremove  /dev/vg0/lv0_snap      ##删除逻辑卷快照
lvremove  /dev/vg0/lv0              ##删除逻辑卷
vgremove vg0                        ##删除物理卷组
pvremove  /dev/vdb5             ##移除物理卷
pvremove  /dev/vdb7                 ##移除物理卷
fdisk  /dev/vdb                     ##删除LVM分区
partprobe                           ##重新加载分区表

示例:

[root@localhost ~]# umount /mnt/
[root@localhost ~]# lvremove /dev/vg0/lv0_snap
Do you really want to remove active logical volume lv0_snap? [y/n]: yLogical volume "lv0_snap" successfully removed
[root@localhost ~]# lvremove /dev/vg0/lv0
Do you really want to remove active logical volume lv0? [y/n]: yLogical volume "lv0" successfully removed
[root@localhost ~]# vgremove vg0Volume group "vg0" successfully removed
[root@localhost ~]# pvremove /dev/vdb5Labels on physical volume "/dev/vdb5" successfully wiped
[root@localhost ~]# pvremove /dev/vdb7Labels on physical volume "/dev/vdb7" successfully wiped
[root@localhost ~]# fdisk /dev/vdb
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): d
Partition number (1-7, default 7): 5
Partition 5 is deletedCommand (m for help): d
Partition number (1-6, default 6): 6
Partition 6 is deletedCommand (m for help): pDisk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x9ee14f08Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux
/dev/vdb2          206848      411647      102400   83  Linux
/dev/vdb3          411648      616447      102400   83  Linux
/dev/vdb4          616448    20971519    10177536    5  Extended
/dev/vdb5         1644544     2668543      512000   8e  Linux LVMCommand (m for help): d
Partition number (1-5, default 5): 5
Partition 5 is deletedCommand (m for help): pDisk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x9ee14f08Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux
/dev/vdb2          206848      411647      102400   83  Linux
/dev/vdb3          411648      616447      102400   83  Linux
/dev/vdb4          616448    20971519    10177536    5  ExtendedCommand (m for help): wq
The partition table has been altered!Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]#


Linux之LVM篇相关推荐

  1. 菜鸟学Linux 第026篇笔记 LVM

    菜鸟学Linux 第026篇笔记 LVM lsmod - program to show the status of modules in the Linux Kernel LVM 可以将物理硬盘组合 ...

  2. windows pxe 安装linux,菜鸟学Linux 第103篇笔记 pxe自动化安装linux

    菜鸟学Linux 第103篇笔记 pxe自动化安装linux 内容总览 linux的系统安装 kickstart文件的组成部分 DHCP (Dynamic Host Configuration Pro ...

  3. 菜鸟学Linux 第033篇笔记 bootloader,inittab

    菜鸟学Linux 第033篇笔记 bootloader,inittab Linux 系统自启动流程 PC OS (Linux) POST-->BIOS(Boot Sequence)-->M ...

  4. Linux中LVM的工作原理是什么?优缺点有哪些?

    在Linux操作系统中,LVM全称Logical Volume Manager,中文意思为逻辑卷管理.之前文章为大家介绍过什么是LVM.LVM名词解释.LVM写入模式等内容,本篇文章为大家介绍一下LV ...

  5. Linux下lvm中PV丢失找回方法

    一直对Linux下面lvm感兴趣,对于添加PV. 加入VG卷组. 新建LV都玩了.顺着来看看教程都还挺简单的,但是一直很想玩玩在出错的情况下,如何进行恢复: 一个偶然的情况下,虚拟机开机直接进入维护模 ...

  6. 菜鸟学Linux 第044篇笔记 算法和私有CA

    菜鸟学Linux 第044篇笔记 算法和私有CA 证书吊销列表CRL(Certificate Revocation List ) 如何解决私钥丢失 PKI: Public Key Infrastruc ...

  7. 计算机书籍-Linux内核 入门篇

    去天猫看看智能鼠标 书名:奔跑吧 Linux内核 入门篇 作者:张天飞 出版社:人民邮电出版社 出版时间:2019年02月

  8. 学着学着Linux系统写篇心得

    2019独角兽企业重金招聘Python工程师标准>>> 学着学着Linux系统写篇心得 本学期对于linux系统的学习,和自己买的一本<linux就该这么学>这本书,让我 ...

  9. 菜鸟学Linux 第050篇笔记 dhcp

    菜鸟学Linux 第050篇笔记 dhcp DHCP (Dynamic Host Configuration Protocol) 早期bootp (boot protocol) lease Clien ...

最新文章

  1. Integer的自动缓存
  2. java上机练习01
  3. codeforces37C
  4. Silverlight 5 深入理解 - TechEd2011葡萄城讲师课程
  5. 前松后紧和前紧后松——想起PM的点滴
  6. linux如何自动清buff,centos7
  7. php数组循环便利,浅析PHP中for与foreach两个循环结构遍历数组的区别
  8. java发送消息_通过java给qq邮箱发送信息
  9. BZOJ-1055 玩具取名
  10. Nutch1.2二次开发详细攻略(一)【图文】------Windows平台下Cygwin环境的搭建
  11. shell 判断目录还是文件
  12. mysql中where的用法_详解MySQL中WHERE子句的用法
  13. 使用ROS或Iptables作为ADSL上网路由器时,部分网站无法打开的问题
  14. CentOS7.2 安装L2TP/IPSec 服务端/客户端 和部分心得 ( libreswan+xl2tpd )
  15. 如何删除github中的提交历史记录
  16. Three Bags CodeForces - 1467C (贪心)
  17. 信息安全实验:实现一个fake-wifi
  18. 域名邮箱什么,如何开通自定义邮箱后缀的邮箱?
  19. 赞叹AI的力量-TopazLabs 全家桶使用经历
  20. navicat 表合并查询_数据库两个表合并命令

热门文章

  1. ROS学习第三天 ROS的通讯机制
  2. C# 程序关闭托盘图标不会自动消失
  3. 小学三年级计算机考试题,小学三年级信息技术考试试题
  4. 靠股票能改变普通人命运吗?
  5. 模拟信号拉线位移编码器是如何来校准的?
  6. macbook打印出现乱码解决方案
  7. 解决usr/bin/ld: cannot find -lxxx问题
  8. linux网络hack用法之onlink路由
  9. 【超全超详细】2W字零基础黑客学习路线,知识体系(附学习路线图)
  10. 尼康d850相机参数测试软件,新功能介绍二:景深合成与自动调焦_尼康 D850_数码影像评测-中关村在线...