LVM是逻辑盘卷管理(LogicalVolume Manager)的简称,它是Linux环境下对磁盘进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提对硬盘的管理,来提高管理的灵活性。
1)lvm基本术语
#物理介质(Thephysical media)
这里指系统的存储设备:硬盘,是存储系统最低层的存储单元。
#物理卷(pv--physical volume)
物理卷就是指硬盘分区或从逻辑上与磁盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,却包含有与LVM相关的管理参数。
#卷组(vg-- Volume Group)
LVM卷组类似于非LVM系统中的物理硬盘,其由物理卷组成。可以在卷组上创建一个或多个“LVM分区”(逻辑卷),LVM卷组由一个或多个物理卷组成。
#逻辑卷(lv-- logical volume)
LVM的逻辑卷类似于非LVM系统中的硬盘分区,在逻辑卷之上可以建立文件系统(比如/home或者/usr等)。
#PE(physical extent)
每一个物理卷被划分为称为PE(PhysicalExtents)的基本单元,具有唯一编号的PE是可以被LVM寻址的最小单元。PE的大小是可配置的,默认为4MB。
#LE(logical extent)
逻辑卷也被划分为被称为LE(LogicalExtents)的可被寻址的基本单位。在同一个卷组中,LE的大小和PE是相同的,并且一一对应。
2)lvm工作方式
1.硬盘先从分区创建为pv,pv包含一个个pe,对应将来的le
2.一个或多个pv创建为一个vg,vg内的le对应pe,默认4M,ps:pe的大小决定最大vg的大小4M对应32G
3.然后从创建的vg内划出一个lv(可以自定义大小)最为逻辑盘使用
4.然后格式化新建的lv,并挂在到需要的目录下
显示信息 |
创建 |
删除组员 |
扩大大小 |
缩减大小 |
|
PV |
pvdisplay |
pvcreat |
pvremove |
----- |
----- |
VG |
vgdisplay |
vgcreat |
vgremove |
vgextend |
vgreduce |
LV |
lvdispaly |
lvcreat |
lvremove |
lvextend |
lvreduce |
3)LVM基本应用、扩大及缩小卷:
//查看磁盘状态
[[email protected] ~] # fdisk -l
Disk /dev/sda: 107.3 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x000cd6a5
Device Boot Start End Blocks Id System
/dev/sda1 1 143 1148616 82 Linux swap / Solaris
/dev/sda2 * 144 13054 103707607+ 83 Linux
Disk /dev/sdb: 107.3 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x00000000
Disk /dev/sdb doesn‘t contain a valid partition table
//使用fdisk对/dev/sdb分区
[[email protected] ~] # fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI orOSF disklabel
Building a new DOS disklabel with disk identifier 0xe5260b42.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won‘t be recoverable.
The number of cylinders for this disk is set to 13054.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2FDISK)
Warning: invalid flag 0x0000 of partition table 4 will be correctedby w(rite)
//创建新分区
Command (m for help): new
Command action
e extended
p primary partition (1-4)
//设置成主分区
p
Partition number (1-4): 1
First cylinder (1-13054, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-13054, default 13054):
Using default value 13054
Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition‘s system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
//继续下一步操作之前先确认对相应分区的修改正确无误。如果一切妥当则写入新分区表
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
//查看分区后的物理磁盘
[[email protected] ~] # fdisk -l
Disk /dev/sda: 107.3 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x000cd6a5
Device Boot Start End Blocks Id System
/dev/sda1 1 143 1148616 82 Linux swap / Solaris
/dev/sda2 * 144 13054 103707607+ 83 Linux
Disk /dev/sdb: 107.3 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xe5260b42
Device Boot Start End Blocks Id System
/dev/sdb1 1 13054 104856223+ 83 Linux
//进入LVM模式
[[email protected] ~] # lvm
//查看LVM磁盘状态----目前没有任何LVM磁盘信息
lvm> lvmdiskscan
/dev/sda1 [ 1.10 GB]
/dev/root [ 98.90 GB]
/dev/sdb1 [ 20.00 GB]
1 disk
2 partitions
0 LVM physical volume wholedisks
0 LVM physical volumes
//查看LVM的配置文件
lvm> dumpconfig
devices {
dir="/dev"
scan="/dev"
filter=["r|/dev/.*/by-path/.*|","r|/dev/.*/by-id/.*|", "a/.*/"]
cache="/etc/lvm/.cache"
write_cache_state=1
sysfs_scan=1
md_component_detection=1
}
dmeventd {
mirror_library="libdevmapper-event-lvm2mirror.so.2.02"
snapshot_library="libdevmapper-event-lvm2snapshot.so.2.02"
}
activation {
missing_stripe_filler="/dev/ioerror"
mirror_region_size=512
reserved_stack=256
reserved_memory=8192
process_priority=-18
}
global {
umask=63
test=0
activation=1
proc="/proc"
locking_type=1
locking_dir="/var/lock/lvm"
}
shell {
history_size=100
}
backup {
backup=1
backup_dir="/etc/lvm/backup"
archive=1
archive_dir="/etc/lvm/archive"
retain_min=10
retain_days=30
}
log {
verbose=0
syslog=1
overwrite=0
level=0
indent=1
command_names=0
prefix=" "
}
//创建PV
lvm> pvcreate /dev/sdb1
No physical volume labelread from /dev/sdb1
Physical volume"/dev/sdb1" successfully created
//查看PV状态
lvm> pvdisplay
"/dev/sdb1" is anew physical volume of "20.00 GB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 20.00 GB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 7daSq9-7KXU-Lvil-SUHc-ELrI-fyX7-FM9Bfy
//再次查看LVM磁盘状态
lvm> lvmdiskscan
/dev/sda1 [ 1.10 GB]
/dev/root [ 98.90 GB]
/dev/sdb1 [ 20.00 GB] LVM physical volume
1 disk
1 partition
0 LVM physical volume wholedisks
1 LVM physical volume
//创建VG
lvm> vgcreate vg_test /dev/sdb1
Volume group"vg_test" successfully created
//查看VG状态
lvm> vgdisplay
--- Volume group ---
VG Name vg_test
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 20.00 GB
PE Size 4.00 MB
Total PE 25599
Alloc PE / Size 0 / 0
Free PE / Size 25599 / 20.00 GB
VG UUID oSUogC-mV2F-b7Dg-5sFa-tztZ-NDh4-fpL6zu
//查看所有VG
lvm> vgs
VG #PV #LV #SN Attr VSize VFree
vg_test 1 0 0 wz--n- 100.00G 100.00G
//创建LV
lvm> lvcreate --size 10G --name lvm_u1 vg_test
Logical volume"lvm_u1" created
//查看LV状态
lvm> lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
lvm_u1 vg_test -wi-a-10.00G
lvm> vgs
VG #PV #LV #SN Attr VSize VFree
vg_test 1 1 0 wz--n- 100.00G 90.00G
//退出LVM配置模式
lvm> exit
Exiting.
//ext3格式化逻辑卷
[[email protected] ~] # mkfs -t ext3 -j /dev/vg_test/lvm_u1
mke2fs 1.41.1 (01-Sep-2008)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
655360 inodes, 2621440 blocks
131072 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2684354560
80 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.
//使用LVM
// 首先创建挂载点
[[email protected] ~]# mkdir /mnt/u1
//挂载卷
[[email protected] ~] # mount /dev/vg_test/lvm_u1 /mnt/u1/
//检查磁盘空间
[[email protected] ~] # df -m /mnt/u1/
Filesystem 1M-blocks Used Available Use% Mounted on
/dev/mapper/vg_test-lvm_u1
10080 151 9417 2% /mnt/u1
[[email protected] ~] # ls /mnt/u1/
lost+found
[[email protected] ~] # ll /boot/
total 14056
-rw------- 1 root root 512 2012-12-20 09:15 backup_mbr
lrwxrwxrwx 1 root root 12012-12-20 09:00 boot -> .
-rw-r--r-- 1 root root 12362009-02-24 06:40 boot.readme
-rw-r--r-- 1 root root 901372009-02-28 03:09 config-2.6.27.19-5-default
drwxr-xr-x 2 root root 5682012-12-20 09:15 grub
lrwxrwxrwx 1 root root 262012-12-20 09:21 initrd -> initrd-2.6.27.19-5-default
-rw-r--r-- 1 root root 6199394 2012-12-20 09:21initrd-2.6.27.19-5-default
-rw-r--r-- 1 root root 4352002012-12-20 09:15 message
-rw-r--r-- 1 root root 1412062009-02-28 03:13 symsets-2.6.27.19-5-default.tar.gz
-rw-r--r-- 1 root root 4048042009-02-28 03:12 symtypes-2.6.27.19-5-default.gz
-rw-r--r-- 1 root root 1195802009-02-28 03:10 symvers-2.6.27.19-5-default.gz
-rw-r--r-- 1 root root 1419451 2009-02-28 03:01System.map-2.6.27.19-5-default
-rw-r--r-- 1 root root 3004323 2009-02-28 03:09vmlinux-2.6.27.19-5-default.gz
lrwxrwxrwx 1 root root 272012-12-20 09:06 vmlinuz -> vmlinuz-2.6.27.19-5-default
-rw-r--r-- 1 root root 2537184 2009-02-28 03:01vmlinuz-2.6.27.19-5-default
//在新建文件系统里复制一个大文件例如在/boot目录里挑个内核文件复制到/mnt/u1
[[email protected] ~] # cp /boot/vmlinux-2.6.27.19-5-default.gz /mnt/u1/
//对复制过来的文件运行md5sum并保存校验值留待后用
[[email protected] ~] # md5sum /mnt/u1/vmlinux-2.6.27.19-5-default.gz
71603654a26edc4faa85054cbe2900be /mnt/u1/vmlinux-2.6.27.19-5-default.gz
[[email protected] ~] # ll /mnt/u1/
total 2956
drwx------ 2 root root 163842013-01-05 01:43 lost+found
-rw-r--r-- 1 root root 3004323 2013-01-05 01:51 vmlinux-2.6.27.19-5-default.gz
//扩大LVM卷
假定空间不够用准备向LVM卷增加更多空间。首先卸载该卷并使用lvresize命令调整卷大小。然后还必须用e2fsck检查整个文件系统并运行resize2fs调整该卷上ext3文件系统的大小
//卸载
[[email protected] ~] # umount /mnt/u1/
//调整卷大小
[[email protected] ~] # lvresize --size 20G /dev/vg_test/lvm_u1
Extending logical volumelvm_u1 to 20.00 GB
Logical volume lvm_u1successfully resized
//检查文件系统
[[email protected] ~] # e2fsck -f /dev/vg_test/lvm_u1
e2fsck 1.41.1 (01-Sep-2008)
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/vg_test/lvm_u1: 12/655360 files (0.0% non-contiguous),80431/2621440 blocks
//调整文件系统大小
[[email protected] ~] # resize2fs /dev/vg_test/lvm_u1 20G
resize2fs 1.41.1 (01-Sep-2008)
Resizing the filesystem on /dev/vg_test/lvm_u1 to 5242880 (4k)blocks.
The filesystem on /dev/vg_test/lvm_u1 is now 5242880 blocks long.
在上面的例子中卷和文件系统的大小都调整为20G。下面重新挂载卷检查磁盘空间和之前计算的md5sum
//重新挂载
[[email protected] ~] # mount /dev/vg_test/lvm_u1 /mnt/u1
[[email protected] ~] # df -m /mnt/u1/
Filesystem 1M-blocks Used Available Use%Mounted on
/dev/mapper/vg_test-lvm_u1
20159 159 18977 1% /mnt/u1
[[email protected] ~] # ll /mnt/u1/
total 2956
drwx------ 2 root root 163842013-01-05 01:43 lost+found
-rw-r--r-- 1 root root 3004323 2013-01-05 01:51vmlinux-2.6.27.19-5-default.gz
[[email protected] ~] # md5sum /mnt/u1/vmlinux-2.6.27.19-5-default.gz
71603654a26edc4faa85054cbe2900be /mnt/u1/vmlinux-2.6.27.19-5-default.gz
[[email protected] ~] # lvm
lvm> lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
lvm_u1 vg_test -wi-ao20.00G
lvm> vgs
VG #PV #LV #SN Attr VSize VFree
vg_test 1 1 0 wz--n- 100.00G 80.00G
lvm> lvdisplay
--- Logical volume ---
LV Name /dev/vg_test/lvm_u1
VG Name vg_test
LV UUID hO9SlO-6t9u-nHfE-tm0V-SQPC-827D-1EksZT
LV Write Access read/write
LV Status available
# open 1
LV Size 20.00 GB
Current LE 5120
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
lvm> exit
Exiting.
[[email protected] ~] #
[[email protected] ~] # lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
lvm_u1 vg_test -wi-ao20.00G
缩小LVM卷
缩减现有LVM卷里不需要的空间时也可以使用lvresize。和前面一样在调整卷大小之前先卸载这个卷然后运行e2fsck检查文件系统和resize2fs调整成较小尺寸
[[email protected] ~] # umount /mnt/u1/
[[email protected] ~] # e2fsck -f /dev/vg_test/lvm_u1
e2fsck 1.41.1 (01-Sep-2008)
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/vg_test/lvm_u1: 12/1310720 files (0.0% non-contiguous),122833/5242880 blocks
[[email protected] ~] # resize2fs /dev/vg_test/lvm_u1 15G
resize2fs 1.41.1 (01-Sep-2008)
Resizing the filesystem on /dev/vg_test/lvm_u1 to 3932160 (4k)blocks.
The filesystem on /dev/vg_test/lvm_u1 is now 3932160 blocks long.
[[email protected] ~] # lvresize --size 15G /dev/vg_test/lvm_u1
WARNING: Reducing activelogical volume to 15.00 GB
THIS MAY DESTROY YOUR DATA(filesystem etc.)
Do you really want to reduce lvm_u1? [y/n]: y
Reducing logical volumelvm_u1 to 15.00 GB
Logical volume lvm_u1successfully resized
[[email protected] ~] # mount /dev/vg_test/lvm_u1 /mnt/u1/
[[email protected] ~] # lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
lvm_u1 vg_test -wi-ao15.00G
[[email protected] ~] # vgs
VG #PV #LV #SN Attr VSize VFree
vg_test 1 1 0 wz--n- 100.00G 85.00G
[[email protected] ~] # ll /mnt/u1/
total 2956
drwx------ 2 root root 163842013-01-05 01:43 lost+found
-rw-r--r-- 1 root root 3004323 2013-01-05 01:51vmlinux-2.6.27.19-5-default.gz
[[email protected] ~] # md5sum /mnt/u1/vmlinux-2.6.27.19-5-default.gz
71603654a26edc4faa85054cbe2900be /mnt/u1/vmlinux-2.6.27.19-5-default.gz
[[email protected] ~] # pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 vg_test lvm2 a- 100.00G 85.00G
[[email protected] ~] # lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
lvm_u1 vg_test -wi-ao 15.00G
删除LVM逻辑卷和组 使用lvremove命令可以删除卷组里的LVM逻辑卷
[[email protected] ~] # lvremove /dev/ vg_test /lvm_u1
Do you really want to remove active logical volume"lvm_u1" ? [y/n]: y Logicalvolume "lvm_u1" successfully removed
使用vgremove命令可以删除现有LVM卷组
[[email protected] ~] #vgremove vg_test
Volume group "vgusb"successfully removed