Linux对LVM操作

公司的FTP服务器经过几年的运行,现在出现了空间不足的情况,决定用LVM方式实现扩容工作。
思路:先拷贝数据,再购置一块大容量硬盘添加到服务器上,然后将己有的普通分区转换为物理卷,最后通过物理卷创建逻辑卷组来实现容量的扩充。
实现过程:
A、检查系统中是否安装了LVM工具,如果没有安装就下载安装。
[root@linuxas ~]# rpm -qa | grep lvm
system-config-lvm-1.0.22-1.0.el5
lvm2-2.02.16-3.el5
B、 创建和管理LVM
1、创建或转换分区
[root@linuxas ~]# fdisk /dev/sda
The number of cylinders for this disk is set to 1044.
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): p(查看分区情况)
Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 25 200781 83 Linux
/dev/sda2 26 535 4096575 83 Linux
/dev/sda3 536 854 2562367+ 83 Linux
/dev/sda4 855 1044 1526175 5 Extended
/dev/sda5 855 905 409626 82 Linux swap / Solaris
/dev/sda6 906 1044 1116486 83 Linux
Command (m for help): t (更改分区类型)
Partition number (1-6): 6 (将第6个分区转换成LVM)
Hex code (type L to list codes): 8e(将分区类型指定为“8e”就是LVM)
Changed system type of partition 6 to 8e (Linux LVM)
Command (m for help): w (保存)
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: 璁惧鎴栬祫婧愬繖.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.
2、创建物理卷
[root@linuxas /]# pvcreate /dev/sda6
Can't open /dev/sda6 exclusively. Mounted filesystem?
出现以上错误的原因是分区文件正在使用中,必须先umount才行。
[root@linuxas /]# pvcreate /dev/sda6
Physical volume "/dev/sda6" successfully created
建好物理卷后可以用“pvdisplay”命令查看物理卷情况
[root@linuxas /]# pvdisplay
--- NEW Physical volume ---
PV Name /dev/sda6
VG Name
PV Size 1.06 GB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID N2LgeT-RB4Y-8YEP-lO2J-tDWu-UeCT-4Obl8p
3、创建逻辑卷组
[root@linuxas /]# vgcreate tgt /dev/sda6
Volume group "tgt" successfully created
You have new mail in /var/spool/mail/root
查看逻辑卷组情况
[root@linuxas /]# vgdisplay
--- Volume group ---
VG Name tgt
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 1.06 GB (VG为逻辑卷组的大小)
PE Size 4.00 MB (PE为逻辑卷组最小存储单位)
Total PE 272
Alloc PE / Size 0 / 0
Free PE / Size 272 / 1.06 GB
VG UUID g33wzW-yCvX-gc64-YQuD-LYeP-zgra-TGAa8G
4、 激活逻辑卷组
[root@linuxas /]# vgchange -a y tgt
0 logical volume(s) in volume group "tgt" now active
5、创建逻辑卷
[root@linuxas /]# lvcreate -L 1000M -n lvm tgt (-L指定大小,-n指定该逻辑卷的名称)
Logical volume "lvm" created
6、创建文件系统
[root@linuxas /]# mkfs -j /dev/tgt/lvm
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
128000 inodes, 256000 blocks
12800 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=264241152
8 block groups
32768 blocks per group, 32768 fragments per group
16000 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 31 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
7、挂载文件系统
[root@linuxas /]# mount -t ext3 /dev/tgt/lvm /home
C、 为LVM扩容
1、新添一块硬盘并进行分区操作,并指定为LVM
[root@linuxas /]# fdisk -l
Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 25 200781 83 Linux
/dev/sda2 26 535 4096575 83 Linux
/dev/sda3 536 854 2562367+ 83 Linux
/dev/sda4 855 1044 1526175 5 Extended
/dev/sda5 855 905 409626 82 Linux swap / Solaris
/dev/sda6 906 1044 1116486 8e Linux LVM
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 1 652 5237158+ 8e Linux LVM
2、创建物理卷
[root@linuxas /]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created
=====================================
最后,w保存退出。再使用sfsik -l即可看到新增的磁盘。

此时,使用lvm的初始化磁盘工具或者直接使用pvcreate /dev/sda3命令会提示出错,提示

Device “/dev/sda3” not found (or ignored by filtering)

出错的原因在于使用fdisk修改配置时没有在runlevel 1下进行,重启系统后可解决此问题。
====================================
3、将新添的物理卷加入到已有的逻辑卷组中
[root@linuxas /]# vgextend tgt /dev/sdb1
Volume group "tgt" successfully extended
4、为LVM扩容
[root@linuxas /]# lvextend -L 5368M /dev/tgt/lvm (此命令将此卷的总容量定为5368M)
Extending logical volume lvm to 5.24 GB
Logical volume lvm successfully resized
或者用[root@linuxas /]# lvextend -L +1000M /dev/tgt/lvm(此命令将此卷的总容量增加1000M)
[root@linuxas /]# e2fsck -f /dev/tgt/lvm                          ( 检查LVM设备对应inode和block之间的关系)
[root@linuxas /]# resize2fs /dev/tgt/lvm (使增加的容量生效)
 
 

删除lv、vg、pv
lvremove /dev/arch01vg/arch01lv
vgremove arch01vg
pvremove /dev/sde

相关推荐