user mode linux hint update

Randy Hron rwhron at
Thu Aug 2 20:07:55 PDT 2001

-------------- next part --------------
TITLE:		User Mode Linux
AUTHOR:		Randy Hron <rwhron (at)>
	How to configure a basic user-mode-linux on lfs.
This hint is for user-mode-linux (uml) for linux-2.4.7.  We just set
up a small root filesystem, you may want to do more when you get up
and running.  

Why user-mode-linux?
It's cool.  You can try a new glibc or gcc without trashing your LFS
by running it in user-mode-linux first.  Setup a virtual network with only 
one box.  Lots of possibilities.

Where can I find out more?

The patch for user-mode-linux is at the site too.  You need the patch
for the version of the kernel you are running.  jdike and friends usually 
have a uml patch out within a day or so of a new Linus Torvalds (vanilla) 
kernel hitting

Assumptions for this hint
o  You are Braveheart.  That's a given, you're an LFSer :)
o  You have Loopback Device (CONFIG_BLK_DEV_LOOP) support configured in 
   your host kernel (this doesn't mean lo0 =  It means a regular
   file can be mounted as a filesystem).
o  You have Ethertap CONFIG_ETHERTAP configured in your kernel.
o  You aren't using devfs (though it might work).
o  You need the uml-net-tools (possibly), (uml_net.c and Makefile)(for sure),
   from the project site too.  
o  You don't have anything in /mnt/uml or /usr/src/uml, as the build scripts
   included here will unquestionably stomp these directories.
o  You have reiserfsprogs installed.

host kernel:		The kernel for your LFS system.  It "hosts" the
			uml kernel.
user-mode-linux:	Let's user run a virtual instance of linux
uml:			User Mode Linux
vanilla kernel:		Term some kernel hackers call the official Linus 
kernel pool:		Source code tree of Linus kernel.  AKA extracted
			vanilla kernel.

Installing uml_net
We'll start with the easiest part.  uml_net is used for setting
up a network between the host system and uml.  Grab uml_net.c and it's
Makefile from the download page for user-mode-linux.  make 
and install uml_net on your "host" (the only system you have
so far).

Building the user-mode-linux kernel
IMPORTANT NOTE: Don't build user-mode-linux in /usr/src/linux because uml 
replaces some headers that other linux programs could use.  Also, you don't 
want to stomp your "host" kernel.

Note: the commands below assume your linux kernel source is in 
/usr/src/sources/l and the user-mode-linux patch in /usr/src/sources/u.
Change these paths to match where you have the kernel and uml-patch 
tarballs.  There may be an updated kernel or uml-patch that you want 
to use.  At first the commands below may not seem to do anything.
That is because it takes the kernel a while to extract.  Use tar xvyf
if you want more visual feedback.

Build uml kernel script
# begin of build_uml
# silent_make="-s"
[ -c /dev/tap0 ] || mknod /dev/tap0 c 36 16 &&
cd /usr/src &&
rm -rf uml &&
mkdir uml &&
cd uml &&
echo "extracting kernel source" &&
tar xyf /usr/src/sources/l/linux-2.4.7.tar.bz2 &&
cd linux &&
bzcat /usr/src/sources/u/uml-patch-2.4.7-3.bz2|patch -p1 &&
make mrproper &&
make mrproper ARCH=um &&
unset PAGER &&
cp /usr/src/linux/.config /usr/src/uml/linux &&
yes "y" | make oldconfig ARCH=um &&
sed 's:^CONFIG_GPROF.*:# CONFIG_GPROF is not set:' .config>.config~ &&
mv .config~ .config &&
sed 's:^CONFIG_GCOV.*:# CONFIG_GCOV is not set:' .config>.config~ &&
mv .config~ .config &&
yes "" | make config ARCH=um &&
make dep ARCH=um &&
make linux ARCH=um &&
make modules ARCH=um 
# end of build_uml

Comments on uml kernel build commands
Extract the pristine linux source for your uml tree.  Install the uml patch.
Keep most of your current configuration by copying .config from /usr/src/linux 
(not really necessary, just convenient).  Say "y" to the new config options
from the uml patch.  We run 'sed' against the .config file as CONFIG_GPROF
and CONFIG_GCOV were not be working perfectly when this was written.
We unset PAGER because I saw "make oldconfig" using $PAGER once.

