kvm虚拟化模式下,给虚拟机增加硬盘空间概念上有3种办法;首先执行指令:virsh resize VR.img +2G
其中方法一,可参考http://geekpeek.net/resize-filesystem-fdisk-resize2fs/,及动态扩充非lvm磁盘空间,此处不可行的原因在于扩充需要卸载挂载点,而根节点或者boot节点不可卸载,否则会出问题;
方法一:fdisk模式下直接将新增磁盘空间给予/,及根挂载点;
方法二:fdisk模式下,将新增磁盘空间成为一个新的分区,如/dev/sda3,然后mkfs -t ext4 /dev/sda3,然后挂载到某一目录;
方法三:将新增磁盘空间成为一个新的磁盘;具体方法参照http://blog.fens.me/vps-kvm-disk/;概括如下:
1. host增加物理硬盘并分区
HOST作为KVM的宿主计算机,管理所有GUEST虚拟机。我们通过给HOST增加物理硬盘,然后分给GUEST,从而实现给虚拟机硬盘扩容的效果。
如何给计算机增加物理硬盘并分区,请参考:多硬盘分区管理fdisk 文章
查看HOST机的硬盘
~ sudo fdisk -l
Disk /dev/sda: 299.4 GB, 299439751168 bytes
255 heads, 63 sectors/track, 36404 cylinders, total 584843264 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 identifier: 0x000efd7c
Device Boot Start End Blocks Id System
/dev/sda1 2048 97656831 48827392 82 Linux swap / Solaris
/dev/sda2 97656832 136718335 19530752 83 Linux
/dev/sda3 136718336 214843335 39062500 83 Linux
/dev/sda4 * 214843392 215037951 97280 83 Linux
Disk /dev/sdb: 1999.3 GB, 1999307276288 bytes
255 heads, 63 sectors/track, 243068 cylinders, total 3904897024 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 identifier: 0xf919a976
Device Boot Start End Blocks Id System
/dev/sdb1 2048 1952448511 976223232 7 HPFS/NTFS/exFAT
/dev/sdb2 1952448512 3904897023 976224256 5 Extended
/dev/sdb5 1952450560 2267023360 157286400+ 83 Linux
/dev/sdb6 2267025409 2581596160 157285376 83 Linux
/dev/sdb7 2581598209 2896168960 157285376 83 Linux
/dev/sdb8 2896171009 3210741760 157285376 83 Linux
/dev/sdb9 3210743809 3525314560 157285376 83 Linux
/dev/sdb10 3525316609 3904897023 189790207+ 83 Linux
~ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 37G 3.4G 32G 10% /
udev 24G 4.0K 24G 1% /dev
tmpfs 9.5G 1.1M 9.5G 1% /run
none 5.0M 8.0K 5.0M 1% /run/lock
none 24G 152K 24G 1% /run/shm
none 100M 28K 100M 1% /run/user
cgroup 24G 0 24G 0% /sys/fs/cgroup
/dev/sda2 19G 3.6G 14G 21% /home
/dev/sda4 92M 34M 54M 39% /boot
/dev/sdb1 931G 100G 832G 11% /disk/sdb1
/dev/sdb6 148G 188M 140G 1% /disk/sdb6
下面将进行两个测试:
通过virsh给guest增加文件硬盘:通过文件硬盘的镜像/disk/sdb6/c1d6.img
通过virsh给guest增加分区硬盘:直接使用分区硬盘/dev/sdb5
2. 通过virsh给guest增加文件硬盘
创建文件硬盘的镜像
~ cd /disk/sdb6/
~ sudo qemu-img create -f raw /disk/sdb6/c1d6.img 10G
Formatting '/disk/sdb6/c1d6.img', fmt=raw size=10737418240
~ ls -l
-rw-r--r-- 1 root root 10737418240 Jul 8 16:37 c1d6.img
drwx------ 2 root root 16384 Jul 8 09:03 lost+found/
通过virsh管理工具加载硬盘
~ sudo virsh
Welcome to virsh, the virtualization interactive terminal.
Type: 'help' for help with commands
'quit' to quit
#查看系统内的虚拟机
virsh # list
Id Name State
----------------------------------------------------
5 server3 running
6 server4 running
7 d2 running
8 r1 running
9 server2 running
12 c1 running
#在这里我们要对c1进行硬盘扩容
virsh # edit c1
#找到硬盘配置(原来的系统硬盘)
<disk type='file' device='disk'>
<driver name='qemu' type='raw'/>
<source file='/disk/sdb1/c1.img'/>
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
</disk>
#增加文件硬盘,vdb
<disk type='file' device='disk'>
<driver name='qemu' type='raw' cache='none'/>
<source file='/disk/sdb6/c1d6.img'/>
<target dev='vdb' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</disk>
#保存退出
重启c1虚拟机
#请使用destroy命令,reboot和shutdown不管用。
~ virsh # destroy c1
Domain c1 destroyed
#list找不到c1
~ virsh # list
Id Name State
----------------------------------------------------
5 server3 running
6 server4 running
7 d2 running
8 r1 running
9 server2 running
#启动虚拟机c1
~ virsh # start c1
Domain c1 started
#进入虚拟机c1
~ console c1
在c1中,进行硬盘查检并分区