一.查看系统状态
1.查看Linux系统上的文件系统的磁盘使用情况,发现/目录快满了。
[root@Redhat6 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_starnet-lv_root
18G 14G 2.7G 84% /
tmpfs 5.2G 224K 5.2G 1% /dev/shm
/dev/sda1 477M 40M 412M 9% /boot
2.查看挂载硬盘及分区情况,发现LINUX磁盘实际53.7GB,实际挂载的才20G左右。
[root@Redhat6 ~]# fdisk -l
Disk /dev/sda: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00003ec7Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 2611 20458496 8e Linux LVMDisk /dev/mapper/vg_starnet-lv_root: 18.8 GB, 18798870528 bytes
255 heads, 63 sectors/track, 2285 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/mapper/vg_starnet-lv_swap: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
3.查看PV,发现只有一个20G的/dev/sda2
[root@Redhat6 ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name vg_starnet
PV Size 19.51 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 4994
Free PE 0
Allocated PE 4994
PV UUID AYnwsY-Il90-KuTY-Sh2y-IwMb-7CiY-orm4w5
查看VG
[root@Redhat6 ~]# vgdisplay
--- Volume group ---
VG Name vg_starnet
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 19.51 GiB
PE Size 4.00 MiB
Total PE 4994
Alloc PE / Size 4994 / 19.51 GiB
Free PE / Size 0 / 0
VG UUID oAVlIk-yvc0-sGc9-RAPi-ceYQ-tFS3-PAGEgn
二.创建新的主分区
fdisk /dev/sda依次输入(n->p ->3->回车->回车-> t->8e -> w)
Command (m for help): n # 新建分区
Command action
e extended
p primary partition (1-4)
p # 创建一个主分区
Partition number (1-4): 3 #已经有/dev/sda1和/dev/sda2,所以从3开始
First cylinder (2611-6527, default 2611): #设置起始柱面,回车选择默认
Using default value 2611
Last cylinder, +cylinders or +size{K,M,G} (2611-6527, default 6527): #设置终止柱面,回车选择默认
Using default value 6527Command (m for help): t #设置分区类型
Partition number (1-4): 3
Hex code (type L to list codes): 8e #设置分区类型为8e
Changed system type of partition 3 to 8e (Linux LVM)Command (m for help): w #保存
The partition table has been altered!
Calling ioctl() to re-read partition table.
三.让内核立刻读取最新的分区表,而不需要重启系统
[root@Redhat6 ~]# partprobe
查看分区,已经多了一个/dev/sda3
[root@Redhat6 ~]# fdisk -lDisk /dev/sda: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00003ec7Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 2611 20458496 8e Linux LVM
/dev/sda3 2611 6527 31456607+ 8e Linux LVMDisk /dev/mapper/vg_starnet-lv_root: 18.8 GB, 18798870528 bytes
255 heads, 63 sectors/track, 2285 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/mapper/vg_starnet-lv_swap: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
查看文件系统,/目录剩余空间没变。还需待扩展。
[root@Redhat6 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_starnet-lv_root
18G 14G 2.7G 84% /
tmpfs 5.2G 76K 5.2G 1% /dev/shm
/dev/sda1 477M 40M 412M 9% /boot
四.创建PV
[root@Redhat6 ~]# pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created
查看PV,可以看到多了个/dev/sda3
[root@Redhat6 ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name vg_starnet
PV Size 19.51 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 4994
Free PE 0
Allocated PE 4994
PV UUID AYnwsY-Il90-KuTY-Sh2y-IwMb-7CiY-orm4w5
"/dev/sda3" is a new physical volume of "30.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sda3
VG Name
PV Size 30.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 7nBK0C-5fqV-z1tM-IFn2-vFoh-UiQK-s4NIDA
五.扩容VG
[root@Redhat6 ~]# vgdisplay
--- Volume group ---
VG Name vg_starnet
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 19.51 GiB
PE Size 4.00 MiB
Total PE 4994
Alloc PE / Size 4994 / 19.51 GiB
Free PE / Size 0 / 0
VG UUID oAVlIk-yvc0-sGc9-RAPi-ceYQ-tFS3-PAGEgn
将新建的/dev/sda3增加到vg_starnet中
[root@Redhat6 ~]# vgextend vg_starnet /dev/sda3
Volume group "vg_starnet" successfully extended
再执行vgdisplay,可以看到VG已经增加到50G了。
[root@Redhat6 ~]# vgdisplay
--- Volume group ---
VG Name vg_starnet
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 2
Act PV 2
VG Size 49.50 GiB
PE Size 4.00 MiB
Total PE 12673
Alloc PE / Size 4994 / 19.51 GiB
Free PE / Size 7679 / 30.00 GiB
VG UUID oAVlIk-yvc0-sGc9-RAPi-ceYQ-tFS3-PAGEgn
五.扩容LV
每个PE大小4M,30G*1024/4= 7680. 最多有7680个PE。
[root@Redhat6 ~]# lvresize -l +7679 /dev/vg_starnet/lv_root
六.刷新文件系统大小
[root@Redhat6 ~]# resize2fs /dev/vg_starnet/lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg_starnet/lv_root is mounted on /; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 3
Performing an on-line resize of /dev/vg_starnet/lv_root to 12452864 (4k) blocks.
The filesystem on /dev/vg_starnet/lv_root is now 12452864 blocks long.
查看文件系统,根目录的可用空间已经增加。
[root@Redhat6 ~]# df -l
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/vg_starnet-lv_root
48901412 14254964 32170940 31% /
tmpfs 5444328 76 5444252 1% /dev/shm
/dev/sda1 487652 40801 421251 9% /boot
名词解释:
LVM 的全名是 Logical Volume Manager,中文可以翻译作逻辑卷轴管理员。之所以称为『卷轴』可能是因为可以将 filesystem 像卷轴一样伸长或缩短之故吧!LVM 的作法是将几个实体的 partitions (或 disk) 透过软件组合成为一块看起来是独立的大磁碟 (VG) ,然后将这块大磁碟再经过分割成为可使用分割槽 (LV), 最终就能够挂载使用了。但是为什么这样的系统可以进行 filesystem 的扩充或缩小呢?其实与一个称为 PE 的项目有关! 底下我们就得要针对这几个项目来好好聊聊!
- Physical Volume, PV, 实体卷轴
我们实际的 partition 需要调整系统识别码 (system ID) 成为 8e (LVM 的识别码),然后再经过 pvcreate 的命令将他转成 LVM 最底层的实体卷轴 (PV) ,之后才能够将这些 PV 加以利用! 调整 system ID 的方是就是透过 fdisk 啦!
- Volume Group, VG, 卷轴群组
所谓的 LVM 大磁碟就是将许多 PV 整合成这个 VG 的东西就是啦!所以 VG 就是 LVM 组合起来的大磁碟!这么想就好了。 那么这个大磁碟最大可以到多少容量呢?这与底下要说明的 PE 有关喔~因为每个 VG 最多仅能包含 65534 个 PE 而已。 如果使用 LVM 默认的参数,则一个 VG 最大可达 256GB 的容量啊!(参考底下的 PE 说明)
- Physical Extend, PE, 实体延伸区块
LVM 默认使用 4MB 的 PE 区块,而 LVM 的 VG 最多仅能含有 65534 个 PE ,因此默认的 LVM VG 会有 4M*65534/(1024M/G)=256G。 这个 PE 很有趣喔!他是整个 LVM 最小的储存区块,也就是说,其实我们的文件数据都是藉由写入 PE 来处理的。 简单的说,这个 PE 就有点像文件系统里面的 block 大小啦。 这样说应该就比较好理解了吧?所以调整 PE 会影响到 VG 的最大容量喔!
- Logical Volume, LV, 逻辑卷轴
最终的 VG 还会被切成 LV,这个 LV 就是最后可以被格式化使用的类似分割槽的咚咚了!那么 LV 是否可以随意指定大小呢? 当然不可以!既然 PE 是整个 LVM 的最小储存单位,那么 LV 的大小就与在此 LV 内的 PE 总数有关。 为了方便使用者利用 LVM 来管理其系统,因此 LV 的装置档名通常指定为『 /dev/vgname/lvname 』的样式!
参考资料:
鸟哥的Linux私房菜: http://cn.linux.vbird.org/linux_basic/0420quota_3.php