First boot (maybe)
After running the commands above you will have a file called 
/usr/src/uml/linux/linux.  This is the uml kernel.  You can run it now as
any user (suggest that you aren't root, just to be safe).  It should panic
nicely for you because there is no root filesystem for uml yet.  

Cleaning up uml processes
If you were couragous and booted the kernel to see it panic, you can clean 
things up quickly my killing the "linux" processes for the uml kernel:

kill $(ps -fu $LOGNAME|awk '/linux/ {print $2}')

or better yet, setup an alias:
alias kuml="kill \$(ps -fu\$LOGNAME|
	awk '/[l]inux.*kern|[l]inux.*init|[l]inux.*thread/{print \$2}')"

Creating a root filesystem for user-mode-linux
User mode linux will have it's own filesystem.  You will need to have 
CONFIG_BLK_DEV_LOOP defined in your kernel to create a root filesystem 
for user-mode-linux.  You can look at your /usr/src/linux/.config file 
to see if you already have that.  If you don't, configure and compile 
your normal (host) boot kernel before continuing.

We'll create a 100 megabyte root filesystem using reiserfs.  You can use
ext2 or another fileystem if you want.  This filesystem will not contain
everything that a basic LFS system has, but it could.

Note: The script below uses "here documents" and is very sensitive to 
extraneous spaces.  I.E. a space after a '!' character can cause the
script to not complete.  The safe way to execute this is to pop this
file into vim and grab the script verbatum.

Also note that down about 13 lines from here is an "export user=hrandoz"
line.  You want to change that to whatever user you typically login as.  

Root filesystem build script
# begin of root_fs setup
# environment for this script
# $uml is where root_fs will be mounted and built.
export uml=/mnt/uml &&
# src is where uml kernel source will be installed.
export uml_src=/usr/src/uml &&
# user is a regular user who will run uml at the end of the script
export user=hrandoz
# loopback block device
export loop=/dev/loop0
# argument for make to be quiet
[ -c /dev/tap0 ] || mknod /dev/tap0 c 36 16 
(( $? == 0 )) &&
cd $uml_src/linux &&
# create 100 meg reiserfs root fs
dd if=/dev/zero of=root_fs seek=100 count=1 bs=1M &&
losetup $loop $uml_src/linux/root_fs &&
yes | mkreiserfs $loop &&
mkdir -p $uml &&
mount -t reiserfs $loop $uml &&
cd $uml &&
# create directory structure and copy /lib /sbin /dev into rootfs
mkdir -p bin dev etc lib mnt proc root sbin usr/bin usr/sbin var/run var/log &&
cd /dev &&
find . -print|cpio -pdm $uml/dev &&
mknod $uml/dev/tap0 c 36 16
cd /sbin &&
find . -print|cpio -pdm $uml/sbin &&
cd /bin &&
find . -print|cpio -pdm $uml/bin &&
cd /lib &&
find . -maxdepth 1 -print|cpio -pdm $uml/lib &&
# grab a couple useful tools
cp -p /usr/bin/vi /usr/bin/grep $uml/usr/bin &&
for f in /usr/sbin/{inetd,in.telnetd,in.rlogind,in.ftpd,tcpd} 
do	[ -f $f ] && cp -vp $f ${uml}${f}
# cd /ro ??? what is missing here????
/dev/bd0	/		reiserfs	defaults	0 0
proc		/proc		proc		defaults	0 0
none		/dev/pts	devpts		defaults	0 0
(( $? == 0 )) &&
cat <<!>$uml/etc/inittab
# user-mode-linux inittab
ca:12345:ctrlaltdel:/sbin/shutdown -t1 -a -r now
1:2:wait:/sbin/agetty console 115000 linux
(( $? == 0 )) &&
mkdir $uml/etc/rc.d &&
cat <<!>$uml/etc/rc.d/rc.0
# /etc/rc.d/rc.0
#Sending TERM signal to all processes.
/sbin/killall5 -15

#Sending KILL signal to all processes.
/sbin/killall5 -9

#Unmounting swap partitions.
#/sbin/swapoff -a

case "$0" in
          /sbin/reboot -w
          /sbin/halt -w

#Syncing filesystems.

#Remounting root filesystem ro.
/bin/mount -n -o remount,ro /

#Unmounting other filesystems
/bin/umount -a

case "$0" in
          /sbin/reboot -d -f -i
          /sbin/halt -d -f -p
# end of /etc/rc.d/rc.0
(( $? == 0 )) &&
# /etc/rc.d/rc.1
#Disabling eth0.
#/sbin/ifconfig eth0 down

