Quick virtual machine notes for Fedora 11

Quickest way to try it out:

  1. install qemu-kvm (or just qemu if your machine doesn’t support hardware virtualisation), libvirt and virt-manager and make sure libvirtd is running

  2. download a livecd for the distribution you want to run, call it somelivecd.iso. This will be the “guest” OS. Fedora 11 is the “host” OS here.
  3. If you have tried to use libvirtd or virt-manager before without success, it would be wise to rm -r ~/.virt-manager ~/.libvirtd
  4. fire up virt-manager.
  5. click on New (bottom right)
  6. enter a name for the virtual machine in question (an identifier, ie no spaces), lets say “Fremsley”
  7. leave “Local install media” selected
  8. click Forward
  9. leave “Use iso image” selected and enter the absolute path to somelivecd.iso
  10. leave os type and version both as generic
  11. click Forward
  12. Select enough RAM to run the OS in
  13. click Forward
  14. untick “Enable storage for this machine” — we're not planning to use any disc space for this quick trial
  15. click Forward
  16. click Finish; a window should come up titled “Fremsley”
  17. click on Run in this window and the virtual machine should boot. After a short pause you’ll see the usual boot messages for your chosen OS. Once you click in this window your mouse will be grabbed, so you need to know that pressing Alt and Ctrl together will ungrab it. If you are using a graphical front end to your OS, you’s notice that the mouse behaviour is a bit odd. This comes from motion translation between the mouse pointer on your host OS and the mouse device on the guest OS.

    The solution to this is to configure the guest to have a tablet rather than a mouse (assuming that your guest OS supports the available tablet, which isn’t particularly likely).

  18. Shut the virtual machine down in whatever way is usual for the guest OS. Depending on the OS you may need to use Virtual Machine => Shutdown => Force Off to actually power off the virtual machine once it has shut down, but with unixes you can probably shutdown -[p/P] now

Creating a custom virtual machine with a webserver

These web pages are darcs repository, so if you install darcs (sudo yum install darcs), you can then

darcs get --lazy http://www.chaos.org.uk/~jf/Virtual-machine-notes

You’ll need to install livecd-tools (sudo yum install livecd-tools). If you want something with more than the minimal set of rpms that I talk about here, you should consider installing the spin-kickstarts rpm

Now change to the directory created by darcs (Virtual-machine-notes) and have a look around. You’ll want to edit the makefile to change YUM_CACHE_DIR to somewhere to keep a cache of rpms downloaded the first time you make something, and then

make livecd-fedora-webserver.iso

should make a livecd of a minimal machine with a webserver, and you can use the recipe above to make a virtual machine out of it.

The directory webserver-templates is used for files that you want to install into the .iso; for example webserver-templates/etc/sysconfig/network would be copied to /etc/sysconfig/network on the virtual machine, so you may want to create it and add HOSTNAME=… to it.

Most files are just copied from the template directory into the virtual machine, but if a file is called something-or-other.append our kickstart file’s post-install script will append it to something-or-other in the virtual machine.

Getting your web pages into the virtual machine

There are several ways of doing this — probably the best would be to assign a disk partition to the webserver virtual machine, but here I’m going to describe what I think is currently the easiest way.

  1. Create an empty file to hold a disc image
    dd if=/dev/zero of=webserver-state.img bs=1024 seek=$[256*1024] count=1
    
  2. Make a filesystem on it
    mke2fs -j -L var-www-html webserver-state.img
    

    (answer y to “Proceed anyway?”, you do want to create a filesystem on something that isn’t a block-special device.

  3. mount it somewhere

    mkdir /tmp/webserver-state
    sudo mount webserver-state.img /tmp/webserver-state/ -o loop
    
  4. Put some web content into that directory

    
    echo "First webpage" > /tmp/webserver-state/index.txt
    
  5. Make sure the selinux security context is correct

    
    chcon --recur -t httpd_sys_content_t /tmp/webserver-state/
    

    You’ll get an error about not being able to change the context of lost+found; this is OK.

  6. Unmount the image
    
    sudo umount /tmp/webserver-state/
    

    This is important. Bad things can happen if you have two different OSs accessing the same filesystem at once.

  7. Use an .append file in the template directory to modify /etc/fstab on the virtual machine:

    echo "LABEL=var-www-html	/var/www/html	ext3	defaults 0 0" > webserver-templates/etc/fstab.append
    
  8. Re-make the virtual machine

    make livecd-fedora-webserver.iso
  9. Make sure the virtual machine isn’t running, and use virt-manager to add webserver-state.img as a new hard disc.

    1. Click on the Details tab of the webserver virt-manager window. At this point under “Overview” it should say Status: Shutoff.
    2. Click on “+ Add hardware” (bottom left)
    3. Select Hardware Type => Storage
    4. Click Forward
    5. Enter the file path of webserver-state.img into the “File (disk image): Location” box.
    6. Click Forward
    7. Click Finish
    8. Select the Console tab and click Run to start the machine again

Stuff not sorted out yet

Add a physical device eg network card

  1. Create a machine
  2. Under the details tab for that machine select “Add hardware”
  3. Hardware type: Physical Host Device
  4. Select Device Type: PCI or USB as appropriate for the device
  5. Select Device: from the list
  6. Selinux Administration: select Boolean; filter on virt
  7. Tick Allow svirt to manage device configuration, (pci) and/or Allow svirt to use usb devices — at time of writing, this isn’t sufficient, and anyway, although putting selinux in permissive mode avoids this, the vm just disappears on attempting to boot.

Adding an absolute tablet

  1. Click on the Details tab of the “Fremsley” window. At this point under “Overview” it should say Status: Shutoff.
  2. Click on “+ Add hardware” (bottom left)
  3. Select Hardware Type => Input
  4. Click Forward
  5. Select EvTouch USB Tablet
  6. Click Forward
  7. Click Finish
  8. Select the Console tab and click Run to start the machine again
  9. Configure the OS to use the graphics tablet as you would if it were a real machine.