假如我的/data目录磁盘空间快满了,应该怎么扩容呢?这里用到了lvm.
LVM是Linux环境中对磁盘分区进行管理的一种机制,是建立在硬盘和分区之上、文件系统之下的一个逻辑层,可提高磁盘分区管理的灵活性。RHEL5默认安装的分区格式就是LVM逻辑卷的格式,需要注意的是/boot分区不能基于LVM创建,必须独立出来。
一:添加一块儿硬盘
右击虚拟机-设置-‘添加’
看到新添加的磁盘:
重启虚拟机
二:创建分区
查看磁盘分区情况,看到了我们新增的/dev/sdb
[root@localhost ~]# fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x000c6b7d
Device Boot Start End Blocks Id System
/dev/sda1 * 1 39 307200 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 39 2358 18631680 83 Linux
/dev/sda3 2358 2611 2031616 82 Linux swap / Solaris
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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@localhost ~]# fdisk /dev/sdb
Device contains neither a valid DOSpartition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with diskidentifier 0x91872026.
Changes will remain in memory only, untilyou decide to write them.
After that, of course, the previous contentwon't be recoverable.
Warning: invalid flag 0x0000 of partitiontable 4 will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated.It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibilityflag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOSpartition table
p print the partition table
q quit without saving changes
s create a new empty Sundisklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (expertsonly)
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-2610, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G}(1-2610, default 2610): (回车,默认将所有空间分配给第一个主分区)
Using default value 2610
Command (m for help): t (修改磁盘格式为LVM)
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e(Linux LVM)
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280bytes
Sector size (logical/physical): 512 bytes /512 bytes
I/O size (minimum/optimal): 512 bytes / 512bytes
Disk identifier: 0x000c6b7d
Device Boot Start End Blocks Id System
/dev/sda1 * 1 39 307200 83 Linux
Partition 1 does not end on cylinderboundary.
/dev/sda2 39 2358 18631680 83 Linux
/dev/sda3 2358 2611 2031616 82 Linux swap / Solaris
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280bytes
Sector size (logical/physical): 512 bytes /512 bytes
I/O size (minimum/optimal): 512 bytes / 512bytes
Disk identifier: 0x91872026
Device Boot Start End Blocks Id System
/dev/sdb1 1 2610 20964793+ 8e Linux LVM
三:创建物理卷
将新创建的主分区创建为物理卷
[root@localhost ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 lvm2 a-- 19.99g 19.99g
[root@localhost ~]# pvdisplay
"/dev/sdb1" is a new physical volume of "19.99 GiB"
---NEW Physical volume ---
PVName /dev/sdb1
VGName
PVSize 19.99 GiB
Allocatable NO
PESize 0
Total PE 0
Free PE 0
Allocated PE 0
PVUUID cnHu1a-PvGm-KFuQ-DkgP-Lcte-ra3z-lei7NB
四:扩展逻辑卷
df命令查看自己的/目录属于哪个逻辑卷(我们要做的是对逻辑卷扩展)
[root@localhost download]# df-h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 18G 4.1G 13G 25% /
tmpfs 491M 232K 491M 1% /dev/shm
/dev/sda1 291M 34M 242M 13% /boot
这里根本没有创建逻辑卷。所以,需要先创建逻辑卷,在创建逻辑卷前,也要先创建卷组。
如果机器上已经有逻辑卷了,请直接参考步骤五。
4.1 创建卷组
创建卷组 vgdata ,并将刚才创建好的物理卷加入该卷组.
[root@localhost ~]# vgcreate vgdata/dev/sdb1
Volume group "vgdata" successfully created
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vgdata 1 0 0wz--n- 19.99g 19.99g
[root@localhost ~]# vgdisplay
---Volume group ---
VGName vgdata
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 1
VGAccess read/write
VGStatus resizable
MAXLV 0
CurLV 0
Open LV 0
MaxPV 0
CurPV 1
ActPV 1
VGSize 19.99 GiB
PESize 4.00 MiB
Total PE 5118
Alloc PE / Size 0 / 0
Free PE / Size 5118 / 19.99 GiB
VGUUID LYynzk-qyDQ-stg3-QOkA-eckl-nDNf-CPxEl9
4.2 创建逻辑卷
[root@localhost ~]# lvcreate -L 19G -nlvdata1 vgdata
Logical volume "lvdata1" created
[root@localhost ~]# lvdisplay
---Logical volume ---
LVPath /dev/vgdata/lvdata1
LVName lvdata1
VGName vgdata
LVUUID IzeCPz-4upw-i9ov-qtnf-h5x1-tLQF-22sKFm
LVWrite Access read/write
LVCreation host, time localhost.localdomain, 2016-06-11 22:57:28 -0700
LVStatus available
#open 0
LVSize 19.00 GiB
Current LE 4864
Segments 1
Allocation inherit
Read ahead sectors auto
-currently set to 256
Block device 253:0
4.3 创建文件系统
使用mkfs.ext4命令在逻辑卷lvdata1上创建ext4文件系统
[root@localhost ~]# mkfs.ext4 /dev/vgdata/lvdata1
创建好之后,会在/dev/mapper/生成一个软连接名字为”卷组-逻辑卷”
[root@localhost ~]# cd /dev/mapper
[root@localhost mapper]# ll
total 0
crw-rw----. 1 root root 10, 58 Jun 11 22:30control
lrwxrwxrwx. 1 root root 7 Jun 11 22:59 vgdata-lvdata1 ->../dm-0
4.4 挂载
现在/data下是空的,会把之前/data下面的文件全部覆盖掉了。所以创建逻辑卷,文件系统等一定得在最先做。现在只能新建一个目录/data_new,然后挂载到该新目录,然后把/data下的东西迁移到新目录下……
将创建好的文件系统挂载到/data_new上.
[root@localhost download]# mount -t ext4/dev/vgdata/lvdata1 /data_new
[root@localhost download]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 18G 4.1G 13G 25% /
tmpfs 491M 72K 491M 1% /dev/shm
/dev/sda1 291M 34M 242M 13% /boot
/dev/mapper/vgdata-lvdata1 19G 172M 18G 1% /data_new
4.5 设置开机自动挂载
需要将创建好的文件系统挂载信息添加到/etc/fstab里面
.UUID可以通过 blkid命令查询.
[root@localhost ~]# blkid
/dev/sda1:UUID="8ea5fb6c-af7c-44fc-a02d-3a3d95134547" TYPE="ext4"
/dev/sda2:UUID="cc295a02-7d63-43a2-98d0-99e9ac0cd8a7" TYPE="ext4"
/dev/sda3:UUID="22e285a9-bac6-4874-9cca-506ea5f24e73" TYPE="swap"
/dev/sdb1:UUID="cnHu1a-PvGm-KFuQ-DkgP-Lcte-ra3z-lei7NB"TYPE="LVM2_member"
/dev/mapper/vgdata-lvdata1:UUID="88768e4c-e5df-4bf7-a020-428559ee2daf" TYPE="ext4"
添加:UUID=88768e4c-e5df-4bf7-a020-428559ee2daf /data_new ext4 defaults 0 0
五:扩展逻辑卷
假如/data_new磁盘空间还是不够,就再新增一块儿磁盘,建个物理卷(请参考前面步骤一,二,三),加入到卷组中,扩展到逻辑卷里。
--这里增添一个2G大小的硬盘。
5.1 扩展卷组
#只往卷组里扩展物理卷的话,命令:将刚创建的物理卷/dev/sdb1添加到卷组中
vgextend vgdata /dev/sdc1
5.2 扩展逻辑卷
将卷组的2GB扩展给根所在的逻辑卷
lvextend -L +2G /dev/vgdata/lvdata1
用resize2fs命令来同步文件系统
resize2fs /dev/vgdata/lvdata1
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 18G 4.0G 13G 25% /
tmpfs 491M 72K 491M 1% /dev/shm
/dev/sda1 291M 34M 242M 13% /boot
/dev/mapper/vgdata-lvdata1 21G 203M 20G 2% /data_new