---
tags: Virtual Machine
---
# QEMU installation and launch the VM
```
$ sudo apt-get install libperl-dev
$ sudo apt-get install libgtk2.0-dev
$ sudo apt-get install libgtk-3-dev
$ sudo apt-get install openvpn
$ wget https://download.qemu.org/qemu-3.0.0-rc0.tar.xz
$ tar xvJf qemu-3.0.0-rc0.tar.xz
$ cd qemu-3.0.0-rc0
$ ./configure --target-list=x86_64-softmmu
$ make -j8
```
[Download Ubuntu 16.04 iso](https://drive.google.com/file/d/0B9au9R9FzSWKUjRZclBXbXB0eEk/view)
```
$ ./qemu-img create -f qcow2 ubuntu1604.qcow2 60G
```
```
Formatting 'ubuntu1604.qcow2', fmt=qcow2 size=64424509440 cluster_size=65536 lazy_refcounts=off refcount_bits=16
```
```
$ ./x86_64-softmmu/qemu-system-x86_64 -cdrom ubuntu-16.04-desktop-amd64.iso ubuntu1604.qcow2 -enable-kvm -m 6G
```
* After installation
* Add a bridge (modify /etc/network/interfaces)
```
# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback
auto br0
iface br0 inet static
bridge_ports enp3s0
bridge_maxwait 0
address 192.168.11.9
netmask 255.255.255.0
gateway 192.168.11.1
dns-nameservers 8.8.8.8 140.96.254.98
auto enp3s0
iface enp3s0 inet static
address 0.0.0.0
```
```
$ sudo reboot
```
* Perpare set-net.sh
```
#!/bin/sh
/usr/sbin/openvpn --mktun --dev tap0 --user `id -un`
ifconfig tap0 promisc up
brctl addif br0 tap0
```
```
$ sudo sh set-net.sh
```
* Perpare run.sh
```
#!/bin/bash
sudo ./x86_64-softmmu/qemu-system-x86_64 -drive if=none,id=drive0,cache=none,format=qcow2,file=ubuntu1604.qcow2 \
-device virtio-blk,drive=drive0,scsi=off \
-m 6G -enable-kvm \
-netdev tap,id=tap0,vhost=off \
-device virtio-net-pci,id=net-pci0,netdev=tap0 \
-smp 2
```
```
$ sh ./run.sh
```
* In the new VM, modify the /etc/network/interfaces
```
# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback
#auto eth0
#iface eth0 inet dhcp
auto ens4
iface ens4 inet static
address 192.168.11.20
netmask 255.255.255.0
gateway 192.168.11.1
dns-nameservers 8.8.8.8 140.96.254.98
```
* Reboot your VM and check the network
qemu-system-x86_64 --enable-kvm -drive format=raw,file=ubuntu.img,if=virtio -m 2048 -net nic,model=virtio -net user -rtc base=localtime,clock=host -curses
## using QEMU to create and migrate instance
create another mv on one of the hosts
install qemu
```
sudo apt-get install qemu-kvm qemu virt-manager virt-viewer libvirt-bin
```
prepare set-net.sh
```
#!/bin/sh
/usr/sbin/openvpn --mktun --dev tap1 --user `id -un`
ifconfig tap1 promisc up
brctl addif br0 tap1
```
prepare run.sh
```
#!/bin/bash
sudo qemu-system-x86_64 -drive if=none,id=drive0,cache=none,format=raw,file=ubuntu.img \
-device virtio-blk,drive=drive0,scsi=off \
-m 1G -enable-kvm \
-net tap,ifname=tap0 -net nic,model=virtio,vlan=0,macaddr=ae:ae:00:00:00:24\
-smp 1 \
-curses
```
stuck at boot
-use ctrl+alt+2 or Esc+2
create a backup host using the same image and add to the script to open a listening port
```
-incoming tcp:0:4444
```
on the first host enter the qemu monitor(ctrl+alt +2 or Esc+2)
```
migrate -d tcp:192.168.8.3:4444
```
## using virsh
#### create share storage(NFS)
using virsh to create instance
```
virt-install \
-n vm1\
--description "mv for migration" \
--os-type=Linux \
--os-variant=ubuntu16.04 \
--ram=1024 --vcpus=1 --disk path=/home/ting/nfsfolder/ubuntu.img,bus=virtio,size=1,cache=none \
--graphics none --cdrom /home/ting/nfsfolder/ubuntu-16.04.5-server-amd64.iso \
--network bridge:br0
```
using virsh to migrate instance
##### inport a running vm
create a xml file for the existing vm and then import and define the vm
```
$ cat > demo.args <<EOF
LC_ALL=C PATH=/bin HOME=/home/test USER=test \
LOGNAME=test /usr/bin/qemu -S -M pc -m 214 -smp 1 \
-nographic -monitor pty -no-acpi -boot c -hda \
/dev/HostVG/QEMUGuest1 -net none -serial none \
-parallel none -usb
EOF
```
```
$ virsh domxml-from-native qemu-argv demo.args
<domain type='qemu'>
<uuid>00000000-0000-0000-0000-000000000000</uuid>
<memory>219136</memory>
<currentMemory>219136</currentMemory>
<vcpu>1</vcpu>
<os>
<type arch='i686' machine='pc'>hvm</type>
<boot dev='hd'/>
</os>
<clock offset='utc'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<devices>
<emulator>/usr/bin/qemu</emulator>
<disk type='block' device='disk'>
<source dev='/dev/HostVG/QEMUGuest1'/>
<target dev='hda' bus='ide'/>
</disk>
</devices>
</domain>
```