#Sending TERM signal to all processes.
/sbin/killall5 -15

#Sending KILL signal to all processes.
/sbin/killall5 -9
(( $? == 0 )) &&
# /etc/rc.d/rc.2
#Initializing eth0.
#/sbin/ifconfig eth0 broadcast netmask

#Starting inetd server.
[ -f /etc/inetd.conf ] && /usr/sbin/inetd

#Starting NFS server.
[ -x /usr/sbin/rpc.portmap ] && /usr/sbin/rpc.portmap

#Mount other filesystems
/bin/mount -a &
# end of /etc/rc.d/rc.1
(( $? == 0 )) &&
cd $uml/etc/rc.d &&
ln -s rc.2 rc.3 &&
ln -s rc.2 rc.4 &&
ln -s rc.2 rc.5 &&
ln -s rc.0 rc.6 &&
# begin of /etc/rc.d/rc.sysinit

#Mounting root device ro.
/bin/mount -n -o remount,ro /

#Mounting swap partitions.
#/sbin/swapon -a

#Remounting root device rw.
/bin/mount -n -v -o remount,rw /

echo "" > /etc/mtab
/bin/mount -f -o remount,rw /

#Mount special filesystems
/bin/mount /proc
/bin/mount /dev/pts

#Initializing loopback network interface.
/sbin/ifconfig lo
/sbin/route add -net netmask lo

#Setting hostname.
/bin/hostname uml

#Recreate the utmp file so w/who is correct.
rm -f /var/run/utmp &&
touch /var/run/utmp &&
chmod 0644 /var/run/utmp

#Loading kernel modules.
#depmod|grep ethertap || insmod ethertap

#Setup eth0
#/sbin/ifconfig eth0
# end of /etc/rc.d/rc.sysinit
(( $? == 0 )) &&
cd $uml/etc/rc.d &&
chmod 754 rc.[0-2] &&
chmod 754 rc.sysinit &&
cd $uml_src/linux &&
make $silent_make modules_install INSTALL_MOD_PATH=$uml ARCH=um &&
cd / &&
umount $uml &&
losetup -d $loop &&
modprobe ethertap || insmod ethertap &&
echo "change user=$user at the top of $0 to whatever your login name is" &&
chown $user:root $uml_src/linux/root_fs &&
cd $uml_src/linux &&
#su - $user -c "cd $uml_src/linux&&./linux rootfs=root_fs eth0=ethertap,tap0,fe:fd:0:0:0:1,"
su - $user -c "cd $uml_src/linux&&./linux rootfs=root_fs init=/bin/bash"
# end of root_fs setup

Comments on commands to build root_fs
Create a 100 megabyte sparse file called root_fs and associate it
with /dev/loop0.  Copy devices, lib, sbin, bin, and a few things
from /etc into the loopback root filesystem.  Build bsd style init
scripts (thanks saai).  

Switch back to a non-priveledged user and run the kernel:
./linux init=/bin/bash

You will be root in the uml environment.  Take a look around.
When you are done, type "exit" to kill init=/bin/bash.

Now that you can boot, you'll probably want to get the real
init and initscripts working.

Taking down uml
halt	# from within uml
kuml	# alias mentioned earlier if uml processes are still out there.
umount /mnt/uml
losetup -d /dev/loop0

o If your X terminal goes haywire, after killing uml, try "clear;reset;clear".
o Kernel panic when you have a root_fs.  If you run uml as non-root, the 
  user running uml must own root_fs.  chown the root_fs file, not all the
  everything in it.
o Error building kernel:
  gcc -Wl,-T,/usr/src/uml/linux/arch/um/link.ld  -o linux -static \
        /usr/src/uml/linux/arch/um/main.o vmlinux.o -L/usr/lib
	vmlinux.o: In function `linux_main':
	/usr/src/uml/linux/arch/um/kernel/um_arch.c:211: undefined reference
	to `remap_profiling_buffers'
	collect2: ld returned 1 exit status
	make: *** [linux] Error 1
  Somehow CONFIG_GPROF is defined to 1.  make mrproper and try building
  the kernel again.

Other helpful docs
HOWTO: Loopback Encrypted Filesystem HOWTO
HOWTO: The Linux Bootdisk HOWTO

========= #kernelnewbies and #uml

Help save the world
If you have suggestions, or want clarification, please send comments so we 
can make this hint more helpful.

Thu Aug  2 23:03:35 EDT 2001

More information about the hints mailing list