Lab 12: Setting up your KVM Environment
In this lab, we will set up your Kernel Virtual Machine (KVM) environment for building and testing operating systems. I will show you how to create your own KVM image, create a user account on this image, and build and install the kernels on this image.

No submission for this lab
 

Table of Contents:

 

Creating Your Image
You will first need to create a differential image off the base image. This is done in order to save space in your user account. In order to create and use your differential image you MUST be logged into cycle1.eecs.ku.edu if you wish to ssh into qemu with a port id. . 
  1. Log into cycle1.eecs.ku.edu and create a directory for your kvm-image. You should use -Y when logging in if you want to use the graphical KVM. Also, you need at least 1.5GB to create the differential image. You will need to free up some space if you are using more than 68% or so of your allocated space.
        amodarre@2041-1$ ssh -Y cycle1.eecs.ku.edu
        amodarre@cycle1.eecs.ku.edu's password:
        Last login: Fri Mar  1 13:55:48 2013 from 2041-1.eecs.ku.edu
        You have used 63.31% of your allocated storage space.
        amodarre@cycle1$ mkdir kvm-image; cd kvm-image
        
  2. Create the differential image off the base image. The base image for this lab is stored as /usr/share/kvm/EECS678-kvm-lab/eecs678_base.qcow on cycle1.eecs.ku.edu. You may have to re-create your differential image if we update this image for future projects. The command is:

    > qemu-img create -f qcow2 -b [base_image] [diff_image] 

    This example creates a differential image named 'amir.qcow':
        amodarre@cycle1$ qemu-img create -f qcow2 -b /usr/share/kvm/EECS678-kvm-lab/eecs678_base.qcow amir.qcow

 

Boot Your Image
You will do this much more often. There are several options for booting into your image.
Setting up Your Account
Boot into your image as the root user. Your image comes with Debian 6.0 pre-installed. Most software you will need is already installed. The last section in this guide describes how to install new software on your KVM. The source code of the kernel we will use is stored on your image in /kernel-src and the config file we will use is in /configs. We need to do a little more setup before we build our kernel.
  1. Create a user account for yourself:

    > adduser [username] 

    This example creates an account for user 'amir'. After you have given the user a password, you can ignore adding the other information:
    root@debian:~# adduser amir
    Adding user `amir' ...
    Adding new group `amir' (1000) ...
    Adding new user `amir' (1000) with group `amir' ...
    Creating home directory `/home/amir' ...
    Copying files from `/etc/skel' ...
    Enter new UNIX password:
    Retype new UNIX password:
    passwd: password updated successfully
    Changing the user information for amir
    Enter the new value, or press ENTER for the default
            Full Name []: Amir Modarresi
            Room Number []:
            Work Phone []:
            Home Phone []:
            Other []:
    Is the information correct? [Y/n]
    root@debian:~#
    

  2. Add your new user to the sudoers file:
    root@debian:~# vi /etc/sudoers
      
    This will open vi on /etc/sudoers. Add your new user to this file as shown:
    # User privilege specification
    root ALL=(ALL) ALL
    amir ALL=(ALL) ALL
    

    It may also be helpful to add yourself to the sudo group: root@debian:~# usermod -a -G sudo amir
  3. Next we want to move the kernel source and the config file to the user's home directory and change ownership to the user:
    root@debian:~# mkdir /home/amir/kernel
    root@debian:~# tar xfzv linux-2.6.32.60.tar.gz
    root@debian:~# mv ~/linux-2.6.32.60 /home/amir/kernel/
    root@debian:~# cp ~/kernel-configs/kvm-kernel.config /home/amir/kernel/linux-2.6.32.60/.config
    root@debian:~# chown -R amir:amir /home/amir/kernel
    


  4. Optionally, you can copy the .vimrc from /root to your user's home directory (and change ownership to your user):
    root@debian:~# cp /root/.vimrc /home/amir/
    root@debian:~# chown amir:amir /home/amir/.vimrc
      
  5. Finally, switch user to the new user you have just created:
    root@debian:~# su amir
