一、LVM概念
LVM是逻辑盘卷管理(Logical Volume Manager)的简称,它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵活性。通过LVM系统管理员可以轻松管理磁盘分区,如:将若干个磁盘分区连接为一个整块的卷组(volume group),形成一个存储池。管理员可以在卷组上随意创建逻辑卷组(logical volumes),并进一步在逻辑卷组上创建文件系统。管理员通过LVM可以方便的调整存储卷组的大小,并且可以对磁盘存储按照组的方式进行命名、管理和分配。当系统添加了新的磁盘,通过LVM管理员就不必将磁盘的文件移动到新的磁盘上以充分利用新的存储空间,而是直接扩展文件系统跨越磁盘即可。
二、LVM结构
三、大纲
大纲:
一、将硬盘分区并划分为lvm卷
二、将分区转换为pv物理卷
三、将pv加入到一个vg组中
四、从vg划出lv逻辑卷
五、格式化lv卷
六、开机自动挂载
七、扩大逻辑卷(lv),扩大卷组(vg)
八、减小卷组(vg),减小逻辑卷(lv)
九、创建逻辑卷快照
十、删除卷组(vg),删除物理卷(pv),删除逻辑卷(lv)
十一、在lvm逻辑卷中常用的命令
四、实战动起来
fdisk最多支持15个分区,这是fdisk天生的限制
一、将硬盘分区并划分为lvm卷 [root@localhost ~]# fdisk /dev/sdb #对/dev/sdb进行磁盘分区 Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0xafb2301b. 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): p #打印分区表 Disk /dev/sdb: 10.7 GB, 10737418240 bytes 255 heads, 63 sectors/track, 1305 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0xafb2301b Device Boot Start End Blocks Id System Command (m for help): n #增加一个新分区 Command action e extended #扩展分区 p primary partition (1-4) #主分区 p Partition number (1-4): 1 #分区编号 First cylinder (1-1305, default 1): 1 #起初柱为1 Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305): +2000MB #分区的大小为2G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 #第二个分区 First cylinder (245-1305, default 245): Using default value 245 Last cylinder, +cylinders or +size{K,M,G} (245-1305, default 1305): +2000MB Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 3 #第三个分区 First cylinder (489-1305, default 489): Using default value 489 Last cylinder, +cylinders or +size{K,M,G} (489-1305, default 1305): +2000MB Command (m for help): n Command action e extended p primary partition (1-4) p Selected partition 4 #第四个分区 First cylinder (733-1305, default 733): Using default value 733 Last cylinder, +cylinders or +size{K,M,G} (733-1305, default 1305): +2000MB Command (m for help): p Disk /dev/sdb: 10.7 GB, 10737418240 bytes 255 heads, 63 sectors/track, 1305 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0xafb2301b Device Boot Start End Blocks Id System /dev/sdb1 1 244 1959898+ 83 Linux /dev/sdb2 245 488 1959930 83 Linux /dev/sdb3 489 732 1959930 83 Linux /dev/sdb4 733 976 1959930 83 Linux Command (m for help): t #改变一个分区的系统id Partition number (1-4): 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 #Linux LVM编号 Changed system type of partition 1 to 8e (Linux LVM) Command (m for help): t Partition number (1-4): 2 Hex code (type L to list codes): 8e Changed system type of partition 2 to 8e (Linux LVM) Command (m for help): t Partition number (1-4): 3 Hex code (type L to list codes): 8e Changed system type of partition 3 to 8e (Linux LVM) Command (m for help): t Partition number (1-4): 4 Hex code (type L to list codes): 8e Changed system type of partition 4 to 8e (Linux LVM) Command (m for help): p Disk /dev/sdb: 10.7 GB, 10737418240 bytes 255 heads, 63 sectors/track, 1305 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0xafb2301b Device Boot Start End Blocks Id System /dev/sdb1 1 244 1959898+ 8e Linux LVM /dev/sdb2 245 488 1959930 8e Linux LVM /dev/sdb3 489 732 1959930 8e Linux LVM /dev/sdb4 733 976 1959930 8e Linux LVM Command (m for help): w #保存分区表、打印分区表信息并退出 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. 二、将分区转换为pv物理卷 注释:PE的大小默认是4M 1、安装必要的软件包 yum -y install parted lvm2 2、让内核重新载入分区 注释:新硬盘分区完成后,使用partprobe将新的分区表读入内存 [root@localhost ~]# partprobe /dev/sdb #内核加载硬盘分区并读入到内存中 [root@localhost ~]# cat /proc/partitions #查看是否重新加载硬盘分区 [root@localhost ~]# pvcreate /dev/sdb{1,2,3,4} #将分区转换成物理卷 Physical volume "/dev/sdb1" successfully created Physical volume "/dev/sdb2" successfully created Physical volume "/dev/sdb3" successfully created Physical volume "/dev/sdb4" successfully created [root@localhost ~]# pvscan #查看物理卷,方法1 PV /dev/sdb1 lvm2 [1.87 GiB] PV /dev/sdb2 lvm2 [1.87 GiB] PV /dev/sdb3 lvm2 [1.87 GiB] PV /dev/sdb4 lvm2 [1.87 GiB] Total: 4 [7.48 GiB] / in use: 0 [0 ] / in no VG: 4 [7.48 GiB] [root@localhost ~]# pvs #查看物理卷,方法2 PV VG Fmt Attr PSize PFree /dev/sdb1 lvm2 a-- 1.87g 1.87g /dev/sdb2 lvm2 a-- 1.87g 1.87g /dev/sdb3 lvm2 a-- 1.87g 1.87g /dev/sdb4 lvm2 a-- 1.87g 1.87g [root@localhost ~]# pvdisplay #更详细的查看pv的相关信息 "/dev/sdb1" is a new physical volume of "1.87 GiB" --- NEW Physical volume --- PV Name /dev/sdb1 VG Name PV Size 1.87 GiB Allocatable NO PE Size 0 #在pv没有加入vg之前,pv是没有PE的 Total PE 0 Free PE 0 Allocated PE 0 PV UUID iuJHvt-P5by-2VIz-fZJX-WTXe-tYb5-57EcVk "/dev/sdb2" is a new physical volume of "1.87 GiB" --- NEW Physical volume --- PV Name /dev/sdb2 VG Name PV Size 1.87 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID sGFf2W-O9Ss-9z5R-JvQz-czoz-Gihq-8TmqnL "/dev/sdb3" is a new physical volume of "1.87 GiB" --- NEW Physical volume --- PV Name /dev/sdb3 VG Name PV Size 1.87 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID rRAyyb-I1XL-cpya-516K-h8Qs-ur3N-RgrH0X "/dev/sdb4" is a new physical volume of "1.87 GiB" --- NEW Physical volume --- PV Name /dev/sdb4 VG Name PV Size 1.87 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID 1wy8w7-fHvU-UgrS-jLpy-v7we-FaOt-KyAaOZ 三、将pv加入到一个vg组中 [root@localhost ~]# vgcreate myvg /dev/sdb{1,2,3} #将物理卷1,2,3加入到卷组,卷组名为myvg Volume group "myvg" successfully created [root@localhost ~]# vgscan #查看卷组 Reading all physical volumes. This may take a while... Found volume group "myvg" using metadata type lvm2 [root@localhost ~]# vgs #查看卷组 VG #PV #LV #SN Attr VSize VFree myvg 3 0 0 wz--n- 5.60g 5.60g [root@localhost ~]# vgdisplay myvg --- Volume group --- VG Name myvg System ID Format lvm2 Metadata Areas 3 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 3 Act PV 3 VG Size 5.60 GiB PE Size 4.00 MiB #pv加入vg后,才会有PE,默认是4M Total PE 1434 Alloc PE / Size 0 / 0 Free PE / Size 1434 / 5.60 GiB VG UUID 9LyXRu-JUCz-pcw6-Afkq-tAgN-A3pQ-G2RS4I 四、从vg划出lv逻辑卷 [root@localhost ~]# lvcreate -n mydata -L 4G myvg Logical volume "mydata" created [root@localhost ~]# lvscan ACTIVE ‘/dev/myvg/mydata‘ [4.00 GiB] inherit [root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert mydata myvg -wi-a----- 4.00g 五、格式化lv卷 [root@localhost ~]# mkfs.ext4 /dev/myvg/mydata mke2fs 1.41.12 (17-May-2010) 文件系统标签= 操作系统:Linux 块大小=4096 (log=2) 分块大小=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 262144 inodes, 1048576 blocks 52428 blocks (5.00%) reserved for the super user 第一个数据块=0 Maximum filesystem blocks=1073741824 32 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 正在写入inode表: 完成 Creating journal (32768 blocks): 完成 Writing superblocks and filesystem accounting information: 完成 This filesystem will be automatically checked every 36 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. 六、开机自动挂载 [root@localhost ~]# mkdir /mysql [root@localhost ~]# mount /dev/myvg/mydata /mysql [root@localhost ~]# ll /mysql/ 总用量 16 drwx------ 2 root root 16384 8月 24 10:17 lost+found [root@localhost ~]# vim /etc/fstab #加入到开启自启动中 /dev/myvg/mydata /mysql ext4 defaults 0 0 [root@localhost ~]# df 文件系统 1K-块 已用 可用 已用% 挂载点 /dev/sda3 39016904 1291960 35742944 4% / tmpfs 506272 0 506272 0% /dev/shm /dev/sda1 198337 25824 162273 14% /boot /dev/mapper/myvg-mydata 4128448 139256 3779480 4% /mysql [root@localhost ~]# umount /mysql #卸载挂载盘 [root@localhost ~]# df 文件系统 1K-块 已用 可用 已用% 挂载点 /dev/sda3 39016904 1291960 35742944 4% / tmpfs 506272 0 506272 0% /dev/shm /dev/sda1 198337 25824 162273 14% /boot [root@localhost ~]# mount -a #重新载入/etc/fstab配置文件 [root@localhost ~]# df #已经挂载/mysql 文件系统 1K-块 已用 可用 已用% 挂载点 /dev/sda3 39016904 1291960 35742944 4% / tmpfs 506272 0 506272 0% /dev/shm /dev/sda1 198337 25824 162273 14% /boot /dev/mapper/myvg-mydata 4128448 139256 3779480 4% /mysql 七、扩大逻辑卷(lv),扩大卷组(vg) 1、扩大逻辑卷(lv) [root@localhost ~]# vgdisplay myvg #查看卷组的信息 --- Volume group --- VG Name myvg System ID Format lvm2 Metadata Areas 3 Metadata Sequence No 2 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 5.60 GiB #卷组大小为5.6G PE Size 4.00 MiB #默认PE大小为4M Total PE 1434 Alloc PE / Size 1024 / 4.00 GiB Free PE / Size 410 / 1.60 GiB VG UUID Aeyfd0-9GUH-GHNg-G5qR-wN0T-r14b-ojIWJ2 [root@localhost ~]# lvdisplay /dev/myvg/mydata #查看lv逻辑卷的信息 --- Logical volume --- LV Path /dev/myvg/mydata LV Name mydata VG Name myvg LV UUID HzL6Hz-gy1z-bte2-Yvzc-tzpX-ZzHX-omel2Y LV Write Access read/write LV Creation host, time localhost, 2014-08-24 10:15:21 +0800 LV Status available # open 1 LV Size 4.00 GiB #卷组大小为4G Current LE 1024 Segments 3 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0 [root@localhost ~]# df -TH #查看磁盘的相关信息 文件系统 类型 容量 已用 可用 已用%% 挂载点 /dev/sda3 ext4 40G 1.4G 37G 4% / tmpfs tmpfs 519M 0 519M 0% /dev/shm /dev/sda1 ext4 204M 27M 167M 14% /boot /dev/mapper/myvg-mydata ext4 4.3G 143M 3.9G 4% /mysql #首先查看lv开头的相关命令,然后针对性的分析学习 [root@localhost ~]# lv lvchange lvdisplay lvmchange lvmdump lvmsar lvrename lvscan lvconvert lvextend lvmconf lvmetad lvreduce lvresize lvcreate lvm lvmdiskscan lvmsadc lvremove lvs [root@localhost ~]# lvextend -L +1G /dev/myvg/mydata #将逻辑卷mydata增加1G Extending logical volume mydata to 5.00 GiB Logical volume mydata successfully resized [root@localhost ~]# lvdisplay /dev/myvg/mydata #查看逻辑卷的大小 --- Logical volume --- LV Path /dev/myvg/mydata LV Name mydata VG Name myvg LV UUID HzL6Hz-gy1z-bte2-Yvzc-tzpX-ZzHX-omel2Y LV Write Access read/write LV Creation host, time localhost, 2014-08-24 10:15:21 +0800 LV Status available # open 1 LV Size 5.00 GiB #由刚才的4G到现在的5G,增加了1G Current LE 1280 Segments 3 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0 [root@localhost ~]# df -TH 文件系统 类型 容量 已用 可用 已用%% 挂载点 /dev/sda3 ext4 40G 1.4G 37G 4% / tmpfs tmpfs 519M 0 519M 0% /dev/shm /dev/sda1 ext4 204M 27M 167M 14% /boot /dev/mapper/myvg-mydata ext4 4.3G 143M 3.9G 4% /mysql #查看大小还是4.3G,显然没有变化 #在这里即使你重新挂载大小也不会变化,我们需要强制修改分区表 [root@localhost ~]# resize2fs -f /dev/myvg/mydata resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/myvg/mydata is mounted on /mysql; on-line resizing required old desc_blocks = 1, new_desc_blocks = 1 Performing an on-line resize of /dev/myvg/mydata to 1310720 (4k) blocks. The filesystem on /dev/myvg/mydata is now 1310720 blocks long. [root@localhost ~]# df -TH 文件系统 类型 容量 已用 可用 已用%% 挂载点 /dev/sda3 ext4 40G 1.4G 37G 4% / tmpfs tmpfs 519M 0 519M 0% /dev/shm /dev/sda1 ext4 204M 27M 167M 14% /boot /dev/mapper/myvg-mydata ext4 5.3G 143M 4.9G 3% /mysql #大小终于变化了,增加了1G 成功!!! [root@localhost ~]# lvscan ACTIVE ‘/dev/myvg/mydata‘ [5.00 GiB] inherit [root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert mydata myvg -wi-ao---- 5.00g 2、扩大卷组(vg) [root@localhost ~]# vgdisplay #查看卷组的相关信息 --- Volume group --- VG Name myvg System ID Format lvm2 Metadata Areas 3 Metadata Sequence No 3 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 5.60 GiB #卷组的大小为5.6G PE Size 4.00 MiB Total PE 1434 Alloc PE / Size 1280 / 5.00 GiB Free PE / Size 154 / 616.00 MiB VG UUID Aeyfd0-9GUH-GHNg-G5qR-wN0T-r14b-ojIWJ2 [root@localhost ~]# vgs VG #PV #LV #SN Attr VSize VFree myvg 3 1 0 wz--n- 5.60g 616.00m root@localhost ~]# vgextend myvg /dev/sdb4 #将分区4加入到myvg卷组中 Volume group "myvg" successfully extended [root@localhost ~]# vgdisplay #查看卷组相关信息 --- Volume group --- VG Name myvg System ID Format lvm2 Metadata Areas 4 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 4 Act PV 4 VG Size 7.47 GiB #卷组大小为7.47G 增加了2G PE Size 4.00 MiB Total PE 1912 Alloc PE / Size 1280 / 5.00 GiB Free PE / Size 632 / 2.47 GiB VG UUID Aeyfd0-9GUH-GHNg-G5qR-wN0T-r14b-ojIWJ2 [root@localhost ~]# vgs VG #PV #LV #SN Attr VSize VFree myvg 4 1 0 wz--n- 7.47g 2.47g 八、减小卷组(vg),减小逻辑卷(lv),减小物理卷(pv) 1、减小卷组(vg) [root@localhost ~]# pvscan #查看一个myvg卷组中包含哪些分区或者硬盘 PV /dev/sdb1 VG myvg lvm2 [1.87 GiB / 0 free] PV /dev/sdb2 VG myvg lvm2 [1.87 GiB / 0 free] PV /dev/sdb3 VG myvg lvm2 [1.87 GiB / 616.00 MiB free] PV /dev/sdb4 VG myvg lvm2 [1.87 GiB / 1.87 GiB free] Total: 4 [7.47 GiB] / in use: 4 [7.47 GiB] / in no VG: 0 [0 ] [root@localhost ~]# pvmove dev/sdb4 #在使用缩减vgreduce之前,首先移动PE,然后再缩减 No data to move for myvg [root@localhost ~]# vgreduce myvg /dev/sdb4 #将物理卷4移除myvg卷组 Removed "/dev/sdb4" from volume group "myvg" [root@localhost ~]# pvscan #再次查看/dev/sdb4已经被移除 PV /dev/sdb1 VG myvg lvm2 [1.87 GiB / 0 free] PV /dev/sdb2 VG myvg lvm2 [1.87 GiB / 0 free] PV /dev/sdb3 VG myvg lvm2 [1.87 GiB / 616.00 MiB free] PV /dev/sdb4 lvm2 [1.87 GiB] #已经被移除 Total: 4 [7.47 GiB] / in use: 3 [5.60 GiB] / in no VG: 1 [1.87 GiB] [root@localhost ~]# vgdisplay --- Volume group --- VG Name myvg System ID Format lvm2 Metadata Areas 3 Metadata Sequence No 5 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 5.60 GiB #myvg的大小由7.47G到现在的5.60G PE Size 4.00 MiB Total PE 1434 Alloc PE / Size 1280 / 5.00 GiB Free PE / Size 154 / 616.00 MiB VG UUID Aeyfd0-9GUH-GHNg-G5qR-wN0T-r14b-ojIWJ2 2、减小逻辑卷(lv) #首先查看逻辑卷的大小 [root@localhost ~]# df -TH 文件系统 类型 容量 已用 可用 已用%% 挂载点 /dev/sda3 ext4 40G 1.4G 37G 4% / tmpfs tmpfs 519M 0 519M 0% /dev/shm /dev/sda1 ext4 204M 27M 167M 14% /boot /dev/mapper/myvg-mydata ext4 4.3G 143M 3.9G 4% /mysql #大小为4.3G [root@localhost ~]# umount /mysql/ #注释:减小逻辑卷时必须卸载 [root@localhost ~]# resize2fs -f /dev/myvg/mydata 3G #改变逻辑卷的大小3G resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/myvg/mydata to 786432 (4k) blocks. The filesystem on /dev/myvg/mydata is now 786432 blocks long. [root@localhost ~]# lvreduce -L 3G /dev/myvg/mydata #将逻辑卷mydata大小调为3G WARNING: Reducing active logical volume to 3.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce mydata? [y/n]: y Reducing logical volume mydata to 3.00 GiB Logical volume mydata successfully resized [root@localhost ~]# mount -a #再次挂载 [root@localhost ~]# df -TH 文件系统 类型 容量 已用 可用 已用%% 挂载点 /dev/sda3 ext4 40G 1.4G 37G 4% / tmpfs tmpfs 519M 0 519M 0% /dev/shm /dev/sda1 ext4 204M 27M 167M 14% /boot /dev/mapper/myvg-mydata ext4 3.2G 141M 2.9G 5% /mysql #大小为3.2G 3、减小物理卷(pv) [root@localhost ~]# pvs PV VG Fmt Attr PSize PFree /dev/sdb1 myvg lvm2 a-- 1.87g 1.87g /dev/sdb2 myvg lvm2 a-- 1.87g 1.87g /dev/sdb3 lvm2 a-- 1.87g 1.87g [root@localhost ~]# pvremove /dev/sdb3 Labels on physical volume "/dev/sdb3" successfully wiped [root@localhost ~]# pvs PV VG Fmt Attr PSize PFree /dev/sdb1 myvg lvm2 a-- 1.87g 1.87g /dev/sdb2 myvg lvm2 a-- 1.87g 1.87g 九、创建逻辑卷快照 后面练习题补充...... 十、删除卷组(vg),删除物理卷(pv),删除逻辑卷(lv) [root@localhost ~]# umount /dev/myvg/mydata #下载挂载目录 [root@localhost ~]# lvscan #查看逻辑卷 ACTIVE Original ‘/dev/myvg/mydata‘ [3.00 GiB] inherit ACTIVE Snapshot ‘/dev/myvg/sanp‘ [1.00 GiB] inherit [root@localhost ~]# lvremove /dev/myvg/mydata #移除逻辑卷|卸载所有目录 Do you really want to remove active logical volume sanp? [y/n]: y Logical volume "sanp" successfully removed Do you really want to remove active logical volume mydata? [y/n]: y Logical volume "mydata" successfully removed [root@localhost ~]# vgscan #查看卷组 Reading all physical volumes. This may take a while... Found volume group "myvg" using metadata type lvm2 [root@localhost ~]# vgremove myvg #移除卷组 Volume group "myvg" successfully removed [root@localhost ~]# pvscan #查看物理卷 PV /dev/sdb1 lvm2 [1.87 GiB] PV /dev/sdb2 lvm2 [1.87 GiB] PV /dev/sdb3 lvm2 [1.87 GiB] PV /dev/sdb4 lvm2 [1.87 GiB] Total: 4 [7.48 GiB] / in use: 0 [0 ] / in no VG: 4 [7.48 GiB] [root@localhost ~]# pvremove /dev/sdb{1,2,3,4} #移除物理卷 Labels on physical volume "/dev/sdb1" successfully wiped Labels on physical volume "/dev/sdb2" successfully wiped Labels on physical volume "/dev/sdb3" successfully wiped Labels on physical volume "/dev/sdb4" successfully wiped 十一、在lvm逻辑卷中常用的命令 pvcreate --建物理分区建成pv pvscan --显示具有pv的磁盘 pvdisplay --列出系统上的pv pvremove --删除pv属性 vgcreate --创建vg命令 vgscan --查找系统中是否有vg存在 vgdisplay --目前系统vg的状态 vgextend --在vg中增加pv vgremove --删除一个vg vgchange --设置vg是否启动 vgreduce --在vg中删除pv lvcreate --创建lv lvscan --查询系统上的lv lvdisplay --显示系统上的lv状态 lvextend --在lv中增加容量 lvreduce --在lv中减少容量 lvremove --删除lv lvresize --对lv的容量进行调整
练习
一、扩展逻辑卷 注意:没有风险。可以在线扩展无需卸载 1、先扩展物理边界 lvextend -L [+]# /PATH/TO/LV 2、在扩展逻辑边界 resize2fs -p /PATH/TO/LV 二、缩减逻辑卷 注意:不能在线缩减,得先卸载; 确保缩减后的空间大小依然能存储原有的所有数据; 在缩减之前应该先强行检查文件系统,以确保文件系统处于一致性状态。 1、先缩减逻辑边界 resize2fs /PATH/TO/LV 3G (缩减到3G) 2、在缩减物理边界 lvreduce -L [-]# /PATH/TO/LV 3、重新挂在 三、快照卷 通过它能够访问某一时刻的数据 1、生命周期为整个数据时长,在这段时长内,数据的增长量不鞥产出快照卷大小;放弃所有操作,自我损毁; 建议跟原有数据大小一样 2、快照卷只读的 3、跟原卷在同一卷组内 lvcreate -s(指定为快照卷) -p r|w (指定权限) lvcreate -L # -n SLV_NAME -p r /PATH/TO/LV lvextend:扩展物理边界 resize2fs:扩展逻辑边界 resize2fs /PATH/TO/LV 5G(扩展到多大) resize2fs -p /PATH/TO/LV (-p 能有多大扩展到多大) [root@localhost ~]# pvcreate /dev/sdb{1,2,3} Physical volume "/dev/sdb1" successfully created Physical volume "/dev/sdb2" successfully created Physical volume "/dev/sdb3" successfully created [root@localhost ~]# vgcreate myvg /dev/sdb{1,2,3} Volume group "myvg" successfully created [root@localhost ~]# lvcreate -n mydata -L 2G myvg Logical volume "mydata" created [root@localhost ~]# mkfs.ext mkfs.ext2 mkfs.ext3 mkfs.ext4 mkfs.ext4dev [root@localhost ~]# mkfs.ext4 /dev/myvg/mydata mke2fs 1.41.12 (17-May-2010) 文件系统标签= 操作系统:Linux 块大小=4096 (log=2) 分块大小=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 131072 inodes, 524288 blocks 26214 blocks (5.00%) reserved for the super user 第一个数据块=0 Maximum filesystem blocks=536870912 16 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912 正在写入inode表: 完成 Creating journal (16384 blocks): 完成 Writing superblocks and filesystem accounting information: 完成 This filesystem will be automatically checked every 22 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override [root@localhost ~]# mount /dev/myvg/mydata /mysql [root@localhost ~]# cd /mysql/ [root@localhost mysql]# ls lost+found [root@localhost mysql]# cp /etc/inittab . [root@localhost mysql]# ls inittab lost+found [root@localhost mysql]# df -lh #现在是2G,想把它扩展到5G 文件系统 容量 已用 可用 已用%% 挂载点 /dev/sda3 38G 1.3G 35G 4% / tmpfs 495M 0 495M 0% /dev/shm /dev/sda1 194M 26M 159M 14% /boot /dev/mapper/myvg-mydata 2.0G 68M 1.9G 4% /mysql 练习:一、扩展逻辑卷 把现在2G的逻辑卷扩展到5G 思考:想把逻辑卷从2G扩展到5G,首先你的卷组myvg的大小要大于5G,否则扩展没有意义;在线扩展不需要卸载,风险很小 1、查看卷组的大小为5.6G 可以 [root@localhost ~]# vgs VG #PV #LV #SN Attr VSize VFree myvg 3 1 0 wz--n- 5.60g 3.60g 2、查看逻辑卷磁盘大小,仍然是2G 大小没有变化 [root@localhost mysql]# df -lh 文件系统 容量 已用 可用 已用%% 挂载点 /dev/sda3 38G 1.3G 35G 4% / tmpfs 495M 0 495M 0% /dev/shm /dev/sda1 194M 26M 159M 14% /boot /dev/mapper/myvg-mydata 2.0G 68M 1.9G 4% /mysql [root@localhost mysql]# lvs LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert mydata myvg -wi-ao---- 5.00g 由以上可以得到答案,我们只扩展了物理边界没有扩展逻辑编辑,接下来就是扩展逻辑边界 3、扩展逻辑边界 [root@localhost mysql]# df -lh 文件系统 容量 已用 可用 已用%% 挂载点 /dev/sda3 38G 1.3G 35G 4% / tmpfs 495M 0 495M 0% /dev/shm /dev/sda1 194M 26M 159M 14% /boot /dev/mapper/myvg-mydata 2.0G 68M 1.9G 4% /mysql [root@localhost mysql]# resize2fs -p /dev/myvg/mydata #参数-p 是指有多大边界扩展多大,不需要手动指定 resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/myvg/mydata is mounted on /mysql; on-line resizing required old desc_blocks = 1, new_desc_blocks = 1 Performing an on-line resize of /dev/myvg/mydata to 1310720 (4k) blocks. The filesystem on /dev/myvg/mydata is now 1310720 blocks long. [root@localhost mysql]# df -lh #扩展到5G没有问题 文件系统 容量 已用 可用 已用%% 挂载点 /dev/sda3 38G 1.3G 35G 4% / tmpfs 495M 0 495M 0% /dev/shm /dev/sda1 194M 26M 159M 14% /boot /dev/mapper/myvg-mydata 5.0G 68M 4.7G 2% /mysql 练习:二、缩减逻辑卷 想把以上5G的逻辑卷缩减到3G 1、先检查逻辑卷使用的空间 68M [root@localhost ~]# df -lh 文件系统 容量 已用 可用 已用%% 挂载点 /dev/sda3 38G 1.3G 35G 4% / tmpfs 495M 0 495M 0% /dev/shm /dev/sda1 194M 26M 159M 14% /boot /dev/mapper/myvg-mydata 5.0G 68M 4.7G 2% /mysql 2、卸载并强行文件系统检测 [root@localhost ~]# umount /mysql/ [root@localhost ~]# mount /dev/sda3 on / type ext4 (rw) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) devpts on /dev/pts type devpts (rw,gid=5,mode=620) tmpfs on /dev/shm type tmpfs (rw) /dev/sda1 on /boot type ext4 (rw) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) [root@localhost ~]# e2fsck -f /dev/myvg/mydata e2fsck 1.41.12 (17-May-2010) 第一步: 检查inode,块,和大小 第二步: 检查目录结构 第3步: 检查目录连接性 Pass 4: Checking reference counts 第5步: 检查簇概要信息 /dev/myvg/mydata: 12/327680 files (0.0% non-contiguous), 37983/1310720 blocks 3、缩减逻辑边界和物理边界 [root@localhost ~]# resize2fs /dev/myvg/mydata 3G resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/myvg/mydata to 786432 (4k) blocks. The filesystem on /dev/myvg/mydata is now 786432 blocks long. [root@localhost ~]# lvreduce -L 3G /dev/myvg/mydata WARNING: Reducing active logical volume to 3.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce mydata? [y/n]: y 提示:有可能损坏数据 责任自负 Reducing logical volume mydata to 3.00 GiB Logical volume mydata successfully resized [root@localhost ~]# mount -a [root@localhost ~]# mount /dev/sda3 on / type ext4 (rw) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) devpts on /dev/pts type devpts (rw,gid=5,mode=620) tmpfs on /dev/shm type tmpfs (rw) /dev/sda1 on /boot type ext4 (rw) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) /dev/mapper/myvg-mydata on /mysql type ext4 (rw) [root@localhost ~]# df -lh 文件系统 容量 已用 可用 已用%% 挂载点 /dev/sda3 38G 1.3G 35G 4% / tmpfs 495M 0 495M 0% /dev/shm /dev/sda1 194M 26M 159M 14% /boot /dev/mapper/myvg-mydata 3.0G 68M 2.8G 3% /mysql 练习:三、快照卷 [root@localhost ~]# lvcreate -L 50M -n mydata-snap -s -p r /dev/myvg/mydata Rounding up size to full physical extent 52.00 MiB Logical volume "mydata-snmp" created [root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert mydata myvg owi-aos--- 3.00g mydata-snap myvg sri-a-s--- 52.00m mydata 0.02 [root@localhost ~]# mount /dev/myvg/mydata-snap /mnt/ mount: block device /dev/mapper/myvg-mydata--snap is write-protected, mounting read-only [root@localhost ~]# ll /mnt/ 总用量 20 -rw-r--r-- 1 root root 884 8月 24 12:07 inittab drwx------ 2 root root 16384 8月 24 12:04 lost+found [root@localhost ~]# cd /mnt/ [root@localhost mnt]# cat inittab |wc -l 26 接下来测试 首先在原卷中清空所有数据 [root@localhost ~]# cd /mysql/ [root@localhost mysql]# cat inittab |wc -l 26 [root@localhost mysql]# sed -i ‘N;$d;d‘ inittab [root@localhost mysql]# cat inittab |wc -l 0 然后再快照卷中查看 [root@localhost ~]# cd /mnt/ [root@localhost mnt]# cat inittab |wc -l 26 结论:快照创建以后,所有原卷中的改变,不会影响快照中所访问数据的本身 这个时候对快照卷中的数据进行备份 [root@localhost ~]# tar jcf /root/inittab.tar.bz2 /mnt/inittab tar: 从成员名中删除开头的“/” [root@localhost ~]# ll /root/ 总用量 4 -rw-r--r-- 1 root root 619 8月 24 12:51 inittab.tar.bz2 快照卷数据备份之后就可以卸载快照卷,删除快照卷了 [root@localhost ~]# umount /mnt/ [root@localhost ~]# lvremove /dev/myvg/mydata-snap Do you really want to remove active logical volume mydata-snmp? [y/n]: y Logical volume "mydata-snap" successfully removed 结论:快照卷的生命周期就是用来做备份的,当做完备份,那么快照卷就没有意义了。 假设 不小心把原卷中的数据删除了,这个时候就可以利用备份快照中的数据就行恢复 [root@localhost ~]# cd /mysql/ [root@localhost mysql]# ls inittab lost+found [root@localhost mysql]# rm -rf inittab [root@localhost mysql]# ls lost+found [root@localhost mysql]# tar xf /root/inittab.tar.bz2 -C . [root@localhost mysql]# ls lost+found mnt [root@localhost mysql]# ll mnt/ 总用量 4 -rw-r--r-- 1 root root 884 8月 24 12:07 inittab [root@localhost mysql]# cat mnt/inittab |wc -l 26 快速演示一遍 [root@localhost ~]# cd /mysql/ [root@localhost mysql]# cp /etc/inittab . [root@localhost mysql]# cat inittab |wc -l 26 [root@localhost mysql]# cd [root@localhost ~]# lvcreate -L 50M -s mydata-snap -p r /dev/myvg/mydata ^C [root@localhost ~]# cd /mysql/ [root@localhost mysql]# cp /etc/issue . [root@localhost mysql]# cat issue |wc -l 3 [root@localhost mysql]# cd [root@localhost ~]# clear [root@localhost ~]# [root@localhost ~]# [root@localhost ~]# lvcreate -s -L 50M -p r -n mydata-snap /dev/myvg/mydata Rounding up size to full physical extent 52.00 MiB Logical volume "mydata-snap" created [root@localhost ~]# mount /dev/myvg/mydata-snap /mnt/ mount: block device /dev/mapper/myvg-mydata--snap is write-protected, mounting read-only [root@localhost ~]# cd /mnt/ [root@localhost mnt]# ls inittab issue lost+found [root@localhost mnt]# tar jcf /root/mnt.tar.bz2 inittab issue [root@localhost mnt]# cd [root@localhost ~]# ls mnt.tar.bz2 [root@localhost ~]# umount /mnt/ [root@localhost ~]# lvremove /dev/myvg/mydata-snap Do you really want to remove active logical volume mydata-snap? [y/n]: y Logical volume "mydata-snap" successfully removed [root@localhost ~]# [root@localhost ~]# [root@localhost ~]# cd /mysql/ [root@localhost mysql]# rm -rf inittab issue [root@localhost mysql]# ls lost+found [root@localhost mysql]# tar xf /root/mnt.tar.bz2 -C . [root@localhost mysql]# ls inittab issue lost+found
这篇博文纯属意外,本来今天晚上是学习hadoop生态系统的!谁知道半夜11点半的时候,外面却下起了小雨,导致宿舍的网上不去了!上不去网学习hadoop就没有戏了,我这人也是闲不下来,于是就想温习下lvm ;因为lvm在工作中也常常用到!比如 我们公司几乎用的都是云主机,本地数据盘也就200G左右,经常需要挂在存储,但是云主机的存储每次挂载最大的也就2T 一台云主机最多挂在4次,也就是8T的存储,为了便于管理这8T的存储,经常性需要将这8T的存储做成lvm便于管理,使其动态的增缩!
本文出自 “郑彦生” 博客,请务必保留此出处http://467754239.blog.51cto.com/4878013/1543956
原文地址:http://467754239.blog.51cto.com/4878013/1543956