大纲
一、简介
二、LVM 模块
三、具体操作
对添加的硬盘进行分区(fdisk /dev/[hs]d[a-z])
对创建的分区创建物理卷(pvcreate)
给逻辑卷创建逻辑容器(卷组)
在卷组创建大小不同的逻辑卷(lvcreate)
给以存在的卷组扩大容量
实现在线扩大LVM容量
实现缩减LVM容量(不支持在线缩减)
减小卷组容量
利用给LVM创建快照,并完成备份并还原数据
一、简介
LVM是 Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制,它由Heinz Mauelshagen在Linux 2.4内核上实现,于1998年发布到Linux社区中,它允许你在Linux系统上用简单的命令行管理一个完整的逻辑卷管理环境。
二、LVM 模块
Physical volume (PV)、Volume group (VG)、Logical volume(LV)、 Physical extent (PE),下面我们用一个简单的图来说明下物理卷、卷组、逻辑卷他们之间的关系(此图只是个人理解,仅供参考)
简而言之:
逻辑卷的创建,就是将多块硬盘创建物理卷,而将这些物理卷以逻辑的形式总成一个容器,然后从这个容器里面创建大小不同的分区文件,而这个容器就是所谓的逻辑卷,而从这个容器里创建大小不同的分区文件,这个分区文件就叫做逻辑卷。三、具体操作
1. 分区
[root@ha-node2 ~]# fdisk -l Disk /dev/sda: 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: 0x000b08b2 Device Boot Start End Blocks Id System /dev/sda1 * 1 26 204800 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 26 281 2048000 82 Linux swap / Solaris Partition 2 does not end on cylinder boundary. /dev/sda3 281 2611 18717696 83 Linux 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: 0x00097443 Device Boot Start End Blocks Id System /dev/sdb1 1 1045 8393931 83 Linux /dev/sdb2 1046 1305 2088450 83 Linux Disk /dev/sdc: 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: 0x00000000 #使用fdisk -l对/dev/sdc进行分区后的结果 [root@ha-node2 ~]# fdisk -l |grep sdc Disk /dev/sdc: 21.5 GB, 21474836480 bytes /dev/sdc1 1 654 5253223+ 8e Linux LVM /dev/sdc2 655 1308 5253255 8e Linux LVM /dev/sdc3 1309 2610 10458315 8e Linux LVM [root@ha-node2 ~]# partprobe /dev/sdc #通知内核重新读取硬盘信息2. 将物理分区与硬盘创建为物理卷(pvcreate)
[root@ha-node2 ~]# pvcreate /dev/sdc[1-3] #将/dev/sdc[1-3全部做成pv] Physical volume "/dev/sdc1" successfully created Physical volume "/dev/sdc2" successfully created Physical volume "/dev/sdc3" successfully created [root@ha-node2 ~]# pvs #查看创建的pv PV VG Fmt Attr PSize PFree /dev/sdc1 lvm2 a-- 5.01g 5.01g /dev/sdc2 lvm2 a-- 5.01g 5.01g /dev/sdc3 lvm2 a-- 9.97g 9.97g #这里显示的是每个pv的大小3. 将物理卷(pv)创建为卷组(vgcreate),名为myvg
[root@ha-node2 ~]# vgcreate myvg /dev/sdc[1-3] #将/dev/sdc[1-3创建成vg] Volume group "myvg" successfully created [root@ha-node2 ~]# vgs #查看vg信息 VG #PV #LV #SN Attr VSize VFree myvg 3 0 0 wz--n- 19.99g 19.99g #显示myvg一共有19.99G的空间 [root@ha-node2 ~]# vgdisplay --- 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 19.99 GiB PE Size 4.00 MiB #物理盘的基本单位:默认4MB Total PE 5117 Alloc PE / Size 0 / 0 Free PE / Size 5117 / 19.99 GiB VG UUID bbGOB5-i8vP-s1AV-twoR-D0XU-UWeF-A6UNMz4. 在卷组里创建逻辑卷并格式化、挂载使用
[root@ha-node2 ~]# lvcreate -L 2G -n mylv1 myvg #在卷组myvg中创建一个大小为2G,名为mylv1的逻辑卷 Logical volume "mylv1" created [root@ha-node2 ~]# lvs #查看逻辑卷 LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert mylv1 myvg -wi-a---- 2.00g [root@ha-node2 ~]# mke2fs -j /dev/myvg/mylv1 #格式化新创建的逻辑卷mylv1 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 131072 inodes, 524288 blocks 26214 blocks (5.00%) reserved for the super user First data block=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 Writing inode tables: done Creating journal (16384 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 32 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@ha-node2 ~]# mkdir /mydata #创建挂载目录 [root@ha-node2 ~]# mount /dev/myvg/mylv1 /mydata/ #将mylv1挂载到/mydata下 [root@ha-node2 ~]# 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-mylv1 on /mydata type ext3 (rw) [root@ha-node2 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 18G 2.6G 14G 16% / tmpfs 495M 0 495M 0% /dev/shm /dev/sda1 190M 48M 133M 27% /boot /dev/mapper/myvg-mylv1 2.0G 68M 1.9G 4% /mydata #查看mylv1的容量5. 发现卷组空间不够,我们扩大卷组空间
[root@ha-node2 ~]# pvcreate /dev/sdb1 #将sdb做成pv Physical volume "/dev/sdb1" successfully created [root@ha-node2 ~]# pvs #查看pv信息 PV VG Fmt Attr PSize PFree /dev/sdb1 lvm2 a-- 10.00g 10.00g /dev/sdc1 myvg lvm2 a-- 5.01g 3.01g /dev/sdc2 myvg lvm2 a-- 5.01g 5.01g /dev/sdc3 myvg lvm2 a-- 9.97g 9.97g [root@ha-node2 ~]# vgextend myvg /dev/sdb1 #对卷组myvg进行扩展 Volume group "myvg" successfully extended [root@ha-node2 ~]# vgs VG #PV #LV #SN Attr VSize VFree myvg 4 1 0 wz--n- 29.98g 27.98g #这里我们能看到myvg的大小从原来的19.99变成27.98G了6. 扩展逻辑卷 (支持在线扩展)
#将/dev/myvg/mylv1 扩展到4G,并且要求数据可以正常访问 首先我们在/mydata下创建一个文件test.txt [root@ha-node2 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 18G 2.6G 14G 16% / tmpfs 495M 0 495M 0% /dev/shm /dev/sda1 190M 48M 133M 27% /boot /dev/mapper/myvg-mylv1 2.0G 68M 1.9G 4% /mydata [root@ha-node2 ~]# touch /mydata/test.txt [root@ha-node2 ~]# ls /mydata/ lost+found test.txt [root@ha-node2 ~]# lvextend -L +2G /dev/myvg/mylv1 #将逻辑卷mylv1扩展到4G Extending logical volume mylv1 to 4.00 GiB Logical volume mylv1 successfully resized [root@ha-node2 ~]# resize2fs -p /dev/myvg/mylv1 #通过resize2fs扩展容量 resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/myvg/mylv1 is mounted on /mydata; on-line resizing required old desc_blocks = 1, new_desc_blocks = 1 Performing an on-line resize of /dev/myvg/mylv1 to 1048576 (4k) blocks. The filesystem on /dev/myvg/mylv1 is now 1048576 blocks long. [root@ha-node2 ~]# lvs LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert mylv1 myvg -wi-ao--- 4.00g #这里能看到逻辑卷mylv1已经成功的扩展到了4G [root@ha-node2 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 18G 2.6G 14G 16% / tmpfs 495M 0 495M 0% /dev/shm /dev/sda1 190M 48M 133M 27% /boot /dev/mapper/myvg-mylv1 4.0G 69M 3.7G 2% /mydata #成功增加了2G [root@ha-node2 ~]# ls /mydata/ #之前创建的文件还存在 lost+found test.txt
7. 缩减逻辑卷
查看逻辑卷使用空间状况
不能在线缩减,得先卸载
确保缩减后的空间大小依然能存储原有的所有数据
在缩减之前应该先强行检查文件,以确保文件系统处于一至性状态
#将/dev/myvg/mylv1 缩减到1G,并且要求数据可以正常访问 (所以我们就按上面的提示在操作) [root@ha-node2 ~]# df -h #查看已使用空间 Filesystem Size Used Avail Use% Mounted on /dev/sda3 18G 2.6G 14G 16% / tmpfs 495M 0 495M 0% /dev/shm /dev/sda1 190M 48M 133M 27% /boot /dev/mapper/myvg-mylv1 4.0G 69M 3.7G 2% /mydata #这里显示只使用了69M [root@ha-node2 ~]# umount /mydata/ #卸载分区 [root@ha-node2 ~]# e2fsck -f /dev/myvg/mylv1 #强制检查文件系统 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/myvg/mylv1: 12/262144 files (0.0% non-contiguous), 33887/1048576 blocks [root@ha-node2 ~]# resize2fs /dev/myvg/mylv1 1G #将逻辑卷缩小到1G,缩减逻辑大小 resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/myvg/mylv1 to 262144 (4k) blocks. The filesystem on /dev/myvg/mylv1 is now 262144 blocks long. [root@ha-node2 ~]# lvs LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert mylv1 myvg -wi-a---- 4.00g #这里还是4G [root@ha-node2 ~]# lvreduce -L 1G /dev/myvg/mylv1 #缩减物理边界大小到1G WARNING: Reducing active logical volume to 1.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce mylv1? [y/n]: y Reducing logical volume mylv1 to 1.00 GiB Logical volume mylv1 successfully resized [root@ha-node2 ~]# lvs LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert mylv1 myvg -wi-a---- 1.00g #已成功缩减逻辑卷mylv1大小到1G [root@ha-node2 ~]# mount /dev/myvg/mylv1 /mydata/ [root@ha-node2 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 18G 2.6G 14G 16% / tmpfs 495M 0 495M 0% /dev/shm /dev/sda1 190M 48M 133M 27% /boot /dev/mapper/myvg-mylv1 1008M 67M 891M 7% /mydata [root@ha-node2 ~]# ls /mydata/ lost+found test.txt8. 缩减磁盘空间
发现物理磁盘空间使用不足,将其中一块硬盘或分区拿掉
pvmove /dev/sdc1 #将/dev/sdc1上存储的数据移到其它物理卷中
vgreduce myvg /dev/sdc1 #将/dev/sdc1从myvg卷组中移除
pvremove /dev/sdc1 #将/dev/sdc1从物理卷上移除
[root@ha-node2 ~]# pvs PV VG Fmt Attr PSize PFree /dev/sdb1 myvg lvm2 a-- 9.99g 9.99g #这里我们将/dev/sdb1拿掉 /dev/sdc1 myvg lvm2 a-- 5.01g 4.01g /dev/sdc2 myvg lvm2 a-- 5.01g 5.01g /dev/sdc3 myvg lvm2 a-- 9.97g 9.97g [root@ha-node2 ~]# pvmove /dev/sdb1 #将/dev/sdb1上的数据移到其他的pv中 No data to move for myvg [root@ha-node2 ~]# vgreduce myvg /dev/sdb1 #将/dev/sdb1从myvg中移除 Removed "/dev/sdb1" from volume group "myvg" [root@ha-node2 ~]# pvremove /dev/sdb1 #将/dev/sdb1从物理卷中移除 Labels on physical volume "/dev/sdb1" successfully wiped [root@ha-node2 ~]# vgs VG #PV #LV #SN Attr VSize VFree myvg 3 1 0 wz--n- 19.99g 18.99g #最后的结果是myvg只有18.99G [root@ha-node2 ~]# pvs PV VG Fmt Attr PSize PFree /dev/sdc1 myvg lvm2 a-- 5.01g 4.01g /dev/sdc2 myvg lvm2 a-- 5.01g 5.01g /dev/sdc3 myvg lvm2 a-- 9.97g 9.97g [root@ha-node2 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 18G 2.6G 14G 16% / tmpfs 495M 0 495M 0% /dev/shm /dev/sda1 190M 48M 133M 27% /boot /dev/mapper/myvg-mylv1 1008M 67M 891M 7% /mydata [root@ha-node2 ~]# ls /mydata/ lost+found test.txt9. 实现快照,进行备份还原
在/mnt/lvm目录上,我们将原始的目录文件进行快照,然后将/mydata目录中的内容清空,并进行还原
[root@ha-node2 ~]# cat /mydata/test.txt lvm [root@ha-node2 ~]# lvcreate -L 30M -n backup -s -p r /dev/myvg/mylv1 #-L 快照大小 �Cn:快照名称 �Cp 权限只读 �Cs 创建快照 Rounding up size to full physical extent 32.00 MiB Logical volume "backup" created [root@ha-node2 ~]# ll /mydata/ total 20 drwx------ 2 root root 16384 Jun 23 15:41 lost+found -rw-r--r-- 1 root root 4 Jun 23 16:12 test.txt [root@ha-node2 ~]# mkdir /tmp/backup #创建挂载目录 [root@ha-node2 ~]# mount /dev/myvg/backup /tmp/backup/ #挂载 mount: block device /dev/mapper/myvg-backup is write-protected, mounting read-only [root@ha-node2 ~]# cd /tmp/backup/ [root@ha-node2 backup]# ls lost+found test.txt [root@ha-node2 backup]# mkdir /tmp/lvmbackup #创建备份目录 [root@ha-node2 backup]# tar zcf /tmp/lvmbackup/test.tar.gz test.txt #将text.txt文件打包 [root@ha-node2 backup]# ll /tmp/lvmbackup/ #查看备份 total 4 -rw-r--r-- 1 root root 124 Jun 23 16:17 test.tar.gz [root@ha-node2 backup]# rm -rf /mydata/* #删除/mydata目录下的所有文件 [root@ha-node2 backup]# ls /mydata/ [root@ha-node2 backup]# tar xf /tmp/lvmbackup/test.tar.gz -C /mydata/ [root@ha-node2 backup]# ls /mydata/ test.txt