Building and Installing Your Kernel
Building and installing the kernel on our Debian system is easy. Follow these simple instructions:
  1. To build the kernel, run the command:

    > kvm-kernel-build [revision] 

    kvm-kernel-build is actually a script. It's contents are shown below:
    #!/bin/sh
    rev=$1
    if [ -z "$rev" ]; then
            echo "Usage: build64 "
            exit 1
    fi
    make-kpkg --rootcmd fakeroot --initrd --revision=$rev kernel_image 2>&1 | tee build.log
      
    This script takes a single argument, a revision number, which is used to give a unique name to your built kernel package. You always need to run this command from your kernel source directory. For example:
    amir@debian:~$ cd /home/amir/kernel/linux-2.6.32.60/
    amir@debian:~/kernel/linux-2.6.32.60$ kvm-kernel-build 1
      
    This will build the kernel in the KVM. This takes some time (approximately 10 to 15 minutes for a clean build). Go ahead and relax, check your favorite website, and bide your time for the next 10 minutes or so. When the build process is complete, you should find a .deb file in the directory above your kernel source.

  2. To install your kernel, the command is:

    > dpkg -i [kernel-deb-pkg] 

    You should run this command as root (using su then typing in the root password) as in:
    amir@debian:~/kernel# su
    Password:
    root@debian:home/amir/kernel$ dpkg -i linux-image-2.6.32.60_1_i386.deb
    Selecting previously deselected package linux-image-2.6.32.60.
    (Reading database ... 30041 files and directories currently installed.)
    Unpacking linux-image-2.6.32.60 (from linux-image-2.6.32.60_1_i386.deb) ...
    Done.
    Setting up linux-image-2.6.32.60 (1) ...
    Running depmod.
    Examining /etc/kernel/postinst.d.
    run-parts: executing /etc/kernel/postinst.d/initramfs-tools 2.6.32.60 /boot/vmlinuz-2.6.32.60
    update-initramfs: Generating /boot/initrd.img-2.6.32.60
    run-parts: executing /etc/kernel/postinst.d/zz-update-grub 2.6.32.60 /boot/vmlinuz-2.6.32.60
    Generating grub.cfg ...
    Found linux image: /boot/vmlinuz-2.6.32.60
    Found initrd image: /boot/initrd.img-2.6.32.60
    Found linux image: /boot/vmlinuz-2.6.32-5-686
    Found initrd image: /boot/initrd.img-2.6.32-5-686
    done
      
    When the command completes, the kernel is installed. Go back to being logged into your user using su amir

  3. Our installation should have updated the boot loader to select our kernel on the next boot. To check that the bootloader is set up correctly, do:
    amir@eecs678-kvm:~/kernel$ cat /boot/grub/grub.cfg | more
      
    You should see a line that reads:
    set default="0"
      
    The first 'menuentry' in this file should correspond to the kernel you just built (i.e. the version number should match 2.6.32.60). The next time you boot your kvm, it will have both the original kernel and the newly installed kernel selectable from the boot menu. If you use the -nographic option, you will not have the opportunity to select a kernel from the menu and it will automatically boot the default kernel. To change the default boot kernel, you need to change the GRUB_DEFAULT variable in /etc/default/grub to the number of the kernel in the boot menu that you want to boot into. The kernels in the boot menu are listed in /boot/grub/grub.cfg and are indexed at 0. If you change GRUB_DEFAULT, you need to update the grub menu by running (as root):

    > update-grub 

    For now, leave this option as GRUB_DEFAULT=0 and reboot your kvm (as root):
    amir@debian:~/kernel# su
    Password:
    root@debian:/home/amir/kernel# reboot
    
    Broadcast message from root@debian (pts/0) (Fri Mar  1 22:31:19 2013):
    The system is going down for reboot NOW!
    root@debian:/home/amir/kernel# Connection to localhost closed by remote host.
    Connection to localhost closed.
      
    This will kick you out of your kvm session. You need to wait for the KVM to boot before you can log back in.

  4. Next, we need to check that the kvm has booted into the newly built kernel. Go ahead and log back in through ssh. You can log in directly to your newly created user account:
    amodarre@cycle1$ ssh -p12345 -l amir localhost
    amodarre@localhost's password:
    Linux debian 2.6.32.60 #1 SMP PREEMPT Fri Mar 1 21:56:30 CST 2013 i686
    
    The programs included with the Debian GNU/Linux system are free software;
    the exact distribution terms for each program are described in the
    individual files in /usr/share/doc/*/copyright.
    
    Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
    permitted by applicable law.
    amir@debian:~$
      
    You should see that you are running kernel version 2.6.32.60. If you want to double-check this from the command line inside KVM, you can do:
    amir@debian:~$ uname -a
    Linux eecs678-kvm 2.6.32.60 #1 SMP PREEMPT Fri Mar 1 21:56:30 CST 2013 i686 GNU/Linux
Other Important Commands
There are some important commands you should know for using your KVM. This is not a complete list. I will add more commands and information to this section as necessary.
  1. If you would like to install or remove software, use the APT package utility:



  2. To transfer files to and from your KVM, use scp:

    > scp [user@][src-host]:[src-file] [user@][dst-host]:[dst-file] 

    You can omit the user and host information if it matches the environment you are running the command in. In this example, I run scp from the KVM to copy the file foo.pdf on cycle1.eecs.ku.edu to my kernel directory in the KVM:
    amir@debian:~$ scp cycle1.eecs.ku.edu:~/Desktop/foo.pdf /home/amir/kernel/linux-2.6.32.60/
    amodarre@cycle1.eecs.ku.edu's password:
    foo.pdf                                                               100%  954KB 954.3KB/s   00:00
    amir@debian:~$
        
    This example, which is also run from within the KVM, copies foo.pdf from the kvm to my Desktop on cycle1.eecs.ku.edu:
    amir@eecs678-kvm:~$ scp /home/amir/kernel/linux-2.6.32.60/foo.pdf cycle1.eecs.ku.edu:~/Desktop/
    amodarre@cycle1.eecs.ku.edu's password:
    foo.pdf                                                               100%  954KB 954.3KB/s   00:00
    amir@debian:~$
        
  3. If you need to remove a built kernel, use dpkg:

    > dpkg -r [kernel-package] 

    Note that, to remove a built kernel, you cannot be currently running that same kernel. This example removes the kernel we built in this lab:
    root@debian:~# dpkg -r linux-image-2.6.32.60
    (Reading database ... 30089 files and directories currently installed.)
    Removing linux-image-2.6.32.60 ...
    Examining /etc/kernel/prerm.d.
    Examining /etc/kernel/postrm.d .
    run-parts: executing /etc/kernel/postrm.d/initramfs-tools 2.6.32.60 /boot/vmlinuz-2.6.32.60
    update-initramfs: Deleting /boot/initrd.img-2.6.32.60
    run-parts: executing /etc/kernel/postrm.d/zz-update-grub 2.6.32.60 /boot/vmlinuz-2.6.32.60
    Generating grub.cfg ...
    Found linux image: /boot/vmlinuz-2.6.32-5-686
    Found initrd image: /boot/initrd.img-2.6.32-5-686
    done
      
    Note that you might need to reconfigure your boot loader after you uninstall the kernel package.

    In most cases, you will reinstall your built kernel over a kernel you have previously installed. The reinstall-kernel script provided with the second project simply uninstalls and reinstalls the 2.6.32.60 kernel in one command.

  4. Finally, to exit the kvm, run halt (as root) from your kvm terminal:

    > halt 

    For example:
    root@debian:~# halt
    
    Broadcast message from root@kvm (pts/0) (Thu Mar 21 19:58:54 2013):
    
    The system is going down for system halt NOW!
      
This completes the guide for setting up and using your KVM. There is no lab assignment this week. Please make sure you have completed this guide.

Debugging the Linux Kernel

Sometimes, it is necessary to add sometime to Linux kernel for experiment or fix a bug in the kernel. Debugging the kernel is a little bit different than debugging an application; because OS doesn't let any access from user to kernel mode directly. The following slides help you to understand how to debug the kernel.

Debugging the Linux Kernel

 

< Back to the Lab Home Page