kvm虚拟机扩展磁盘空间

来源:互联网 发布:107火箭炮 知乎 编辑:程序博客网 时间:2024/06/12 19:51

一、静态扩展磁盘

kvm虚拟机磁盘空间扩展与xen虚拟机磁盘空间扩展思路一致。原因在于xen/kvm默认的虚拟机磁盘格式为raw,所以方式可以通用。

raw磁盘格式扩展思路如下

(1) 新添加一块raw格式的磁盘加入到KVM虚拟机,然后通过虚拟机系统lvm逻辑卷管理方式进行管理,扩展磁盘空间。

(2) 直接通过dd命令扩展现有虚拟机磁盘大小,扩展之后,原磁盘大小增大,进入虚拟机系统后,直接通过fdisk分区新添加的空间,然后将该分区并入lvm逻辑卷中,扩大磁盘空间。

xen虚拟化实战系列(四)之xen虚拟机扩展磁盘空间一法   http://koumm.blog.51cto.com/703525/1285460

xen虚拟化实战系列(五)之xen虚拟机扩展磁盘空间再一法http://koumm.blog.51cto.com/703525/1285489

qcow2磁盘格式扩展思路如下

(1) 可以采用raw磁盘格式磁盘的扩展方式一致的方式进行。

(2) qcow2格式磁盘,直接通过qemu-img 直接扩展qcow2磁盘, 新添加一块raw格式的磁盘加入到KVM虚拟机,然后通过虚拟机系统lvm逻辑卷管理方式进行管理,扩展磁盘空间。

下面将开始通过qcow2格式添加磁盘。

方式一:添加一块qcow2磁盘加入虚拟机

1.  查看现在虚拟机磁盘信息

(1)  磁盘大小与格式信息

查看虚拟机磁盘格式。

image

查看磁盘格式信息

image

进入虚拟机,查看磁盘容量。

[root@test01 ~]# df -h   
Filesystem Size Used Avail Use% Mounted on    
/dev/mapper/vg-root 5.7G 3.1G 2.4G 57% /    
/dev/hda1 99M 24M 71M 25% /boot    
tmpfs 249M 0 249M 0% /dev/shm

(2) 添加一块qcow2磁盘

# qemu-img create -f qcow2 test01_add.qcow2 2G 

image

(3) 添加一块qcow2磁盘信息加入配置文件

[root@node1 data]# virsh shutdown oeltest01   
域 oeltest01 被关闭 

[root@node1 data]#    
[root@node1 data]# virsh list --all        
 Id    名称                         状态    
----------------------------------------------------    
 -     oeltest01                      关闭    
 -     oeltest02                      关闭    
 -     oeltest03                      关闭    
 -     wintest01                      关闭

在配置文件中加入如下内容:

# virsh edit oeltest01

<disk type='file' device='disk'>     
  <driver name='qemu' type='qcow2' cache='none'/>      
  <source file='/data/test01_add.qcow2'/>      
  <target dev='hdb' bus='ide'/>      
 </disk>

(4) 启动虚拟机并配置磁盘

# virsh start oeltest01

# virsh console oeltest01

image

2.  配置LVM扩展磁盘

(1) 分区

image

(2) 扩展lvm逻辑卷。

image

方式二:直接扩展qcow2磁盘

接上文恢复测试环境。

(1)  关闭虚拟机。

(2)  删除配置文件中第二块磁盘信息

(3)  通过kvm虚拟化学习笔记(十)之kvm虚拟机快照备份中创建的快照恢复虚拟机。

1. 直接扩展qcow2磁盘

(1)  查看磁盘

# virsh edit oeltest01

<disk type='file' device='disk'>   
  <driver name='qemu' type='qcow2' cache='none'/>    
  <source file='/data/test01.qcow2'/>
  <target dev='hda' bus='ide'/>    
  <address type='drive' controller='0' bus='0' target='0' unit='0'/>    
</disk>

(2)  直接扩展qcow2磁盘

# qemu-img resize test01.qcow2 +2G

image

经过前后对比,磁盘大小已由7G扩展到9G,已扩展,qcow2磁盘格式必须采用此方式进行扩展,不要使用dd方式,dd方式适用于raw格式。

2. 进入系统开始扩展磁盘

(1) 查看分区大小

[root@test01 ~]# fdisk -l 

