title: centos LVM详解
date: 2018-04-24 14:00:03 tags: [linux,centos,LVM] ---知识了解
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
参考网址: