kvm虚拟机安装
参考已下资料
KVM虚拟机在Ubuntu16.04下的环境搭建
安装所需软件
sudo apt-get install qemu-kvm
sudo apt-get install qemu
sudo apt-get install libvirt-bin
sudo apt-get install bridge-utils
sudo apt-get install virt-manager
sudo apt-get install virt-viewer
配置网络桥接
修改完成后建议重启
vim /etc/network/interfaces
# modify like ↓
# Enabing Bridge networking br0 interface
auto br0
iface br0 inet static
address 192.168.1.130
network 192.168.1.0
netmask 255.255.255.0
broadcast 192.168.1.255
gateway 192.168.1.1
dns-nameservers 223.5.5.5
bridge_ports eth0
bridge_stp off
虚拟机xml
使用xml文件对虚拟机进行配置,这里提供一份能使用的版本(双网卡)
相关的键的意思可以参考利用virsh和xml文件创建虚拟机
<domain type='kvm'>
<name>fly-1</name>
<uuid>98c38a09-395b-d7e9-81e8-44d46ef7d61c</uuid>
<memory unit='GiB'>32</memory><!--内存-->
<currentMemory unit='GiB'>32</currentMemory><!--修改成与上面一样-->
<vcpu placement='static'>12</vcpu><!--cpu数量-->
<resource>
<partition>/machine</partition>
</resource>
<os>
<type arch='x86_64' machine='pc-i440fx-trusty'>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/bin/kvm-spice</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/> <!--注意文件格式-->
<source file='/root/fly-kvm/fly-1.qcow2'/> <!--qcow文件-->
<target dev='hda' bus='ide'/>
<alias name='ide0-0-0'/>
<address type='drive' controller='0' bus='0' 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='pci' index='0' model='pci-root'>
<alias name='pci.0'/>
</controller>
<controller type='ide' index='0'>
<alias name='ide0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
</controller>
<interface type='bridge'> <!--网卡1-->
<mac address='52:54:00:84:b0:f2'/>
<source bridge='br1'/>
<target dev='vnet3'/>
<model type='rtl8139'/>
<alias name='net0'/> <!--注意slot为pci插槽-->
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<interface type='bridge'> <!--网卡2-->
<mac address='52:54:00:5d:a5:42'/>
<source bridge='br2'/>
<target dev='vnet4'/>
<model type='rtl8139'/>
<alias name='net1'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</interface>
<serial type='pty'>
<source path='/dev/pts/2'/>
<target port='0'/>
<alias name='serial0'/>
</serial>
<console type='pty' tty='/dev/pts/2'>
<source path='/dev/pts/2'/>
<target type='serial' port='0'/>
<alias name='serial0'/>
</console>
<input type='mouse' bus='ps2'/>
<input type='keyboard' bus='ps2'/>
<graphics type='vnc' port='5903' autoport='yes' listen='127.0.0.1'>
<listen type='address' address='127.0.0.1'/>
</graphics>
<sound model='ich6'>
<alias name='sound0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
</sound>
<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='dynamic' model='apparmor' relabel='yes'>
<label>libvirt-98c38a09-395b-d7e9-81e8-44d46ef7d61c</label>
<imagelabel>libvirt-98c38a09-395b-d7e9-81e8-44d46ef7d61c</imagelabel>
</seclabel>
</domain>
对于不同的虚拟机,需要配置不相同的uuid和mac地址,以下是生成mac和uuid的方法
生成mac地址
echo 52:54:00:`openssl rand -hex 3 | sed 's/\(..\)/\1:/g; s/.$//'`
生成uuid
uuidgen
virsh操作虚拟机
virsh define vm0.xml //导入虚拟机配置
virsh create vm0.xml //临时启动虚拟机,关机后将从列表删除
virsh start vm0 //开启vm0
virsh list --all // 显示所有虚拟机状态
virsh shutdown vm0 //正常关机
virsh destroy vm0 //强制关闭(转为停止状态)
virsh undefine vm1 //删除虚拟机配置
virsh dumpxml vm0 //显示虚拟机xml配置
virsh console vm0 //进入虚拟机控制台(可以查看开机过程的问题)
这里使用 create 可以立刻加载并启动。
修改磁盘大小
# 查看键像信息
qemu-img info my-image.qcow2
# 修改大小为50g
qemu-img resize t.qcow2 50G
#增加大小1G
qemu-img resize t.qcow2 +1G
修改网络
有两种方式可以修改网络
进入虚拟机后修改
vim /etc/network/interfaces
#替换成以下格式内容
auto lo
iface lo inet loopback
#ens3网卡配置,修改成与配置文件中对应插槽3网口的桥接网段
auto ens3
iface ens3 inet static
address 172.0.10.224
netmask 255.255.255.0
gateway 172.0.10.1
dns-nameserver 8.8.8.8
#ens3网卡配置,修改成与配置文件中对应插槽6网口的桥接网段
auto ens6
iface ens6 inet static
address 172.0.5.204
netmask 255.255.255.0
gateway 172.0.5.1
dns-nameserver 8.8.8.8
修改完成后,使用reboot重启虚拟机即可生效。
未启动时修改
未启动虚拟机时,可以通过挂在qcow2文件的方式修改机镜像内文件
详细操作可以参考在 Linux 上如何挂载 qcow2 磁盘镜像
# 文件挂载成设备
qemu-nbd --connect=/dev/nbd0 my-image.qcow2
# 准备一个挂载点:未被占用的文件夹,挂载后原内容会被隐藏
mkdir img
# 挂载到img目录
mount /dev/nbd0p1 img
## 进行修改操作
vim ./img/etc/network/interfaces
# 卸载
sudo umount img/
sudo qemu-nbd --disconnect /dev/nbd0
done.
另一种参考挂载方法
virt-install -n vm3 -r 4096 --vcpus 2 --description "This is a vm" --network bridge=br1,model=e1000 --network bridge=br2,model=e1000 --graphics vnc,listen=0.0.0.0 --disk path=my-image.qcow2 --boot hd=my-image.qcow2
网友评论