Author: Falko Timme
英文地址:http://www.howtoforge.com/linux_lvm
翻译:xlai
xuelai3000@yahoo.com.cn
本指南将介绍如何使用lvm-逻辑卷管理。同时,在附加章节中,还将讲述如何使用RAID1.lvm是一个很抽象的概念。所以我提供了一个Debian Etch VMware image的虚拟机映像文件,你可以下载并在上面实验我们在这里讲的命令,然后再把结果和我们的做个比较。从而,加强学习效果。 这样,你会很快学会lvm的使用方法。但是,我对本教程效用不做任何保证。(实际上,我们完全可以在自己的ubuntu 什么操作啦)
1 预备
我是通过以下两篇文章得到灵感,才写出此教程的。
http://www.linuxdevcenter.com/pub/a/lin ... h-lvm.html
http://www.debian-administration.org/articles/410
这两篇文章都很不错,不过如果之前没有用过lvm,就会很难理解它的内容。所以,我才写了该文档来简化lvm的学习过程。首先,我们要安装所有必须的工具软件包。
$ sudo apt-get install lvm2 dmsetup mdadm reiserfsprogs xfsprogs
apt-get install lvm2 dmsetup mdadm reiserfsprogs xfsprogs
2 lvm层次结构
大体上,LVM的层次结构是这样的:

