首页 > 代码库 > Linux之lvm管理
Linux之lvm管理
一、简介
LVM,即Logical Volume Manager(逻辑卷管理)的缩写,是Linux环境下对磁盘分区进行管理的一种机制。
二、版本
LVM1:最初在1998年发布,在Linux内核2.4版本上实现。
LVM2:LVM1的升级版,在Linux内核2.6版本上可用。
[root@wsh mysh]# uname -r #查看内核版本 2.6.32-431.el6.x86_64 [root@wsh mysh]# rpm -qa | grep lvm #查看是否安装lvm lvm2-2.02.100-8.el6.x86_64
三、LVM的模型:
由上图可知,lv的创建过程:
磁盘上的若干分区(/dev/[sh]d[a-z]#)创建为物理卷(physical volume),将多个物理卷组合成卷组(volume group),在卷组上创建若干逻辑卷(logical volume)。
四、lvm2的功能:
1.便捷的调整文件系统的大小;
2.支持快照
五、具体操作
1.查看系统分区
[root@wsh mysh]# fdisk -l #查看当前系统的分区情况 Disk /dev/sda: 128.8 GB, 128849018880 bytes 255 heads, 63 sectors/track, 15665 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: 0x0005e677 Device Boot Start End Blocks Id System /dev/sda1 * 1 13 102400 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 13 6387 51200000 83 Linux /dev/sda3 6387 6910 4194304 82 Linux swap / Solaris #下面我们以/dev/sdb和/dev/sdc这两块磁盘为例进行演示: Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 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: 0x896b252c Device Boot Start End Blocks Id System Disk /dev/sdc: 53.7 GB, 53687091200 bytes 255 heads, 63 sectors/track, 6527 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: 0xf7ef9915 Device Boot Start End Blocks Id System
2.创建分区:
[root@wsh mysh]# fdisk /dev/sdb #在/dev/sdb上创建分区 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: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 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: 0x896b252c 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 #选择1号分区 First cylinder (1-2610, default 1): #默认从第1个柱面开始 Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +5G #创建5G大小的分区 Command (m for help): n #再创建分区 Command action e extended p primary partition (1-4) p #创建主分区 Partition number (1-4): 2 #选择2号分区 First cylinder (655-2610, default 655): #默认从655柱面开始 Using default value 655 Last cylinder, +cylinders or +size{K,M,G} (655-2610, default 2610): +5G #创建5G大小的分区 Command (m for help): t #修改分区的systemID Partition number (1-4): 1 #选择修改1号分区的systemID Hex code (type L to list codes): l #小写的L查看对应的格式的systemID 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对应的systemID 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): p Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 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: 0x896b252c Device Boot Start End Blocks Id System /dev/sdb1 1 654 5253223+ 8e Linux LVM /dev/sdb2 655 1308 5253255 8e Linux LVM Command (m for help): w #保存分区 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
[root@wsh mysh]# fdisk /dev/sdc 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): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-6527, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-6527, default 6527): +5G Command (m for help): t Selected partition 1 Hex code (type L to list codes): 8e Changed system type of partition 1 to 8e (Linux LVM) Command (m for help): p Disk /dev/sdc: 53.7 GB, 53687091200 bytes 255 heads, 63 sectors/track, 6527 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: 0xf7ef9915 Device Boot Start End Blocks Id System /dev/sdc1 1 654 5253223+ 8e Linux LVM Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
3.让内核重新探测分区信息
[root@wsh mysh]# partx -a /dev/sdb #让内和重新探测系统分区信息 BLKPG: Device or resource busy #会出现报错信息 error adding partition 1 BLKPG: Device or resource busy error adding partition 2 [root@wsh mysh]# partx -a /dev/sdc BLKPG: Device or resource busy error adding partition 1 [root@wsh mysh]# cat /proc/partitions #查看内核识别到的分区 major minor #blocks name 8 0 125829120 sda 8 1 102400 sda1 8 2 51200000 sda2 8 3 4194304 sda3 8 16 20971520 sdb 8 17 5253223 sdb1 #可以看出/dev/sdb{1,2}已经被识别 8 18 5253255 sdb2 8 32 52428800 sdc 8 33 5253223 sdc1 #/dev/sdc也被识别
4.创建pv:
[root@wsh mysh]# pvcreate /dev/sd{b,c}1 #pvcreate命令将此两个分区创建为pv Physical volume "/dev/sdb1" successfully created Physical volume "/dev/sdc1" successfully created [root@wsh mysh]# pvs #显示pv PV VG Fmt Attr PSize PFree /dev/sdb1 lvm2 a-- 5.01g 5.01g /dev/sdc1 lvm2 a-- 5.01g 5.01g [root@wsh ~]# pvdisplay #pvdisplay可以显示所有pv的详细信息 "/dev/sdb1" is a new physical volume of "5.01 GiB" --- NEW Physical volume --- PV Name /dev/sdb1 VG Name PV Size 5.01 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID 4cAb4A-pt3Q-ffxt-AFsX-fzPZ-ieqe-0PW3Ja "/dev/sdc1" is a new physical volume of "5.01 GiB" --- NEW Physical volume --- PV Name /dev/sdc1 VG Name PV Size 5.01 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID ZqkCHt-fcxy-2eJa-uEcu-KY62-Ztpe-K0H1j1 [root@wsh ~]# pvdisplay /dev/sdb1 #pvdisplay /path/to/pv可以显示指定pv的详细信息 "/dev/sdb1" is a new physical volume of "5.01 GiB" --- NEW Physical volume --- PV Name /dev/sdb1 VG Name PV Size 5.01 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID 4cAb4A-pt3Q-ffxt-AFsX-fzPZ-ieqe-0PW3Ja
5.创建卷组VG
[root@wsh ~]# vgcreate myvg /dev/sd{b,c}1 #用vgcreate创建名为myvg的卷组,并将/dev/sdb1,/dev/sdc1加入此卷组 Volume group "myvg" successfully created [root@wsh ~]# vgs #vgs查看vg的简要信息 VG #PV #LV #SN Attr VSize VFree myvg 2 0 0 wz--n- 10.02g 10.02g #两个5G的PV创建为一个10G的VG [root@wsh ~]# vgdisplay #查看全部vg的详细信息,vgdisplay VG_NAME可以查看某个vg的详细信息 --- Volume group --- VG Name myvg System ID Format lvm2 Metadata Areas 2 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 2 Act PV 2 VG Size 10.02 GiB PE Size 4.00 MiB #在使用vgcreate创建vg时可以使用-s PE选项指定PE的大小,默认为4MB。当后面创建LV时,其大小肯定为PE的整数倍。 Total PE 2564 Alloc PE / Size 0 / 0 Free PE / Size 2564 / 10.02 GiB VG UUID BlbIst-Nkwf-Ipu4-TZFr-0U8L-jfNz-wcLlP
6.创建逻辑卷LV
[root@wsh ~]# lvcreate -L 7G -n lv1 myvg #创建大小为7G,名为lv1的LV,也可以-l指定PE的个数 Logical volume "lv1" created #显示简要的LV信息 [root@wsh ~]# lvs LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert lv1 myvg -wi-a----- 7.00g [root@wsh ~]# lvdisplay #显示全部的LV详细信息 --- Logical volume --- LV Path /dev/myvg/lv1 LV Name lv1 VG Name myvg LV UUID cf3Y8v-PZES-BHal-mLen-EkC7-zAXN-qsMXs9 LV Write Access read/write LV Creation host, time wsh.example.com, 2014-05-19 21:16:43 +0800 LV Status available # open 0 LV Size 7.00 GiB Current LE 1792 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0
7.逻辑卷设备文件路径:
[root@wsh ~]# ls /dev/mapper/myvg-lv1 #/dev/mapper/VG_NAME-lv_name /dev/mapper/myvg-lv1 [root@wsh ~]# ls /dev/myvg/lv1 #/dev/VG_NAME/lv_name /dev/myvg/lv1
8.创建文件系统
[root@wsh ~]# mke2fs -t ext4 /dev/mapper/myvg-lv1 #必须使用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 458752 inodes, 1835008 blocks 91750 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=1879048192 56 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 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 26 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
9.挂载文件系统
[root@wsh ~]# mount /dev/mapper/myvg-lv1 /mnt/ #挂载至/mnt [root@wsh ~]# df -lh #查看lv1大小为7G Filesystem Size Used Avail Use% Mounted on /dev/sda2 49G 3.6G 43G 8% / tmpfs 495M 0 495M 0% /dev/shm /dev/sda1 97M 30M 63M 32% /boot /dev/mapper/myvg-lv1 6.9G 144M 6.4G 3% /mnt
10.扩展lv1的大小:
[root@wsh ~]# vgs #查看VG还有3G未使用 VG #PV #LV #SN Attr VSize VFree myvg 2 1 0 wz--n- 10.02g 3.02g [root@wsh ~]# lvextend -L 10G /dev/mapper/myvg-lv1 #将lv1的物理边界扩展到10G Extending logical volume lv1 to 10.00 GiB Logical volume lv1 successfully resized [root@wsh ~]# lvs #lv1的物理边界为10G LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert lv1 myvg -wi-ao---- 10.00g [root@wsh ~]# df -lh #此时,lv1的逻辑边界依然为7G Filesystem Size Used Avail Use% Mounted on /dev/sda2 49G 3.6G 43G 8% / tmpfs 495M 0 495M 0% /dev/shm /dev/sda1 97M 30M 63M 32% /boot /dev/mapper/myvg-lv1 6.9G 144M 6.4G 3% /mnt [root@wsh ~]# umount /dev/mapper/myvg-lv1 #卸载挂载的lv1 [root@wsh ~]# e2fsck -f /dev/mapper/myvg-lv1 #强行检测lv1文件系统 e2fsck 1.41.12 (17-May-2010) 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/mapper/myvg-lv1: 11/458752 files (0.0% non-contiguous), 65599/1835008 blocks [root@wsh ~]# resize2fs /dev/mapper/myvg-lv1 #使用resize2fs将lv1的逻辑边界扩展到10G,如果没有指定扩展到多大,则其逻辑边界自动适应其物理边界 resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/mapper/myvg-lv1 to 2621440 (4k) blocks. The filesystem on /dev/mapper/myvg-lv1 is now 2621440 blocks long. [root@wsh ~]# mount /dev/mapper/myvg-lv1 /mnt/ #挂载 [root@wsh ~]# df -lh #查看其大小已为10G Filesystem Size Used Avail Use% Mounted on /dev/sda2 49G 3.6G 43G 8% / tmpfs 495M 0 495M 0% /dev/shm /dev/sda1 97M 30M 63M 32% /boot /dev/mapper/myvg-lv1 9.9G 144M 9.3G 2% /mnt
11.扩展VG大小,并扩展LV大小:
#由于VG的空间已被全部占用,如果还需扩展LV的大小,就需要创建其他PV,并将其加入VG,扩展VG,再扩展LV的物理边界,进而扩展LV的逻辑边界: [root@wsh ~]# pvcreate /dev/sdb2 #将/dev/sdb2创建为PV Physical volume "/dev/sdb2" successfully created [root@wsh ~]# pvs #查看PV PV VG Fmt Attr PSize PFree /dev/sdb1 myvg lvm2 a-- 5.01g 0 /dev/sdb2 lvm2 a-- 5.01g 5.01g /dev/sdc1 myvg lvm2 a-- 5.01g 16.00m [root@wsh ~]# vgextend myvg /dev/sdb2 #将/dev/sdb2加入VG Volume group "myvg" successfully extended [root@wsh ~]# vgs #查看VG,增加了5G VG #PV #LV #SN Attr VSize VFree myvg 3 1 0 wz--n- 15.02g 5.02g [root@wsh ~]# umount /dev/mapper/myvg-lv1 #卸载lv1,为以后扩展作准备 [root@wsh ~]# lvextend -L 15G /dev/mapper/myvg-lv1 #扩展lv1的物理边界 Extending logical volume lv1 to 15.00 GiB Logical volume lv1 successfully resized [root@wsh ~]# lvs #查看LV,物理边界已扩展至15G LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert lv1 myvg -wi-a----- 15.00g [root@wsh ~]# e2fsck -f /dev/mapper/myvg-lv1 #强行检测lv1的文件系统 e2fsck 1.41.12 (17-May-2010) 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/mapper/myvg-lv1: 11/655360 files (0.0% non-contiguous), 77935/2621440 blocks [root@wsh ~]# resize2fs /dev/mapper/myvg-lv1 #上面检测无问题,扩展其逻辑边界至物理边界 resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/mapper/myvg-lv1 to 3932160 (4k) blocks. The filesystem on /dev/mapper/myvg-lv1 is now 3932160 blocks long. [root@wsh ~]# mount /dev/mapper/myvg-lv1 /mnt/ #挂载 [root@wsh ~]# fd -lh #查看其大小,已扩展为15G -bash: fd: command not found [root@wsh ~]# df -lh Filesystem Size Used Avail Use% Mounted on /dev/sda2 49G 3.6G 43G 8% / tmpfs 495M 0 495M 0% /dev/shm /dev/sda1 97M 30M 63M 32% /boot /dev/mapper/myvg-lv1 15G 146M 14G 2% /mnt
12.缩减LV大小
#缩减LV的前提:至少能够容纳原来的数据 #步骤: # 1.缩减LV的逻辑边界 # 2.缩减LV的物理边界 [root@wsh ~]# cp /etc/fstab /mnt/ #复制fstab至/mnt下 [root@wsh ~]# ls /mnt/ #查看/mnt下文件 fstab lost+found [root@wsh ~]# umount /dev/mapper/myvg-lv1 #卸载/lv1 [root@wsh ~]# e2fsck -f /dev/mapper/myvg-lv1 #强行检测lv1文件系统 e2fsck 1.41.12 (17-May-2010) 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/mapper/myvg-lv1: 12/983040 files (0.0% non-contiguous), 98945/3932160 blocks [root@wsh ~]# resize2fs /dev/mapper/myvg-lv1 10G #检测无问题,将lv1逻辑边界缩减至10G resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/mapper/myvg-lv1 to 2621440 (4k) blocks. The filesystem on /dev/mapper/myvg-lv1 is now 2621440 blocks long. [root@wsh ~]# mount /dev/mapper/myvg-lv1 /mnt/ #挂载lv1,大小为10G [root@wsh ~]# df -lh Filesystem Size Used Avail Use% Mounted on /dev/sda2 49G 3.6G 43G 8% / tmpfs 495M 0 495M 0% /dev/shm /dev/sda1 97M 30M 63M 32% /boot /dev/mapper/myvg-lv1 9.9G 144M 9.3G 2% /mnt [root@wsh ~]# lvs #查看lv1物理边界,依然为15G LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert lv1 myvg -wi-ao---- 15.00g [root@wsh ~]# lvreduce -L 10G /dev/mapper/myvg-lv1 #缩减lv1的物理边界至10G WARNING: Reducing active and open logical volume to 10.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce lv1? [y/n]: y #提示是否确认缩减 Reducing logical volume lv1 to 10.00 GiB Logical volume lv1 successfully resized [root@wsh ~]# lvs #lv1物理边界已缩减至10G,至此缩减完成 LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert lv1 myvg -wi-ao---- 10.00g [root@wsh ~]# cat /mnt/fstab #查看fstab文件正常 # # /etc/fstab # Created by anaconda on Wed Apr 30 18:22:17 2014 # # Accessible filesystems, by reference, are maintained under ‘/dev/disk‘ # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=6229f8ea-4270-43c5-b5cf-0df08f0613e6 / ext4 defaults 1 1 UUID=9cded63a-de55-49d5-9bf5-3a023619dc22 /boot ext4 defaults 1 2 UUID=97c1673e-18b8-4d3f-b443-14b3074e51f1 swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 /dev/myvg/lv1 /users ext4 defaults 0 0
13.缩减VG
# 1.确认要移除的pv
# 2.将该pv中的数据转移至同组vg的其他pv中
# 3.从vg中移除该pv,实现vg的缩减
#下面以移除/dev/sdb2为例:
[root@wsh ~]# pvs #查看/dev/sdb2剩余空间为0 PV VG Fmt Attr PSize PFree /dev/sdb1 myvg lvm2 a-- 5.01g 5.01g /dev/sdb2 myvg lvm2 a-- 5.01g 0 /dev/sdc1 myvg lvm2 a-- 5.01g 4.00m [root@wsh ~]# pvmove /dev/sdb2 #转移/dev/sdb2的数据 /dev/sdb2: Moved: 0.2% /dev/sdb2: Moved: 12.8% /dev/sdb2: Moved: 25.9% /dev/sdb2: Moved: 39.0% /dev/sdb2: Moved: 52.0% /dev/sdb2: Moved: 65.6% /dev/sdb2: Moved: 78.9% /dev/sdb2: Moved: 91.6% /dev/sdb2: Moved: 100.0% [root@wsh ~]# pvs #查看数据已全部转移 PV VG Fmt Attr PSize PFree /dev/sdb1 myvg lvm2 a-- 5.01g 0 /dev/sdb2 myvg lvm2 a-- 5.01g 5.01g /dev/sdc1 myvg lvm2 a-- 5.01g 4.00m [root@wsh ~]# vgs VG #PV #LV #SN Attr VSize VFree myvg 3 2 1 wz--n- 15.02g 5.01g [root@wsh ~]# vgreduce myvg /dev/sdb2 #移除/dev/sdb2 Removed "/dev/sdb2" from volume group "myvg" [root@wsh ~]# vgs #VG已成功缩减 VG #PV #LV #SN Attr VSize VFree myvg 2 2 1 wz--n- 10.02g 4.00m [root@wsh ~]# mount /dev/mapper/myvg-lv1 /mnt/ [root@wsh ~]# cat /mnt/fstab #文件访问正常 # # /etc/fstab # Created by anaconda on Wed Apr 30 18:22:17 2014 # # Accessible filesystems, by reference, are maintained under ‘/dev/disk‘ # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=6229f8ea-4270-43c5-b5cf-0df08f0613e6 / ext4 defaults 1 1 UUID=9cded63a-de55-49d5-9bf5-3a023619dc22 /boot ext4 defaults 1 2 UUID=97c1673e-18b8-4d3f-b443-14b3074e51f1 swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 /dev/myvg/lv1 /users ext4 defaults 0 0
14.删除不用的PV
[root@wsh ~]# pvs #查看所有pv,/dev/sdb2依然存在 PV VG Fmt Attr PSize PFree /dev/sdb1 myvg lvm2 a-- 5.01g 0 /dev/sdb2 lvm2 a-- 5.01g 5.01g /dev/sdc1 myvg lvm2 a-- 5.01g 4.00m [root@wsh ~]# pvremove /dev/sdb2 #删除该pv Labels on physical volume "/dev/sdb2" successfully wiped [root@wsh ~]# pvs #查看/dev/sdb2已从PV中删除 PV VG Fmt Attr PSize PFree /dev/sdb1 myvg lvm2 a-- 5.01g 0 /dev/sdc1 myvg lvm2 a-- 5.01g 4.00m
15.创建快照卷
#前提: # 1.快照卷和原卷在同一卷组,要确认创建的快照卷大小不能大于卷组的剩余空间 # 2.生命周期为整个数据的时长,在这段时间内,数据的增量不能超过快照卷的大小 # 3.快照卷应该是只读的 [root@wsh ~]# lvcreate -L 10M -s -p r -n lv1-snap /dev/mapper/myvg-lv1 #-s指定创建快照卷,-p r指定快照卷为只读,-n指定快照卷名称 Rounding up size to full physical extent 12.00 MiB Logical volume "lv1-snap" created [root@wsh ~]# lvs #查看LV LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert lv1 myvg owi-aos--- 10.00g lv1-snap myvg sri-a-s--- 12.00m lv1 0.10 [root@wsh ~]# mount -o ro /dev/myvg/lv1-snap /media/ [root@wsh ~]# df -lh #查看挂载的文件系统使用信息 Filesystem Size Used Avail Use% Mounted on /dev/sda2 49G 3.6G 43G 8% / tmpfs 495M 0 495M 0% /dev/shm /dev/sda1 97M 30M 63M 32% /boot /dev/mapper/myvg-lv1 9.9G 144M 9.3G 2% /mnt /dev/mapper/myvg-lv1--snap 9.9G 144M 9.3G 2% /media#创建的10M的快照卷显示的大小和原卷大小一样 [root@wsh ~]# ls /media/ #查看快照卷有哪些文件 fstab lost+found [root@wsh ~]# ls /mnt/ #查看原卷有哪些文件 fstab lost+found [root@wsh ~]# cp /etc/inittab /mnt/ #复制文件至原卷 [root@wsh ~]# ls /mnt/ #查看原卷文件 fstab inittab lost+found [root@wsh ~]# ls /media/ #查看快照卷文件 fstab lost+found
#snapshot简述: # 1.snapshot仅对LVM有效 # 2.用于保证数据状态的一致性,进而对数据进行备份 # 3.简单原理描述:原卷中将要改变的数据,在改变之前复制一份到快照卷,未改变的数据依然存#放在原卷,这样,通过快照卷可以访问改变前的数据,而且如果通过快照访问未改变的数据时,是通#过重定向到原卷访问的(这样通过snapshot就保持了数据改变前的状态的一致性);通过原卷只能访#问改变后的数据和未改变的数据。
#更过详细原理,请参照:http://blog.chinaunix.net/uid-8650524-id-6166.html
16.归档备份lv1数据
#我们先移除lv1-snap,然后演示将快照卷的数据归档压缩备份
[root@wsh /]# umount /dev/myvg/lv1-snap #卸载快照卷 [root@wsh /]# lvremove /dev/myvg/lv1-snap #lvremove命令删除快照卷 Do you really want to remove active logical volume lv1-snap? [y/n]: y Logical volume "lv1-snap" successfully removed [root@wsh /]# cp /etc/{fstab,inittab} /mnt/ #复制文件至原卷 [root@wsh /]# lvcreate -L 10M -s -p r -n lv1-snap /dev/myvg/lv1 #对原卷创建快照 Rounding up size to full physical extent 12.00 MiB Logical volume "lv1-snap" created [root@wsh /]# mount /dev/myvg/lv1-snap /media/ #挂载快照卷至/media mount: block device /dev/mapper/myvg-lv1--snap is write-protected, mounting read-only [root@wsh /]# cd /media/ [root@wsh media]# ls fstab inittab lost+found [root@wsh media]# tar -Jcf /tmp/backup.tar.xz fstab inittab #归档压缩数据做备份 [root@wsh media]# ls /tmp/ | grep "backup.tar.xz" #确认备份数据 backup.tar.xz [root@wsh media]# tar -tf /tmp/backup.tar.xz #备份内容正确 fstab inittab #快照卷中数据备份完成之后,快照卷就可以用lvremove命令删除了。
17.删除不用的lv,vg及pv
[root@wsh /]# lvremove /dev/myvg/lv1 #删除lv Do you really want to remove active logical volume lv1? [y/n]: y Logical volume "lv1" successfully removed [root@wsh /]# lvs [root@wsh /]# vgremove myvg #删除vg Volume group "myvg" successfully removed [root@wsh /]# vgs No volume groups found [root@wsh /]# pvs PV VG Fmt Attr PSize PFree /dev/sdb1 lvm2 a-- 5.01g 5.01g /dev/sdc1 lvm2 a-- 5.01g 5.01g [root@wsh /]# pvremove /dev/sdb1 #删除pv Labels on physical volume "/dev/sdb1" successfully wiped [root@wsh /]# pvremove /dev/sdc1 #删除pv Labels on physical volume "/dev/sdc1" successfully wiped
本文出自 “大灰狼” 博客,请务必保留此出处http://huifei1314.blog.51cto.com/4681402/1413884