分类: LINUX
2011-01-24 20:39:12
关于linux下面LVM逻辑卷的创建的讨论,
逻辑卷的介绍
LVM就是logical volume manager(逻辑卷的管理)
LVM就是可以随意拉升和缩小,SCSI的硬盘最多只能够分15个区,如果需要更多的分区,怎么办呢,如果分区在规划的时候分小了或者大了,怎么办呢,而LVM很好的就解决了这些问题。
下面开始进行LVM逻辑卷创建的讨论
从上面这个图可以看到,创建逻辑卷的步骤:
Linux分区----------物理卷-----------卷组-----------逻辑卷
这个就是创建逻辑卷的顺序,下面就按照这个顺序来创建逻辑卷。
1. 创建linux分区(物理分区)
首先创建三个分区
[root@localhost ~]# fdisk /dev/sda
The number of cylinders for this disk is set to 1958.
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): n
First cylinder (1316-1958, default 1316):
Using default value 1316
Last cylinder or +size or +sizeM or +sizeK (1316-1958, default 1958): +200M
Command (m for help): n
First cylinder (1341-1958, default 1341):
Using default value 1341
Last cylinder or +size or +sizeM or +sizeK (1341-1958, default 1958): +200M
Command (m for help): n
First cylinder (1366-1958, default 1366):
Using default value 1366
Last cylinder or +size or +sizeM or +sizeK (1366-1958, default 1958): +200M
Command (m for help):
然后在分区的主界面按“t”来转换分区的类型,LVM的id为8e
这些分区的类型的id号,我们在分区的主界面按“l”都是可以看到的。
Command (m for help): t
Partition number (1-10): 8
Hex code (type L to list codes): 8e
Changed system type of partition 8 to 8e (Linux LVM)
Command (m for help): t
Partition number (1-10): 9
Hex code (type L to list codes): 8e
Changed system type of partition 9 to 8e (Linux LVM)
Command (m for help): t
Partition number (1-10): 10
Hex code (type L to list codes): 8e
Changed system type of partition 10 to 8e (Linux LVM)
Command (m for help):
完成分区后,按“p”来查看我们做的操作是否正确。
Command (m for help): p
Disk /dev/sda: 16.1 GB, 16106127360 bytes
255 heads, 63 sectors/track, 1958 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 535 4192965 83 Linux
/dev/sda3 536 1057 4192965 83 Linux
/dev/sda4 1058 1958 7237282+ 5 Extended
/dev/sda5 1058 1188 1052226 82 Linux swap / Solaris
/dev/sda6 1189 1253 522081 83 Linux
/dev/sda7 1254 1315 497983+ 83 Linux
/dev/sda8 1316 1340 200781 8e Linux LVM
/dev/sda9 1341 1365 200781 8e Linux LVM
/dev/sda10 1366 1390 200781 8e Linux LVM
Command (m for help):
确定没有问题后,按“wq”保存并退出。
Command (m for help): wq
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.
Syncing disks.
[root@localhost ~]#
退出后一定要用partprobe工具来让刚才的操作立即生效。
[root@localhost ~]# partprobe /dev/sda
[root@localhost ~]#
到这里,第一步创建物理分区完成哈~
下面进行创建物理卷
2. 创建物理卷
使用命令
#pvcreate /dev/sda8 /dev/sda9 /dev/sda10
Pvcreate是系统创建物理卷的命令,/dev/sda8,/dev/sda9./dev/sda10这些是刚才创建的物理分区,是要转换成物理卷的分区。
[root@localhost ~]# pvcreate /dev/sda8 /dev/sda9 /dev/sda10
Physical volume "/dev/sda8" successfully created
Physical volume "/dev/sda9" successfully created
Physical volume "/dev/sda10" successfully created
[root@localhost ~]#
从上面的提示,可以看到,刚才创建的物理卷已经成功了。
创建成功后,可以使用pvdisplay 查看
查询物理卷的命令
#pvdisplay [dev/sda8 /dev/sda9 /dev/sda10]
可以用pvdisplay来查询我们刚才转换的物理卷,
[root@localhost ~]#
[root@localhost ~]# pvdisplay
"/dev/sda8" is a new physical volume of "196.08 MB"
--- NEW Physical volume ---
PV Name /dev/sda8
VG Name
PV Size 192.08MB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID zFk3Gq-4yrt-qIHn-7PLw-zuSS-jXcI-fFyUtn
"/dev/sda9" is a new physical volume of "196.08 MB"
--- NEW Physical volume ---
PV Name /dev/sda9
VG Name
PV Size 196.08MB
Allocatable NO
PE Size (KByte) 0
Total PE
Free PE 0
Allocated PE 0
PV UUID QOnkEu-CyF4-plef-tkiG-ve4u-X9F4-rrTben
"/dev/sda10" is a new physical volume of "196.08 MB"
--- NEW Physical volume ---
PV Name /dev/sda10
VG Name
PV Size 196.08 MB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID IghGjp-Q6Ra-2Z1X-Md23-zXus-oO8i-63ddOe
[root@localhost ~]#
通过pvdisplay可以看到很多信息,物理卷的名字,物理卷的大小,物理卷的UUID,还有一点,我们看到这个物理卷是个新的,这个是因为我的物理卷还没有加入到任何的卷组。
到这里,第二步创建物理卷完成哈~
下面进行创建卷组
3. 创建卷组
使用命令
#vgcreate vg01 /dev/sda8 /dev/sda9 /dev/sda10
Vgcreate是系统创建卷组的命令,vg01是给这个卷组起的一个名字,后面的/dev/sda8 /dev/sda9 /dev/sda10是需要将那些物理卷转换成卷组。
[root@localhost ~]#
[root@localhost ~]# vgcreate vg01 /dev/sda8 /dev/sda9 /dev/sda10
Volume group "vg01" successfully created
[root@localhost ~]#
从上面的提示,可以看到,我刚才创建的卷组成功了。
创建成功后,可以使用vgdisplay查看
查询卷组命令
#vgdisplay [vg01 vg02]
可以用vgdisplay来查询刚才转换成功的卷组。
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name vg01
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 1
VG Access read/write
VG Status esizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 3
Act PV 3
VG Size 576.00 MB
PE Size 4.000MB
Total PE 144
Alloc PE / Size 0 / 0
Free PE / Size 144 / 576.00 MB
VG UUID vmWZOj-XA4V-4Kvz-wmx7-BMdv-np45-lEiuG0
[root@localhost ~]#
通过vgdisplay可以看到很多的信息,卷组的名字, PE的size,卷组的大小为576.00MB,我们刚才分了三个200.00MB大小的物理卷,合并成卷组也就大概600.00MB左右。
到这里,第三步创建卷组完成哈~
下面进行创建逻辑卷的学习
4. 创建逻辑卷
使用命令
#lvcreate -L/-l -n lv01 vg01
Lvcreate 是系统创建逻辑卷的的命令
-L 直接就指定分区的大小
-l 是指指定PE的数量
-n name
Lv01 逻辑卷的名字
Vg01 哪个卷组
[root@localhost ~]# lvcreate -L 100M vg01 -n lv01
Logical volume "lv01" created
[root@localhost ~]#
从上面的提示,可以看到,刚才创建的逻辑卷成功了。
创建成功后,可以使用lvdisplay来查看。
查询逻辑卷命令
#lvdisplay [/dev/vg01/lv01 /dev/vg01/lv02]
可以用 lvdispalay来查看刚才创建的逻辑卷,
[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Name /dev/vg01/lv01
VG Name vg01
LV UUID 7oARRY-vvp2-gSHw-W60h-LtbC-YBVs-JU2c8n
LV Write Access read/write
LV Status aailable
# open 0
LV Size 100.00 MB
Current LE 25
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
[root@localhost ~]#
通过lvdisplay可以看到很多的信息,逻辑卷的名字,卷组的名字,还有逻辑卷的大小100.00MB。
到这里,第四步创建逻辑卷完成哈~
下面进行格式化逻辑卷。
5. 格式化逻辑卷
使用命令
#mkfs.ext3 /dev/vg01/lv01
[root@localhost ~]# mkfs.ext3 /dev/vg01/lv01
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
25688 inodes, 102400 blocks
5120 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
13 block groups
8192 blocks per group, 8192 fragments per group
1976 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 22 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost ~]#
格式化成功,接下来就是挂载了。
6. 挂载逻辑卷
#mkdir /mnt/lvm
#mount /dev/vg01/lv01 /mnt/lv01
[root@localhost ~]# mkdir /mnt/lvm
[root@localhost ~]# mount /dev/vg01/lv01 /mnt/lvm/
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 3.9G 2.3G 1.5G 61% /
/dev/sda6 494M 11M 458M 3% /home
/dev/sda2 3.9G 1.8G 2.0G 49% /var
/dev/sda1 99M 14M 81M 15% /boot
tmpfs 188M 0 188M 0% /dev/shm
/dev/md1 198M 14M 174M 8% /mnt/raid5
/dev/hdc 2.8G 2.8G 0 100% /media/RHEL_5.4 i386 DVD
/dev/mapper/vg01-lv01
97M 5.6M 87M 7% /mnt/lvm
[root@localhost ~]#
现在我们创建的逻辑卷就可以使用了,如果下次开机启动时候生效,也必须写在/etc/fstab这个文件里面。
/dev/vg01/lv01 /mnt/lvm ext3 defaults 0 0
~
这样,系统下次启动就可以挂载逻辑卷了
7. 扩展卷组
当有一块新的硬盘加进来了,怎么将一块新硬盘加入到卷组
在已有卷组中添加新的物理卷
#vgextend vg01 /dev/sda11
Vg01 卷组
/dev/sda11 新的硬盘
首先我们去创建一个新的分区,转换成物理卷
[root@localhost ~]# pvcreate /dev/sda11
Physical volume "/dev/sda11" successfully created
[root@localhost ~]#
然后扩展卷组vg01
[root@localhost ~]#
[root@localhost ~]# vgextend vg01 /dev/sda11
Volume group "vg01" successfully extended
[root@localhost ~]#
可以看到,vg01被成功的扩展。
用vgdisplay来查看卷组是否变大了
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name vg01
System ID
Format lvm2
Metadata Areas 4
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 4
Act PV 4
VG Size 768.00 MB
PE Size 4.00 MB
Total PE 192
Alloc PE / Size 25 / 100.00 MB
Free PE / Size 167 / 668.00 MB
VG UUID vmWZOj-XA4V-4Kvz-wmx7-BMdv-np45-lEiuG0
[root@localhost ~]#
卷组被扩大了200.00MB
8. 扩展逻辑卷
当逻辑卷不够用的时候,可以从当前卷组中去扩展大小。
#lvextend -L 150M / +50M /dev/vg01/lv01
-L 150M 指定扩展逻辑卷的大小为150M
+50M 当前基础上,增加50M
/dev/vg01/lv01 需要扩展的逻辑卷
[root@localhost ~]#
[root@localhost ~]# lvextend -L 150M /dev/vg01/lv01
Rounding up size to full physical extent 152.00 MB
Extending logical volume lv01 to 152.00 MB
Logical volume lv01 successfully resized
[root@localhost ~]#
逻辑卷被成功的扩大到150MB
用lvdisplay来查看卷组是否变大了
[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Name /dev/vg01/lv01
VG Name vg01
LV UUID 7oARRY-vvp2-gSHw-W60h-LtbC-YBVs-JU2c8n
LV Write Access read/write
LV Status available
# open 1
LV Size 152.00MB
Current LE 38
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
[root@localhost ~]#
可以看到,/dev/vg01/lv01的确被扩大到150MB
但是,有个很重要的地方,虽然我们用lvdisplay看到逻辑卷的确被扩展到150MB但用df –h查看到,我们逻辑卷的大小还是100MB。
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 3.9G 2.3G 1.5G 61% /
/dev/sda6 494M 11M 458M 3% /home
/dev/sda2 3.9G 1.8G 2.0G 49% /var
/dev/sda1 99M 14M 81M 15% /boot
tmpfs 188M 0 188M 0% /dev/shm
/dev/md1 198M 14M 174M 8% /mnt/raid5
/dev/hdc 2.8G 2.8G 0 100% /media/RHEL_5.4 i386 DVD
/dev/mapper/vg01-lv01
97M 5.6M 87M 7% /mnt/lvm
[root@localhost ~]#
现在有条命令可以解决这个问题
这条命令的作用就是将逻辑卷的大小拉升到挂载点上面,不需离线操作
#resize2fs /dev/vg01/lv01
[root@localhost ~]#
[root@localhost ~]# resize2fs /dev/vg01/lv01
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/vg01/lv01 is mounted on /mnt/lvm; on-line resizing required
Performing an on-line resize of /dev/vg01/lv01 to 155648 (1k) blocks.
The filesystem on /dev/vg01/lv01 is now 155648 blocks long.
[root@localhost ~]#
根据提示,可以看到逻辑卷被拉升到挂载点了,用df –h看下
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 3.9G 2.3G 1.5G 61% /
/dev/sda6 494M 11M 458M 3% /home
/dev/sda2 3.9G 1.8G 2.0G 49% /var
/dev/sda1 99M 14M 81M 15% /boot
tmpfs 188M 0 188M 0% /dev/shm
/dev/md1 198M 14M 174M 8% /mnt/raid5
/dev/hdc 2.8G 2.8G 0 100% /media/RHEL_5.4 i386 DVD
/dev/mapper/vg01-lv01
148M 5.6M 135M 4% /mnt/lvm
[root@localhost ~]#
Lv01果然就在线的扩展到150M了。
9. 减小卷组(将一个物理卷从卷组中T出去)
#vgreduce vg01 /dev/sda11
[root@localhost ~]#
[root@localhost ~]# vgreduce vg01 /dev/sda11
Removed "/dev/sda11" from volume group "vg01"
[root@localhost ~]#
这样就将一个物理卷从卷组中移除出去了。
10. 删除逻辑卷
#lvremove /dev/vg01/lv01
[root@localhost ~]#
[root@localhost ~]# lvremove /dev/vg01/lv01
Do you really want to remove active logical volume lv01? [y/n]: y
Logical volume "lv01" successfully removed
[root@localhost ~]#
逻辑卷就已经成功删除了,删除前先卸载
11.删除卷组
#vgremove vg01
[root@localhost ~]#
[root@localhost ~]# vgremove vg01
Volume group "vg01" successfully removed
[root@localhost ~]#
卷组就被移除了
12.删除物理卷
#pvremove /dev/sda11 /devsda9 /dev/sda10
[root@localhost ~]#
[root@localhost ~]# pvremove /dev/sda8 /dev/sda9 /dev/sda10
Labels on physical volume "/dev/sda8" successfully wiped
Labels on physical volume "/dev/sda9" successfully wiped
Labels on physical volume "/dev/sda10" successfully wiped
[root@localhost ~]#
物理卷就全部删除了
建立逻辑卷(create)
Linux分区-------物理卷--------卷组----------逻辑卷
删除(remove)
逻辑卷-------卷组--------物理卷-----------linux分区
重点:
13. 减小逻辑卷
减小逻辑卷必须离线,必须卸载
#umount /mnt/lvm 卸载
#e2fsck –f /dev/vg01/lv01 扫描
#resize2fs /dev/vg01/lv01 300M 减小文件系统
#lvreduce -L 300M /dev/vg01/lv01 减小逻辑卷
首先复制些数据到/mnt/lvm下面
[root@localhost ~]#
[root@localhost ~]# cp /boot/* /mnt/lvm/
cp: omitting directory `/boot/grub'
cp: omitting directory `/boot/lost+found'
[root@localhost ~]# cd /mnt/lvm/
[root@localhost lvm]# ls
config-2.6.18-164.el5 symvers-2.6.18-164.el5.gz
initrd-2.6.18-164.el5.img System.map-2.6.18-164.el5
initrd-2.6.18-164.el5kdump.img vmlinuz-2.6.18-164.el5
lost+found
[root@localhost lvm]#
这样有利于做试验,下面开始正式减小逻辑卷。
首先卸载文件系统
[root@localhost ~]# umount /mnt/lvm/
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 3.9G 2.3G 1.5G 61% /
/dev/sda6 494M 11M 458M 3% /home
/dev/sda2 3.9G 1.8G 2.0G 49% /var
/dev/sda1 99M 14M 81M 15% /boot
tmpfs 188M 0 188M 0% /dev/shm
/dev/md1 198M 14M 174M 8% /mnt/raid5
/dev/hdc 2.8G 2.8G 0 100% /media/RHEL_5.4 i386 DVD
[root@localhost ~]#
然后扫描文件系统
[root@localhost ~]# e2fsck -f /dev/vg01/lv01
e2fsck 1.39 (29-May-2006)
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/vg01/lv01: 17/25688 files (11.8% non-contiguous), 16616/102400 blocks
[root@localhost ~]#
减小文件系统
[root@localhost ~]#
[root@localhost ~]# resize2fs /dev/vg01/lv01 50M
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/vg01/lv01 to 51200 (1k) blocks.
The filesystem on /dev/vg01/lv01 is now 51200 blocks long.
[root@localhost ~]#
最好减小逻辑卷
[root@localhost ~]# lvreduce -L 50M /dev/vg01/lv01
Rounding up size to full physical extent 52.00 MB
WARNING: Reducing active logical volume to 52.00 MB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv01? [y/n]: y
Reducing logical volume lv01 to 52.00 MB
Logical volume lv01 successfully resized
[root@localhost ~]#
我们的逻辑卷被成功的减小到50M了
用df –h看下
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 3.9G 2.3G 1.5G 61% /
/dev/sda6 494M 11M 458M 3% /home
/dev/sda2 3.9G 1.8G 2.0G 49% /var
/dev/sda1 99M 14M 81M 15% /boot
tmpfs 188M 0 188M 0% /dev/shm
/dev/md1 198M 14M 174M 8% /mnt/raid5
/dev/hdc 2.8G 2.8G 0 100% /media/RHEL_5.4 i386 DVD
/dev/mapper/vg01-lv01
49M 13M 34M 28% /mnt/lvm
[root@localhost ~]#
我们的逻辑卷被减小到了50M
看看数据丢了没有
[root@localhost ~]#
[root@localhost ~]# cd /mnt/lvm/
[root@localhost lvm]# ls
config-2.6.18-164.el5 symvers-2.6.18-164.el5.gz
initrd-2.6.18-164.el5.img System.map-2.6.18-164.el5
initrd-2.6.18-164.el5kdump.img vmlinuz-2.6.18-164.el5
lost+found
[root@localhost lvm]#
可以看到,我们的数据还是好的,
在linux下面创建逻辑卷的讨论就到这里了。