底层是物理卷physical volume,一个多个都可以(如/dev/sda1到/dev/sde1,实际上是和分区一
一对应的,只不过要通过相关工具创建),然后在把这些物理卷(一个或多个)集合为一个逻辑卷组
volume group.柔和在一起后,我们就可以重新分割啦。可以分割为一个或多个逻辑卷(logical volume),
此时,就可以再分割成的逻辑卷上面格式化了,然后就可以向上面存放数据了(就想我们原来使用的分区
一样)。这样的逻辑卷比制作前的任何一个物理分区或物理卷,可以都要大,当然也可以小,这就看你自
己怎么分割了。(我本人觉得这就好比和面,刚开始时,有几块都比较小,分散。我们加点水,柔和在一
起,我们就又可以制作任何大小的块了,面条也可以啊。)
使用逻辑卷时候,最好不要把所有的磁盘空间都使用了。这样,以后某块逻辑卷需要加大,就可
以使用留下的空闲空间。
本教程例子中将创建逻辑卷组volume group,fileserver,创建逻辑卷/dev/fileserver/share,
/dev/fileserver/backup, and /dev/fileserver/media (逻辑卷只是使用物理卷的一半空间,RAID1使用
另外的空间)
3 开始LVM设置
第一步,找出硬盘及分区
server1:~# fdisk -l
Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 18 144553+ 83 Linux
/dev/sda2 19 2450 19535040 83 Linux
/dev/sda4 2451 2610 1285200 82 Linux swap / Solaris
Disk /dev/sdb: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk /dev/sdb doesn't contain a valid partition table
Disk /dev/sdc: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk /dev/sdc doesn't contain a valid partition table
Disk /dev/sdd: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk /dev/sdd doesn't contain a valid partition table
Disk /dev/sde: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk /dev/sde doesn't contain a valid partition table
Disk /dev/sdf: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk /dev/sdf doesn't contain a valid partition table
我们看到了/dev/sdb到/dev/sdf都没有分区。我们将创建/dev/sdb1, /dev/sdc1, /dev/sdd1, and
/dev/sde1分区,/dev/sdf暂时不用。每个分区都只分配25G的分区。
第一块硬盘
server1:~# fdisk /dev/sdb
The number of cylinders for this disk is set to 10443.
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/2 FDISK)
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): <-- n创建新分区new
Command action
e extended 扩展分区
p primary partition (1-4)主分区
<-- p选主分区
Partition number (1-4): <-- 1分区编号
First cylinder (1-10443, default 1): <-- <ENTER>开始磁道 ,直接回车默认为第一个磁道)
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-10443, default 10443): <-- +25000M我们只使用
25G空间
Command (m for help): <-- t改变分区id
Selected partition 1
Hex code (type L to list codes): <-- L列出所有的代码意思
0 Empty 1e Hidden W95 FAT1 80 Old Minix be Solaris boot
1 FAT12 24 NEC DOS 81 Minix / old Lin bf Solaris
2 XENIX root 39 Plan 9 82 Linux swap / So c1 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 83 Linux c4 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
5 Extended 41 PPC PReP Boot 85 Linux extended c7 Syrinx
6 FAT16 42 SFS 86 NTFS volume set da Non-FS data
7 HPFS/NTFS 4d QNX4.x 87 NTFS volume set db CP/M / CTOS / .
8 AIX 4e QNX4.x 2nd part 88 Linux plaintext de Dell Utility
9 AIX bootable 4f QNX4.x 3rd part 8e Linux LVM df BootIt
a OS/2 Boot Manag 50 OnTrack DM 93 Amoeba e1 DOS access
b W95 FAT32 51 OnTrack DM6 Aux 94 Amoeba BBT e3 DOS R/O
c W95 FAT32 (LBA) 52 CP/M 9f BSD/OS e4 SpeedStor
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi eb BeOS fs
f W95 Ext'd (LBA) 54 OnTrackDM6 a5 FreeBSD ee EFI GPT
10 OPUS 55 EZ-Drive a6 OpenBSD ef EFI (FAT-12/16/
11 Hidden FAT12 56 Golden Bow a7 NeXTSTEP f0 Linux/PA-RISC b
12 Compaq diagnost 5c Priam Edisk a8 Darwin UFS f1 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor a9 NetBSD f4 SpeedStor
16 Hidden FAT16 63 GNU HURD or Sys ab Darwin boot f2 DOS secondary
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fd Linux raid auto
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fe LANstep
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid ff BBT
1c Hidden W95 FAT3 75 PC/IX
Hex code (type L to list codes): <-- 8e表示linux的lvm
Changed system type of partition 1 to 8e (Linux LVM)
Command (m for help): <-- w将设置写入
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
下面的三块硬盘我们也使用类似的方法,
fdisk /dev/sdc
fdisk /dev/sdd
fdisk /dev/sde
完成后,使用fdisk -l输出应该是这样:
server1:~# fdisk -l
Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 18 144553+ 83 Linux
/dev/sda2 19 2450 19535040 83 Linux
/dev/sda4 2451 2610 1285200 82 Linux swap / Solaris
Disk /dev/sdb: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 1 3040 24418768+ 8e Linux LVM
Disk /dev/sdc: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdc1 1 3040 24418768+ 8e Linux LVM
Disk /dev/sdd: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdd1 1 3040 24418768+ 8e Linux LVM
Disk /dev/sde: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sde1 1 3040 24418768+ 8e Linux LVM
Disk /dev/sdf: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk /dev/sdf doesn't contain a valid partition table
下面我们创建物理卷
pvcreate /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1(看到了吧,物理卷和分区是一一对应的哦)
server1:~# pvcreate /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1
Physical volume "/dev/sdb1" successfully created
Physical volume "/dev/sdc1" successfully created
Physical volume "/dev/sdd1" successfully created
Physical volume "/dev/sde1" successfully created
如果你想再练习一下,可以删除刚才的创建的物理卷。
pvremove /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1
server1:~# pvremove /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1
Labels on physical volume "/dev/sdb1" successfully wiped
Labels on physical volume "/dev/sdc1" successfully wiped
Labels on physical volume "/dev/sdd1" successfully wiped
Labels on physical volume "/dev/sde1" successfully wiped
然后再创建:
pvcreate /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1
again:
server1:~# pvcreate /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1
Physical volume "/dev/sdb1" successfully created
Physical volume "/dev/sdc1" successfully created
Physical volume "/dev/sdd1" successfully created
Physical volume "/dev/sde1" successfully created
然后:
pvdisplay来显示物理卷的状态。
server1:~# pvdisplay
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 23.29 GB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID G8lu2L-Hij1-NVde-sOKc-OoVI-fadg-Jd1vyU
--- NEW Physical volume ---
PV Name /dev/sdc1
VG Name
PV Size 23.29 GB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 40GJyh-IbsI-pzhn-TDRq-PQ3l-3ut0-AVSE4B
--- NEW Physical volume ---
PV Name /dev/sdd1
VG Name
PV Size 23.29 GB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 4mU63D-4s26-uL00-r0pO-Q0hP-mvQR-2YJN5B
--- NEW Physical volume ---
PV Name /dev/sde1
VG Name
PV Size 23.29 GB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 3upcZc-4eS2-h4r4-iBKK-gZJv-AYt3-EKdRK6
好像每个都没有25G哦,那当然啦,因为我们输入的是大概值。