Using QEMU on Linux

This topic was published by and viewed 3002 times since "". The last page revision was "".

Viewing 1 post (of 1 total)
  • Author
    Posts

  • DevynCJohnson
    Keymaster
    • Topics - 437
    • @devyncjohnson

    Qemu is another virtual machine application for Linux and an alternative to Oracle's VirtualBox. With Qemu, users can run a larger variety of virtual machines than VirtualBox. Oracle's VBox supports x86-64 and x86 operating systems as guests while Qemu supports other architectures such as arm, mips, and more. However, many users report difficulties with network support for the guest systems. This article will discuss the basics of using Qemu, but not the networking.

    To create virtual hard-drives for the virtual machines, run "qemu-img create /PATH/AND/FILENAME SIZE" where "SIZE" is the desired virtual hard-drive size (3G is 3-gigabytes, 512M = 512-megabytes, etc.). Also, specify the desired location and name for the file. Alternately, the virtual hard-drives can be create via dd if=/dev/zero of=~/vhdd.img bs=1G count=1" which will create a one gigabyte file in the user's home folder titled "vhdd.img". Change the location, name, and size as needed. However, both of those commands create raw image files (*.img). There are ways to create virtual hard-drive files of other formats as seen below.

    • Template: qemu-img create -f TYPE /PATH/AND/FILENAME SIZE
    • Example: qemu-img create -f qcow2 /media/VM/vhdd.img 10G

    Types

    • cloop - Compressed Loop format (Knoppix and other live-distro images)
    • cow - Copy-On-Write (very old version; use qcow2 instead)
    • qcow - Qemu Copy-On-Write (older version)
    • qcow2 - Qemu Copy-On-Write (newer version)
    • vmdk - VMware image; formats 3, 4, and 6
    • vdi - VirtualBox v1.1 hard-drive images

    To format (apply a filesystem) an image file (*.img) in the host, use a command like "mkfs.* [parameters] /PATH/TO/FILE". For example, to format an image file in the current directory to Fat32, type "mkfs.vfat -s 16 -F 32 ./vhdd.img". To mount that same image file in the host, type mount -t vfat -o loop,rw ./vhdd.img ~/vdrive/. Remember, the host and Qemu cannot use/access the same virtual hard-drive simultaneously.

    Once a virtual hard-drive is ready, users can boot from an ISO file, another virtual hard-drive, or a real storage unit (like a live-CD). For instance, to boot from a live-disc (like ReactOS), type "qemu-system-i386 -hda ./vhdd -cdrom /dev/sr0 -m 512 -boot d" after inserting the live-CD. The "-boot d" parameter informs Qemu to boot from the virtual cdrom/disc. The real cdrom is set to also be the virtual cdrom (-cdrom /dev/sr0). The virtual hard-drive is set as the virtual hard-drive (-hda ./vhdd -cdrom). 512 megabytes of real memory is dedicated to the virtual machine (-m 512). The Qemu command used is "qemu-system-i386" which is for i386 guests. The different commands for various architectures are listed below.

    ReactOS running in QEMU ReactOS running in QEMU

    qemu-system-* -

    • qemu-system-alpha
    • qemu-system-arm
    • qemu-system-cris
    • qemu-system-i386
    • qemu-system-lm32
    • qemu-system-m68k
    • qemu-system-microblaze
    • qemu-system-microblazeel
    • qemu-system-mips
    • qemu-system-mips64
    • qemu-system-mips64el
    • qemu-system-mipsel
    • qemu-system-moxie
    • qemu-system-or32
    • qemu-system-ppc
    • qemu-system-ppc64
    • qemu-system-ppcemb
    • qemu-system-s390x
    • qemu-system-sh4
    • qemu-system-sh4eb
    • qemu-system-sparc
    • qemu-system-sparc64
    • qemu-system-unicore32
    • qemu-system-x86_64
    • qemu-system-xtensa
    • qemu-system-xtensaeb

    To list specific CPUs belonging to the desired architecture, type "qemu-system-ARCH -cpu ?". So, to list supported i386 CPUs, a user would type "qemu-system-i386 -cpu ?". When loading a virtual machine, the CPU can be specified with the parameter "-cpu pentium3".

    In general, the following commands will work. However, remember to replace "ARCH" with the appropriate architecture of the guest. Some users may also need to tweak the following commands to use more or less memory or to specify a different device of path.

    # Boot from a live optical disc for quick testing

    qemu-system-ARCH -cdrom /dev/sr0 -m 512 -boot d

    # Boot from an ISO image for testing

    qemu-system-ARCH -cdrom /PATH/FILE.iso -m 512 -boot d

    # Boot from some installable media that will be installed in the virtual machine

    qemu-system-ARCH -hda ./vhdd -cdrom /dev/sr0 -m 512 -boot d

    # Boot from a virtual hard-drive

    qemu-system-ARCH -hda ./vhdd -m 512

    # Boot from a virtual hard-drive, but add a cdrom for data access

    qemu-system-ARCH -hda ./vhdd -m 512 -cdrom /dev/sr0

    # Boot with multiple virtual hard-drives attached (the max is four: hda, hdb, hdc, hdd)

    qemu-system-ARCH -hda ./vhdd -hdb ./vhdd1 -hdc ./vhdd2 -m 512

    # Boot with a host directory attached as a hard-drive (replace "PATH" with the desired path to folder;

    # "host0" is the name of the virtual drive to the host's shared directory)

    qemu-system-ARCH -hda ./vhdd -m 512 -virtfs local,path=/PATH/,mount_tag=host0,security_model=passthrough,id=host_share

    # To mount the host's directory in the guest OS mount -t 9p -o trans=virtio host0 /mnt/host/ -oversion=9p2000.L

    Minix3 running in QEMU Minix3 running in QEMU

    RPi

    Many developers may want to boot a virtual Raspberry Pi (RPi) system (like Raspbian). This is possible with Qemu. Depending on the RPi Linux distro, the user may need to obtain a specialized kernel that has added Qemu features for the guest (http://xecdesign.com/downloads/linux-qemu/kernel-qemu). To obtain normal images of various RPi distros, visit http://www.raspberrypi.org/downloads/.

    In the command-line, go to the directory containing all of the files needed for the virtual RPi (the specialized kernel and the *.img file for the desired distro). Next, run a command like the one below.

    qemu-system-arm -kernel kernel-qemu -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw init=/bin/bash" -hda raspbian.img

    The modified kernel must have no more than 256 megabytes of memory (-m 256). The "-kernel" parameter specifies that the modified kernel kernel be used, and the "-hda" parameter specifies the image file for the desired distro. The other options are specific parameters that make the virtual Raspberry Pi work correctly.

    A command-line should be seen due to "init=/bin/bash" typed in the entered. This is needed because the user must comment out "/usr/lib/arm-linux-gnueabihf/libcofi_rpi.so" in /etc/ld.so.preload. If that is the only line in that file, then run X=cat /etc/ld.so.preload; echo \#$X > /etc/ld.so.preload. Otherwise, if other lines are in the file, open the file with "nano" and comment out the specified line. Afterwards, restart Qemu and the virtual machine, but use the command below to start the virtual RPi instead of the formerly used command. The only differences between the commands is the presence/absence of "init=/bin/bash".

    qemu-system-arm -kernel kernel-qemu -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" -hda raspbian.img

    Convert Disk Images

    If users need to convert a disk image from one type to another, then type a command like this - "qemu-img convert -O DESIRED-FORMAT ORIG-FILE DESIRED-FILE". So, to convert a virtual hard-drive to a raw image (*.img) type "qemu-img convert -O raw original.qcow2 new.raw". If a virtual hard-drive is used by Qemu, but later the user wishes to convert the virtual drive file to a *.vdi file to use in Oracle's VirtualBox, use the following commands in the order they are given.

    qemu-img convert -O raw ORIG-FILE NEW.raw
    VBoxManage convertfromraw -format VDI NEW.raw FINAL.vdi
    VBoxManage modifyvdi FINAL.vdi compact

    To write a virtual hard-drive file to a real hard-drive, type "qemu-img convert -O raw VIRT-FILE /dev/SOME_DEVICE".

    To get info on your virtual hard-drive, type "qemu-img info PATH" where path specifies the location and name of the virtual hard-drive file.

    Further Reading

    Attachments:
    1. Minix3 running in QEMU
    2. ReactOS running in QEMU
Viewing 1 post (of 1 total)