user-mode Linux

User-mode Linux is cool. It takes a little bit of knowledge, and then you can work with the Linux kernel as a normal user-mode process.

However the Ubuntu docs are a little old an obsolete. My documentation is here:

sudo apt-get install user-mode-linux uml-utilities bridge-utils debootstrap -y

# Create an empty file to contain the root filesystem
dd if=/dev/zero of=uml-root-hardy bs=4096 seek=1M count=1

# Create the filesystem on the file
mkfs.xfs uml-root-hardy

# Mount the filesystem
sudo mount -o loop uml-root-hardy /mnt

# Switch to root account for the rest
sudo -i

# Install a base system on the mounted partition. Use i386 or amd64 for the --arch option
debootstrap --arch amd64 hardy /mnt http://us.archive.ubuntu.com/ubuntu --include openssh-server

# Create an fstab file
echo "/dev/ubd0 / xfs defaults 0 1" > /mnt/etc/fstab
echo "proc /proc proc defaults 0 0" >> /mnt/etc/fstab

# Create the device file for the root file system and configure permissions
mknod --mode=660 /mnt/dev/ubd0 b 98 0
chown root:disk /mnt/dev/ubd0

# Create a hosts file
echo "127.0.0.1 localhost" > /mnt/etc/hosts

# Configure network interfaces: eth0 using dhcp
echo "auto lo" > /mnt/etc/network/interfaces
echo "iface lo inet loopback" >> /mnt/etc/network/interfaces
echo "auto eth0" >> /mnt/etc/network/interfaces
echo "iface eth0 inet dhcp" >> /mnt/etc/network/interfaces

# Add first terminal and serial terminal to secure list
echo "tty0" >> /mnt/etc/securetty
echo "ttys/0" >> /mnt/etc/securetty

# Remove the other terminal events
rm /mnt/etc/event.d/tty2
rm /mnt/etc/event.d/tty3
rm /mnt/etc/event.d/tty4
rm /mnt/etc/event.d/tty5
rm /mnt/etc/event.d/tty6
rm /mnt/etc/udev/rules.d/75-persistent-net-generator.rules

#unmount /mnt
umount /mnt

# Return to normal account
exit

Now we need to modify /etc/default/uml-utilities. It should look like the following:

# Options to pass to uml_switch.

# set to "false" if you want to prevent uml_switch from
# starting with SysV scripts in /etc/init.d
UML_SWITCH_START="true"

# For preconfigured tap setup, see
# /usr/share/doc/uml-utilities/examples/interfaces.example
UML_SWITCH_OPTIONS="-tap tap0"

# User as which to run uml_switch
UML_SWITCH_USER="myusername"

# Socket file to use
# Debian's default is:
UML_SWITCH_CTL="/var/run/uml-utilities/uml_switch.ctl"
#
# if you instead use your rolled up kernel from upstream
# sources you may want to uncomment the following:
#UML_SWITCH_CTL="/tmp/uml.ctl"

Then /etc/init.d/uml-utilities restart

Start the COW filesystem without network: linux ubd0=uml-root-hardy.cow1,uml-root-hardy umid=uml1 or modify the filesystem linux ubd0=uml-root-hardy umid=uml1, but if you are like me this will open up 9 xterms which is not preferable, and you actually want a network, so I arrived at the following:linux ubd0=uml-root-hardy-cow1,uml-root-hardy umid=uml1 con2=foo con3=foo con4=foo con5=foo con6=foo eth0=tuntap,,,192.168.99.5/24 mem=256M

Now we should get a single xterm which we can use to ifconfig eth0 192.168.99.6 netmask 255.255.255.0, and we can even configure our host as a gateway to allow our UML guest access to the Internet. This means our host computer which is running UML will have the IP 192.168.99.5 on the interface tap0 and our guest computer which is an UML instance will have the IP 192.168.99.6 and firewalls permitting, you should be able to ping, ssh, proxy, IP masquerade, etc.

This is not very interesting using the stock Ubuntu kernel. In our next post we will explore using a custom kernel.

Leave a Reply