centos LVM详解

时间:2022-08-28 23:41:24

title: centos LVM详解

date: 2018-04-24 14:00:03

tags: [linux,centos,LVM]

知识了解

LVM关系图

centos LVM详解

fdisk命令详解

    [root@localhost beinan]# fdisk /dev/xvde
Command (m for help): 在这里按 m ,就会输出帮助;
Command action
a toggle a bootable flag #设置启动分区
b edit bsd disklabel #编辑分区标签
c toggle the dos compatibility flag
d delete a partition #这是删除一个分区的动作
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types #l 是列出分区类型,以供我们设置相应分区的类型
m print this menu #m 是列出帮助信息
n add a new partition #添加一个分区
o create a new empty DOS partition table
p print the partition table #p 列出分区表
q quit without saving changes #不保存退出
s create a new empty Sun disklabel
t change a partition‘s system id #t 改变分区类型
u change display/entry units #改变显示的单位
v verify the partition table #检查验证分区表
w write table to disk and exit #把分区表写入硬盘并退出
x extra functionality (experts only) #扩展 Command (m for help): 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

新建LVM

查看分区和磁盘情况

    [root@vs-yth12 ~]$ fdisk -l

    Disk /dev/xvda: 64.4 GB, 64424509440 bytes, 125829120 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: 0x000743c6 Device Boot Start End Blocks Id System
/dev/xvda1 * 2048 100663295 50330624 83 Linux
/dev/xvda2 100663296 125829119 12582912 82 Linux swap / Solaris Disk /dev/xvde: 42.9 GB, 42949672960 bytes, 83886080 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: 0x7262c8b2 Device Boot Start End Blocks Id System
/dev/xvde1 2048 83886079 41942016 82 Linux swap / Solaris
[root@vs-yth12 ~]$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
xvda 202:0 0 60G 0 disk
├─xvda1 202:1 0 48G 0 part /
└─xvda2 202:2 0 12G 0 part [SWAP]
xvde 202:64 0 40G 0 disk
└─xvde1 202:65 0 40G 0 part

新建分区和修改分区类型ID为8e

    [root@vs-yth12 ~]$ fdisk /dev/xvde
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 0x7262c8b2. Command (m for help): t
No partition is defined yet! 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-83886079, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-83886079, default 83886079):
Using default value 83886079
Partition 1 of type Linux and of size 40 GiB is set Command (m for help): t
Selected partition 1
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@vs-yth12 ~]$ partprobe #在线更新

创建PV

    $ pvscan   #查询pv
PV /dev/xvde1 lvm2 [40.00 GiB]
Total: 1 [40.00 GiB] / in use: 0 [0 ] / in no VG: 1 [40.00 GiB]
$ pvdisplay #同上
$ pvcreate /dev/xvde1 #结果可以通过pvs pvscan pvdisplay 查看

创建VG

    $ vgcreate myvgname /dev/xvde1  #将所有PV组成一个VG,这里只有一个PV,myvgname是自定义的VG名
#创建成功后可以通过vgscan vgdisplay pvscan查看
$ pvscan
PV /dev/xvde1 VG myvgname lvm2 [40.00 GiB / 40.00 GiB free] #这里比上面的多了个VG
Total: 1 [40.00 GiB] / in use: 1 [40.00 GiB] / in no VG: 0 [0 ]

创建LV

    lvcreate 常用参数详解
