驗(yàn)證一些平臺(tái)無(wú)關(guān)的特性的時(shí)候,如果用VM代替真實(shí)的物理機(jī)無(wú)疑更高效冒黑。下面列舉一些Qemu啟動(dòng)VM的方式熄攘,順便澄清一些概念。
本文驗(yàn)證需要的環(huán)境:
1. ubuntu 18.04
2. 安裝軟件:
a)? sudo apt-get install qemu-kvm libvirt-bin
b)? vncviewer to access VM via X window(optional)
https://www.realvnc.com/en/connect/download/viewer/linux/
3. VM鏡像
xx.iso 或者 xx.img
方式1
方式1是用libvirt以xml方式啟動(dòng)VM孕蝉,VM所有的配置都放在xml文件中屡律,優(yōu)點(diǎn)是不用輸入復(fù)雜的參數(shù)。
如果想用qcow2格式的啟動(dòng)鏡像降淮,可以用如下命令轉(zhuǎn)換img超埋。
qemu-img convert -f raw -O qcow2 xx.img xx.qcow2
創(chuàng)建test.xml,內(nèi)容如下佳鳖。注意boot dev如果是cdrom則是iso啟動(dòng)霍殴,如果是hd則是硬盤啟動(dòng)。
<domain type='kvm'>????<name>test</name>????<memory>1048576</memory>????<currentMemory>1048576</currentMemory>????<vcpu>2</vcpu>????<os>??????<type arch='x86_64' machine='pc'>hvm</type>??????<boot dev='hd'/>???</os>???<features>?????<acpi/>?????<apic/>?????<pae/>???</features>???<clock offset='localtime'/>???<on_poweroff>destroy</on_poweroff>???<on_reboot>restart</on_reboot>???<on_crash>destroy</on_crash>???<devices>?????????<emulator>/usr/bin/qemu-system-x86_64</emulator>?????<video>???????<model type='vga' vram='16384' heads='1'>???????</model>???????<driver name='qemu'/>?????</video>?????<disk type='file' device='disk'>??????<driver name='qemu' type='qcow2'/>???????<source file='/home/xx/VM/xx.qcow2'/>???????<target dev='hda' bus='ide'/>?????</disk>????<interface type='bridge'>??????<source bridge='virbr0'/>??????<mac address="00:16:3e:5d:aa:a6"/>??????????<model type="e1000" />????</interface>????<input type='mouse' bus='ps2'/>?????<graphics type='vnc' port='-1' autoport='yes' listen = '0.0.0.0' keymap='en-us'/>???</devices></domain>
然后創(chuàng)建啟動(dòng)虛擬機(jī)
sudo virsh create test.xml
方式2
命令行方式
最簡(jiǎn)方式系吩,這種方式啟動(dòng)后来庭,需要vncview連接VM
sudo qemu-system-x86_64 -nographic -drive format=raw,file=usb.img
用bzImage和rootfs啟動(dòng)
sudo qemu-system-x86_64 -enable-kvm -kernel ./bzImage -hda ./rootfs.ext3 -m 2048 -net user -nographic -append "console=ttyS0,115200 ip=dhcp root=/dev/sda init=/sbin/init clock=pit oprofile.timer=1 UMA=1"
啟用網(wǎng)卡,啟動(dòng)成功后可以在host上ifconfig查看到tap0設(shè)備,sudo brctl addif virbr0 tap0將tap0加入virbr0網(wǎng)橋穿挨。
#e1000 as front-end connect to backend of type tap, backend with id hn2. sudo qemu-system-x86_64 -nographic -netdev tap,id=hn2 -device e1000,netdev=hn2 -drive format=raw,file=xx.img
參考:libvirt