[iyunv@10 ~]# dd if=/dev/zero of=1.0G.img bs=1M count=30000
dd: writing `1.0G.img': No space left on device
26223+0 records in
26222+0 records out
27496136704 bytes (27 GB) copied, 104.717 s, 263 MB/s
[iyunv@10 ~]#
提示说磁盘空间不足(No space left on device)。虽然我们给了虚拟机足够的空间,但是虚拟机里的系统(这里是 CentOS)并没有将这一部分空间利用上。可以看到,虚拟机里的磁盘空间只有 37G,原始分配的 50G 没有用完。这同时也说明,即使创建虚拟机的时候使用动态分配磁盘大小,虚拟机里的系统也并不会自动修改自己的磁盘。如果是正常使用,即使虚拟机的虚拟磁盘 足够大,还是可能会在虚拟机中遇到磁盘空间不足的警告。
下面调整虚拟机操作系统的文件系统,使之将所有空间利用上。
将空余磁盘创建为 sda4
重新启动虚拟机,登入 CentOS,使用 fdisk 将虚拟磁盘的空闲空间创建为一个新的分区。注意要使用代表 Linux LVM 的分区号 8e 来作为 ID。
[iyunv@10 ~]# fdisk /dev/sda
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 compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
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 (experts only)
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Selected partition 4
First cylinder (5222-6527, default 5222):
Using default value 5222
Last cylinder, +cylinders or +size{K,M,G} (5222-6527, default 6527):
Using default value 6527
Command (m for help): t
Partition number (1-4): 4
Hex code (type L to list codes): 8e
Changed system type of partition 4 to 8e (Linux LVM)
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[iyunv@10 ~]#
上述命令执行后,可以使用fdisk -l /dev/sda来查看结果,来查看 sda4 是否成功创建并标记为 LVM,如果没有,则需要重新启动。
[iyunv@10 ~]# fdisk -l /dev/sda
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: 0x0000698e
Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 1045 7875584 8e Linux LVM
/dev/sda3 1045 5221 33549074+ 8e Linux LVM
/dev/sda4 5222 6527 10490445 8e Linux LVM
[iyunv@10 ~]#
执行完后重启。
调整 LVM 大小
先看看当前 Volume Group:
[iyunv@10 ~]# vgdisplay
--- Volume group ---
VG Name vg_yuxuechengcentos6
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 5
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 39.50 GiB
PE Size 4.00 MiB
Total PE 10112
Alloc PE / Size 10112 / 39.50 GiB
Free PE / Size 0 / 0
VG UUID 8oim1v-kbuK-5RjL-MchZ-PLqp-3Uco-OCTXMu
[iyunv@10 ~]#
[iyunv@10 ~]# lvscan
ACTIVE '/dev/vg_yuxuechengcentos6/lv_root' [37.53 GiB] inherit
ACTIVE '/dev/vg_yuxuechengcentos6/lv_swap' [1.97 GiB] inherit
[iyunv@10 ~]#
先将分配过来的新磁盘空间创建为一个新的物理卷:
[iyunv@localhost ~]# pvcreate /dev/sda4
Writing physical volume data to disk "/dev/sda4"
Physical volume "/dev/sda4" successfully created
[iyunv@localhost ~]#
[iyunv@localhost ~]# resize2fs /dev/mapper/vg_yuxuechengcentos6-lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/vg_yuxuechengcentos6-lv_root is mounted on /; on-line resizing required
old desc_blocks = 3, new_desc_blocks = 3
Performing an on-line resize of /dev/mapper/vg_yuxuechengcentos6-lv_root to 12460032 (4k) blocks.
The filesystem on /dev/mapper/vg_yuxuechengcentos6-lv_root is now 12460032 blocks long.
[iyunv@localhost ~]#
调整完后,看看效果:
[iyunv@localhost ~]# lvscan
ACTIVE '/dev/vg_yuxuechengcentos6/lv_root' [47.53 GiB] inherit
ACTIVE '/dev/vg_yuxuechengcentos6/lv_swap' [1.97 GiB] inherit
[iyunv@localhost ~]#
另外,几个有用的 LVM 指令(lvm2):
lvmdiskscan
vgdisplay
pvdisplay
lvdispay
vgcreate
pvcreate
lvscan
pvscan
vgscan
我这里用的是 CentOS 系统。如果是 Windows 就更简单了,可通过 Windows 的磁盘管理来重新调整分区大小以占用所有可用磁盘空间。