lvcreate -L 15G myvgname #在名为myvgname的卷组中创建15G大小的逻辑卷,固定大小
lvcreate -L 2500 -n mylvname myvgname #在名为myvgname的卷组中创建2500M大小的逻辑卷,并命名为mylvname
# -n 命名LV
lvcreate -l 50%VG -n mylvname myvgname #用myvgname的50%大小创建一个LV
lvcreate --name centos7newvol -l 100%FREE vgnewlvm #用卷组剩下的所有空间创建逻辑卷 lvcreate: Create a logical volume(创建逻辑卷)
lvcreate
[-A|--autobackup {y|n}](自动备份)
[-a|--activate [a|e|l]{y|n}]
[--addtag Tag](增加标签)
[--alloc AllocationPolicy](分配策略)
[--cachemode CacheMode](Cache模式)
[-C|--contiguous {y|n}]
[-d|--debug]
[-h|-?|--help]
[--ignoremonitoring](忽略监控)
[--monitor {y|n}](监控)
[-i|--stripes Stripes [-I|--stripesize StripeSize]]
[-k|--setactivationskip {y|n}]
[-K|--ignoreactivationskip]
{-l|--extents LogicalExtentsNumber[%{VG|PVS|FREE}] |(逻辑扩展数)
-L|--size LogicalVolumeSize[bBsSkKmMgGtTpPeE]}(逻辑卷大小)
[-M|--persistent {y|n}] [--major major] [--minor minor]
[-m|--mirrors Mirrors [--nosync] [{--mirrorlog {disk|core|mirrored}|--corelog}]](镜像)
[-n|--name LogicalVolumeName](逻辑卷名字)
[--noudevsync]
[-p|--permission {r|rw}]
[--[raid]minrecoveryrate Rate]
[--[raid]maxrecoveryrate Rate]
[-r|--readahead ReadAheadSectors|auto|none](读取头扇区)
[-R|--regionsize MirrorLogRegionSize](镜像逻辑区域尺寸)
[-T|--thin [-c|--chunksize ChunkSize](块大小)
[--discards {ignore|nopassdown|passdown}]
[--poolmetadatasize MetadataSize[bBsSkKmMgG]]]
[--poolmetadataspare {y|n}]
[--thinpool ThinPoolLogicalVolume{Name|Path}] (精简池逻辑卷)
[-t|--test]
[--type VolumeType](卷类型)
[-v|--verbose]
[-W|--wipesignatures {y|n}]
[-Z|--zero {y|n}]
[--version]
VolumeGroupName [PhysicalVolumePath...]
    $ lvcreate --name mylvname -l 100%FREE myvgname    #用卷组剩下的所有空间创建逻辑卷
#创建成功后可以通过vgdisplay lsblk lvdisplay查看

格式化并挂载

    [root@vs-yth12 ~]$ lvdisplay    #查看lv情况,记住LV PATH
--- Logical volume ---
LV Path /dev/myvgname/mylvname
LV Name mylvname
VG Name myvgname
LV UUID 9FceM6-gvcT-xQDw-BL9t-5m8W-XXxY-zDmDma
LV Write Access read/write
LV Creation host, time vs-yth12.localdomain, 2018-04-28 17:38:17 +0800
LV Status available
$ open 0
LV Size 40.00 GiB
Current LE 10239
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0 [root@vs-yth12 ~]$ mkfs -t ext4 /dev/myvgname/mylvname #格式化
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
2621440 inodes, 10484736 blocks
524236 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2157969408
320 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,
4096000, 7962624 Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done [root@vs-yth12 ~]$ mount /dev/myvgname/mylvname /data 临时挂载
[root@vs-yth12 ~]$ vi /etc/fstab 添加永久挂载
#/dev/myvgname/mylvname /data ext4 defaults 0 0

删除LVM

查看分区和磁盘情况

    [root@vs-yth12 ~]$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 48G 3.6G 42G 8% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.6G 84K 1.6G 1% /dev/shm
tmpfs 1.6G 33M 1.6G 3% /run
tmpfs 1.6G 0 1.6G 0% /sys/fs/cgroup
tmpfs 327M 16K 327M 1% /run/user/42
/dev/mapper/vg_data-lv_data 35G 49M 33G 1% /data #这个就是我们要删除的
tmpfs 327M 0 327M 0% /run/user/0
[root@vs-yth12 ~]$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
xvda 202:0 0 60G 0 disk
├─xvda1 202:1 0 48G 0 part /
└─xvda2 202:2 0 12G 0 part [SWAP]
xvde 202:64 0 40G 0 disk
└─vg_data-lv_data 253:0 0 35G 0 lvm /data #这个就是我们要删除的

卸载LVM文件系统

    $ umount /data  #卸载成功后,用df -h可以看到结果

移除LV

    [root@vs-yth12 ~]$ lvscan  #查看活动中的LV
ACTIVE '/dev/vg_data/lv_data' [35.00 GiB] inherit
[root@vs-yth12 ~]$ lvdisplay #查看活动中的LV,同上
--- Logical volume ---
LV Path /dev/vg_data/lv_data
LV Name lv_data
VG Name vg_data
LV UUID FhRjBR-Y4w2-1hSR-9J1X-l41a-FTLe-RZNvRb
LV Write Access read/write
LV Creation host, time vs-yth12.localdomain, 2018-04-20 17:05:35 +0800
LV Status available
....
[root@vs-yth12 ~]$ lvremove /dev/vg_data/lv_data #移除后用lsblk可看到结果
Do you really want to remove active logical volume vg_data/lv_data? [y/n]: y
Logical volume "lv_data" successfully removed

移除VG

    [root@vs-yth12 ~]$ vgscan  #查看活动中的VG
