woensdag, januari 11, 2006


This is a short howto, explaining how to setup User Mode Linux for simple Linux kernel module development and experimentation. I'm using the current Linux kernel 2.6.15 version (actually, todays GIT tree :) )

I like to keep my filesystem structure nice and clean, which is why I tend to use multiple partitions and directories for separate things. I've got a /usr/local/src directory which contains the linux-2.6 subdirectory contains the Linux kernel sourcecode. All my builddirectories are stored in /mnt/build, and so the first thing to do is to create a new entry for the Linux 2.6 UML build:
mkdir /mnt/build/linux-2.6-um

Next, enter the Linux kernel sourcecode directory:
cd /usr/local/src/linux-2.6

We'll start from the default UML configuration file for the kernel:
cp arch/um/defconfig /mnt/build/linux-2.6-um/.config

Use the defaults from that default config file:
make oldconfig ARCH=um O=/mnt/build/linux-2.6-um/

In case there's anything you want to change in the configuration of the UML kernel, start
the configuration menu:
make menuconfig ARCH=um O=/mnt/build/linux-2.6-um/

Now, we're set to do the actual building of the kernel. This will take a while...
make ARCH=um O=/mnt/build/linux-2.6-uml/

We need a disk image for the UML kernel to use. So, we'll create one. There's many ways to do this, this is a rather simple one.

First create an empty file which will kind of represent a disk drive (or actually a partition on a diskdrive):
dd if=/dev/zero of=/usr/tmp/sarge.ext2 bs=1M count=500

Put a filesystem on this partition, ext2 is fine for such a simple development system:
mkfs.ext2 /usr/tmp/sarge.ext2

Mount the filesystem image using loopback:
mount /usr/tmp/sarge.ext2 /mnt/loop/ -o loop

Copy an existing Linux distribution in this mounted filesystem image:
cp -var /usr/chroots/sarge/* /mnt/loop/

Move into the mounted filesystem and create the needed device nodes for UML:
cd /mnt/loop/dev

Next we'll install the kernel modules into the mounted filesystem:
cd /usr/local/src/linux-2.6
make modules_install O=/mnt/build/linux-2.6-uml ARCH=um INSTALL_MOD_PATH=/mnt/loop/

And finally, umount the filesystem image:
umount /mnt/loop/

Make a link named root_fs to your new partition image:
cd /usr/tmp
ln sarge.ext2 root_fs

Start the new UML kernel (and in this case, skip the default initscripts to get a real quick boot):
/mnt/build/linux-2.6-uml/linux init=/bin/bash

On the host system, create a directory for sharing files with the guest:
mkdir /tmp/forguest

In the guest system, mount this:
mount none /tmp -t hostfs -o /tmp/forguest

Now, any file appearing on the host system in /tmp/forguest, will also appear within the guest system in the /tmp directory.

Write whatever kernelmodule code (which doesn't actually access hardware since we're using UML)
you want in the /tmp/forguest directory.

Execute the following command once:
make -C /mnt/build/linux-2.6-uml/ modules ARCH=um

Build the module with the following command:
make -C /mnt/build/linux-2.6-uml/ SUBDIRS=$(pwd) modules ARCH=um

From the guest system, load the kernel module using:
insmod ./mymodule.ko

Remove it with:
rmmod mymodule

That's it! :)

2 opmerkingen:

Anoniem zei

hello. im jaya. im new in uml. want to ask the part:

Copy an existing Linux distribution in this mounted filesystem image:
cp -var /usr/chroots/sarge/* /mnt/loop/

do u mean that i have to install linux first, then copy the existing linux distribution.
is there any other way i can do?

desa[a]t]ftsl[dot[]itb[[dot]ac[]dot] id

Takis zei

Yes, if you are using Debian or a derived distro such as Ubuntu, you can use debootstrap, which allows you to install Debian or Ubuntu in a directory from a running system.

So, to install Ubuntu Dapper Drake from within Ubuntu you could for example do:
debootstrap dapper /mnt/loop

Or to install the Debian Sarge release from within Debian:
debootstrap sarge /mnt/loop