qemu-img
• qemu-img 是虚拟机的磁盘管理命令
• qemu-img 支持非常多的磁盘格式,例如 raw、
qcow2、vdi、vmdk 等等
• qemu-img 命令格式
– qemu-img 命令 参数 块文件名称 大小
– 常用的命令有
– create 创建一个磁盘
– convert 转换磁盘格式
– info 查看磁盘信息
– snapshot 管理磁盘快照qemu-img
• 创建新的镜像盘文件
– qemu-img create -f 格式 磁盘路径 大小
– qemu-img create -f qcow2 disk.img 50G
• 查询镜像盘文件的信息
– qemu-img info 磁盘路径
– qemu-img info disk.img
• -b 使用后端模板文件
– qemu-img create -b disk.img -f qcow2 disk1.img
++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++
虚拟机由xml文件和镜像盘文件和网络接口文件组成:
libvirtd 网络接口
– 原理:调用 dnsmasq 提供DNS、DHCP等功能
– 创建配置文件 /etc/libvirt/qemu/networks/vbr.xml
[root@rootroom9pc01 ~]# cat /etc/libvirt/qemu/networks/vbr.xml
<network>
<name>vbr</name>
<bridge name="vbr"/>
<forward mode="nat"/>
<ip address="192.168.1.254" netmask="255.255.255.0">
<dhcp>
<range start="192.168.1.100" end="192.168.1.200"/>
</dhcp>
</ip>
</network>
生成xml配置文件:
cd /var/lib/libvirt/images/
[root@rootroom9pc01 qemu]# ls
demo.xml networks node1.xml rh7_node01.xml win2008.xml
[root@rootroom9pc01 qemu]#
[root@rootroom9pc01 qemu]# ls
demo.xml networks node1.xml rh7_node01.xml win2008.xml
[root@rootroom9pc01 qemu]# sed 's/demo/node1/' demo.xml >/etc/libvirt/qemu/node1.xml
[root@rootroom9pc01 qemu]# ls
demo.xml networks node1.xml rh7_node01.xml win2008.xml
[root@rootroom9pc01 qemu]# pwd
/etc/libvirt/qemu //xml路径
[root@rootroom9pc01 qemu]# cat demo.xml
<domain type='kvm'>
<name>demo</name>
<memory unit='KB'>2097152</memory>
<currentMemory unit='KB'>2097152</currentMemory>
<vcpu placement='static'>2</vcpu>
<os>
<type arch='x86_64' machine='pc'>hvm</type>
<boot dev='hd'/>
<bootmenu enable='yes'/>
<bios useserial='yes'/>
</os>
<features>
<acpi/>
<apic/>
</features>
<cpu mode='host-passthrough'>
</cpu>
<clock offset='localtime'/>
<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'/>
<source file='/var/lib/libvirt/images/demo.img'/>
<target dev='vda' bus='virtio'/>
</disk>
<interface type='bridge'>
<source bridge='vbr'/>
<model type='virtio'/>
</interface>
<channel type='unix'>
<target type='virtio' name='org.qemu.guest_agent.0'/>
</channel>
<serial type='pty'></serial>
<console type='pty'>
<target type='serial'/>
</console>
<memballoon model='virtio'></memballoon>
</devices>
</domain>
[root@rootroom9pc01 qemu]# cat node1.xml //虚拟机各种参数配置文件
<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
virsh edit node1
or other application using the libvirt API.
-->
<domain type='kvm'>
<name>node1</name>
<uuid>94ae8691-9884-4df0-bd35-26c0e74c616e</uuid>
<memory unit='KiB'>2048000</memory>
<currentMemory unit='KiB'>2048000</currentMemory>
<vcpu placement='static'>2</vcpu>
<os>
<type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
<boot dev='hd'/>
<bootmenu enable='yes'/>
<bios useserial='yes'/>
</os>
<features>
<acpi/>
<apic/>
</features>
<cpu mode='host-passthrough' check='none'/>
<clock offset='localtime'/>
<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'/> //虚拟机的磁盘镜像文件格式
<source file='/var/lib/libvirt/images/node1.img'/> //虚拟机的磁盘镜像文件路径
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
</disk>
<controller type='usb' index='0' model='piix3-uhci'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
</controller>
<controller type='pci' index='0' model='pci-root'/>
<controller type='virtio-serial' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
</controller>
<interface type='bridge'>
<mac address='52:54:00:9a:b6:ab'/>
<source bridge='vbr'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<serial type='pty'>
<target port='0'/>
</serial>
<console type='pty'>
<target type='serial' port='0'/>
</console>
<channel type='unix'>
<target type='virtio' name='org.qemu.guest_agent.0'/>
<address type='virtio-serial' controller='0' bus='0' port='1'/>
</channel>
<input type='mouse' bus='ps2'/>
<input type='keyboard' bus='ps2'/>
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</memballoon>
</devices>
</domain>
[root@rootroom9pc01 images]# pwd
/var/lib/libvirt/images //后端境像模版和前端境像模版路径
//创建模版境像文件
[root@rootroom9pc01 images]#qemu-img create -f qcow2 node.qcow2 16G
[root@rootroom9pc01 images]# qemu-img info node.qcow2
image: node.qcow2
file format: qcow2
virtual size: 16G (17179869184 bytes)
disk size: 1.1G
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
[root@rootroom9pc01 images]#qemu-img create -f qcow2 -b node.qcow2 node1.img 30G //以node.qcow2为后端模版创建前端境像文件node1.img
[root@rootroom9pc01 images]# qemu-img info node1.img //前端文件
image: node1.img
file format: qcow2
virtual size: 30G (32212254720 bytes)
disk size: 6.4M
cluster_size: 65536
backing file: node.qcow2 //后端境像文件node.qcow2
Format specific information:
compat: 1.1
lazy refcounts: false
启动虚拟机:
virsh define /etc/libvirt/qemu/node1.xml
[root@rootroom9pc01 images]# virsh
欢迎使用 virsh,虚拟化的交互式终端。
输入:'help' 来获得命令的帮助信息
'quit' 退出
virsh # start node1
域 node1 已开始
virsh # console node1
连接到域 node1
换码符为 ^]
CentOS Linux 7 (Core)
Kernel 3.10.0-693.el7.x86_64 on an x86_64
localhost login: root
Password:
Last login: Thu Jul 19 15:33:04 on ttyS0
[root@localhost ~]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.6.154 netmask 255.255.255.0 broadcast 192.168.6.255
inet6 fe80::5054:ff:fe9a:b6ab prefixlen 64 scopeid 0x20<link>
ether 52:54:00:9a:b6:ab txqueuelen 1000 (Ethernet)
RX packets 70 bytes 7761 (7.5 KiB)
RX errors 0 dropped 7 overruns 0 frame 0
TX packets 43 bytes 3968 (3.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost ~]#
[root@rootroom9pc01 ~]# virsh define /etc/libvirt/qemu/node2.xml
定义域 node2(从 /etc/libvirt/qemu/node2.xml)
[root@rootroom9pc01 ~]# virsh
欢迎使用 virsh,虚拟化的交互式终端。
输入:'help' 来获得命令的帮助信息
'quit' 退出
virsh # list --all
Id 名称 状态
----------------------------------------------------
- node1 关闭
- node2 关闭
- rh7_node01 关闭
- win2008 关闭
virsh # net-list
名称 状态 自动开始 持久
----------------------------------------------------------
default 活动 是 是
private1 活动 是 是
private2 活动 是 是
public1 活动 是 是
public2 活动 是 是
rhce 活动 是 是
vbr 活动 是 是
virsh # start node2
域 node2 已开始
virsh # console node2
连接到域 node2
换码符为 ^]
CentOS Linux 7 (Core)
Kernel 3.10.0-693.el7.x86_64 on an x86_64
localhost login: root
Password:
Last login: Thu Jul 19 23:58:13 on ttyS0
[root@localhost ~]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.6.136 netmask 255.255.255.0 broadcast 192.168.6.255
inet6 fe80::5054:ff:fe6a:ed72 prefixlen 64 scopeid 0x20<link>
ether 52:54:00:6a:ed:72 txqueuelen 1000 (Ethernet)
RX packets 74 bytes 7442 (7.2 KiB)
RX errors 0 dropped 8 overruns 0 frame 0
TX packets 44 bytes 4028 (3.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost ~]# [ 99.798985] random: crng init done
[root@localhost ~]# // Ctrl+] 可以回到virsh命令行操作
[root@localhost ~]#
virsh # exit
[root@rootroom9pc01 ~]#