1.LVM2原理解析
首先我们在一块硬盘上划分几个分区(相当于我们所拥有的多块硬盘),用命令 fdisk /dev/sda对硬盘进行分区的划分。步骤如下
[[email protected] ~]# fdisk /dev/sda
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): 3
First cylinder (7859-15665, default 7859):
Using default value 7859
Last cylinder, +cylinders or +size{K,M,G} (7859-15665, default 15665): +5G
按照上述步骤重复两次,得到三个空白分区
Command (m for help): p
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: 0x0008ea5a
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 7859 62914560 8e Linux LVM
/dev/sda3 7859 8512 5252256 83 Linux
/dev/sda4 8513 15665 57456472+ 5 Extended
/dev/sda5 8513 9166 5253223+ 83 Linux
/dev/sda6 9167 9820 5253223+ 83 Linux
之后我们对这三个分区进行类型转变,即变为lvm格式的分区。相关的fdisk子命令有:
p:显示已有的分区
n:创建一个新分区
d:删除一个分区
w:将相关的操作写入磁盘并退出
q:放弃更新并退出
m:获取帮助
l:列出分区类型的id
t:调整分区类型的id
通过下面的操作实现对分区类型的设定
Command (m for help): t
Partition number (1-6): 3
Hex code (type L to list codes): 8e
Changed system type of partition 3 to 8e (Linux LVM)
重复上述操作三次,实现对3,5,6三个分区的类型设定
查看分区的可能类型的子命令是:l
Command (m for help): 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
即通过上述的操作得到的分区形式如下:
Command (m for help): p
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: 0x0008ea5a
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 7859 62914560 8e Linux LVM
/dev/sda3 7859 8512 5252256 8e Linux LVM
/dev/sda4 8513 15665 57456472+ 5 Extended
/dev/sda5 8513 9166 5253223+ 8e Linux LVM
/dev/sda6 9167 9820 5253223+ 8e Linux LVM
之后w保存并退出。
2.这样第一步基本完成了,我们还要通知内核重新读取硬盘分区表,否则当我们做其他操作时会因为找不到相关分区使得操作失败
partx -a /dev/DEVICE
kpartx -a /dev/DEVICE
注意在centos5中使用命令partprobe
partprobe [/dev/DEVICE]
执行此命令,得到如下结果表明执行成功
[[email protected] ~]# partx -a /dev/sda
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
[[email protected] ~]# partx -a /dev/sda
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
BLKPG: Device or resource busy
error adding partition 4
BLKPG: Device or resource busy
error adding partition 5
BLKPG: Device or resource busy
error adding partition 6
3.接下来第三步就是创建pv,vg以及lv啦,具体的关于lvm的运行原理这里就不再赘述,仅谈谈如何创建的。
有关pv的管理工具:
pvs: 简要pv信息的显示
pvdisplay:相识pv的详细信息
pvcreate /dev/DEVICE:创建pv
pvremove /dev/DEVICE:删除这个盘区
pvmove:将盘区内的数据进行转移
那么接下来我们就可以使用pvcreate命令来创建pv卷了
[[email protected] ~]# pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created
重复上述操作三次,将三个盘区都制作为pv卷
接着我们就要在pv卷的基础上创建vg卷了,有关vg卷的相关命令有
vgs:查看vg的相关信息
vgdisplay:显示vg的详细信息
vgcreate [-s#[kKmMgGtTpPeE]] VolumeGroupName PhysicalDevicePath... -s用于指定物理盘区的大小
vgextend VolumeGroupName PhysicalDevicePath [PhysicalDe-vicePath...]:扩展
vgreduce:移动,缩减
那么我们就用vgcreate这个命令来创建vg卷
[[email protected] ~]# vgcreate -s2G myvg /dev/sda3 /dev/sda5 /dev/sda6
Volume group "myvg" successfully created
如此就创建成功了!
最后我们要在vg的基础上创建lv卷,也就是对于用户来说相当于实体硬盘分区一样的存在。相关的命令有:
lvs:简要显示lv的相关信息
lvdisplay:
lvcreate -L #[mMgGtT] -n NAME VoluneGroup
lvextend:扩展
lvreduce:缩减
通过命令来创建lv卷的方式:
[[email protected] ~]# lvcreate -L 2G -n mylv myvg
Logical volume "mylv" created
这样我们的lvm2也就创建完成了。
4.第四部的操作就是格式化lv卷,创建文件系统在卷上面,然后挂载在目录上,进而实现对其的读写操作。
但在这里要介绍lvm逻辑设备的访问路径的设定:
/dev/mapper/VG_NAME-LV_NAME 例:/dev/mapper/vo10_root
或者/dev/VG_NAME/LV_NAME 例:/dev/vo10/root
现在我们要实现在逻辑卷上创建文件系统,使用命令:
[[email protected] ~]# mke2fs -t ext4 -b 1024 -L "the ext4 on lvm" /dev/myvg/mylv
mke2fs 1.41.12 (17-May-2010)
Filesystem label=the ext4 on lvm
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 23 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
最后创建一个空目录,将其挂在上去,就万事大吉了!
[[email protected] ~]# mkdir /backup
[[email protected] ~]# mount /dev/myvg/mylv /backup
[[email protected] ~]# cd /backup
[[email protected] backup]# ll
total 12
drwx------ 2 root root 12288 Aug 17 09:08 lost+found
如果要实现开机时自动实现挂载,那么可以把相关的信息写到/etc/fstab中去
#vim /etc/fstab
在其中添加这一行:
/dev/myvg/mylv /backup ext4 defaults 0 0
5.深入内容:我们应该如何实现lvm的扩展与删减
这里我们用到了几个命令:
lvextend:扩展逻辑卷的物理边界
-L [+]# /PATH/TO/LV
resize2fs:扩展文件系统的边界
/PATH/TO/LV #G
首先我们先实现对逻辑卷的扩展操作。我们再划分一个分区,并将其添加进vg卷中
首先用命令 #fdisk /dev/sda 创建一个id为8e的分区,然后用命令 #partx -a /dev/sda 重读一下硬盘分区表。
将其创建为pv卷
[[email protected] ~]# pvcreate /dev/sda7
Physical volume "/dev/sda7" successfully created
接着加入到vg卷中
[[email protected] ~]# vgextend myvg /dev/sda7
Volume group "myvg" successfully extended
然后扩展lv卷
[[email protected] ~]# lvextend -L +5G /dev/myvg/mylv
Rounding size to boundary between physical extents: 6.00 GiB
Size of logical volume myvg/mylv changed from 2.00 GiB (1 extents) to 8.00 GiB (4 extents).
Logical volume mylv successfully resized
此时查看一下文件系统占用的空间信息,会发现lv卷空间大小仍为2G大小,也就是说在此处存在着逻辑边界和物理边界的区别。而df命令查看的是逻辑边界的大小
[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg0-root 20G 287M 19G 2% /
tmpfs 935M 0 935M 0% /dev/shm
/dev/sda1 190M 33M 148M 18% /boot
/dev/mapper/vg0-usr 9.8G 1.9G 7.4G 21% /usr
/dev/mapper/vg0-var 20G 195M 19G 2% /var
/dev/mapper/myvg-mylv
2.0G 3.1M 1.9G 1% /backup
之后将文件系统的逻辑边界扩展至lv的边界
[[email protected] ~]# resize2fs /dev/myvg/mylv 5G
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/myvg/mylv is mounted on /backup; on-line resizing required
old desc_blocks = 17, new_desc_blocks = 20
Performing an on-line resize of /dev/myvg/mylv to 5242880 (1k) blocks.
The filesystem on /dev/myvg/mylv is now 5242880 blocks long.
此时再查看文件系统占用空间的大小,就会发现数字已经变了,这样就验证了我们上述的想法
[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg0-root 20G 287M 19G 2% /
tmpfs 935M 0 935M 0% /dev/shm
/dev/sda1 190M 33M 148M 18% /boot
/dev/mapper/vg0-usr 9.8G 1.9G 7.4G 21% /usr
/dev/mapper/vg0-var 20G 195M 19G 2% /var
/dev/mapper/myvg-mylv
4.9G 3.4M 4.7G 1% /backup
6.缩减逻辑卷
缩减逻辑卷时应该注意的几个问题:
1)不能在线缩减,要先卸载
2)确保缩减后的空间大小依然能存储原有的所有数据
3)在缩减之前应该先强行检查文件系统,以确保文件系统处于一致性状态
所以缩减逻辑卷的相关步骤是:
#umount /dev/VG_NAME/LV_NAME
#e2fsck -f /dev/VG_NAME/LV_NAME
#resize2fs /dev/VG_NAME/LV_NAME
#lvreduce -L [-]#[mMgGtT] /dev/VG_NAME/LV_NAME、
#mount
就可以了!
7.最后一个知识点,创建快照卷(一般为只读权限)
快照监控着“元数据”,将元数据变化的文件存于快照卷中,即快照存储空间中存储的是某修改文件的未修改文件
#lvcreate
-s 创建快照卷
-p r|w 指定权限
#lvcreate -L # -n SLV_NAME -p r /PATH/TO/LV
指定大小 指定快照名称 指定权限
注意:
1)快照的生命周期为整个数据时长,在这段时长内,数据的增长是不能超出快照卷的大小的
2)快照应该是只读
3)跟原卷要在同一个卷组内