--- title: Qemu - Install Guide tags: VM, qemu --- # QEMU-KVM - How to install and create a VM 1. Download and build QEMU source code ```bash=1 wget http://download.qemu-project.org/qemu-2.8.0.tar.xz tar xvJf qemu-2.8.0.tar.xz cd qemu-2.8.0 ./configure --target-list=x86_64-softmmu \ --python=/bin/python2.7 \ --prefix=${TARGET_DIR} make -j 4 make install ``` - `${TARGET_DIR}` is your absolute path you want to install your Qemu to, e.g. */home/demouser/qemu-bin* 2. Create the image format supported by QEMU ```bash=1 mkdir project cd project ${TARGET_DIR}/bin/qemu-img create -f raw ubuntu.img 15G ``` - ubuntu.img is the image file name - 15G is the size of the image file 3. Install ubuntu-16.04 server on VM 1. Prepare to boot the kernel ```bash=1 ${TARGET_DIR}/bin/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 \ -cdrom /home/share/ubuntu/ubuntu.iso -kernel /home/share/ubuntu/vmlinuz \ -nographic -append console=ttyS0 \ -initrd /home/share/ubuntu/initrd.gz ``` Command illustration: - -m : memory size - -net : network interface setting - -cdrom : ubuntu-16.04 image - -kernel vmlinuz : a compresses linux kernel from vmlinux - -nographic -append console=ttyS0 : Redirect standard output to host machine 2. Install ubuntu 16.04 ![](http://i.imgur.com/YK3GsQB.png) ![](http://i.imgur.com/j18mlBK.png) ![](http://i.imgur.com/wXdomGt.png) ![](http://i.imgur.com/FxkkNAI.png) ![](http://i.imgur.com/9SWbhQO.png) ![](http://i.imgur.com/B1tffTm.png) ![](http://i.imgur.com/DEldblx.png) input "your studentID"-0 ![](http://i.imgur.com/3e9v0bm.png) ![](http://i.imgur.com/7yBgdJY.png) ![](http://i.imgur.com/3a0r3e6.png) ![](http://i.imgur.com/qmgwUla.png) ![](http://i.imgur.com/ZO7IORO.png) ![](http://i.imgur.com/sIrZjVE.png) ![](http://i.imgur.com/DKhapsf.png) ![](http://i.imgur.com/TCjwTii.png) ![](http://i.imgur.com/BuK2nYB.png) ![](http://i.imgur.com/tWZ8ag2.png) ![](http://i.imgur.com/n1dawBI.png) ![](http://i.imgur.com/xsIBrQi.png) ![](http://i.imgur.com/hVdFNkY.png) ![](http://i.imgur.com/vZwqszA.png) ![](http://i.imgur.com/nU0ortr.png) ![](http://i.imgur.com/9AQrN49.png) ![](http://i.imgur.com/Cl0hojm.png) ![](http://i.imgur.com/z0xHbuQ.png) ![](http://i.imgur.com/drcCWGh.png) ![](http://i.imgur.com/AuCTBIn.png) ![](http://i.imgur.com/e7IVfr0.png) 3. **Terminate QEMU** 1. Open a new session on terminal and login your account 2. **Kill QEMU** ```bash=1 killall qemu-system-x86_64 ``` 4. Exexute the virtual machine: ```bash=1 ${TARGET_DIR}/bin/qemu-system-x86_64 \ --enable-kvm -drive format=raw,file=image_file,if=virtio \ -m 2048 -net nic,model=virtio -net user \ -rtc base=localtime,clock=host \ -curses ``` - You will find out your VM is blocked in this step ... ![](https://i.imgur.com/OObsJvm.png) - Two ways to solve this issue: 1. Switch to QEMU monitor console in the current terminal - Press `alt` + `2` or `esc` + `2` to switch to QEMU console, type `sendkey ctrl-alt-f1`, and then press `enter` ![](https://i.imgur.com/2Hgx5Jn.png) - Press `alt` + `1` or `esc + 1` to switch back to screen ![](http://i.imgur.com/6JzDtyr.png) 2. Access the QEMU monitor console via Telnet - Add the telnet option (`-monitor telnet:127.0.0.1:5566,server,nowait`) when launching the VM: ```bash=1 ${TARGET_DIR}/bin/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 \ -monitor telnet:127.0.0.1:5566,server,nowait ``` - Open a new session on terminal and type: - $ `telnet 127.0.0.1 5566` - Enter the QEMU monitor and input: - (qemu) `sendkey ctrl-alt-f1` - Switch to the original session and log in to your account