Qemu on FreeBSD
  May 11, 2009

Qemu enables you to emulate hardware on a virtual harddrive, allowing you to install an operating system (or systems) and start up the "virtual" computer as if you controlled the BIOS and hardware components.

This guide runs through the basics of installing and configuring Qemu on a FreeBSD host and shows how easy it is to virtualize an operating system.

Although this guide is aimed at using FreeBSD for the Qemu host system, the actual commands and process to interact with Qemu remain the same on virtually any Qemu installation (including Windows). Your mileage may vary.

Installing Qemu

To install Qemu onto our FreeBSD box, we will first need to install the actual Qemu port and load a kernel module (kqemu) to speed things up a bit.

For more information on ports, check out the ports man page

Make sure to check the kqemu module in the config screen!

  1. # cd /usr/ports/emulators/qemu
  2. # make config
  3. qemu make config
  4. # make install clean distclean

Make sure it gets loaded at boot time:

  1. # echo 'kqemu_enable="YES"' >> /etc/rc.conf
  2. # /usr/local/etc/rc.d/kqemu start

Now, you should see the kqemu module loaded in kldstat.

I have a few modules already loaded, so ignore them. You should notice a line containing kqemu.ko as the last column. This means that the module was correctly loaded.

  • # kldstat
  • Id Regs Address Size Name
    1 17 0xc0400000 abff88 kernel
    2 1 0xc0ec0000 75a75c nvidia.ko
    3 3 0xc161b000 28784 linux.ko
    5 1 0xc1665000 6a47c acpi.ko
    6 1 0xc6299000 7000 linprocfs.ko
    7 1 0xc664c000 e000 fuse.ko
    8 1 0xcc3bc000 20000 kqemu.ko

Prepare a Disk Image

I like to have a Qemu working-directory for my all my Qemu images, etc. so I will create one and use this directory from here on out.

  1. $ mkdir ~/qemu

To install an operating system, we need to have an ISO/IMG handy or we can install directly from a CD/DVD drive. Even if I do have an installation CD/DVD, I usually rip it to the harddrive (ISO) for easier access in the future. If you need to, though, you can install directly from a CD/DVD. If you already have an ISO handy or want to install directly from a CD/DVD, omit the next step steps and use the ISO file.

Create ISO Image from CD/DVD Media

  1. $ cd ~/qemu
  2. $ sudo dd if=/dev/acd0 of=win_xp_sp2.iso bs=2048
  3. 298271+0 records in
  4. 298271+0 records out
  5. 610859008 bytes transferred in 343.904946 secs (1776244 bytes/sec)
  6. $ cp /home/myuser/Docs/ISO/another_operating_system.iso ~/qemu

Prepare Virtual Partitions

Now we will create virtual partition(s) to install an operating system on. I use qcow2 compression (from the man page:).

qcow2
   QEMU image format, the most versatile format. Use it to have    smaller images (useful if your filesystem does not supports    holes, for example on Windows), optional AES encryption, zlib    based compression and support of multiple VM snapshots.

Go ahead and give the virtual partition a hefty size. The size parameter of 20GB, for example, will allocate a max capacity of 20GB.. not take 20GB away from your disk! It dynamically grows.

  1. $ qemu-img create -f qcow2 win_xp_sp2.qcow2 20G
  2. $Formatting 'win_xp_sp2.qcow2', fmt=qcow2, size=20971520 kB

Installing an Operating System

To boot the operating system installation ISO of choice, we need to tell Qemu what to use for the hard drive, a file for the virtual CD-ROM, and we should see an installation screen as if we put the CD into a computer and booted the installation CD.

-boot d
boot off the virtual CD-ROM

-cdrom ~/qemu/win_xp_sp2.iso
OR
-cdrom /dev/acd0 use this ISO or CD/DVD device for the virtual CD-ROM

-hda ~/qemu/win_xp_sp2.qcow2
use this virtual partition as the target to write files to

-vnc :1
(optional) this utilizes Qemu's built-in VNC server to view the emulation, compared to the slower and more CPU/Memory intensive X11 viewport. If you didn't use this option, the emulation would be visible in a pop up window.

You must have a VNC viewer/client of some sort and connect to localhost:1. I recommend TightVNC... this is a cross playform client so even if you are on Windows, you can use it.

-localtime
sync the virtual time with the host's time

  1. $ qemu -boot d -cdrom ~/qemu/win_xp_sp2.iso -hda ~/qemu/win_xp_sp2.qcow2 -vnc :1 -localtime
OR
  1. $ sudo qemu -boot d -cdrom /dev/acd0 -hda ~/qemu/win_xp_sp2.qcow2 -vnc :1 -localtime

The next thing you should see in either the window that popped up (without the -vnc :1 option) or in your VNC viewer is a normal Windows XP install screen.

Windows Install Screen

Starting the Operating System

To start the operating system, we basically strip away the cd boot parameters and specify slightly more ram since we might be doing some heavy lifting.