Reading volume groups from cache.
Found volume group "vg_data" using metadata type lvm2
[root@vs-yth12 ~]$ vgdisplay #查看活动中的VG,同上
--- Volume group ---
VG Name vg_data
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
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 40.00 GiB
PE Size 4.00 MiB
Total PE 10239
Alloc PE / Size 0 / 0
Free PE / Size 10239 / 40.00 GiB
VG UUID I2okv5-rIGx-cg5B-4Iyq-3F04-pwZK-G2r5FS
[root@vs-yth12 ~]$ vgchange -a n vg_data #用 vgchange -a n VGname 让 VGname 这个 VG 不具有 Active 的标志
[root@vs-yth12 ~]$ vgremove vg_data #移除VG

移除PV

    [root@vs-yth12 ~]$ pvremove /dev/xvde #删除物理卷
Labels on physical volume "/dev/xvde" successfully wiped. #成功擦除

放大LV容量

用新的硬盘创建PV

    fdisk -l
lsblk
fdisk /dev/xvde #新建分区后修改ID为8e
partprobe #在线更新
pvcreate /dev/xvde1 #用那个新建的分区创建PV,用pvscan pvdisplay

将新空间添加

    vgextend myvgname /dev/xvde1
lvresize -l +64 /dev/myvgname/mylvname #利用 lvresize 将新加入的 PV 内的 PE 加入LV 中
#(vgdisplay vgdisplay lvdisplay lsblk df )
resize2fs /dev/myvgname/mylvname #透过 resize2fs 将文件系统的容量确实添加

如果:

    $ resize2fs /dev/centos/root
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block while trying to open /dev/centos/root
Couldn’t find valid filesystem superblock.
$ xfs_growfs /dev/centos/root #文件系统是xfs,用该命令来扩
$ df -h

[root@xs43254218114 ~]# resize2fs /dev/centos/root

缩小LV,删除

缩小LV是先声明 resize2fs -p 后定义 lvresize -L 的过程

缩小LV

    pvdisplay    #查看PV情况
pvscan #查看PV情况
umount /data #卸载
e2fsck -f /dev/myvgname/mylvname #磁盘检测LV
resize2fs /dev/myvgname/mylvname 3G #使用resize2fs 直接指定降低后的容量,用这个去挂载,
#没有+,-号就是固定值
mount /dev/myvgname/mylvname /data #重新挂载,这里用df -h可以看出大小变了,但实际lv还有空余的
pvdisplay #查看PV情况,可以看到有多少个
pvscan #查看PV情况
lvreszie -L -2G /dev/myvgname/mylvname #使用lvresize 降低LV的容量,这些空间就会回到VG
#假设一共5G,刚刚resize2fs保留了3G,现在这里就减少2G,可以用于其他LV,没有+,-号就是固定值

删除法

    pvdisplay    #查看PV情况
pvscan #查看PV情况
pvmove /dev/xvde1 /dev/xvde2 #将xvde1的数据全部移到xvde2中去
vgreduce myvgname /dev/xvde1 #将xvde1从VG myvgname中移除,这样这个PV可以创建其他VG了

先缩小LV1容量,后增大LV2容量的过程(生产环境下的减小/home 增大/root) ###、

1.卸载LV1

2.使用 e2fsck 检测一下LV1

3.使用resize2fs 重新定义LV1的大小

4.重新挂载,检验一下结果

5.使用lvreduce 或lvresize 减小LV1至指定大小

6.使用lvextend 增加LV2的大小

7.重新定义下LV2大小

一加一减

    $ fdisk -l    #查看磁盘情况