Disk /dev/hda: 9663 MB, 9663676416 bytes
255 heads, 63 sectors/track, 1174 cylinders    
Units = cylinders of 16065 * 512 = 8225280 bytes 

   Device Boot      Start         End      Blocks   Id  System   
/dev/hda1   *           1          13      104391   83  Linux    
/dev/hda2              14         913     7229250   8e  Linux LVM 

Disk /dev/dm-0: 6308 MB, 6308233216 bytes   
255 heads, 63 sectors/track, 766 cylinders    
Units = cylinders of 16065 * 512 = 8225280 bytes 

Disk /dev/dm-0 doesn't contain a valid partition table 

Disk /dev/dm-1: 1073 MB, 1073741824 bytes   
255 heads, 63 sectors/track, 130 cylinders    
Units = cylinders of 16065 * 512 = 8225280 bytes 

Disk /dev/dm-1 doesn't contain a valid partition table   
[root@test01 ~]# 

(2) 开始分区

[root@test01 ~]# fdisk /dev/hda

过程如下,将扩展的空间分成一个区。

image

查看现在磁盘空间

image

(3) 开始创建物理卷,加入卷组,扩展逻辑卷。

image

到此kvm虚拟机扩展磁盘空间成功,当虚拟机磁盘格式换成qcow2后,确实有很多需要注意的地方,qcow2格式支持动态扩展与快照功能,虽然性比raw稍差,但确实很实用。

二、在线扩展磁盘

说明:

1) vmware ESXi虚拟化平台也支持这台在线扩展磁盘功能。

2) kvm虚拟机也支持在线扩展磁盘功能,在线扩展有特定的使用环境,主要用于不能随便停用的生产环境中。

3) 经过测试KVM在线扩展磁盘功能只适用于RHEL/CentOS/OEL6.x环境,5.x不支持在线的扩展磁盘,其它ubuntu等其它Linux我没有测试,不做评论。

本文出自:http://koumm.blog.51cto.com

本文为了测试,专门安装了OEL6.3虚拟机。

1.  安装oel6.3虚拟机

安装oeltest02虚拟机

# qemu-img create -f qcow2  /data/test02.qcow2  7G    
# virt-install --name=oeltest02 --os-variant=RHEL6.3 --ram 512 --vcpus=1 --disk path=/data/test01.qcow2,format=qcow2,size=7,bus=virtio --accelerate --cdrom /data/iso/oel63x64.iso --vnc --vncport=5911 --vnclisten=0.0.0.0 --network bridge=br0,model=virtio --noautoconsole 

目的是使用实验环境磁盘改为virtio驱动下的vda磁盘,这种scsi磁盘可以热插拨,可以动态添加。

整个安装过程略,虚拟机各种配置略,可以参考系列文章前面的内容。

2.  给oeltest02虚拟机在线添加磁盘

(1)  查看现有磁盘

[root@node1 data]# virsh domblklist oeltest02

image

(2) 创建一块qcow2虚拟磁盘

[root@node1 data]# qemu-img create -f qcow2 test02_add01.qcow2 4G

image

(3) 在线添加这台qcow2虚拟磁盘

# virsh attach-disk oeltest02 /data/test02_add01.qcow2 vdb --cache=none --subdriver=qcow2

image

image

(4) 进入oeltest02虚拟机查看添加磁盘情况

[root@node1 data]#    
[root@node1 data]# virsh console oeltest02    
连接到域 oeltest02    
Escape character is ^] 

Oracle Linux Server release 6.3   
Kernel 2.6.32-279.el6.x86_64 on an x86_64 

test02 login: root   
Password:     
Last login: Thu Sep 12 00:46:27 on ttyS0    
[root@test02 ~]# 

[root@test02 ~]# fdisk –l

即可看到刚才添加的磁盘已经被虚拟机适别到了。

image

可以看到有两个scsi存储设备。

image

注:再次说明只有RHEL/CentOS/OEL6.x版本在线添加磁盘,虚拟机直接可以在线识别。

(5) 修改虚拟机配置文件添加新增磁盘配置

1)  查看当前虚拟机配置文件

这里查看到的虚拟机配置文件是内存中存放的配置,重启后会消失,需要将新添的磁盘配置保存到配置文件中。

