[iyunv@localhost ~]# fdisk /dev/sdb //对/dev/sdb硬盘进行分区
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): p
Disk /dev/sdb: 85.9 GB, 85899345920 bytes, 167772160 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0009c3cd
Device Boot Start End Blocks Id System
Command (m for help): n //创建一个新的分区
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-167772159, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-167772159, default 167772159): +100M
Partition 1 of type Linux and of size 100 MiB is set
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e //更改分区类型为8e
Changed type of partition 'Linux' to 'Linux LVM' //显示更改为Linux LVM
Command (m for help): p //查看一下刚才创建的分区,system类型显示为Linux LVM
Disk /dev/sdb: 85.9 GB, 85899345920 bytes, 167772160 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0009c3cd
Device Boot Start End Blocks Id System
/dev/sdb1 2048 206847 102400 8e Linux LVM
Command (m for help): w //存盘退出
The partition table has been altered!
[iyunv@localhost ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): p //显示分区信息
Disk /dev/sdb: 85.9 GB, 85899345920 bytes, 167772160 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0009c3cd
Device Boot Start End Blocks Id System
/dev/sdb1 2048 206847 102400 8e Linux LVM //现在共有三个LVM类型的分区
/dev/sdb2 206848 411647 102400 8e Linux LVM
/dev/sdb3 411648 616447 102400 8e Linux LVM
2)创建PV
1
2
3
4
[iyunv@localhost ~]# pvcreate /dev/sdb{1,2,3} //将/dev/sdb{1,2,3}创建为PV
Physical volume "/dev/sdb1" successfully created
Physical volume "/dev/sdb2" successfully created
Physical volume "/dev/sdb3" successfully created
3)创建VG
1
2
[iyunv@localhost ~]# vgcreate myvg /dev/sdb{1,2} //将/dev/sdb{1,2}创建为卷组,卷组名为myvg
Volume group "myvg" successfully created
4)创建LV
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[iyunv@localhost ~]# lvcreate -n mylv -L 100M myvg //创建大小为100M,名字为mylv的LV
Logical volume "mylv" created.
[iyunv@localhost ~]# lvdisplay //查看系统上LV的信息,这里仅仅有且只有一个,即刚刚创建的
--- Logical volume ---
LV Path /dev/myvg/mylv //路径为/dev/myvg/mylv
LV Name mylv //名称为mylv
VG Name myvg //卷组名为myvg
LV UUID OkCyEj-sTkC-6Kch-svPr-UAgv-mQuf-uRofNj
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2016-08-30 14:46:48 +0800
LV Status available
# open 0
LV Size 100.00 MiB //大小为100MB
Current LE 25 //LE数量,其实就是PE,因为被分配到了LV里面,所以改叫LE
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
[iyunv@localhost ~]# cd /testdir/ //进入挂载的目录
[iyunv@localhost testdir]# ls
lost+found
[iyunv@localhost testdir]# touch f1 //创建文件
[iyunv@localhost testdir]# ls //创建成功!
f1 lost+found
[iyunv@localhost testdir]# pwd
/testdir
如果此分区的空间即将被填满,如何扩展LV的容量?步骤如下
1)如果VG还有空间
1
2
3
4
5
6
7
[iyunv@localhost testdir]# lvextend -L +50M /dev/myvg/mylv //增加50M
Rounding size to boundary between physical extents: 52.00 MiB
Size of logical volume myvg/mylv changed from 100.00 MiB (25 extents) to 152.00 MiB (38 extents).
Logical volume mylv successfully resized.
[iyunv@localhost testdir]# lvs //简略显示LV的信息
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
mylv myvg -wi-ao---- 152.00m //增加了52M,以为PE是最小存储单元,所以必须是PE的整数倍
[iyunv@localhost testdir]# resize2fs /dev/myvg/mylv //重新设置LV的容量
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/myvg/mylv is mounted on /testdir; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 2
The filesystem on /dev/myvg/mylv is now 155648 blocks long.
[iyunv@localhost testdir]# vgextend myvg /dev/sdb3 //将之前创建的PV/dev/sdb3添加至卷组myvg
Volume group "myvg" successfully extended
[iyunv@localhost testdir]# vgdisplay //查看VG的详细信息
--- Volume group ---
VG Name myvg
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 6
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 3
Act PV 3
VG Size 288.00 MiB //增加了/dev/sdb3的容量,现在为288MB的空间。增加成功!
PE Size 4.00 MiB
Total PE 72
Alloc PE / Size 38 / 152.00 MiB //已经分配的PE/空间大小
Free PE / Size 34 / 136.00 MiB //空闲的的PE/空间大小
VG UUID ObuSOV-RmEg-m2Ec-GIe1-JzhC-E3kd-1XgOP
[iyunv@localhost testdir]# fdisk /dev/sdc
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): p
Disk /dev/sdc: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xd7e4d605
Device Boot Start End Blocks Id System
/dev/sdc1 2048 206847 102400 8e Linux LVM //LVM类型的分区
[iyunv@localhost testdir]# pvcreate /dev/sdc1 //创建/dev/sdc1为PV
Physical volume "/dev/sdc1" successfully created
[iyunv@localhost ~]# pvcreate /dev/sd{d,e} //将/dev/sd{d,e}创建为PV
Physical volume "/dev/sdd" successfully created
Physical volume "/dev/sde" successfully created
[iyunv@localhost ~]# resize2fs /dev/myvg/mylv 100M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/myvg/mylv to 102400 (1k) blocks.
The filesystem on /dev/myvg/mylv is now 102400 blocks long.
4、缩减LV的容量
1
2
3
4
5
6
7
8
9
[iyunv@localhost ~]# lvreduce -L 100M /dev/myvg/mylv //将LV缩减到100M
WARNING: Reducing active logical volume to 100.00 MiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce mylv? [y/n]: yes
Size of logical volume myvg/mylv changed from 152.00 MiB (38 extents) to 100.00 MiB (25 extents).
Logical volume mylv successfully resized.
[iyunv@localhost ~]# lvs //查看LV的大小
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
mylv myvg -wi-a----- 100.00m //显示100M,缩减成功