$ df -h #同上
$ lsblk #同上 $ mkdir /mnt/backup #备份LV1的数据
$ tar -jcv -f /mnt/backup/test.tar.bz2 /data/*

$ mv /data/* /mnt/backup/ 3.卸载LV1
$ umount /data $ e2fsck -f /dev/myvgname/mylvname #使用 e2fsck 检测一下LV1 $ resize2fs -p /dev/myvgname/mylvname 2G #使用resize2fs 重新定义LV1的大小 $ mount /dev/myvgname/mylvname /data #重新挂载,检验一下结果
$ df -h $ lvresize -L 2G /dev/myvgname/mylvname #使用lvreduce 或lvresize 减小LV1至指定大小,没有+,-号就是固定值

$ lvreduce -L 2G /dev/myvgname/mylvname #没有+,-号就是固定值 $ lvextend -l +100%FREE /dev/myvgname/my2lvname #使用lvextend 增加LV2的大小

$ lvextend -L +1G /dev/myvgname/my2lvname $ resize2fs -p /dev/myvgname/my2lvname #重新定义下LV2大小 $ df -h #检查结果
$ lsblk

参考网址:

centos LVM详解的更多相关文章

  1. &lpar;转&rpar;CentOS分区操作详解

    CentOS分区操作详解 原文:http://blog.csdn.net/yonggeit/article/details/77924393 磁盘分区 分区格式的两种选择:MBR和GPT 分区命令: ...

  2. Vmware12安装centos系统详解

    vmware12安装centos7系统详解 用虚拟机12安装centos7系统详细安装过程,后附centos7下载地址. 工具/原料 虚拟机12 centos7系统镜像 方法/步骤 1 1.百度搜索c ...

  3. 第6章 LVM详解

    6.1 LVM相关概念和机制 LVM(Logical Volume Manager)可以让分区变得弹性,可以随时随地的扩大和缩小分区大小,前提是该分区是LVM格式的. lvm需要使用的软件包为lvm2 ...

  4. LVM详解笔记pv-vg-lv创建和扩展

    LVM Logical Volume Manager(逻辑卷管理) 是Linux环境下对底层磁盘的一种管理机制(方式),处在物理磁盘和文件系统之间. 名词: PV (Physical Volume)物 ...

  5. LVM 详解

    一.前言<http://blog.chinaunix.net/uid-186064-id-2823296.html> LVM是逻辑卷管理(Logic Volume Manage)的简称,它 ...

  6. Linux学习之CentOS&lpar;八&rpar;----详解文件的搜寻、查找&lpar;转&rpar;

    which (寻找『运行档』) [root@www ~]# which [-a] command 选项或参数: -a :将所有由 PATH 目录中可以找到的命令均列出,而不止第一个被找到的命令名称 分 ...

  7. centos free详解

    CentOS 6及以前 $ free total used free shared buffers cached Mem: 4040360 4012200 28160 0 176628 3571348 ...

  8. CentOS 6&period;5自动化运维之基于DHCP和TFTP服务的PXE自动化安装centos操作系统详解

    前言    如果要给很多台客户端主机安装操作系统,要是每一台都拿张安装光盘一台一台主机的去装系统那就太浪费时间和精力了.在生产环境中也不实际,要实现为多台主机自动安装操作系统,那我们怎么实现自动化安装 ...

  9. linux LVM详解

    1.创建及删除步骤1)创建:linux partition-->pv-->vg-->lv-->fs-->mount2)删除:umount-->lv-->vg- ...

随机推荐

  1. &dollar;&period;ajax返回的JSON格式的数据后无法执行success的解决方法

    近段时间做项目,在项目使用了ajax技术,遇到了一个奇怪的问题:"$.ajax返回的JSON格式的数据无法执行success",代码是这样写的: 1 $.ajax({ 2 .. 3 ...

  2. RT-Thread 线程调度

    /* 变量分配4字节对齐 */ ALIGN(RT_ALIGN_SIZE) /* 静态线程的 线程堆栈*/ ]; ]; /* 静态线程的 线程控制块 */ static struct rt_thread ...

  3. qTip2 精致的jQuery提示信息插件

    qTip2 精致的jQuery提示信息插件    出处:http://www.cnblogs.com/lwme/archive/2012/02/16/qtip2-jquery-plugin.html ...

  4. C&num; list 去重

    /// <summary> /// 汽车商标 获取 /// Redis Key=zgqp315_Redis_TrademarkC_List /// </summary> /// ...

  5. ASP&period;NET中如何删除最近打开的项目和文件的记录

    ASP.NTET中总是保留最近打开的项目和文件的记录,甚至是已删除的它也不删.下面介绍几种删除的方法: 第一种:建立一个bat文件,以后双击即可清除,内置代码如下: @echo off@REG Del ...

  6. 永久关闭防火墙和selinux

    临时关闭selinux: setenforce 0    //设置SELinux 成为permissive模式 彻底禁用selinux: 使用root用户,vim /etc/sysconfig/sel ...

  7. cxgrid 非编辑状态下复制当前列的值 真折腾人

    1.自带的CTRL +C 只能复制整行,不知是不是版本问题. 2.有分组这个代码就不行了 s:= G1DBView.DataController.Values[G1DBView.Controller. ...

  8. 【转】MYSQL-CLUSTER-7&period;5搭建数据库集群

    阅读目录 前言 mysql cluster中的几个概念解释 架构图及说明 下载mysql cluster 安装mysql cluster之前 安装配置管理节点 安装配置数据和mysql节点 测试 启动 ...

  9. 国内CDN加速现状

    什么是CDN CDN的全称是Content Delivery Network,即内容分发网络.是位于网络层与应用层之间的网络应用,其目的是通过在现有的Internet中增加一层新的网络架构,将网站的内 ...

  10. 层层递进Struts1(三)之Struts组成

    这篇博客我们来说一下Struts的主要组成我们,通过前几篇博客,我们知道这个框架最重要的几个步骤:获取路径.封装表单.获取转向列表.转向逻辑处理.转向,与此对应的是:ActionServlet.Act ...