本指南将介绍如何使用lvm-逻辑卷管理。同时,在附加章节中,还将讲述如何使用RAID1.lvm是一个很抽象的概念。所以我提供了一个Debian Etch VMware p_w_picpath的虚拟机映像文件,你可以下载并在上面实验我们在这里讲的命令,然后再把结果和我们的做个比较。从而,加强学习效果。 这样,你会很快学会lvm的使用方法。但是,我对本教程效用不做任何保证。
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的层次结构是这样的:、

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哦,那当然啦,因为我们输入的是大概值。
 
总结来讲,上面主要有两部
第一,fdisk 分区,然后标记8e,lvm
第二,创建pv,物理卷
 
 
创建逻辑卷组fileserver,
vgcreate fileserver /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1
这样就把分开的面团和为一个了,哈哈~

server1:~# vgcreate fileserver /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1
Volume group "fileserver" successfully created

查看逻辑卷组:

vgdisplay

server1:~# vgdisplay
--- Volume group ---
VG Name fileserver
System ID
Format lvm2
Metadata Areas 4
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 4
Act PV 4
VG Size 93.14 GB
PE Size 4.00 MB
Total PE 23844
Alloc PE / Size 0 / 0
Free PE / Size 23844 / 93.14 GB
VG UUID 3Y1WVF-BLET-QkKs-Qnrs-SZxI-wrNO-dTqhFP

vgscan命令也可以查看逻辑卷组。
vgscan

server1:~# vgscan
Reading all physical volumes. This may take a while...
Found volume group "fileserver" using metadata type lvm2
我们可以重命名逻辑卷组名
vgrename fileserver data

server1:~# vgrename fileserver data
Volume group "fileserver" successfully renamed to "data"
然后再用vgdisplay查看

server1:~# vgdisplay
--- Volume group ---
VG Name data
System ID
Format lvm2
Metadata Areas 4
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 4
Act PV 4
VG Size 93.14 GB
PE Size 4.00 MB
Total PE 23844
Alloc PE / Size 0 / 0
Free PE / Size 23844 / 93.14 GB
VG UUID 3Y1WVF-BLET-QkKs-Qnrs-SZxI-wrNO-dTqhFP

vgscan查看

server1:~# vgscan
Reading all physical volumes. This may take a while...
Found volume group "data" using metadata type lvm2

删除逻辑卷组data:

vgremove data

server1:~# vgremove data
Volume group "data" successfully removed


vgdisplay

这下就没有输出了

server1:~# vgdisplay

vgscan

好啦,不折腾了,重新创建fileserver卷组吧
vgcreate fileserver /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1

server1:~# vgcreate fileserver /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1
Volume group "fileserver" successfully created


下面我们在fileserver上面创建逻辑卷了(分面团了嘛)。分别为share(40G),backup(5G),media(1G)这

个大小是可以任意分的,但不能超过fileserver的总大小哦
我们只使用了少于50%的空间,其余用于等会使用RAID1.fileserver大概有96G的空间哦。
lvcreate --name share --size 40G fileserver
命令很简单,也很好理解
server1:~# lvcreate --name share --size 40G fileserver
Logical volume "share" created


lvcreate --name backup --size 5G fileserver

server1:~# lvcreate --name backup --size 5G fileserver
Logical volume "backup" created


lvcreate --name media --size 1G fileserver

server1:~# lvcreate --name media --size 1G fileserver
Logical volume "media" created

查看逻辑卷:
lvdisplay
server1:~# lvdisplay
--- Logical volume ---
LV Name /dev/fileserver/share
VG Name fileserver
LV UUID 280Mup-H9aa-sn0S-AXH3-04cP-V6p9-lfoGgJ
LV Write Access read/write
LV Status available
# open 0
LV Size 40.00 GB
Current LE 10240
Segments 2
Allocation inherit
Read ahead sectors 0
Block device 253:0

--- Logical volume ---
LV Name /dev/fileserver/backup
VG Name fileserver
LV UUID zZeuKg-Dazh-aZMC-Aa99-KUSt-J6ET-KRe0cD
LV Write Access read/write
LV Status available
# open 0
LV Size 5.00 GB
Current LE 1280
Segments 1
Allocation inherit
Read ahead sectors 0
Block device 253:1

--- Logical volume ---
LV Name /dev/fileserver/media
VG Name fileserver
LV UUID usfvrv-BC92-3pFH-2NW0-2N3e-6ERQ-4Sj7YS
LV Write Access read/write
LV Status available
# open 0
LV Size 1.00 GB
Current LE 256
Segments 1
Allocation inherit
Read ahead sectors 0
Block device 253:2


lvscan

server1:~# lvscan
ACTIVE '/dev/fileserver/share' [40.00 GB] inherit
ACTIVE '/dev/fileserver/backup' [5.00 GB] inherit
ACTIVE '/dev/fileserver/media' [1.00 GB] inherit

我们也可以重命名逻辑卷名称
lvrename fileserver media films

也可以删除逻辑卷

lvremove /dev/fileserver/films

server1:~# lvremove /dev/fileserver/films
Do you really want to remove active logical volume "films"? [y/n]: <-- y
Logical volume "films" successfully removed

然后,再创建
lvcreate --name media --size 1G fileserver

server1:~# lvcreate --name media --size 1G fileserver
Logical volume "media" created
扩大大小:
lvextend -L1.5G /dev/fileserver/media

server1:~# lvextend -L1.5G /dev/fileserver/media
Extending logical volume media to 1.50 GB
Logical volume media successfully resized
缩小:

