创建逻辑卷组:
一、将扩展的硬盘分区
新加一块SCSI硬盘,需要将其分成三个区:代码:
#fdisk /dev/sdb进入fdisk模式:Command (m for help):p //查看新硬盘的分区Command (m for help):n //创建新分区
可以用m命令来看fdisk命令的内部命令;n命令创建一个新分区;d命令删除一个存在的分区;p命令显示分区列表;t命令修改分区的类型ID号;l命令显示分区ID号的列表;a命令指定启动分区;w命令是将对分区表的修改存盘让它发生作用。
Command action
e extended //输入e为创建扩展分区p primary partition (1-4) //输入p为创建主分区,这里我们选择p
Partion number(1-4):1 //第一个扩展分区,按你需求可以最多分4个主分区First Cylinder(1-1014,default 1): 1 //第一个主分区起始的磁盘块数Last cylindet or +siza or +sizeM or +sizeK: +1024MB //可以是以MB为单位的数字或者以
磁盘块数,这里我们输入+1024MB表示分区大小为1G。
这样我们就创建完一个分区,如果要创建更多分区可以照上面的步骤继续创建。
创建完后用w保存分区。代码:
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
#partprobe //不用重启可使以上操作生效。
这样就分区完,我们还要进行格式化代码:
#mkfs.ext3 /dev/sdb1 //如果有多个分区,则分区修改为sdb2这样
=====================================================================
二、创建逻辑卷的步骤:1)通过pvcreate命令将linux分区处理成物理卷(PV);2)通过vgcreate命令将创建好的物理卷处理成卷组(Vg);3)通过lvcreate命令将卷组分成若干个逻辑卷(Lv);之后我们可以对逻辑卷进行格式化,挂载,删除等操作,我们可以动态的调整逻辑卷的大小,并且该操作不会影响我们在逻辑卷(Lv)上的数据。
三、物理卷创建管理操作:1)pvcreate创建物理卷:(把普通Linux分区变成物理卷)
格式:pvcreate /dev/sdb{1,2,3...}
———————————————————————————————
[root@station1 ~]# [root@station1 ~]# pvcreate /dev/sdb{1,2,3,5,6,7}
Physical volume "/dev/sdb3" successfully created
Physical volume "/dev/sdb5" successfully created
Physical volume "/dev/sdb6" successfully created
Physical volume "/dev/sdb7" successfully created
————————————————————————————————
2)pvscan查看物理卷信息:会显示所有物理卷的情况。格式:pvscan
————————————————————————————————
[root@station1 ~]# pvscan
PV /dev/sdb1 lvm2 [964.81 MB]
PV /dev/sdb2 lvm2 [964.84 MB]
PV /dev/sdb3 lvm2 [964.84 MB]
PV /dev/sdb5 lvm2 [964.81 MB]
PV /dev/sdb6 lvm2 [964.81 MB]
PV /dev/sdb7 lvm2 [964.81 MB]
Total: 6 [5.65 GB] / in use: 0 [0 ] / in no VG: 6 [5.65 GB]
————————————————————————————————
3)pvdisplay查看各个物理卷的详细参数:格式:pvdisplay [/dev/sdb{1,2,3...}]不跟参数则显示所有的物理卷信息
[root@station1 ~]# pvdisplay /dev/sdb1
"/dev/sdb1" is a new physical volume of "964.81 MB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 964.81 MB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID FKaYKk-d46E-KHCQ-WRnn-jUUE-Cd1d-mM8mPN
————————————————————————————————
4)pvremove删除物理卷:当物理卷里的扩展快没有被使用时可以删除。格式:pvremove /dev/sdb5
——————————————————————————————
[root@station1 ~]# pvscan
PV /dev/sdb7 VG vg1 lvm2 [960.00 MB / 960.00 MB free]
PV /dev/sdb1 VG vg0 lvm2 [964.00 MB / 964.00 MB free]
PV /dev/sdb2 VG vg0 lvm2 [964.00 MB / 964.00 MB free]
PV /dev/sdb3 VG vg0 lvm2 [964.00 MB / 964.00 MB free]
PV /dev/sdb5 lvm2 [964.81 MB]
PV /dev/sdb6 lvm2 [964.81 MB]
Total: 6 [5.65 GB] / in use: 4 [3.76 GB] / in no VG: 2 [1.88 GB]
[root@station1 ~]# pvremove /dev/sdb5
Labels on physical volume "/dev/sdb5" successfully wiped
[root@station1 ~]# pvscan
PV /dev/sdb7 VG vg1 lvm2 [960.00 MB / 960.00 MB free]
PV /dev/sdb1 VG vg0 lvm2 [964.00 MB / 964.00 MB free]
PV /dev/sdb2 VG vg0 lvm2 [964.00 MB / 964.00 MB free]
PV /dev/sdb3 VG vg0 lvm2 [964.00 MB / 964.00 MB free]
PV /dev/sdb6 lvm2 [964.81 MB]
Total: 5 [4.70 GB] / in use: 4 [3.76 GB] / in no VG: 1 [964.81 MB]
————————————————————————————————
四.卷组的创建管理:
1)vgcreate创建卷组:格式:vgcreate [-s <8M|16M|...>] vg0 /dev/sdb{1,2,3...}
-s:指定扩展块(PE)的大小,默认为4MB;(相当与磁盘上的簇,扩展逻辑卷的基本单位。后面的值可以是8M 16M 32M 64M .....最多65532个扩展块。vg0:新创建的卷组的名字。/dev/sdb{1,2,3...}:需要的物理卷可以写多个;————————————————————————————————
[root@station1 ~]# vgcreate vg0 /dev/sdb{1,2,3}
Volume group "vg0" successfully created
————————————————————————————————
2)vgdisplay查看各个卷组的详细参数:格式:vgdisplay /dev/vg0
————————————————————————————————
[root@station1 ~]# vgdisplay /dev/vg0
--- Volume group ---
VG Name vg0
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 1
VG Access read/write
VG Status resizable
Cur PV 3
Act PV 3
VG Size 2.82 GB
PE Size 4.00 MB //扩展快大小 默认4MB
Total PE 723
Alloc PE / Size 0 / 0
Free PE / Size 723 / 2.82 GB
VG UUID w4q8ET-UBLY-rHwg-2ZVv-NOw4-dd1e-1ABr2f
[root@station1 ~]# vgcreate -s 16M vg1 /dev/sdb{5,6,7}
Volume group "vg1" successfully created
[root@station1 ~]# vgdisplay /dev/vg1
--- Volume group ---
VG Name vg1
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 1
VG Access read/write
VG Status resizable
Cur PV 3
Act PV 3
VG Size 2.81 GB
PE Size 16.00 MB //指定扩展块大小为16MB
Total PE 180
Alloc PE / Size 0 / 0
Free PE / Size 180 / 2.81 GB
VG UUID x97eh2-JYxW-4Ygh-5axx-TquS-0vxR-YdFyJ1
———————————————————————————————————
3)vgscan查看卷组的信息:格式:vgsacn
————————————————————————————————
[root@station1 ~]# vgscan
Found volume group "vg1" using metadata type lvm2
Found volume group "vg0" using metadata type lvm2
—————————————————————————————————
4)vgreduce缩小卷组:把物理卷重卷组中删除。格式:vgreduce vg0 /dev/sdb3
——————————————————————————————————
[root@station1 ~]# vgreduce vg0 /dev/sdb3
Removed "/dev/sdb3" from volume group "vg0"
——————————————————————————————————
4)vgextend扩展卷组:把某个物理卷添加到卷组中。格式:vgextend vg0 /dev/sdb2将物理卷/dev/sdb2添加到vg0卷组——————————————————————————————————
[root@station1 ~]# vgextend vg0 /dev/sdb3
Volume group "vg0" successfully extended
——————————————————————————————————
5)vgremove删除卷组:格式:vgremove /dev/vg1
——————————————————————————————————
[root@station1 ~]# pvscan
PV /dev/sdb7 VG vg1 lvm2 [960.00 MB / 960.00 MB free]
PV /dev/sdb5 VG vg1 lvm2 [960.00 MB / 960.00 MB free]
PV /dev/sdb6 VG vg1 lvm2 [960.00 MB / 960.00 MB free]
PV /dev/sdb1 VG vg0 lvm2 [964.00 MB / 964.00 MB free]
PV /dev/sdb2 VG vg0 lvm2 [964.00 MB / 964.00 MB free]
PV /dev/sdb3 VG vg0 lvm2 [964.00 MB / 964.00 MB free]
Total: 6 [5.64 GB] / in use: 6 [5.64 GB] / in no VG: 0 [0 ]
[root@station1 ~]# vgremove /dev/vg1
Volume group "vg1" successfully removed
[root@station1 ~]# pvscan
PV /dev/sdb1 VG vg0 lvm2 [964.00 MB / 964.00 MB free]
PV /dev/sdb2 VG vg0 lvm2 [964.00 MB / 964.00 MB free]
PV /dev/sdb3 VG vg0 lvm2 [964.00 MB / 964.00 MB free]
PV /dev/sdb5 lvm2 [964.81 MB]
PV /dev/sdb6 lvm2 [964.81 MB]
PV /dev/sdb7 lvm2 [964.81 MB]
Total: 6 [5.65 GB] / in use: 3 [2.82 GB] / in no VG: 3 [2.83 GB]
——————————————————————————————————
五、逻辑卷的创建及管理:
1)lvcreate创建逻辑卷命令:格式1:lvcreate -L 512M -n data vg0
-L 512M:设置逻辑卷的大小为512M。-n data:设置逻辑卷的名字为data;表示法:/dev/卷组名/data
vg0:设置是有那个卷组生成的逻辑卷。——————————————————————————————————
[root@station1 ~]# lvcreate -L 1000M -n data vg0
Logical volume "data" created
//创建一个容量为1000MB的逻辑卷data
——————————————————————————————————格式2:lvcreate -l 32 -n data vg0
-l 32:生成的逻辑卷使用32个扩展块,逻辑卷大小=32*PE(默认4M)则为128M
-n data:逻辑卷的名字;vg0:设置是有那个卷组生成的逻辑卷。——————————————————————————————————
[root@station1 ~]# lvcreate -l 32 -n data128 vg0
Logical volume "data128" created
//创建一个32个扩展块,容量为32*4=128MB的逻辑卷data128
——————————————————————————————————
2)格式化逻辑卷:mkfs.ext3 /dev/vg0/data
3)lvsacn查看逻辑卷的信息:¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬——————————————————————————————————
[root@station1 ~]# lvscan
ACTIVE '/dev/vg0/data' [1000.00 MB] inherit
——————————————————————————————————
4)lvdisplay查看逻辑卷的具体参数:——————————————————————————————————
[root@station1 ~]# lvdisplay /dev/vg0/data
--- Logical volume ---
LV Name /dev/vg0/data
VG Name vg0
LV UUID 9Od1nq-cHmY-MlrL-vgXW-7Kam-pAfU-Qc8mal
LV Write Access read/write
LV Status available
# open 0
LV Size 1000.00 MB
Current LE 250
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
——————————————————————————————————
3)增大逻辑卷大小:lvextend命令:增大逻辑卷大小格式:lvextend -L +100M /dev/vg0/data
——————————————————————————————————
[root@station1 ~]# lvextend -L +1000M /dev/vg0/data
Extending logical volume data to 1.95 GB
Logical volume data successfully resized
——————————————————————————————————
resize2fs /dev/vg0/data使上面增加的立即生效。——————————————————————————————————
[root@station1 ~]# resize2fs /dev/vg0/data
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/vg0/data to 512000 (4k) blocks.
The filesystem on /dev/vg0/data is now 512000 blocks long.
[root@station1 ~]# lvscan
ACTIVE '/dev/vg0/data' [1.95 GB] inherit
——————————————————————————————————
4)减少逻辑卷大小:lvreduce命令:减少逻辑卷大小格式:Lvreduce -L -100M /dev/vg0/data
——————————————————————————————————
[root@station1 ~]# lvreduce -L -1000M /dev/vg0/data
WARNING: Reducing active logical volume to 1000.00 MB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce data? [y/n]: y
Reducing logical volume data to 1000.00 MB
Logical volume data successfully resized
——————————————————————————————————
resize2fs /dev/vg0/data使上面增加的立即生效。——————————————————————————————————
[root@station1 ~]# resize2fs /dev/vg0/data
resize2fs 1.39 (29-May-2006)
resize2fs: Bad magic number in super-block while trying to open /dev/vg0/data
Couldn't find valid filesystem superblock.
[root@station1 ~]# lvscan
ACTIVE '/dev/vg0/data' [1000.00 MB] inherit
ACTIVE '/dev/vg0/data128' [128.00 MB] inherit
——————————————————————————————————
5)lvremove删除逻辑卷:格式:lvremove /dev/卷组名/逻辑卷名——————————————————————————————————
[root@station1 ~]# lvremove /dev/vg0/data128
Do you really want to remove active logical volume "data128"? [y/n]: y
Logical volume "data128" successfully removed
[root@station1 ~]# lvscan
ACTIVE '/dev/vg0/data' [1000.00 MB] inherit
——————————————————————————————————六、LVM的快照:1)快照的创建:Lvcreate -l 64 -s -n databackup /dev/vg0/data
-l 64:之前我们已经说过了这里是64个扩展块,总容量=64*单扩展块大小-n databackup:生成的快照的名字。/dev/vg0/data:源逻辑卷名字。————————————————————————————————————
[root@station1 ~]# lvscan
ACTIVE '/dev/vg0/data' [1000.00 MB] inherit
[root@station1 ~]# lvcreate -L 1000M -s -n databackup /dev/vg0/data
Logical volume "databackup" created
[root@station1 ~]# lvscan
ACTIVE Original '/dev/vg0/data' [1000.00 MB] inherit
ACTIVE Snapshot '/dev/vg0/databackup' [1000.00 MB] inherit
————————————————————————————————————
2)快照的删除:lvremove /dev/vg0/databackup
————————————————————————————————————
[root@station1 ~]# lvremove /dev/vg0/databackup
Do you really want to remove active logical volume "databackup"? [y/n]: y
Logical volume "databackup" successfully removed
————————————————————————————————————
七、挂载将新分的卷挂载在需要挂载的目录下:
格式:mount /dev// /要挂载的点
Mkdir –p /tmp/123//建挂载点在/tmp/123下挂载
# Mount /dev/VolGroup0/data /tmp/123
设置永久挂载:
# vi /etc/fstab
/dev/VolGroup0/data /tmp/123 ext3defaults 1 2
Mout可以查看文件系统格式
八、总结:创建逻辑卷的顺序:Linux分区---物理卷---卷组---逻辑卷删除逻辑卷的顺序:逻辑卷---卷组----物理卷---Linux分区
--查看卷组的命令,可以看到总磁盘空间,剩余空间
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name VolGroup00
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 18
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 8
Open LV 8
Max PV 0
Cur PV 1
Act PV 1
VG Size 837.09 GB
PE Size 32.00 MB
Total PE 26787
Alloc PE / Size 21417 / 669.28 GB
Free PE / Size 5370 / 167.81 GB
VG UUIDdM0jwy-1nGJ-FMY1-0I4P-E9BF-bFcb-MKk0cv
--检查文件系统的磁盘空间占用情况
[root@localhost ~]# df -h /u04
文件系统容量已用可用 已用%挂载点
/dev/mapper/VolGroup00-u04
49G 43G3.3G 93% /u04
--扩展逻辑卷大小
[root@localhost ~]# lvextend -L +50G /dev/mapper/VolGroup00-u04
Extending logical volume u04 to 99.03 GB
Logical volume u04 successfully resized
--重新定义ext2,ext3或者是ext4文件系统
[root@localhost ~]# resize2fs /dev/mapper/VolGroup00-u04
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/mapper/VolGroup00-u04 is mounted on /u04; on-line resizing required
Performing an on-line resize of /dev/mapper/VolGroup00-u04 to 25960448 (4k) blocks.
The filesystem on /dev/mapper/VolGroup00-u04 is now 25960448 blocks long.
[root@localhost ~]# df -h /u04
文件系统容量已用可用 已用%挂载点
/dev/mapper/VolGroup00-u04
98G 43G50G 47% /u04