[root@node1 data]# virsh dumpxml oeltest02   
<domain type='kvm' id='5'>    
  <name>oeltest02</name>    
  <uuid>2dfb812d-703c-3d5b-68b7-516f618e7944</uuid>    
  <memory unit='KiB'>1048576</memory>    
  <currentMemory unit='KiB'>1048576</currentMemory>    
  <vcpu placement='static'>2</vcpu>    
  <os>    
    <type arch='x86_64' machine='rhel6.4.0'>hvm</type>    
    <boot dev='hd'/>    
  </os>    
  <features>    
    <acpi/>    
    <apic/>    
    <pae/>    
  </features>    
  <clock offset='utc'/>    
  <on_poweroff>destroy</on_poweroff>    
  <on_reboot>restart</on_reboot>    
  <on_crash>restart</on_crash>    
  <devices>    
    <emulator>/usr/libexec/qemu-kvm</emulator>    
    <disk type='file' device='disk'>    
      <driver name='qemu' type='qcow2' cache='none'/>    
      <source file='/data/test02.qcow2'/>    
      <target dev='vda' bus='virtio'/>    
      <alias name='virtio-disk0'/>    
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>    
    </disk>    
    <disk type='file' device='disk'>       
      <driver name='qemu' type='qcow2' cache='none'/>        
      <source file='/data/test02_add01.qcow2'/>        
      <target dev='vdb' bus='virtio'/>        
      <alias name='virtio-disk1'/>        
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>        
    </disk>

    <disk type='block' device='cdrom'>    
      <driver name='qemu' type='raw'/>    
      <target dev='hdc' bus='ide'/>    
      <readonly/>    
      <alias name='ide0-1-0'/>    
      <address type='drive' controller='0' bus='1' target='0' unit='0'/>    
    </disk>    
    <controller type='usb' index='0'>    
      <alias name='usb0'/>    
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>    
    </controller>    
    <controller type='ide' index='0'>    
      <alias name='ide0'/>    
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>    
    </controller>    
    <interface type='bridge'>    
      <mac address='52:54:00:ff:2f:70'/>    
      <source bridge='br0'/>    
      <target dev='vnet1'/>    
      <model type='virtio'/>    
      <alias name='net0'/>    
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>    
    </interface>    
    <serial type='pty'>    
      <source path='/dev/pts/3'/>    
      <target port='0'/>    
      <alias name='serial0'/>    
    </serial>    
    <console type='pty' tty='/dev/pts/3'>    
      <source path='/dev/pts/3'/>    
      <target type='serial' port='0'/>    
      <alias name='serial0'/>    
    </console>    
    <input type='mouse' bus='ps2'/>    
    <graphics type='vnc' port='5911' autoport='no' listen='0.0.0.0'>    
      <listen type='address' address='0.0.0.0'/>    
    </graphics>    
    <video>    
      <model type='cirrus' vram='9216' heads='1'/>    
      <alias name='video0'/>    
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>    
    </video>    
    <memballoon model='virtio'>    
      <alias name='balloon0'/>    
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>    
    </memballoon>    
  </devices>    
  <seclabel type='none'/>    
</domain> 

[root@node1 data]# 

2) 保存新增磁盘配置

[root@node1 data]# virsh edit oeltest02

image

说明:对RHEL/CentOS/OEL5.x版本虚拟机在线添加磁盘按照第4小节处理。

3. 将新增磁盘并入虚拟机逻辑卷中

(1) 分区过程如下

[root@test02 ~]# fdisk /dev/vdb

[root@test02 ~]# partprobe 

(2) 创建逻辑卷过程如下

225838141.jpg

到此oeltest02虚拟机在线添加磁盘并在线扩展磁盘成功。

2014-03-14日修改, 感谢xiaoli110提供如下解决办法。

4. OEL/RHEL/CentOS5.x虚拟机在线添加磁盘过程

说明:OEL/RHEL/CentOS5.x虚拟机按照上面的过程在线添加磁盘,磁盘识别不出来,需要在添加磁盘后,修改配置文件后重启,不能在线识别到磁盘。需要采用如下方式处理,即可以实现在线添加磁盘的功能。

(1) 先登录进虚拟机执行两次如下命令

modprobe acpiphp
modprobe acpiphp

wKioL1MjDFrBi72iAAF25zSS1v0744.jpg

(2) 虚拟主机上创建磁盘并添加磁盘

wKioL1MjDKXwEp2AAAIpagxtF2k671.jpg

1 0