项目上之前有一台虚拟机,将mysql直接安装在根分区下了,并且虚拟机在创建时硬盘空间分配得比较小,随着数据库的增大,根分区空间不够,因为库实时性比较强,迁移比较麻烦,需要对根分区进行扩容。
1.给这台虚拟机添加一块硬盘,大小为50G
2.添加后使用fdisk -l,可以发现系统并没有识别到新添加的硬盘
[root@localhost ~]# fdisk -l
Disk /dev/sda: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 10443 83778975 8e Linux LVM
Disk /dev/sdb: 32.2 GB, 32212254720 bytesDisk /dev/sdb doesn't contain a valid partition table
3.参照网上资料进行处理
[root@localhost ~]# ls /sys/class/scsi_host/
host0
[root@localhost ~]# echo "- - -" > /sys/class/scsi_host/host0/scan
[root@localhost ~]# fdisk -l
Disk /dev/sda: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 10443 83778975 8e Linux LVM
Disk /dev/sdb: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk /dev/sdb doesn't contain a valid partition table
Disk /dev/sdc: 53.6 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk /dev/sdc doesn't contain a valid partition table
可以看到已经识别到新加的50G硬盘了,名称为/dev/sdc
4.查看lvm相关信息
查看PV
[root@localhost ~]# pvscan
PV /dev/sda2 VG VolGroup00 lvm2 [79.88 GB / 0 free]
Total: 1 [79.88 GB] / in use: 1 [79.88 GB] / in no VG: 0 [0 ]
查看VG
[root@localhost ~]# vgscan
Reading all physical volumes. This may take a while...
Found volume group "VolGroup00" using metadata type lvm2
[root@localhost ~]#
查看LV
[root@localhost ~]# lvscan
ACTIVE '/dev/VolGroup00/LogVol00' [46.50 GB] inherit
ACTIVE '/dev/VolGroup00/LogVol01' [33.38 GB] inherit
查看挂载情况
[root@localhost ~]# cd /dev/VolGroup00/
[root@localhost VolGroup00]# ls -l
总计 0
lrwxrwxrwx 1 root root 31 03-15 09:29 LogVol00 -> /dev/mapper/VolGroup00-LogVol00
lrwxrwxrwx 1 root root 31 03-15 09:29 LogVol01 -> /dev/mapper/VolGroup00-LogVol01
[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/VolGroup00-LogVol00 46G 27G 17G 63% /
/dev/sda1 99M 13M 81M 14% /boot
tmpfs 16G 0 16G 0% /dev/shm
可以看到挂载到根分区下的LV为/dev/VolGroup00/LogVol00
5.操作硬盘
增加pv
[root@localhost ~]# pvcreate /dev/sdc
Writing physical volume data to disk "/dev/sdc"
Physical volume "/dev/sdc" successfully created
[root@localhost ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name VolGroup00
PV Size 79.90 GB / not usable 23.41 MB
Allocatable yes (but full)
PE Size (KByte) 32768
Total PE 2556
Free PE 0
Allocated PE 2556
PV UUID CidZ6B-Vofz-BFl9-bkdT-YPI9-gFqn-NJ1Oiy
"/dev/sdc" is a new physical volume of "50.00 GB"
--- NEW Physical volume ---
PV Name /dev/sdc
VG Name
PV Size 50.00 GB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID T5slwq-zJD9-nKVU-KpU0-7Qd9-TW3w-e0KxXl
可以看到PV已经增加了
扩展vg
[root@localhost ~]# vgscan
Reading all physical volumes. This may take a while...
Found volume group "VolGroup00" using metadata type lvm2
[root@localhost ~]# vgextend VolGroup00 /dev/sdc
Volume group "VolGroup00" successfully extended
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name VolGroup00
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 129.84 GB
PE Size 32.00 MB
Total PE 4155
Alloc PE / Size 2556 / 79.88 GB
Free PE / Size 1599 / 49.97 GB
VG UUID NFxJMZ-JNxk-25UY-2Tua-mjV2-PG29-wgugCD
可以看到VG VolGroup00中已经多出新加的50GPE了,VG总容量也增加了
扩展lv
[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Name /dev/VolGroup00/LogVol00
VG Name VolGroup00
LV UUID uQkwqP-bsc5-ZW93-zRZw-Z0tc-Jvt4-SHX83q
LV Write Access read/write
LV Status available
# open 1
LV Size 46.50 GB
Current LE 1488
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
我们将VG内剩下的50G空间全部扩展到/dev/VolGroup00/LogVol00这个LV中
[root@localhost ~]# lvextend -l +100%FREE /dev/VolGroup00/LogVol00
Extending logical volume LogVol00 to 96.47 GB
Logical volume LogVol00 successfully resized
[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Name /dev/VolGroup00/LogVol00
VG Name VolGroup00
LV UUID uQkwqP-bsc5-ZW93-zRZw-Z0tc-Jvt4-SHX83q
LV Write Access read/write
LV Status available
# open 1
LV Size 96.47 GB
Current LE 3087
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
可以看到LV的容量已经增加了
6.扩展文件系统
虽然LV的容量已经增加了,但是使用df -h查看可以看到根分区的空间并没有扩大,依然是46G
[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/VolGroup00-LogVol00 46G 27G 17G 63% /
/dev/sda1 99M 13M 81M 14% /boot
tmpfs 16G 0 16G 0% /dev/shm
这时可以使用resize2fs 命令重新调整分区大小
[root@localhost ~]# resize2fs /dev/VolGroup00/LogVol00
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/VolGroup00/LogVol00 is mounted on /; on-line resizing required
Performing an on-line resize of /dev/VolGroup00/LogVol00 to 25288704 (4k) blocks.
The filesystem on /dev/VolGroup00/LogVol00 is now 25288704 blocks long.
再次使用df命令查看,可以看到根分区容量已经增加了
[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/VolGroup00-LogVol00 94G 27G 63G 30% /
/dev/sda1 99M 13M 81M 14% /boot
tmpfs 16G 0 16G 0% /dev/shm
至此,根分区扩容实验已经全部完成。
操作磁盘和文件系统需要非常谨慎,数据无价,操作前最好先备份数据。