The -m is to specify how big to make the virtual memory. Unless you will be doing some serious stuff, 256 should be enough. The default is 128MB

  1. $ qemu -hda ~/qemu/win_xp_sp2.qcow2 -m 256 -localtime
qemu windows load screen

Networking Support

By default, Qemu enables internal DHCP that serves up 10.0.2.xx addresses and enables the virtual operating system to have online connectivity. Though it has outside access to the web, the virtualized host is invisible to the world, meaning we can't access it's network resources.

The are a few ways you can do the following, but this is what I use when ever I need to access Qemu.

Qemu uses 2 scripts that are used when Qemu starts and terminates. The scripts are /usr/local/etc/qemu-ifup and /usr/local/etc/qemu-ifdown respectfully. Edit these scripts to create a virtual network interface in FreeBSD and create a bridge between our upstream network device on the Qemu host the and the virtual network interface that the Qemu guest operating system will be using (tap in our case).

Make sure the Qemu host has it's firewall rules tuned or disabled! The firewall might prevent the DHCP request from getting to the guest operating system, amongst other headaches that might occur as a result.

qemu-ifup
  1. # vi /usr/local/etc/qemu-ifup
  2. #!/bin/sh
  3.  
  4. WAN_IF="em0"
  5. BRIDGE_IF="bridge0"
  6. TAP_IF="${1}"
  7.  
  8. # allow packets to forward to another interface
  9. /sbin/sysctl net.inet.ip.forwarding=1
  10.  
  11. # create the bridge
  12. /sbin/ifconfig $BRIDGE_IF create
  13. /sbin/ifconfig $BRIDGE_IF addm $TAP_IF addm $WAN_IF
  14.  
  15. # bring interfaces up
  16. /sbin/ifconfig $WAN_IF up
  17. /sbin/ifconfig $TAP_IF up
  18. /sbin/ifconfig $BRIDGE_IF up

qemu-ifdown
  1. # vi /usr/local/etc/qemu-ifdown
  2. #!/bin/sh
  3.  
  4. BRIDGE_IF="bridge0"
  5.  
  6. # destroy tap interface
  7. /sbin/ifconfig ${1} destroy
  8.  
  9. # destroy bridge interface
  10. /sbin/ifconfig $BRIDGE_IF destroy
  11.  
  12. # disable packet forwarding
  13. /sbin/sysctl net.inet.ip.forwarding=0

Notice that you now have to run qemu with root privileges. so the interface configuration in the scripts can take place. I just use sudo run Qemu.

The new start up command will be the same as before, just with the interface parameters. For more information on Qemu parameters, visit the qemu man page and also take not of the related commands at the bottom of that man page..

Do not forget the MAC address below! I recently had a lot of problems with networking when I omitted it.

  1. # sudo qemu -hda ~/qemu/win_xp_sp2.qcow2 -m 256 -localtime -vnc :1 -net nic,macaddr=12:34:56:78:9A:BC -net tap,ifname=tap0
  2. net.inet.ip.forwarding: 0 -> 1

Now, when you load the virtual operating system, configure it's main network interface for DHCP. I believe Windows sets this to the default on a fresh install, but if you are using Linux, FreeBSD, or something resembling a Unix like system, use dhclient device_name_here as root to get a DHCP lease on a specific network interface if it does not automatically do so.

The following is the result of booting our Windows image after specifying the network parameters with the qemu command.

Qemu Windows ipconfig

As you can see, the subnet range is no longer falling in the 10.0.2.xxx address range, but instead we have a normal DHCP lease from my home router! This means that we should be able to access the virtual operating system's network resources as if it were a physical computer on our hardwired LAN.

Using Qemu

  1. Mouse gets stuck in the virtual desktop window
  2. hit CTRL and SHIFT at the same time to refocus the mouse on the entire screen. Click back in the window containing the virtual environment to refocus the mouse inside the virtual desktop.
  3.  
  4. Guest operating system not getting DHCP lease
  5. Unfortunately, this is a broad item to troubleshoot! Check that your Qemu host operating system as some sort of "ip forwarding" on, allowing you to forward packets from one interface to the other. Also, make sure all firewalls are turned off or tuned to let DHCP traffic pass through freely. Lastly, check the Qemu host's bridge settings in ifconfig. Free free to contact me and maybe I can help.
  6.  
  7. A left over bridge or tap interface is left over and I can't start Qemu with the scripts provided
  8. You can destroy the interfaces (tap and bridge manually by simply doing the following as root.

Your interface index's should be 0, but take a peak at the output of ifconfig and notice the bridge and tap interfaces. You might not have both.

  1. # ifconfig bridge0 destroy
  2. # ifconfig tap0 destroy



Post a New Comment

Name

Message

Security
Code

        (case insensitive & space between words)


Posted Comments
anonymous  Jul 30, 2011
Acid Reflux Medication Medicine http://www.samsungpages.com/ - ambien cr The credit for the invention of Ambien drug goes to one of the UK Pharmacy firm. <a href=http://www.samsungpages.com/>ambien pill</a>