lvreduce -L1G /dev/fileserver/media
server1:~# lvreduce -L1G /dev/fileserver/media
WARNING: Reducing active logical volume to 1.00 GB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce media? [y/n]: <-- y
Reducing logical volume media to 1.00 GB
Logical volume media successfully resized


看到了吧,这真是孙悟空啊,能大能小。
 
目前为止,我们创建了三个逻辑卷,但都还没有创建文件系统,格式化。在这之前,是无法使用的。于是

,我们分别在它们什么创建文件系统。share 为ext3,backup为xfs,media为reiserfs。

第一个: :em20
mkfs.ext3 /dev/fileserver/share

server1:~# mkfs.ext3 /dev/fileserver/share
mke2fs 1.40-WIP (14-Nov-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
5242880 inodes, 10485760 blocks
524288 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=0
320 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624

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.

第二个: :em20
mkfs.xfs /dev/fileserver/backup

server1:~# mkfs.xfs /dev/fileserver/backup
meta-data=/dev/fileserver/backup isize=256 agcount=8, agsize=163840 blks
= sectsz=512 attr=0
data = bsize=4096 blocks=1310720, imaxpct=25
= sunit=0 swidth=0 blks, unwritten=1
naming =version 2 bsize=4096
log =internal log bsize=4096 blocks=2560, version=1
= sectsz=512 sunit=0 blks
realtime =none extsz=65536 blocks=0, rtextents=0

第三个: :em20
mkfs.reiserfs /dev/fileserver/media

server1:~# mkfs.reiserfs /dev/fileserver/media
mkfs.reiserfs 3.6.19 (2003 www.namesys.com)

A pair of credits:
Alexander Lyamin keeps our hardware running, and was very generous to our
project in many little ways.

Chris Mason wrote the journaling code for V3, which was enormously more useful
to users than just waiting until we could create a wandering log filesystem as
Hans would have unwisely done without him.
Jeff Mahoney optimized the bitmap scanning code for V3, and performed the big
endian cleanups.


Guessing about desired format.. Kernel 2.6.17-2-486 is running.
Format 3.6 with standard journal
Count of blocks on the device: 262144
Number of blocks consumed by mkreiserfs formatting process: 8219
Blocksize: 4096
Hash function used to sort names: "r5"
Journal Size 8193 blocks (first block 18)
Journal Max transaction length 1024
inode generation number: 0
UUID: 2bebf750-6e05-47b2-99b6-916fa7ea5398
ATTENTION: YOU SHOULD REBOOT AFTER FDISK!
ALL DATA WILL BE LOST ON '/dev/fileserver/media'!
Continue (y/n):y
Initializing journal - 0%....20%....40%....60%....80%....100%
Syncing..ok

Tell your friends to use a kernel based on 2.4.18 or later, and especially not a
kernel based on 2.4.9, when you use reiserFS. Have fun.

ReiserFS is successfully created on /dev/fileserver/media.

创建文件系统完成后,就该挂载逻辑卷了 :em11 。我将把它们分别挂载share /var/share, backup

/var/backup, media /var/media。挂载之前,先创建挂载所需目录:

mkdir /var/media /var/backup /var/share

下面挂载:

mount /dev/fileserver/share /var/share
mount /dev/fileserver/backup /var/backup
mount /dev/fileserver/media /var/media

查看挂载的文件系统:

server1:~# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 19G 665M 17G 4% /
tmpfs 78M 0 78M 0% /lib/init/rw
udev 10M 88K 10M 1% /dev
tmpfs 78M 0 78M 0% /dev/shm
/dev/sda1 137M 17M 114M 13% /boot
/dev/mapper/fileserver-share
40G 177M 38G 1% /var/share
/dev/mapper/fileserver-backup
5.0G 144K 5.0G 1% /var/backup
/dev/mapper/fileserver-media
1.0G 33M 992M 4% /var/media


这样我们就手动挂载好了。不过,我们还是希望系统能够自动挂载这些。我们可以修改/etc/fstab文件。
首先备份它: :em06
sudo cp /etc/fstab /etc/fstab_orig

编辑:
sudo vi /etc/fstab

# /etc/fstab: static file system information.
#
# <file system> <mount point> <type> <options> <dump> <pass>
proc /proc proc defaults 0 0
/dev/sda2 / ext3 defaults,errors=remount-ro 0 1
/dev/sda1 /boot ext3 defaults 0 2
/dev/hdc /media/cdrom0 udf,iso9660 user,noauto 0 0
/dev/fd0 /media/floppy0 auto rw,user,noauto 0 0
#在末尾添加如下几行 :em01
/dev/fileserver/share /var/share ext3 rw,noatime 0 0
/dev/fileserver/backup /var/backup xfs rw,noatime 0 0
/dev/fileserver/media /var/media reiserfs rw,noatime 0 0

如果你不建意,重启系统. :em06
sudo shutdown -r now

然后查看一下挂载了的文件系统,
df -h

server1:~# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 19G 665M 17G 4% /
tmpfs 78M 0 78M 0% /lib/init/rw
udev 10M 88K 10M 1% /dev
tmpfs 78M 0 78M 0% /dev/shm
/dev/sda1 137M 17M 114M 13% /boot
/dev/mapper/fileserver-share
40G 177M 38G 1% /var/share
/dev/mapper/fileserver-backup
5.0G 144K 5.0G 1% /var/backup
/dev/mapper/fileserver-media
1.0G 33M 992M 4% /var/media

 

注意,如果使用lvextend、lvreduce扩展VG下面的LVM之后,已经mount到系统上的LVM不会动态的变化大小,需要使用resize2fs命令执行扩展

例如:resize2fs /dev/DBgroup/Share