LVM配置
推荐相关文章:
LVM的定义
LVM: Logical Volume Manager ,逻辑分卷管理器。,可用于管理磁盘驱动或其他类似的大量存储设备。LVM将一个或多个磁盘的分区在逻辑上集合当做一个磁盘来使用,我们可以根据实际需求增加磁盘,或磁盘分区来扩大空间容量。
LVM基本组成
-
物理存储介质(The physical media):这里指系统的存储设备:硬盘,如:/dev/hda1、/dev/sda等等,是存储系统最低层的存储单元。
-
物理卷(physical volume):物理卷就是指硬盘分区或从逻辑上与磁盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,却包含有与LVM相关的管理参数。
-
卷组(Volume Group):LVM卷组类似于非LVM系统中的物理硬盘,其由物理卷组成。可以在卷组上创建一个或多个“LVM分区”(逻辑卷),LVM卷组由一个或多个物理卷组成。
-
逻辑卷(logical volume):LVM的逻辑卷类似于非LVM系统中的硬盘分区,在逻辑卷之上可以建立文件系统(比如/home或者/usr等)。
-
PE(physical extent):每一个物理卷被划分为称为PE(Physical Extents)的基本单元,具有唯一编号的PE是可以被LVM寻址的最小单元。PE的大小是可配置的,默认为4MB。
-
LE(logical extent):逻辑卷也被划分为被称为LE(Logical Extents) 的可被寻址的基本单位。在同一个卷组中,LE的大小和PE是相同的,并且一一对应。
简单来说就是:
PV:是物理的磁盘分区
VG:LVM中的物理的磁盘分区,也就是PV,必须加入VG,可以将VG理解为一个仓库或者是几个大的硬盘。
LV:也就是从VG中划分的逻辑分区 ,
LVM的配置过程
1.首先我们先创建几个分区,注意要将分区类型设置为Linux LVM
Command (m for help): t #修改命令 Partition number (1-8): 6 Hex code (type L to list codes): 8e #选择分区类型为Linux LVM Changed system type of partition 6 to 8e (Linux LVM) Command (m for help): w # 保存退出 The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) Syncing disks. [root@localhost ~]# partx -a /dev/sda #将修改的磁盘信息更新至内核中 [root@localhost ~]# fdisk -l #查看磁盘信息,已创建成功 Disk /dev/sda: 64.4 GB, 64424509440 bytes 255 heads, 63 sectors/track, 7832 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: 0x00074ab5 Device Boot Start End Blocks Id System /dev/sda1 * 1 39 307200 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 39 4965 39570432 83 Linux /dev/sda3 4965 5222 2064384 82 Linux swap / Solaris /dev/sda4 5222 7832 20967500 5 Extended /dev/sda5 5222 5875 5247866 8e Linux LVM /dev/sda6 5876 6529 5253223+ 8e Linux LVM /dev/sda7 6530 7183 5253223+ 8e Linux LVM /dev/sda8 7184 7832 5213061 8e Linux LVM
2.首先来创建PV,先来认识一些PV的管理工具:
-
pvs: 简要显示pv格式
-
pvdisplay:显示pv的详细信息
-
pvcreate: 创建pv
[root@localhost ~]# pvcreate /dev/sda5 #将/dev/sda5创建为PV
[root@localhost ~]# pvdisplay "/dev/sda5" is a new physical volume of "5.00 GiB" — NEW Physical volume — PV Name /dev/sda5 VG Name
PV Size 5.00 GiB Allocatable NO PE Size 0 #因为还没创建VG,所以此时没有设置PE大小,数量。 Total PE 0 Free PE 0 Allocated PE 0 PV UUID J2ECPI-K90e-vKoH-rCYC-IM11-16W1-fFh7f
此时我们PV就已经创建好了!
3.接下来就开始介绍如何创建VG了,先来介绍一下VG的管理工具
-
vgs:简要显示VG信息
-
vgdisplay:显示VG详细信息
-
vgcreate -s PE_size /dev/sda#: 创建VG,-s指定PE大小。
[root@localhost ~]# vgcreate -s 8M myvg /dev/sda5 Volume group "myvg" successfully created [root@localhost ~]# vgdisplay myvg --- Volume group --- VG Name myvg System ID Format lvm2 Metadata Areas 1 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 1 Act PV 1 VG Size 5.00 GiB PE Size 8.00 MiB Total PE 640 Alloc PE / Size 0 / 0 Free PE / Size 640 / 5.00 GiB VG UUID DRLdb2-BawM-vO1R-3THE-4h6L-8b12-jxLCX4
此时PV中的PE大小确定,数量也确定下来了。
[root@localhost ~]# pvdisplay /dev/sda5 --- Physical volume --- PV Name /dev/sda5 VG Name myvg PV Size 5.00 GiB / not usable 4.87 MiB Allocatable yes PE Size 8.00 MiB #看,现在的PE大小就是创建VG时指定的PE大小。 Total PE 640 Free PE 640 Allocated PE 0 PV UUID J2ECPI-K90e-vKoH-rCYC-IM11-16W1-fFh7fF
4.现在开始扩展卷组,卷组可以在线扩展,所以可以直接增加一块新的磁盘分区。并使用磁盘扩展命令。
[root@localhost ~]# pvcreate /dev/sda6 #新增加磁盘分区/dev/sda6 Physical volume "/dev/sda6" successfully created [root@localhost ~]# vgextend myvg /dev/sda6 # 将/dev/sda6直接扩展到myvg卷组中 Volume group "myvg" successfully extended [root@localhost ~]# vgdisplay myvg --- Volume group --- VG Name myvg System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 2 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.01 GiB #看,此时卷组大小变成10G了。 PE Size 8.00 MiB Total PE 1281 Alloc PE / Size 0 / 0 Free PE / Size 1281 / 10.01 GiB VG UUID DRLdb2-BawM-vO1R-3THE-4h6L-8b12-jxLCX4
-
如果此时我们想缩减卷组的话,只需两步,第一,将要移除的PV中的数据转移到该卷组其他的PV中。第二,使用vgreduce缩减卷组
[root@localhost ~]# pvmove /dev/sda6 #转移数据 No data to move for myvg [root@localhost ~]# vgreduce myvg /dev/sda6 Removed "/dev/sda6" from volume group "myvg" [root@localhost ~]# vgdisplay myvg #移除卷组 --- Volume group --- VG Name myvg 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 5.00 GiB #卷组大小变成了5G PE Size 8.00 MiB Total PE 640 Alloc PE / Size 0 / 0 Free PE / Size 640 / 5.00 GiB VG UUID DRLdb2-BawM-vO1R-3THE-4h6L-8b12-jxLCX4
6.最后我们可以来创建LV了,先来介绍LV的管理工具:
-
lvs:简要介绍LV
-
lvdisplay:显示LV详细信息
-
lvcreate -L SIZE LV_Name
-
-l 指明盘区的数量,Logical Extents
-
-n NAME VolumeGroup
以下是示例:
[root@localhost ~]# lvcreate -L 2G -n mylv myvg Logical volume "mylv" created [root@localhost ~]# ls /dev/mapper control myvg-mylv ##此时,我们就可以确认LV已经创建好了。 [root@localhost ~]# mke2fs -t ext4 -b 1024 -L MYLV /dev/myvg/mylv mke2fs 1.41.12 (17-May-2010) Filesystem label=MYLV OS type: Linux Block size=1024 (log=0) Fragment size=1024 (log=0) Stride=0 blocks, Stripe width=0 blocks 131072 inodes, 2097152 blocks 104857 blocks (5.00%) reserved for the super user First data block=1 Maximum filesystem blocks=69206016 256 block groups 8192 blocks per group, 8192 fragments per group 512 inodes per group Superblock backups stored on blocks: 8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409, 663553, 1024001, 1990657 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 24 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
7.现在我们就可以进行挂载和创建文件系统了。
[root@localhost ~]# mkdir /backup [root@localhost ~]# mount /dev/myvg/mylv /backup/ [root@localhost ~]# ls /backup/ lost+found [root@localhost ~]# mount /dev/mapper/myvg-mylv on /backup type ext4 (rw) #已挂载完成
8.最后补充一点,如何扩展和缩减逻辑卷
逻辑卷支持在线扩展
[root@localhost ~]# lvextend -L 5G /dev/myvg/mylv #直接指定扩展到5G,也可以使用 -L +2G,表示扩展2G。 Extending logical volume mylv to 5.00 GiB Logical volume mylv successfully resized [root@localhost ~]# df -lh #显示分区大小信息。 Filesystem Size Used Avail Use% Mounted on /dev/sda2 38G 2.5G 33G 7% / tmpfs 504M 84K 504M 1% /dev/shm /dev/sda1 291M 33M 244M 12% /boot /dev/mapper/myvg-mylv 2.0G 36M 1.9G 2% /backup
注意此时LV大小未改变,这是因为我们现在仅仅扩展了LV的大小,而并没有扩展文件系统的大小,所以此时显示的仍是文件系统的空间大小。
[root@localhost ~]# resize2fs /dev/myvg/mylv 扩展文件系统到LV的最大边界,也可指定大小 resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/myvg/mylv is mounted on /backup; on-line resizing required old desc_blocks = 8, new_desc_blocks = 20 Performing an on-line resize of /dev/myvg/mylv to 5242880 (1k) blocks. ^[[BThe filesystem on /dev/myvg/mylv is now 5242880 blocks long. [root@localhost ~]# df -lh Filesystem Size Used Avail Use% Mounted on /dev/sda2 38G 2.5G 33G 7% / tmpfs 504M 84K 504M 1% /dev/shm /dev/sda1 291M 33M 244M 12% /boot /dev/mapper/myvg-mylv 5.0G 36M 4.7G 1% /backup #看,现在大小变成5G了吧。
缩减逻辑卷
首先要缩减文件系统大小至指定大小,此时必须要先卸载文件系统,然后做文件系统的强制检测和修复,缩减文件系统并指明大小。最后缩减逻辑卷
[root@localhost ~]# umount /dev/myvg/mylv #卸载文件系统 [root@localhost ~]# e2fsck -f /dev/myvg/mylv #强制检测修复 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 MYLV: 11/327680 files (0.0% non-contiguous), 119693/5242880 blocks [root@localhost ~]# resize2fs /dev/myvg/mylv 3G #缩减文件系统至指定大小 resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/myvg/mylv to 3145728 (1k) blocks. The filesystem on /dev/myvg/mylv is now 3145728 blocks long. [root@localhost ~]# lvreduce -L 3G /dev/myvg/mylv #缩减LV至指定大小 WARNING: Reducing active logical volume to 3.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce mylv? [y/n]: y Reducing logical volume mylv to 3.00 GiB Logical volume mylv successfully resized [root@localhost ~]# mount /dev/myvg/mylv /backup/ #重新挂载 [root@localhost ~]# df -lh Filesystem Size Used Avail Use% Mounted on /dev/sda2 38G 2.5G 33G 7% / tmpfs 504M 84K 504M 1% /dev/shm /dev/sda1 291M 33M 244M 12% /boot /dev/mapper/myvg-mylv 3.0G 36M 2.8G 2% /backup #看,此时大小缩减了
创建快照卷快照
快照: snapshot
-
关键是瞬间完成。
-
创建快照及瞬间产生一个监控对方的监视器。
-
快照卷是访问原卷的另一个访问路径,若元卷发生变化,则将元卷要发生变化的元数据复制一份至快照卷中,要访问未改变的数据时则访问元卷。
-
快照卷与元卷必须在同一卷组中。
[root@localhost ~]# lvcreate -s -L 512M -n mylv-snap -p r /dev/myvg/mylv Logical volume "mylv-snap" created [root@localhost ~]# mount /dev/myvg/mylv-snap /mnt mount: block device /dev/mapper/myvg-mylv--snap is write-protected, mounting read-only
删除LVM
[root@localhost ~]# umount /dev/myvg/mylv #卸载文件系统 [root@localhost ~]# lvremove /dev/myvg/mylv Do you really want to remove active logical volume mylv? [y/n]: y Logical volume "mylv" successfully removed [root@localhost ~]# vgremove myvg Volume group "myvg" successfully removed #删除VG [root@localhost ~]# pvremove /dev/sda5 Labels on physical volume "/dev/sda5" successfully wiped #删除PV [root@localhost ~]# pvremove /dev/sda6 Labels on physical volume "/dev/sda6" successfully wiped #删除PV
原创文章,作者:21期-扬州-蓝,如若转载,请注明出处:http://www.178linux.com/24270
评论列表(1条)
写的很好,排版也很漂亮,加油