User Mode Linux HOWTO¶
| Author: | User Mode Linux Core Team |
|---|---|
| Last-updated: | Sat Jan 25 16:07:55 CET 2020 |
This document describes the use and abuse of Jeff Dike’s User ModeLinux: a port of the Linux kernel as a normal Intel Linux process.
1. Introduction¶
Welcome to User Mode Linux. It’s going to be fun.
1.1. How is User Mode Linux Different?¶
Normally, the Linux Kernel talks straight to your hardware (videocard, keyboard, hard drives, etc), and any programs which run ask thekernel to operate the hardware, like so:
+-----------+-----------+----+| Process 1 | Process 2 | ...|+-----------+-----------+----+| Linux Kernel |+----------------------------+| Hardware |+----------------------------+The User Mode Linux Kernel is different; instead of talking to thehardware, it talks to areal Linux kernel (called thehost kernelfrom now on), like any other program. Programs can then run insideUser-Mode Linux as if they were running under a normal kernel, likeso:
+----------------+ | Process 2 | ...|+-----------+----------------+| Process 1 | User-Mode Linux|+----------------------------+| Linux Kernel |+----------------------------+| Hardware |+----------------------------+
1.2. Why Would I Want User Mode Linux?¶
- If User Mode Linux crashes, your host kernel is still fine.
- You can run a usermode kernel as a non-root user.
- You can debug the User Mode Linux like any normal process.
- You can run gprof (profiling) and gcov (coverage testing).
- You can play with your kernel without breaking things.
- You can use it as a sandbox for testing new apps.
- You can try new development kernels safely.
- You can run different distributions simultaneously.
- It’s extremely fun.
2. Compiling the kernel and modules¶
2.1. Compiling the kernel¶
Compiling the user mode kernel is just like compiling any otherkernel.
Download the latest kernel from your favourite kernel mirror,such as:
https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.4.14.tar.xz
Make a directory and unpack the kernel into it:
host%mkdir ~/umlhost%cd ~/umlhost%tar xvf linux-5.4.14.tar.xzRun your favorite config;
makexconfigARCH=umis the mostconvenient.makeconfigARCH=umandmakemenuconfigARCH=umwill work as well. The defaults will give you a useful kernel. Ifyou want to change something, go ahead, it probably won’t hurtanything.Note: If the host is configured with a 2G/2G address space splitrather than the usual 3G/1G split, then the packaged UML binarieswill not run. They will immediately segfault. See4. UML on 2G/2G hosts for the scoop on running UML on your system.
Finish with
makelinuxARCH=um: the result is a file calledlinuxin the top directory of your source tree.
2.2. Compiling and installing kernel modules¶
UML modules are built in the same way as the native kernel (with theexception of the ‘ARCH=um’ that you always need for UML):
host% make modules ARCH=umAny modules that you want to load into this kernel need to be built inthe user-mode pool. Modules from the native kernel won’t work.
You can install them by using ftp or something to copy them into thevirtual machine and dropping them into
/lib/modules/$(uname-r).You can also get the kernel build process to install them as follows:
with the kernel not booted, mount the root filesystem in the toplevel of the kernel pool:
host% mount root_fs mnt -o looprun:
host%make modules_install INSTALL_MOD_PATH=`pwd`/mnt ARCH=umunmount the filesystem:
host% umount mntboot the kernel on it
When the system is booted, you can use insmod as usual to get themodules into the kernel. A number of things have been loaded into UMLas modules, especially filesystems and network protocols and filters,so most symbols which need to be exported probably already are.However, if you do find symbols that need exporting, let usknow athttp://user-mode-linux.sourceforge.net/, andthey’ll be “taken care of”.
2.3. Compiling and installing uml_utilities¶
Many features of the UML kernel require a user-space helper program,so a uml_utilities package is distributed separately from the kernelpatch which provides these helpers. Included within this is:
port-helper - Used by consoles which connect to xterms or ports
tunctl - Configuration tool to create and delete tap devices
uml_net - Setuid binary for automatic tap device configuration
uml_switch - User-space virtual switch required for daemontransport
The uml_utilities tree is compiled with:
host#make && make installNote that UML kernel patches may require a specific version of theuml_utilities distribution. If you don’t keep up with the mailinglists, ensure that you have the latest release of uml_utilities if youare experiencing problems with your UML kernel, particularly whendealing with consoles or command-line switches to the helper programs
3. Running UML and logging in¶
3.1. Running UML¶
It runs on 2.2.15 or later, and all kernel versions since 2.4.
Booting UML is straightforward. Simply run ‘linux’: it will try tomount the file
root_fsin the current directory. You do not need torun it as root. If your root filesystem is not namedroot_fs, thenyou need to put aubd0=root_fs_whateverswitch on the linux commandline.You will need a filesystem to boot UML from. There are a numberavailable for download fromhttp://user-mode-linux.sourceforge.net.There are also several tools athttp://user-mode-linux.sourceforge.net/ which can beused to generate UML-compatible filesystem images from media.The kernel will boot up and present you with a login prompt.
- Note:
- If the host is configured with a 2G/2G address space splitrather than the usual 3G/1G split, then the packaged UML binaries willnot run. They will immediately segfault. See4. UML on 2G/2G hostsfor the scoop on running UML on your system.
3.2. Logging in¶
The prepackaged filesystems have a root account with password ‘root’and a user account with password ‘user’. The login banner willgenerally tell you how to log in. So, you log in and you will findyourself inside a little virtual machine. Our filesystems have avariety of commands and utilities installed (and it is fairly easy toadd more), so you will have a lot of tools with which to poke aroundthe system.
There are a couple of other ways to log in:
On a virtual console
Each virtual console that is configured (i.e. the device exists in/dev and /etc/inittab runs a getty on it) will come up in its ownxterm. If you get tired of the xterms, read5. Setting up serial lines and consoles to see how to attachthe consoles to something else, like host ptys.
Over the serial line
In the boot output, find a line that looks like:
serial line 0 assigned pty /dev/ptyp1Attach your favorite terminal program to the corresponding tty. I.e.for minicom, the command would be:
host% minicom -o -p /dev/ttyp1
Over the net
If the network is running, then you can telnet to the virtualmachine and log in to it. See6. Setting up the network to learnabout setting up a virtual network.
When you’re done using it, run halt, and the kernel will bring itselfdown and the process will exit.
3.3. Examples¶
Here are some examples of UML in action:
- A login sessionhttp://user-mode-linux.sourceforge.net/old/login.html
- A virtual networkhttp://user-mode-linux.sourceforge.net/old/net.html
4. UML on 2G/2G hosts¶
4.1. Introduction¶
Most Linux machines are configured so that the kernel occupies theupper 1G (0xc0000000 - 0xffffffff) of the 4G address space andprocesses use the lower 3G (0x00000000 - 0xbfffffff). However, somemachine are configured with a 2G/2G split, with the kernel occupyingthe upper 2G (0x80000000 - 0xffffffff) and processes using the lower2G (0x00000000 - 0x7fffffff).
4.2. The problem¶
The prebuilt UML binaries on this site will not run on 2G/2G hostsbecause UML occupies the upper .5G of the 3G process address space(0xa0000000 - 0xbfffffff). Obviously, on 2G/2G hosts, this is rightin the middle of the kernel address space, so UML won’t even load - itwill immediately segfault.
4.3. The solution¶
The fix for this is to rebuild UML from source after enablingCONFIG_HOST_2G_2G (under ‘General Setup’). This will cause UML toload itself in the top .5G of that smaller process address space,where it will run fine. See2. Compiling the kernel and modules ifyou need help building UML from source.
5. Setting up serial lines and consoles¶
It is possible to attach UML serial lines and consoles to many typesof host I/O channels by specifying them on the command line.
You can attach them to host ptys, ttys, file descriptors, and ports.This allows you to do things like:
- have a UML console appear on an unused host console,
- hook two virtual machines together by having one attach to a ptyand having the other attach to the corresponding tty
- make a virtual machine accessible from the net by attaching aconsole to a port on the host.
The general format of the command line option is
device=channel.
5.1. Specifying the device¶
Devices are specified with “con” or “ssl” (console or serial line,respectively), optionally with a device number if you are talkingabout a specific device.
Using just “con” or “ssl” describes all of the consoles or seriallines. If you want to talk about console #3 or serial line #10, theywould be “con3” and “ssl10”, respectively.
A specific device name will override a less general “con=” or “ssl=”.So, for example, you can assign a pty to each of the serial linesexcept for the first two like this:
ssl=pty ssl0=tty:/dev/tty0 ssl1=tty:/dev/tty1The specificity of the device name is all that matters; order on thecommand line is irrelevant.
5.2. Specifying the channel¶
There are a number of different types of channels to attach a UMLdevice to, each with a different way of specifying exactly what toattach to.
pseudo-terminals - device=pty pts terminals - device=pts
This will cause UML to allocate a free host pseudo-terminal for thedevice. The terminal that it got will be announced in the bootlog. You access it by attaching a terminal program to thecorresponding tty:
screen /dev/pts/n
screen /dev/ttyxx
minicom -o -p /dev/ttyxx - minicom seems not able to handle ptsdevices
kermit - start it up, ‘open’ the device, then ‘connect’
terminals - device=tty:tty device file
This will make UML attach the device to the specified tty (i.e:
con1=tty:/dev/tty3will attach UML’s console 1 to the host’s /dev/tty3). If the tty thatyou specify is the slave end of a tty/pty pair, something else musthave already opened the corresponding pty in order for this to work.
xterms - device=xterm
UML will run an xterm and the device will be attached to it.
Port - device=port:port number
This will attach the UML devices to the specified host port.Attaching console 1 to the host’s port 9000 would be done likethis:
con1=port:9000Attaching all the serial lines to that port would be done similarly:
ssl=port:9000You access these devices by telnetting to that port. Each activetelnet session gets a different device. If there are more telnets to aport than UML devices attached to it, then the extra telnet sessionswill block until an existing telnet detaches, or until another devicebecomes active (i.e. by being activated in /etc/inittab).
This channel has the advantage that you can both attach multiple UMLdevices to it and know how to access them without reading the UML bootlog. It is also unique in allowing access to a UML from remotemachines without requiring that the UML be networked. This could beuseful in allowing public access to UMLs because they would beaccessible from the net, but wouldn’t need any kind of networkfiltering or access control because they would have no network access.
If you attach the main console to a portal, then the UML boot willappear to hang. In reality, it’s waiting for a telnet to connect, atwhich point the boot will proceed.
already-existing file descriptors - device=file descriptor
If you set up a file descriptor on the UML command line, you canattach a UML device to it. This is most commonly used to put themain console back on stdin and stdout after assigning all the otherconsoles to something else:
con0=fd:0,fd:1 con=ptsNothing - device=null
This allows the device to be opened, in contrast to ‘none’, butreads will block, and writes will succeed and the data will bethrown out.
None - device=none
This causes the device to disappear.
You can also specify different input and output channels for a deviceby putting a comma between them:
ssl3=tty:/dev/tty2,xtermwill cause serial line 3 to accept input on the host’s /dev/tty2 anddisplay output on an xterm. That’s a silly example - the most commonuse of this syntax is to reattach the main console to stdin and stdoutas shown above.
If you decide to move the main console away from stdin/stdout, theinitial boot output will appear in the terminal that you’re runningUML in. However, once the console driver has been officiallyinitialized, then the boot output will start appearing wherever youspecified that console 0 should be. That device will receive allsubsequent output.
5.3. Examples¶
There are a number of interesting things you can do with thiscapability.
First, this is how you get rid of those bleeding console xterms byattaching them to host ptys:
con=pty con0=fd:0,fd:1This will make a UML console take over an unused host virtual console,so that when you switch to it, you will see the UML login promptrather than the host login prompt:
con1=tty:/dev/tty6You can attach two virtual machines together with what amounts to aserial line as follows:
Run one UML with a serial line attached to a pty:
ssl1=ptyLook at the boot log to see what pty it got (this example will assumethat it got /dev/ptyp1).
Boot the other UML with a serial line attached to the correspondingtty:
ssl1=tty:/dev/ttyp1Log in, make sure that it has no getty on that serial line, attach aterminal program like minicom to it, and you should see the loginprompt of the other virtual machine.
6. Setting up the network¶
This page describes how to set up the various transports and toprovide a UML instance with network access to the host, other machineson the local net, and the rest of the net.
As of 2.4.5, UML networking has been completely redone to make it mucheasier to set up, fix bugs, and add new features.
There is a new helper, uml_net, which does the host setup thatrequires root privileges.
There are currently five transport types available for a UML virtualmachine to exchange packets with other hosts:
ethertap
TUN/TAP
Multicast
a switch daemon
slip
slirp
pcap
The TUN/TAP, ethertap, slip, and slirp transports allow a UMLinstance to exchange packets with the host. They may be directedto the host or the host may just act as a router to provide accessto other physical or virtual machines.
The pcap transport is a synthetic read-only interface, using thelibpcap binary to collect packets from interfaces on the host andfilter them. This is useful for building preconfigured trafficmonitors or sniffers.
The daemon and multicast transports provide a completely virtualnetwork to other virtual machines. This network is completelydisconnected from the physical network unless one of the virtualmachines on it is acting as a gateway.
With so many host transports, which one should you use? Here’s whenyou should use each one:
ethertap - if you want access to the host networking and it isrunning 2.2
TUN/TAP - if you want access to the host networking and it isrunning 2.4. Also, the TUN/TAP transport is able to use apreconfigured device, allowing it to avoid using the setuid uml_nethelper, which is a security advantage.
Multicast - if you want a purely virtual network and you don’t wantto set up anything but the UML
a switch daemon - if you want a purely virtual network and youdon’t mind running the daemon in order to get somewhat betterperformance
slip - there is no particular reason to run the slip backend unlessethertap and TUN/TAP are just not available for some reason
slirp - if you don’t have root access on the host to setupnetworking, or if you don’t want to allocate an IP to your UML
pcap - not much use for actual network connectivity, but great formonitoring traffic on the host
Ethertap is available on 2.4 and works fine. TUN/TAP is preferredto it because it has better performance and ethertap is officiallyconsidered obsolete in 2.4. Also, the root helper only needs torun occasionally for TUN/TAP, rather than handling every packet, asit does with ethertap. This is a slight security advantage sinceit provides fewer opportunities for a nasty UML user to somehowexploit the helper’s root privileges.
6.1. General setup¶
First, you must have the virtual network enabled in your UML. If arerunning a prebuilt kernel from this site, everything is alreadyenabled. If you build the kernel yourself, under the “Network devicesupport” menu, enable “Network device support”, and then the threetransports.
The next step is to provide a network device to the virtual machine.This is done by describing it on the kernel command line.
The general format is:
eth <n> = <transport> , <transport args>For example, a virtual ethernet device may be attached to a hostethertap device as follows:
eth0=ethertap,tap0,fe:fd:0:0:0:1,192.168.0.254This sets up eth0 inside the virtual machine to attach itself to thehost /dev/tap0, assigns it an ethernet address, and assigns the hosttap0 interface an IP address.
Note that the IP address you assign to the host end of the tap devicemust be different than the IP you assign to the eth device inside UML.If you are short on IPs and don’t want to consume two per UML, thenyou can reuse the host’s eth IP address for the host ends of the tapdevices. Internally, the UMLs must still get unique IPs for their ethdevices. You can also give the UMLs non-routable IPs (192.168.x.x or10.x.x.x) and have the host masquerade them. This will let outgoingconnections work, but incoming connections won’t without more work,such as port forwarding from the host.Also note that when you configure the host side of an interface, it isonly acting as a gateway. It will respond to pings sent to itlocally, but is not useful to do that since it’s a host interface.You are not talking to the UML when you ping that interface and get aresponse.
You can also add devices to a UML and remove them at runtime. See the10. The Management Console page for details.
The sections below describe this in more detail.
Once you’ve decided how you’re going to set up the devices, you bootUML, log in, configure the UML side of the devices, and set up routesto the outside world. At that point, you will be able to talk to anyother machines, physical or virtual, on the net.
If ifconfig inside UML fails and the network refuses to come up, runtell you what went wrong.
6.2. Userspace daemons¶
You will likely need the setuid helper, or the switch daemon, or both.They are both installed with the RPM and deb, so if you’ve installedeither, you can skip the rest of this section.
If not, then you need to check them out of CVS, build them, andinstall them. The helper is uml_net, in CVS /tools/uml_net, and thedaemon is uml_switch, in CVS /tools/uml_router. They are both builtwith a plain ‘make’. Both need to be installed in a directory that’sin your path - /usr/bin is recommend. On top of that, uml_net needsto be setuid root.
6.3. Specifying ethernet addresses¶
Below, you will see that the TUN/TAP, ethertap, and daemon interfacesallow you to specify hardware addresses for the virtual ethernetdevices. This is generally not necessary. If you don’t have aspecific reason to do it, you probably shouldn’t. If one is notspecified on the command line, the driver will assign one based on thedevice IP address. It will provide the address fe:fd:nn:nn:nn:nnwhere nn.nn.nn.nn is the device IP address. This is nearly alwayssufficient to guarantee a unique hardware address for the device. Acouple of exceptions are:
Another set of virtual ethernet devices are on the same network andthey are assigned hardware addresses using a different scheme whichmay conflict with the UML IP address-based scheme
You aren’t going to use the device for IP networking, so you don’tassign the device an IP address
If you let the driver provide the hardware address, you should makesure that the device IP address is known before the interface isbrought up. So, inside UML, this will guarantee that:
UML#ifconfig eth0 192.168.0.250 upIf you decide to assign the hardware address yourself, make sure thatthe first byte of the address is even. Addresses with an odd firstbyte are broadcast addresses, which you don’t want assigned to adevice.
6.4. UML interface setup¶
Once the network devices have been described on the command line, youshould boot UML and log in.
The first thing to do is bring the interface up:
UML# ifconfig ethn ip-address upYou should be able to ping the host at this point.
To reach the rest of the world, you should set a default route to thehost:
UML# route add default gw host ipAgain, with host ip of 192.168.0.4:
UML# route add default gw 192.168.0.4This page used to recommend setting a network route to your local net.This is wrong, because it will cause UML to try to figure out hardwareaddresses of the local machines by arping on the interface to thehost. Since that interface is basically a single strand of ethernetwith two nodes on it (UML and the host) and arp requests don’t crossnetworks, they will fail to elicit any responses. So, what you wantis for UML to just blindly throw all packets at the host and let itfigure out what to do with them, which is what leaving out the networkroute and adding the default route does.
Note: If you can’t communicate with other hosts on your physicalethernet, it’s probably because of a network route that’sautomatically set up. If you run ‘route -n’ and see a route thatlooks like this:
Destination Gateway Genmask Flags Metric Ref Use Iface192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0with a mask that’s not 255.255.255.255, then replace it with a routeto your host:
UML#route del -net 192.168.0.0 dev eth0 netmask 255.255.255.0UML#route add -host 192.168.0.4 dev eth0This, plus the default route to the host, will allow UML to exchangepackets with any machine on your ethernet.
6.5. Multicast¶
The simplest way to set up a virtual network between multiple UMLs isto use the mcast transport. This was written by Harald Welte and ispresent in UML version 2.4.5-5um and later. Your system must havemulticast enabled in the kernel and there must be a multicast-capablenetwork device on the host. Normally, this is eth0, but if there isno ethernet card on the host, then you will likely get strange errormessages when you bring the device up inside UML.
To use it, run two UMLs with:
eth0=mcaston their command lines. Log in, configure the ethernet device in eachmachine with different IP addresses:
UML1# ifconfig eth0 192.168.0.254UML2# ifconfig eth0 192.168.0.253and they should be able to talk to each other.
The full set of command line options for this transport are:
ethn=mcast,ethernet address,multicastaddress,multicast port,ttlThere is also a related point-to-point only “ucast” transport.This is useful when your network does not support multicast, andall network connections are simple point to point links.
The full set of command line options for this transport are:
ethn=ucast,ethernet address,remote address,listen port,remote port
6.6. TUN/TAP with the uml_net helper¶
TUN/TAP is the preferred mechanism on 2.4 to exchange packets with thehost. The TUN/TAP backend has been in UML since 2.4.9-3um.
The easiest way to get up and running is to let the setuid uml_nethelper do the host setup for you. This involves insmod-ing the tun.omodule if necessary, configuring the device, and setting up IPforwarding, routing, and proxy arp. If you are new to UML networking,do this first. If you’re concerned about the security implications ofthe setuid helper, use it to get up and running, then read the nextsection to see how to have UML use a preconfigured tap device, whichavoids the use of uml_net.
If you specify an IP address for the host side of the device, theuml_net helper will do all necessary setup on the host - the onlyrequirement is that TUN/TAP be available, either built in to the hostkernel or as the tun.o module.
The format of the command line switch to attach a device to a TUN/TAPdevice is:
eth <n> =tuntap,,, <IP address>For example, this argument will attach the UML’s eth0 to the nextavailable tap device and assign an ethernet address to it based on itsIP address:
eth0=tuntap,,,192.168.0.254Note that the IP address that must be used for the eth device insideUML is fixed by the routing and proxy arp that is set up on theTUN/TAP device on the host. You can use a different one, but it won’twork because reply packets won’t reach the UML. This is a feature.It prevents a nasty UML user from doing things like setting the UML IPto the same as the network’s nameserver or mail server.
There are a couple potential problems with running the TUN/TAPtransport on a 2.4 host kernel
TUN/TAP seems not to work on 2.4.3 and earlier. Upgrade the hostkernel or use the ethertap transport.
With an upgraded kernel, TUN/TAP may fail with:
File descriptor in bad stateThis is due to a header mismatch between the upgraded kernel and thekernel that was originally installed on the machine. The fix is tomake sure that /usr/src/linux points to the headers for the runningkernel.
These were pointed out by Tim Robinson <timro at trkr dot net> in the past.
6.7. TUN/TAP with a preconfigured tap device¶
If you prefer not to have UML use uml_net (which is somewhatinsecure), with UML 2.4.17-11, you can set up a TUN/TAP devicebeforehand. The setup needs to be done as root, but once that’s done,there is no need for root assistance. Setting up the device is doneas follows:
Create the device with tunctl (available from the UML utilitiestarball):
host# tunctl -u uidwhere uid is the user id or username that UML will be run as. Thiswill tell you what device was created.
Configure the device IP (change IP addresses and device name tosuit):
host# ifconfig tap0 192.168.0.254 upSet up routing and arping if desired - this is my recipe, there areother ways of doing the same thing:
host#bash -c 'echo 1 > /proc/sys/net/ipv4/ip_forward'host#route add -host 192.168.0.253 dev tap0host#bash -c 'echo 1 > /proc/sys/net/ipv4/conf/tap0/proxy_arp'host#arp -Ds 192.168.0.253 eth0 pubNote that this must be done every time the host boots - this configu-ration is not stored across host reboots. So, it’s probably a goodidea to stick it in an rc file. An even better idea would be a littleutility which reads the information from a config file and sets updevices at boot time.
Rather than using up two IPs and ARPing for one of them, you canalso provide direct access to your LAN by the UML by using abridge:
host#brctl addbr br0host#ifconfig eth0 0.0.0.0 promisc uphost#ifconfig tap0 0.0.0.0 promisc uphost#ifconfig br0 192.168.0.1 netmask 255.255.255.0 uphost#brctl stp br0 offhost#brctl setfd br0 1host#brctl sethello br0 1host#brctl addif br0 eth0host#brctl addif br0 tap0Note that ‘br0’ should be setup using ifconfig with the existing IPaddress of eth0, as eth0 no longer has its own IP.
Also, the /dev/net/tun device must be writable by the user runningUML in order for the UML to use the device that’s been configuredfor it. The simplest thing to do is:
host# chmod 666 /dev/net/tunMaking it world-writable looks bad, but it seems not to beexploitable as a security hole. However, it does allow anyone to cre-ate useless tap devices (useless because they can’t configure them),which is a DOS attack. A somewhat more secure alternative would to beto create a group containing all the users who have preconfigured tapdevices and chgrp /dev/net/tun to that group with mode 664 or 660.
Once the device is set up, run UML with ‘eth0=tuntap,device name’(i.e. ‘eth0=tuntap,tap0’) on the command line (or do it with themconsole config command).
Bring the eth device up in UML and you’re in business.
If you don’t want that tap device any more, you can make it non-persistent with:
host# tunctl -d tap deviceFinally, tunctl has a -b (for brief mode) switch which causes it tooutput only the name of the tap device it created. This makes itsuitable for capture by a script:
host# TAP=`tunctl -u 1000 -b`
6.8. Ethertap¶
Ethertap is the general mechanism on 2.2 for userspace processes toexchange packets with the kernel.
To use this transport, you need to describe the virtual network deviceon the UML command line. The general format for this is:
eth <n> =ethertap, <device> , <ethernet address> , <tap IP address>So, the previous example:
eth0=ethertap,tap0,fe:fd:0:0:0:1,192.168.0.254attaches the UML eth0 device to the host /dev/tap0, assigns it theethernet address fe:fd:0:0:0:1, and assigns the IP address192.168.0.254 to the tap device.
The tap device is mandatory, but the others are optional. If theethernet address is omitted, one will be assigned to it.
The presence of the tap IP address will cause the helper to run and dowhatever host setup is needed to allow the virtual machine tocommunicate with the outside world. If you’re not sure you know whatyou’re doing, this is the way to go.
If it is absent, then you must configure the tap device and whateverarping and routing you will need on the host. However, even in thiscase, the uml_net helper still needs to be in your path and it must besetuid root if you’re not running UML as root. This is because thetap device doesn’t support SIGIO, which UML needs in order to usesomething as a source of input. So, the helper is used as aconvenient asynchronous IO thread.
If you’re using the uml_net helper, you can ignore the following hostsetup - uml_net will do it for you. You just need to make sure youhave ethertap available, either built in to the host kernel oravailable as a module.
If you want to set things up yourself, you need to make sure that theappropriate /dev entry exists. If it doesn’t, become root and createit as follows:
mknod /dev/tap <minor> c 36 <minor> + 16For example, this is how to create /dev/tap0:
mknod /dev/tap0 c 36 0 + 16You also need to make sure that the host kernel has ethertap support.If ethertap is enabled as a module, you apparently need to insmodethertap once for each ethertap device you want to enable. So,:
host#insmod ethertapwill give you the tap0 interface. To get the tap1 interface, you needto run:
host#insmod ethertap unit=1 -o ethertap1
6.9. The switch daemon¶
Note: This is the daemon formerly known as uml_router, but which wasrenamed so the network weenies of the world would stop growling at me.
The switch daemon, uml_switch, provides a mechanism for creating atotally virtual network. By default, it provides no connection to thehost network (but see -tap, below).
The first thing you need to do is run the daemon. Running it with noarguments will make it listen on a default pair of unix domainsockets.
If you want it to listen on a different pair of sockets, use:
-unix control socket data socketIf you want it to act as a hub rather than a switch, use:
-hubIf you want the switch to be connected to host networking (allowingthe umls to get access to the outside world through the host), use:
-tap tap0Note that the tap device must be preconfigured (see “TUN/TAP with apreconfigured tap device”, above). If you’re using a different tapdevice than tap0, specify that instead of tap0.
uml_switch can be backgrounded as follows:
host%uml_switch [ options ] < /dev/null > /dev/nullThe reason it doesn’t background by default is that it listens tostdin for EOF. When it sees that, it exits.
The general format of the kernel command line switch is:
ethn=daemon,ethernet address,sockettype,control socket,data socketYou can leave off everything except the ‘daemon’. You only need tospecify the ethernet address if the one that will be assigned to itisn’t acceptable for some reason. The rest of the arguments describehow to communicate with the daemon. You should only specify them ifyou told the daemon to use different sockets than the default. So, ifyou ran the daemon with no arguments, running the UML on the samemachine with:
eth0=daemonwill cause the eth0 driver to attach itself to the daemon correctly.
6.10. Slip¶
Slip is another, less general, mechanism for a process to communicatewith the host networking. In contrast to the ethertap interface,which exchanges ethernet frames with the host and can be used totransport any higher-level protocol, it can only be used to transportIP.
The general format of the command line switch is:
ethn=slip,slip IPThe slip IP argument is the IP address that will be assigned to thehost end of the slip device. If it is specified, the helper will runand will set up the host so that the virtual machine can reach it andthe rest of the network.
There are some oddities with this interface that you should be awareof. You should only specify one slip device on a given virtualmachine, and its name inside UML will be ‘umn’, not ‘eth0’ or whateveryou specified on the command line. These problems will be fixed atsome point.
6.11. Slirp¶
slirp uses an external program, usually /usr/bin/slirp, to provide IPonly networking connectivity through the host. This is similar to IPmasquerading with a firewall, although the translation is performed inuser-space, rather than by the kernel. As slirp does not set up anyinterfaces on the host, or changes routing, slirp does not requireroot access or setuid binaries on the host.
The general format of the command line switch for slirp is:
ethn=slirp,ethernet address,slirp pathThe ethernet address is optional, as UML will set up the interfacewith an ethernet address based upon the initial IP address of theinterface. The slirp path is generally /usr/bin/slirp, although itwill depend on distribution.
The slirp program can have a number of options passed to the commandline and we can’t add them to the UML command line, as they will beparsed incorrectly. Instead, a wrapper shell script can be written orthe options inserted into the /.slirprc file. More information onall of the slirp options can be found in its man pages.
The eth0 interface on UML should be set up with the IP 10.2.0.15,although you can use anything as long as it is not used by a networkyou will be connecting to. The default route on UML should be set touse:
UML#route add default dev eth0slirp provides a number of useful IP addresses which can be used byUML, such as 10.0.2.3 which is an alias for the DNS server specifiedin /etc/resolv.conf on the host or the IP given in the ‘dns’ optionfor slirp.
Even with a baudrate setting higher than 115200, the slirp connectionis limited to 115200. If you need it to go faster, the slirp binaryneeds to be compiled with FULL_BOLT defined in config.h.
6.12. pcap¶
The pcap transport is attached to a UML ethernet device on the commandline or with uml_mconsole with the following syntax:
ethn=pcap,host interface,filterexpression,option1,option2The expression and options are optional.
The interface is whatever network device on the host you want tosniff. The expression is a pcap filter expression, which is also whattcpdump uses, so if you know how to specify tcpdump filters, you willuse the same expressions here. The options are up to two of‘promisc’, control whether pcap puts the host interface intopromiscuous mode. ‘optimize’ and ‘nooptimize’ control whether the pcapexpression optimizer is used.
Example:
eth0=pcap,eth0,tcpeth1=pcap,eth0,!tcpwill cause the UML eth0 to emit all tcp packets on the host eth0 andthe UML eth1 to emit all non-tcp packets on the host eth0.
6.13. Setting up the host yourself¶
If you don’t specify an address for the host side of the ethertap orslip device, UML won’t do any setup on the host. So this is what isneeded to get things working (the examples use a host-side IP of192.168.0.251 and a UML-side IP of 192.168.0.250 - adjust to suit yourown network):
The device needs to be configured with its IP address. Tap devicesare also configured with an mtu of 1484. Slip devices areconfigured with a point-to-point address pointing at the UML ipaddress:
host# ifconfig tap0 arp mtu 1484 192.168.0.251 uphost#ifconfig sl0 192.168.0.251 pointopoint 192.168.0.250 upIf a tap device is being set up, a route is set to the UML IP:
UML# route add -host 192.168.0.250 gw 192.168.0.251To allow other hosts on your network to see the virtual machine,proxy arp is set up for it:
host# arp -Ds 192.168.0.250 eth0 pubFinally, the host is set up to route packets:
host# echo 1 > /proc/sys/net/ipv4/ip_forward
7. Sharing Filesystems between Virtual Machines¶
7.1. A warning¶
Don’t attempt to share filesystems simply by booting two UMLs from thesame file. That’s the same thing as booting two physical machinesfrom a shared disk. It will result in filesystem corruption.
7.2. Using layered block devices¶
The way to share a filesystem between two virtual machines is to usethe copy-on-write (COW) layering capability of the ubd block driver.As of 2.4.6-2um, the driver supports layering a read-write privatedevice over a read-only shared device. A machine’s writes are storedin the private device, while reads come from either device - theprivate one if the requested block is valid in it, the shared one ifnot. Using this scheme, the majority of data which is unchanged isshared between an arbitrary number of virtual machines, each of whichhas a much smaller file containing the changes that it has made. Witha large number of UMLs booting from a large root filesystem, thisleads to a huge disk space saving. It will also help performance,since the host will be able to cache the shared data using a muchsmaller amount of memory, so UML disk requests will be served from thehost’s memory rather than its disks.
To add a copy-on-write layer to an existing block device file, simplyadd the name of the COW file to the appropriate ubd switch:
ubd0=root_fs_cow,root_fs_debian_22where ‘root_fs_cow’ is the private COW file and ‘root_fs_debian_22’ isthe existing shared filesystem. The COW file need not exist. If itdoesn’t, the driver will create and initialize it. Once the COW filehas been initialized, it can be used on its own on the command line:
ubd0=root_fs_cowThe name of the backing file is stored in the COW file header, so itwould be redundant to continue specifying it on the command line.
7.3. Note!¶
When checking the size of the COW file in order to see the gobs ofspace that you’re saving, make sure you use ‘ls -ls’ to see the actualdisk consumption rather than the length of the file. The COW file issparse, so the length will be very different from the disk usage.Here is a ‘ls -l’ of a COW file and backing file from one boot andshutdown:
host% ls -l cow.debian debian2.2-rw-r--r-- 1 jdike jdike 492504064 Aug 6 21:16 cow.debian-rwxrw-rw- 1 jdike jdike 537919488 Aug 6 20:42 debian2.2Doesn’t look like much saved space, does it? Well, here’s ‘ls -ls’:
host% ls -ls cow.debian debian2.2 880 -rw-r--r-- 1 jdike jdike 492504064 Aug 6 21:16 cow.debian525832 -rwxrw-rw- 1 jdike jdike 537919488 Aug 6 20:42 debian2.2Now, you can see that the COW file has less than a meg of disk, ratherthan 492 meg.
7.4. Another warning¶
Once a filesystem is being used as a readonly backing file for a COWfile, do not boot directly from it or modify it in any way. Doing sowill invalidate any COW files that are using it. The mtime and sizeof the backing file are stored in the COW file header at its creation,and they must continue to match. If they don’t, the driver willrefuse to use the COW file.
If you attempt to evade this restriction by changing either thebacking file or the COW header by hand, you will get a corruptedfilesystem.
Among other things, this means that upgrading the distribution in abacking file and expecting that all of the COW files using it will seethe upgrade will not work.
7.5. uml_moo : Merging a COW file with its backing file¶
Depending on how you use UML and COW devices, it may be advisable tomerge the changes in the COW file into the backing file every once ina while.
The utility that does this is uml_moo. Its usage is:
host% uml_moo COW file new backing fileThere’s no need to specify the backing file since that information isalready in the COW file header. If you’re paranoid, boot the newmerged file, and if you’re happy with it, move it over the old backingfile.
uml_moo creates a new backing file by default as a safety measure. Italso has a destructive merge option which will merge the COW filedirectly into its current backing file. This is really only usablewhen the backing file only has one COW file associated with it. Ifthere are multiple COWs associated with a backing file, a -d merge ofone of them will invalidate all of the others. However, it isconvenient if you’re short of disk space, and it should also benoticeably faster than a non-destructive merge.
uml_moo is installed with the UML deb and RPM. If you didn’t installUML from one of those packages, you can also get it from the UMLutilitieshttp://user-mode-linux.sourceforge.net/utilities tar filein tools/moo.
8. Creating filesystems¶
You may want to create and mount new UML filesystems, either becauseyour root filesystem isn’t large enough or because you want to use afilesystem other than ext2.
This was written on the occasion of reiserfs being included in the2.4.1 kernel pool, and therefore the 2.4.1 UML, so the examples willtalk about reiserfs. This information is generic, and the examplesshould be easy to translate to the filesystem of your choice.
8.1. Create the filesystem file¶
dd is your friend. All you need to do is tell dd to create an emptyfile of the appropriate size. I usually make it sparse to save timeand to avoid allocating disk space until it’s actually used. Forexample, the following command will create a sparse 100 meg file fullof zeroes:
host%dd if=/dev/zero of=new_filesystem seek=100 count=1 bs=1M8.2. Assign the file to a UML device
Add an argument like the following to the UML command line:
ubd4=new_filesystemmaking sure that you use an unassigned ubd device number.
8.3. Creating and mounting the filesystem
Make sure that the filesystem is available, either by being built intothe kernel, or available as a module, then boot up UML and log in. Ifthe root filesystem doesn’t have the filesystem utilities (mkfs, fsck,etc), then get them into UML by way of the net or hostfs.
Make the new filesystem on the device assigned to the new file:
host# mkreiserfs /dev/ubd/4<----------- MKREISERFSv2 ----------->ReiserFS version 3.6.25Block size 4096 bytesBlock count 25856Used blocks 8212 Journal - 8192 blocks (18-8209), journal header is in block 8210 Bitmaps: 17 Root block 8211Hash function "r5"ATTENTION: ALL DATA WILL BE LOST ON '/dev/ubd/4'! (y/n)yjournal size 8192 (from 18)Initializing journal - 0%....20%....40%....60%....80%....100%Syncing..done.Now, mount it:
UML#mount /dev/ubd/4 /mntand you’re in business.
9. Host file access¶
If you want to access files on the host machine from inside UML, youcan treat it as a separate machine and either nfs mount directoriesfrom the host or copy files into the virtual machine with scp or rcp.However, since UML is running on the host, it can access thosefiles just like any other process and make them available inside thevirtual machine without needing to use the network.
This is now possible with the hostfs virtual filesystem. With it, youcan mount a host directory into the UML filesystem and access thefiles contained in it just as you would on the host.
9.1. Using hostfs¶
To begin with, make sure that hostfs is available inside the virtualmachine with:
UML# cat /proc/filesystems. hostfs should be listed. If it’s not, either rebuild the kernelwith hostfs configured into it or make sure that hostfs is built as amodule and available inside the virtual machine, and insmod it.
Now all you need to do is run mount:
UML# mount none /mnt/host -t hostfswill mount the host’s / on the virtual machine’s /mnt/host.
If you don’t want to mount the host root directory, then you canspecify a subdirectory to mount with the -o switch to mount:
UML# mount none /mnt/home -t hostfs -o /homewill mount the hosts’s /home on the virtual machine’s /mnt/home.
9.2. hostfs as the root filesystem¶
It’s possible to boot from a directory hierarchy on the host usinghostfs rather than using the standard filesystem in a file.
To start, you need that hierarchy. The easiest way is to loop mountan existing root_fs file:
host# mount root_fs uml_root_dir -o loopYou need to change the filesystem type of / in etc/fstab to be‘hostfs’, so that line looks like this:
/dev/ubd/0 / hostfs defaults 1 1Then you need to chown to yourself all the files in that directorythat are owned by root. This worked for me:
host# find . -uid 0 -exec chown jdike {} \;Next, make sure that your UML kernel has hostfs compiled in, not as amodule. Then run UML with the boot device pointing at that directory:
ubd0=/path/to/uml/root/directoryUML should then boot as it does normally.
9.3. Building hostfs¶
If you need to build hostfs because it’s not in your kernel, you havetwo choices:
Compiling hostfs into the kernel:
Reconfigure the kernel and set the ‘Host filesystem’ option under
Compiling hostfs as a module:
Reconfigure the kernel and set the ‘Host filesystem’ option underbe in arch/um/fs/hostfs/hostfs.o. Install that in
/lib/modules/$(uname-r)/fsin the virtual machine, boot it up, and:UML# insmod hostfs
10. The Management Console¶
The UML management console is a low-level interface to the kernel,somewhat like the i386 SysRq interface. Since there is a full-blownoperating system under UML, there is much greater flexibility possiblethan with the SysRq mechanism.
There are a number of things you can do with the mconsole interface:
- get the kernel version
- add and remove devices
- halt or reboot the machine
- Send SysRq commands
- Pause and resume the UML
You need the mconsole client (uml_mconsole) which is present in CVS(/tools/mconsole) in 2.4.5-9um and later, and will be in the RPM in2.4.6.
You also need CONFIG_MCONSOLE (under ‘General Setup’) enabled in UML.When you boot UML, you’ll see a line like:
mconsole initialized on /home/jdike/.uml/umlNJ32yL/mconsoleIf you specify a unique machine id one the UML command line, i.e.:
umid=debianyou’ll see this:
mconsole initialized on /home/jdike/.uml/debian/mconsoleThat file is the socket that uml_mconsole will use to communicate withUML. Run it with either the umid or the full path as its argument:
host% uml_mconsole debianor:
host% uml_mconsole /home/jdike/.uml/debian/mconsoleYou’ll get a prompt, at which you can run one of these commands:
- version
- halt
- reboot
- config
- remove
- sysrq
- help
- cad
- stop
- go
10.1. version¶
This takes no arguments. It prints the UML version:
(mconsole) versionOK Linux usermode 2.4.5-9um #1 Wed Jun 20 22:47:08 EDT 2001 i686There are a couple actual uses for this. It’s a simple no-op whichcan be used to check that a UML is running. It’s also a way ofsending an interrupt to the UML. This is sometimes useful on SMPhosts, where there’s a bug which causes signals to UML to be lost,often causing it to appear to hang. Sending such a UML the mconsoleversion command is a good way to ‘wake it up’ before networking hasbeen enabled, as it does not do anything to the function of the UML.
10.2. halt and reboot¶
These take no arguments. They shut the machine down immediately, withno syncing of disks and no clean shutdown of userspace. So, they arepretty close to crashing the machine:
(mconsole) haltOK
10.3. config¶
“config” adds a new device to the virtual machine. Currently the ubdand network drivers support this. It takes one argument, which is thedevice to add, with the same syntax as the kernel command line:
(mconsole)config ubd3=/home/jdike/incoming/roots/root_fs_debian22OK(mconsole) config eth1=mcastOK
10.4. remove¶
“remove” deletes a device from the system. Its argument is just thename of the device to be removed. The device must be idle in whateversense the driver considers necessary. In the case of the ubd driver,the removed block device must not be mounted, swapped on, or otherwiseopen, and in the case of the network driver, the device must be down:
(mconsole) remove ubd3OK(mconsole) remove eth1OK
10.5. sysrq¶
This takes one argument, which is a single letter. It calls thegeneric kernel’s SysRq driver, which does whatever is called for bythat argument. See the SysRq documentation inDocumentation/admin-guide/sysrq.rst in your favorite kernel tree tosee what letters are valid and what they do.
10.6. help¶
“help” returns a string listing the valid commands and what each onedoes.
10.7. cad¶
This invokes the Ctl-Alt-Del action on init. What exactly this endsup doing is up to /etc/inittab. Normally, it reboots the machine.With UML, this is usually not desired, so if a halt would be better,then find the section of inittab that looks like this:
# What to do when CTRL-ALT-DEL is pressed.ca:12345:ctrlaltdel:/sbin/shutdown -t1 -a -r nowand change the command to halt.
10.8. stop¶
This puts the UML in a loop reading mconsole requests until a ‘go’mconsole command is received. This is very useful for making backupsof UML filesystems, as the UML can be stopped, then synced via ‘sysrqs’, so that everything is written to the filesystem. You can then copythe filesystem and then send the UML ‘go’ via mconsole.
Note that a UML running with more than one CPU will have problemsafter you send the ‘stop’ command, as only one CPU will be held in amconsole loop and all others will continue as normal. This is a bug,and will be fixed.
10.9. go¶
This resumes a UML after being paused by a ‘stop’ command. Note thatwhen the UML has resumed, TCP connections may have timed out and ifthe UML is paused for a long period of time, crond might go a littlecrazy, running all the jobs it didn’t do earlier.
11. Kernel debugging¶
Note: The interface that makes debugging, as described here, possibleis present in 2.4.0-test6 kernels and later.
Since the user-mode kernel runs as a normal Linux process, it ispossible to debug it with gdb almost like any other process. It isslightly different because the kernel’s threads are already beingptraced for system call interception, so gdb can’t ptrace them.However, a mechanism has been added to work around that problem.
In order to debug the kernel, you need build it from source. See2. Compiling the kernel and modules for information on doing that.Make sure that you enable CONFIG_DEBUGSYM and CONFIG_PT_PROXY duringthe config. These will compile the kernel with
-g, and enable theptrace proxy so that gdb works with UML, respectively.
11.1. Starting the kernel under gdb¶
You can have the kernel running under the control of gdb from thebeginning by putting ‘debug’ on the command line. You will get anxterm with gdb running inside it. The kernel will send some commandsto gdb which will leave it stopped at the beginning of start_kernel.At this point, you can get things going with ‘next’, ‘step’, or‘cont’.
There is a transcript of a debugging session here <debug-session.html> , with breakpoints being set in the scheduler and in aninterrupt handler.
11.2. Examining sleeping processes¶
Not every bug is evident in the currently running process. Sometimes,processes hang in the kernel when they shouldn’t because they’vedeadlocked on a semaphore or something similar. In this case, whenyou ^C gdb and get a backtrace, you will see the idle thread, whichisn’t very relevant.
What you want is the stack of whatever process is sleeping when itshouldn’t be. You need to figure out which process that is, which isgenerally fairly easy. Then you need to get its host process id,which you can do either by looking at ps on the host or attask.thread.extern_pid in gdb.
Now what you do is this:
detach from the current thread:
(UML gdb) detattach to the thread you are interested in:
(UML gdb) att <host pid>look at its stack and anything else of interest:
(UML gdb) btNote that you can’t do anything at this point that requires that aprocess execute, e.g. calling a function
when you’re done looking at that process, reattach to the currentthread and continue it:
(UML gdb)att 1(UML gdb)cHere, specifying any pid which is not the process id of a UML threadwill cause gdb to reattach to the current thread. I commonly use 1,but any other invalid pid would work.
11.3. Running ddd on UML¶
ddd works on UML, but requires a special kludge. The process goeslike this:
Start ddd:
host% ddd linuxWith ps, get the pid of the gdb that ddd started. You can ask thegdb to tell you, but for some reason that confuses things andcauses a hang.
run UML with ‘debug=parent gdb-pid=<pid>’ added to the command line- it will just sit there after you hit return
type ‘att 1’ to the ddd gdb and you will see something like:
0xa013dc51 in __kill ()(gdb)At this point, type ‘c’, UML will boot up, and you can use ddd justas you do on any other process.
11.4. Debugging modules¶
gdb has support for debugging code which is dynamically loaded intothe process. This support is what is needed to debug kernel modulesunder UML.
Using that support is somewhat complicated. You have to tell gdb whatobject file you just loaded into UML and where in memory it is. Then,it can read the symbol table, and figure out where all the symbols arefrom the load address that you provided. It gets more interestingwhen you load the module again (i.e. after an rmmod). You have totell gdb to forget about all its symbols, including the main UML onesfor some reason, then load then all back in again.
There’s an easy way and a hard way to do this. The easy way is to usethe umlgdb expect script written by Chandan Kudige. It basicallyautomates the process for you.
First, you must tell it where your modules are. There is a list inthe script that looks like this:
set MODULE_PATHS {"fat" "/usr/src/uml/linux-2.4.18/fs/fat/fat.o""isofs" "/usr/src/uml/linux-2.4.18/fs/isofs/isofs.o""minix" "/usr/src/uml/linux-2.4.18/fs/minix/minix.o"}You change that to list the names and paths of the modules that youare going to debug. Then you run it from the toplevel directory ofyour UML pool and it basically tells you what to do:
******** GDB pid is 21903 ********Start UML as: ./linux <kernel switches> debug gdb-pid=21903GNU gdb 5.0rh-5 Red Hat Linux 7.1Copyright 2001 Free Software Foundation, Inc.GDB is free software, covered by the GNU General Public License, and you arewelcome to change it and/or distribute copies of it under certain conditions.Type "show copying" to see the conditions.There is absolutely no warranty for GDB. Type "show warranty" for details.This GDB was configured as "i386-redhat-linux"...(gdb) b sys_init_moduleBreakpoint 1 at 0xa0011923: file module.c, line 349.(gdb) att 1After you run UML and it sits there doing nothing, you hit return atthe ‘att 1’ and continue it:
Attaching to program: /home/jdike/linux/2.4/um/./linux, process 10xa00f4221 in __kill ()(UML gdb) cContinuing.At this point, you debug normally. When you insmod something, theexpect magic will kick in and you’ll see something like:
*** Module hostfs loaded ***Breakpoint 1, sys_init_module (name_user=0x805abb0 "hostfs", mod_user=0x8070e00) at module.c:349349 char *name, *n_name, *name_tmp = NULL;(UML gdb) finishRun till exit from #0 sys_init_module (name_user=0x805abb0 "hostfs", mod_user=0x8070e00) at module.c:3490xa00e2e23 in execute_syscall (r=0xa8140284) at syscall_kern.c:411411 else res = EXECUTE_SYSCALL(syscall, regs);Value returned is $1 = 0(UML gdb)p/x (int)module_list + module_list->size_of_struct$2 = 0xa9021054(UML gdb) symbol-file ./linuxLoad new symbol table from "./linux"? (y or n) yReading symbols from ./linux...done.(UML gdb)add-symbol-file /home/jdike/linux/2.4/um/arch/um/fs/hostfs/hostfs.o 0xa9021054add symbol table from file "/home/jdike/linux/2.4/um/arch/um/fs/hostfs/hostfs.o" at .text_addr = 0xa9021054 (y or n) yReading symbols from /home/jdike/linux/2.4/um/arch/um/fs/hostfs/hostfs.o...done.(UML gdb) p *module_list$1 = {size_of_struct = 84, next = 0xa0178720, name = 0xa9022de0 "hostfs", size = 9016, uc = {usecount = {counter = 0}, pad = 0}, flags = 1, nsyms = 57, ndeps = 0, syms = 0xa9023170, deps = 0x0, refs = 0x0, init = 0xa90221f0 <init_hostfs>, cleanup = 0xa902222c <exit_hostfs>, ex_table_start = 0x0, ex_table_end = 0x0, persist_start = 0x0, persist_end = 0x0, can_unload = 0, runsize = 0, kallsyms_start = 0x0, kallsyms_end = 0x0, archdata_start = 0x1b855 <Address 0x1b855 out of bounds>, archdata_end = 0xe5890000 <Address 0xe5890000 out of bounds>, kernel_data = 0xf689c35d <Address 0xf689c35d out of bounds>}>> Finished loading symbols for hostfs ...That’s the easy way. It’s highly recommended. The hard way isdescribed below in case you’re interested in what’s going on.
Boot the kernel under the debugger and load the module with insmod ormodprobe. With gdb, do:
(UML gdb) p module_listThis is a list of modules that have been loaded into the kernel, withthe most recently loaded module first. Normally, the module you wantis at module_list. If it’s not, walk down the next links, looking atthe name fields until find the module you want to debug. Take theaddress of that structure, and add module.size_of_struct (which in2.4.10 kernels is 96 (0x60)) to it. Gdb can make this hard additionfor you :-):
(UML gdb)printf "%#x\n", (int)module_list module_list->size_of_structThe offset from the module start occasionally changes (before 2.4.0,it was module.size_of_struct + 4), so it’s a good idea to check theinit and cleanup addresses once in a while, as describe below. Nowdo:
(UML gdb)add-symbol-file /path/to/module/on/host that_addressTell gdb you really want to do it, and you’re in business.
If there’s any doubt that you got the offset right, like breakpointsappear not to work, or they’re appearing in the wrong place, you cancheck it by looking at the module structure. The init and cleanupfields should look like:
init = 0x588066b0 <init_hostfs>, cleanup = 0x588066c0 <exit_hostfs>with no offsets on the symbol names. If the names are right, but theyare offset, then the offset tells you how much you need to add to theaddress you gave to add-symbol-file.
When you want to load in a new version of the module, you need to getgdb to forget about the old one. The only way I’ve found to do thatis to tell gdb to forget about all symbols that it knows about:
(UML gdb) symbol-fileThen reload the symbols from the kernel binary:
(UML gdb) symbol-file /path/to/kerneland repeat the process above. You’ll also need to re-enable break-points. They were disabled when you dumped all the symbols becausegdb couldn’t figure out where they should go.
11.5. Attaching gdb to the kernel¶
If you don’t have the kernel running under gdb, you can attach gdb toit later by sending the tracing thread a SIGUSR1. The first line ofthe console output identifies its pid:
tracing thread pid = 20093When you send it the signal:
host% kill -USR1 20093you will get an xterm with gdb running in it.
If you have the mconsole compiled into UML, then the mconsole clientcan be used to start gdb:
(mconsole) (mconsole) config gdb=xtermwill fire up an xterm with gdb running in it.
11.6. Using alternate debuggers¶
UML has support for attaching to an already running debugger ratherthan starting gdb itself. This is present in CVS as of 17 Apr 2001.I sent it to Alan for inclusion in the ac tree, and it will be in my2.4.4 release.
This is useful when gdb is a subprocess of some UI, such as emacs orddd. It can also be used to run debuggers other than gdb on UML.Below is an example of using strace as an alternate debugger.
To do this, you need to get the pid of the debugger and pass it inwith the
If you are using gdb under some UI, then tell it to ‘att 1’, andyou’ll find yourself attached to UML.
If you are using something other than gdb as your debugger, thenyou’ll need to get it to do the equivalent of ‘att 1’ if it doesn’t doit automatically.
An example of an alternate debugger is strace. You can strace theactual kernel as follows:
Run the following in a shell:
host%sh -c 'echo pid=$$; echo -n hit return; read x; exec strace -p 1 -o strace.out'Run UML with ‘debug’ and ‘gdb-pid=<pid>’ with the pid printed outby the previous command
Hit return in the shell, and UML will start running, and straceoutput will start accumulating in the output file.
Note that this is different from running:
host% strace ./linuxThat will strace only the main UML thread, the tracing thread, whichdoesn’t do any of the actual kernel work. It just oversees the vir-tual machine. In contrast, using strace as described above will showyou the low-level activity of the virtual machine.
12. Kernel debugging examples¶
12.1. The case of the hung fsck¶
When booting up the kernel, fsck failed, and dropped me into a shellto fix things up. I ran fsck -y, which hung:
Setting hostname uml [ OK ]Checking root filesystem/dev/fhd0 was not cleanly unmounted, check forced.Error reading block 86894 (Attempt to read block from filesystem resulted in short read) while reading indirect blocks of inode 19780./dev/fhd0: UNEXPECTED INCONSISTENCY; RUN fsck MANUALLY. (i.e., without -a or -p options)[ FAILED ]*** An error occurred during the file system check.*** Dropping you to a shell; the system will reboot*** when you leave the shell.Give root password for maintenance(or type Control-D for normal startup):[root@uml /root]# fsck -y /dev/fhd0fsck -y /dev/fhd0Parallelizing fsck version 1.14 (9-Jan-1999)e2fsck 1.14, 9-Jan-1999 for EXT2 FS 0.5b, 95/08/09/dev/fhd0 contains a file system with errors, check forced.Pass 1: Checking inodes, blocks, and sizesError reading block 86894 (Attempt to read block from filesystem resulted in short read) while reading indirect blocks of inode 19780. Ignore error? yesInode 19780, i_blocks is 1548, should be 540. Fix? yesPass 2: Checking directory structureError reading block 49405 (Attempt to read block from filesystem resulted in short read). Ignore error? yesDirectory inode 11858, block 0, offset 0: directory corruptedSalvage? yesMissing '.' in directory inode 11858.Fix? yesMissing '..' in directory inode 11858.Fix? yesThe standard drill in this sort of situation is to fire up gdb on thesignal thread, which, in this case, was pid 1935. In another window,I run gdb and attach pid 1935:
~/linux/2.3.26/um 1016: gdb linuxGNU gdb 4.17.0.11 with Linux supportCopyright 1998 Free Software Foundation, Inc.GDB is free software, covered by the GNU General Public License, and you arewelcome to change it and/or distribute copies of it under certain conditions.Type "show copying" to see the conditions.There is absolutely no warranty for GDB. Type "show warranty" for details.This GDB was configured as "i386-redhat-linux"...(gdb) att 1935Attaching to program `/home/dike/linux/2.3.26/um/linux', Pid 19350x100756d9 in __wait4 ()Let’s see what’s currently running:
(gdb) p current_task.pid$1 = 0It’s the idle thread, which means that fsck went to sleep for somereason and never woke up.
Let’s guess that the last process in the process list is fsck:
(gdb) p current_task.prev_task.comm$13 = "fsck.ext2\000\000\000\000\000\000"It is, so let’s see what it thinks it’s up to:
(gdb) p current_task.prev_task.thread$14 = {extern_pid = 1980, tracing = 0, want_tracing = 0, forking = 0, kernel_stack_page = 0, signal_stack = 1342627840, syscall = {id = 4, args = { 3, 134973440, 1024, 0, 1024}, have_result = 0, result = 50590720}, request = {op = 2, u = {exec = {ip = 1350467584, sp = 2952789424}, fork = { regs = {1350467584, 2952789424, 0 <repeats 15 times>}, sigstack = 0, pid = 0}, switch_to = 0x507e8000, thread = {proc = 0x507e8000, arg = 0xaffffdb0, flags = 0, new_pid = 0}, input_request = { op = 1350467584, fd = -1342177872, proc = 0, pid = 0}}}}The interesting things here are the fact that its .thread.syscall.idis __NR_write (see the big switch in arch/um/kernel/syscall_kern.c orthe defines in include/asm-um/arch/unistd.h), and that it neverreturned. Also, its .request.op is OP_SWITCH (seearch/um/include/user_util.h). These mean that it went into a write,and, for some reason, called schedule().
The fact that it never returned from write means that its stack shouldbe fairly interesting. Its pid is 1980 (.thread.extern_pid). Thatprocess is being ptraced by the signal thread, so it must be detachedbefore gdb can attach it:
(gdb) call detach(1980)Program received signal SIGSEGV, Segmentation fault.<function called from gdb>The program being debugged stopped while in a function called from GDB.When the function (detach) is done executing, GDB will silentlystop (instead of continuing to evaluate the expression containingthe function call).(gdb) call detach(1980)$15 = 0The first detach segfaults for some reason, and the second onesucceeds.
Now I detach from the signal thread, attach to the fsck thread, andlook at its stack:
(gdb) detDetaching from program: /home/dike/linux/2.3.26/um/linux Pid 1935(gdb) att 1980Attaching to program `/home/dike/linux/2.3.26/um/linux', Pid 19800x10070451 in __kill ()(gdb) bt#0 0x10070451 in __kill ()#1 0x10068ccd in usr1_pid (pid=1980) at process.c:30#2 0x1006a03f in _switch_to (prev=0x50072000, next=0x507e8000) at process_kern.c:156#3 0x1006a052 in switch_to (prev=0x50072000, next=0x507e8000, last=0x50072000) at process_kern.c:161#4 0x10001d12 in schedule () at core.c:777#5 0x1006a744 in __down (sem=0x507d241c) at semaphore.c:71#6 0x1006aa10 in __down_failed () at semaphore.c:157#7 0x1006c5d8 in segv_handler (sc=0x5006e940) at trap_user.c:174#8 0x1006c5ec in kern_segv_handler (sig=11) at trap_user.c:182#9 <signal handler called>#10 0x10155404 in errno ()#11 0x1006c0aa in segv (address=1342179328, is_write=2) at trap_kern.c:50#12 0x1006c5d8 in segv_handler (sc=0x5006eaf8) at trap_user.c:174#13 0x1006c5ec in kern_segv_handler (sig=11) at trap_user.c:182#14 <signal handler called>#15 0xc0fd in ?? ()#16 0x10016647 in sys_write (fd=3, buf=0x80b8800 <Address 0x80b8800 out of bounds>, count=1024) at read_write.c:159#17 0x1006d5b3 in execute_syscall (syscall=4, args=0x5006ef08) at syscall_kern.c:254#18 0x1006af87 in really_do_syscall (sig=12) at syscall_user.c:35#19 <signal handler called>#20 0x400dc8b0 in ?? ()The interesting things here are:
There are two segfaults on this stack (frames 9 and 14)
The first faulting address (frame 11) is 0x50000800:
(gdb) p (void *)1342179328$16 = (void *) 0x50000800The initial faulting address is interesting because it is on the idlethread’s stack. I had been seeing the idle thread segfault for noapparent reason, and the cause looked like stack corruption. In hopesof catching the culprit in the act, I had turned off all protectionsto that stack while the idle thread wasn’t running. This apparentlytripped that trap.
However, the more immediate problem is that second segfault and I’mgoing to concentrate on that. First, I want to see where the faulthappened, so I have to go look at the sigcontent struct in frame 8:
(gdb) up#1 0x10068ccd in usr1_pid (pid=1980) at process.c:3030 kill(pid, SIGUSR1);(gdb)#2 0x1006a03f in _switch_to (prev=0x50072000, next=0x507e8000) at process_kern.c:156156 usr1_pid(getpid());(gdb)#3 0x1006a052 in switch_to (prev=0x50072000, next=0x507e8000, last=0x50072000) at process_kern.c:161161 _switch_to(prev, next);(gdb)#4 0x10001d12 in schedule () at core.c:777777 switch_to(prev, next, prev);(gdb)#5 0x1006a744 in __down (sem=0x507d241c) at semaphore.c:7171 schedule();(gdb)#6 0x1006aa10 in __down_failed () at semaphore.c:157157 }(gdb)#7 0x1006c5d8 in segv_handler (sc=0x5006e940) at trap_user.c:174174 segv(sc->cr2, sc->err & 2);(gdb)#8 0x1006c5ec in kern_segv_handler (sig=11) at trap_user.c:182182 segv_handler(sc);(gdb) p *scCannot access memory at address 0x0.That’s not very useful, so I’ll try a more manual method:
(gdb) p *((struct sigcontext *) (&sig + 1))$19 = {gs = 0, __gsh = 0, fs = 0, __fsh = 0, es = 43, __esh = 0, ds = 43, __dsh = 0, edi = 1342179328, esi = 1350378548, ebp = 1342630440, esp = 1342630420, ebx = 1348150624, edx = 1280, ecx = 0, eax = 0, trapno = 14, err = 4, eip = 268480945, cs = 35, __csh = 0, eflags = 66118, esp_at_signal = 1342630420, ss = 43, __ssh = 0, fpstate = 0x0, oldmask = 0, cr2 = 1280}The ip is in handle_mm_fault:
(gdb) p (void *)268480945$20 = (void *) 0x1000b1b1(gdb) i sym $20handle_mm_fault + 57 in section .textSpecifically, it’s in pte_alloc:
(gdb) i line *$20Line 124 of "/home/dike/linux/2.3.26/um/include/asm/pgalloc.h" starts at address 0x1000b1b1 <handle_mm_fault+57> and ends at 0x1000b1b7 <handle_mm_fault+63>.To find where in handle_mm_fault this is, I’ll jump forward in thecode until I see an address in that procedure:
(gdb) i line *0x1000b1c0Line 126 of "/home/dike/linux/2.3.26/um/include/asm/pgalloc.h" starts at address 0x1000b1b7 <handle_mm_fault+63> and ends at 0x1000b1c3 <handle_mm_fault+75>.(gdb) i line *0x1000b1d0Line 131 of "/home/dike/linux/2.3.26/um/include/asm/pgalloc.h" starts at address 0x1000b1d0 <handle_mm_fault+88> and ends at 0x1000b1da <handle_mm_fault+98>.(gdb) i line *0x1000b1e0Line 61 of "/home/dike/linux/2.3.26/um/include/asm/pgalloc.h" starts at address 0x1000b1da <handle_mm_fault+98> and ends at 0x1000b1e1 <handle_mm_fault+105>.(gdb) i line *0x1000b1f0Line 134 of "/home/dike/linux/2.3.26/um/include/asm/pgalloc.h" starts at address 0x1000b1f0 <handle_mm_fault+120> and ends at 0x1000b200 <handle_mm_fault+136>.(gdb) i line *0x1000b200Line 135 of "/home/dike/linux/2.3.26/um/include/asm/pgalloc.h" starts at address 0x1000b200 <handle_mm_fault+136> and ends at 0x1000b208 <handle_mm_fault+144>.(gdb) i line *0x1000b210Line 139 of "/home/dike/linux/2.3.26/um/include/asm/pgalloc.h" starts at address 0x1000b210 <handle_mm_fault+152> and ends at 0x1000b219 <handle_mm_fault+161>.(gdb) i line *0x1000b220Line 1168 of "memory.c" starts at address 0x1000b21e <handle_mm_fault+166> and ends at 0x1000b222 <handle_mm_fault+170>.Something is apparently wrong with the page tables or vma_structs, solets go back to frame 11 and have a look at them:
#11 0x1006c0aa in segv (address=1342179328, is_write=2) at trap_kern.c:5050 handle_mm_fault(current, vma, address, is_write);(gdb) call pgd_offset_proc(vma->vm_mm, address)$22 = (pgd_t *) 0x80a548cThat’s pretty bogus. Page tables aren’t supposed to be in processtext or data areas. Let’s see what’s in the vma:
(gdb) p *vma$23 = {vm_mm = 0x507d2434, vm_start = 0, vm_end = 134512640, vm_next = 0x80a4f8c, vm_page_prot = {pgprot = 0}, vm_flags = 31200, vm_avl_height = 2058, vm_avl_left = 0x80a8c94, vm_avl_right = 0x80d1000, vm_next_share = 0xaffffdb0, vm_pprev_share = 0xaffffe63, vm_ops = 0xaffffe7a, vm_pgoff = 2952789626, vm_file = 0xafffffec, vm_private_data = 0x62}(gdb) p *vma.vm_mm$24 = {mmap = 0x507d2434, mmap_avl = 0x0, mmap_cache = 0x8048000, pgd = 0x80a4f8c, mm_users = {counter = 0}, mm_count = {counter = 134904288}, map_count = 134909076, mmap_sem = {count = {counter = 135073792}, sleepers = -1342177872, wait = {lock = <optimized out or zero length>, task_list = {next = 0xaffffe63, prev = 0xaffffe7a}, __magic = -1342177670, __creator = -1342177300}, __magic = 98}, page_table_lock = {}, context = 138, start_code = 0, end_code = 0, start_data = 0, end_data = 0, start_brk = 0, brk = 0, start_stack = 0, arg_start = 0, arg_end = 0, env_start = 0, env_end = 0, rss = 1350381536, total_vm = 0, locked_vm = 0, def_flags = 0, cpu_vm_mask = 0, swap_cnt = 0, swap_address = 0, segments = 0x0}This also pretty bogus. With all of the 0x80xxxxx and 0xaffffxxxaddresses, this is looking like a stack was plonked down on top ofthese structures. Maybe it’s a stack overflow from the next page:
(gdb) p vma$25 = (struct vm_area_struct *) 0x507d2434That’s towards the lower quarter of the page, so that would have tohave been pretty heavy stack overflow:
(gdb) x/100x $250x507d2434: 0x507d2434 0x00000000 0x08048000 0x080a4f8c0x507d2444: 0x00000000 0x080a79e0 0x080a8c94 0x080d10000x507d2454: 0xaffffdb0 0xaffffe63 0xaffffe7a 0xaffffe7a0x507d2464: 0xafffffec 0x00000062 0x0000008a 0x000000000x507d2474: 0x00000000 0x00000000 0x00000000 0x000000000x507d2484: 0x00000000 0x00000000 0x00000000 0x000000000x507d2494: 0x00000000 0x00000000 0x507d2fe0 0x000000000x507d24a4: 0x00000000 0x00000000 0x00000000 0x000000000x507d24b4: 0x00000000 0x00000000 0x00000000 0x000000000x507d24c4: 0x00000000 0x00000000 0x00000000 0x000000000x507d24d4: 0x00000000 0x00000000 0x00000000 0x000000000x507d24e4: 0x00000000 0x00000000 0x00000000 0x000000000x507d24f4: 0x00000000 0x00000000 0x00000000 0x000000000x507d2504: 0x00000000 0x00000000 0x00000000 0x000000000x507d2514: 0x00000000 0x00000000 0x00000000 0x000000000x507d2524: 0x00000000 0x00000000 0x00000000 0x000000000x507d2534: 0x00000000 0x00000000 0x507d25dc 0x000000000x507d2544: 0x00000000 0x00000000 0x00000000 0x000000000x507d2554: 0x00000000 0x00000000 0x00000000 0x000000000x507d2564: 0x00000000 0x00000000 0x00000000 0x000000000x507d2574: 0x00000000 0x00000000 0x00000000 0x000000000x507d2584: 0x00000000 0x00000000 0x00000000 0x000000000x507d2594: 0x00000000 0x00000000 0x00000000 0x000000000x507d25a4: 0x00000000 0x00000000 0x00000000 0x000000000x507d25b4: 0x00000000 0x00000000 0x00000000 0x00000000It’s not stack overflow. The only “stack-like” piece of this data isthe vma_struct itself.
At this point, I don’t see any avenues to pursue, so I just have toadmit that I have no idea what’s going on. What I will do, though, isstick a trap on the segfault handler which will stop if it sees anywrites to the idle thread’s stack. That was the thing that happenedfirst, and it may be that if I can catch it immediately, what’s goingon will be somewhat clearer.
12.2. Episode 2: The case of the hung fsck¶
After setting a trap in the SEGV handler for accesses to the signalthread’s stack, I reran the kernel.
fsck hung again, this time by hitting the trap:
Setting hostname uml [ OK ]Checking root filesystem/dev/fhd0 contains a file system with errors, check forced.Error reading block 86894 (Attempt to read block from filesystem resulted in short read) while reading indirect blocks of inode 19780./dev/fhd0: UNEXPECTED INCONSISTENCY; RUN fsck MANUALLY. (i.e., without -a or -p options)[ FAILED ]*** An error occurred during the file system check.*** Dropping you to a shell; the system will reboot*** when you leave the shell.Give root password for maintenance(or type Control-D for normal startup):[root@uml /root]# fsck -y /dev/fhd0fsck -y /dev/fhd0Parallelizing fsck version 1.14 (9-Jan-1999)e2fsck 1.14, 9-Jan-1999 for EXT2 FS 0.5b, 95/08/09/dev/fhd0 contains a file system with errors, check forced.Pass 1: Checking inodes, blocks, and sizesError reading block 86894 (Attempt to read block from filesystem resulted in short read) while reading indirect blocks of inode 19780. Ignore error? yesPass 2: Checking directory structureError reading block 49405 (Attempt to read block from filesystem resulted in short read). Ignore error? yesDirectory inode 11858, block 0, offset 0: directory corruptedSalvage? yesMissing '.' in directory inode 11858.Fix? yesMissing '..' in directory inode 11858.Fix? yesUntested (4127) [100fe44c]: trap_kern.c line 31I need to get the signal thread to detach from pid 4127 so that I canattach to it with gdb. This is done by sending it a SIGUSR1, which iscaught by the signal thread, which detaches the process:
kill -USR1 4127Now I can run gdb on it:
~/linux/2.3.26/um 1034: gdb linuxGNU gdb 4.17.0.11 with Linux supportCopyright 1998 Free Software Foundation, Inc.GDB is free software, covered by the GNU General Public License, and you arewelcome to change it and/or distribute copies of it under certain conditions.Type "show copying" to see the conditions.There is absolutely no warranty for GDB. Type "show warranty" for details.This GDB was configured as "i386-redhat-linux"...(gdb) att 4127Attaching to program `/home/dike/linux/2.3.26/um/linux', Pid 41270x10075891 in __libc_nanosleep ()The backtrace shows that it was in a write and that the fault address(address in frame 3) is 0x50000800, which is right in the middle ofthe signal thread’s stack page:
(gdb) bt#0 0x10075891 in __libc_nanosleep ()#1 0x1007584d in __sleep (seconds=1000000) at ../sysdeps/unix/sysv/linux/sleep.c:78#2 0x1006ce9a in stop () at user_util.c:191#3 0x1006bf88 in segv (address=1342179328, is_write=2) at trap_kern.c:31#4 0x1006c628 in segv_handler (sc=0x5006eaf8) at trap_user.c:174#5 0x1006c63c in kern_segv_handler (sig=11) at trap_user.c:182#6 <signal handler called>#7 0xc0fd in ?? ()#8 0x10016647 in sys_write (fd=3, buf=0x80b8800 "R.", count=1024) at read_write.c:159#9 0x1006d603 in execute_syscall (syscall=4, args=0x5006ef08) at syscall_kern.c:254#10 0x1006af87 in really_do_syscall (sig=12) at syscall_user.c:35#11 <signal handler called>#12 0x400dc8b0 in ?? ()#13 <signal handler called>#14 0x400dc8b0 in ?? ()#15 0x80545fd in ?? ()#16 0x804daae in ?? ()#17 0x8054334 in ?? ()#18 0x804d23e in ?? ()#19 0x8049632 in ?? ()#20 0x80491d2 in ?? ()#21 0x80596b5 in ?? ()(gdb) p (void *)1342179328$3 = (void *) 0x50000800Going up the stack to the segv_handler frame and looking at where inthe code the access happened shows that it happened near line 110 ofblock_dev.c:
(gdb) up#1 0x1007584d in __sleep (seconds=1000000) at ../sysdeps/unix/sysv/linux/sleep.c:78../sysdeps/unix/sysv/linux/sleep.c:78: No such file or directory.(gdb)#2 0x1006ce9a in stop () at user_util.c:191191 while(1) sleep(1000000);(gdb)#3 0x1006bf88 in segv (address=1342179328, is_write=2) at trap_kern.c:3131 KERN_UNTESTED();(gdb)#4 0x1006c628 in segv_handler (sc=0x5006eaf8) at trap_user.c:174174 segv(sc->cr2, sc->err & 2);(gdb) p *sc$1 = {gs = 0, __gsh = 0, fs = 0, __fsh = 0, es = 43, __esh = 0, ds = 43, __dsh = 0, edi = 1342179328, esi = 134973440, ebp = 1342631484, esp = 1342630864, ebx = 256, edx = 0, ecx = 256, eax = 1024, trapno = 14, err = 6, eip = 268550834, cs = 35, __csh = 0, eflags = 66070, esp_at_signal = 1342630864, ss = 43, __ssh = 0, fpstate = 0x0, oldmask = 0, cr2 = 1342179328}(gdb) p (void *)268550834$2 = (void *) 0x1001c2b2(gdb) i sym $2block_write + 1090 in section .text(gdb) i line *$2Line 209 of "/home/dike/linux/2.3.26/um/include/asm/arch/string.h" starts at address 0x1001c2a1 <block_write+1073> and ends at 0x1001c2bf <block_write+1103>.(gdb) i line *0x1001c2c0Line 110 of "block_dev.c" starts at address 0x1001c2bf <block_write+1103> and ends at 0x1001c2e3 <block_write+1139>.Looking at the source shows that the fault happened during a call tocopy_from_user to copy the data into the kernel:
107 count -= chars;108 copy_from_user(p,buf,chars);109 p += chars;110 buf += chars;p is the pointer which must contain 0x50000800, since buf contains0x80b8800 (frame 8 above). It is defined as:
p = offset + bh->b_data;I need to figure out what bh is, and it just so happens that bh ispassed as an argument to mark_buffer_uptodate and mark_buffer_dirty afew lines later, so I do a little disassembly:
(gdb) disas 0x1001c2bf 0x1001c2e0Dump of assembler code from 0x1001c2bf to 0x1001c2d0:0x1001c2bf <block_write+1103>: addl %eax,0xc(%ebp)0x1001c2c2 <block_write+1106>: movl 0xfffffdd4(%ebp),%edx0x1001c2c8 <block_write+1112>: btsl $0x0,0x18(%edx)0x1001c2cd <block_write+1117>: btsl $0x1,0x18(%edx)0x1001c2d2 <block_write+1122>: sbbl %ecx,%ecx0x1001c2d4 <block_write+1124>: testl %ecx,%ecx0x1001c2d6 <block_write+1126>: jne 0x1001c2e3 <block_write+1139>0x1001c2d8 <block_write+1128>: pushl $0x00x1001c2da <block_write+1130>: pushl %edx0x1001c2db <block_write+1131>: call 0x1001819c <__mark_buffer_dirty>End of assembler dump.At that point, bh is in %edx (address 0x1001c2da), which is calculatedat 0x1001c2c2 as %ebp + 0xfffffdd4, so I figure exactly what that is,taking %ebp from the sigcontext_struct above:
(gdb) p (void *)1342631484$5 = (void *) 0x5006ee3c(gdb) p 0x5006ee3c+0xfffffdd4$6 = 1342630928(gdb) p (void *)$6$7 = (void *) 0x5006ec10(gdb) p *((void **)$7)$8 = (void *) 0x50100200Now, I look at the structure to see what’s in it, and particularly,what its b_data field contains:
(gdb) p *((struct buffer_head *)0x50100200)$13 = {b_next = 0x50289380, b_blocknr = 49405, b_size = 1024, b_list = 0, b_dev = 15872, b_count = {counter = 1}, b_rdev = 15872, b_state = 24, b_flushtime = 0, b_next_free = 0x501001a0, b_prev_free = 0x50100260, b_this_page = 0x501001a0, b_reqnext = 0x0, b_pprev = 0x507fcf58, b_data = 0x50000800 "", b_page = 0x50004000, b_end_io = 0x10017f60 <end_buffer_io_sync>, b_dev_id = 0x0, b_rsector = 98810, b_wait = {lock = <optimized out or zero length>, task_list = {next = 0x50100248, prev = 0x50100248}, __magic = 1343226448, __creator = 0}, b_kiobuf = 0x0}The b_data field is indeed 0x50000800, so the question becomes howthat happened. The rest of the structure looks fine, so this probablyis not a case of data corruption. It happened on purpose somehow.
The b_page field is a pointer to the page_struct representing the0x50000000 page. Looking at it shows the kernel’s idea of the stateof that page:
(gdb) p *$13.b_page$17 = {list = {next = 0x50004a5c, prev = 0x100c5174}, mapping = 0x0, index = 0, next_hash = 0x0, count = {counter = 1}, flags = 132, lru = { next = 0x50008460, prev = 0x50019350}, wait = { lock = <optimized out or zero length>, task_list = {next = 0x50004024, prev = 0x50004024}, __magic = 1342193708, __creator = 0}, pprev_hash = 0x0, buffers = 0x501002c0, virtual = 1342177280, zone = 0x100c5160}Some sanity-checking: the virtual field shows the “virtual” address ofthis page, which in this kernel is the same as its “physical” address,and the page_struct itself should be mem_map[0], since it representsthe first page of memory:
(gdb) p (void *)1342177280$18 = (void *) 0x50000000(gdb) p mem_map$19 = (mem_map_t *) 0x50004000These check out fine.
Now to check out the page_struct itself. In particular, the flagsfield shows whether the page is considered free or not:
(gdb) p (void *)132$21 = (void *) 0x84The “reserved” bit is the high bit, which is definitely not set, sothe kernel considers the signal stack page to be free and available tobe used.
At this point, I jump to conclusions and start looking at my earlyboot code, because that’s where that page is supposed to be reserved.
In my setup_arch procedure, I have the following code which looks justfine:
bootmap_size = init_bootmem(start_pfn, end_pfn - start_pfn);free_bootmem(__pa(low_physmem) + bootmap_size, high_physmem - low_physmem);Two stack pages have already been allocated, and low_physmem points tothe third page, which is the beginning of free memory.The init_bootmem call declares the entire memory to the boot memorymanager, which marks it all reserved. The free_bootmem call frees upall of it, except for the first two pages. This looks correct to me.
So, I decide to see init_bootmem run and make sure that it is markingthose first two pages as reserved. I never get that far.
Stepping into init_bootmem, and looking at bootmem_map before lookingat what it contains shows the following:
(gdb) p bootmem_map$3 = (void *) 0x50000000Aha! The light dawns. That first page is doing double duty as astack and as the boot memory map. The last thing that the boot memorymanager does is to free the pages used by its memory map, so this pageis getting freed even its marked as reserved.
The fix was to initialize the boot memory manager before allocatingthose two stack pages, and then allocate them through the boot memorymanager. After doing this, and fixing a couple of subsequent buglets,the stack corruption problem disappeared.
13. What to do when UML doesn’t work¶
13.1. Strange compilation errors when you build from source¶
As of test11, it is necessary to have “ARCH=um” in the environment oron the make command line for all steps in building UML, includingclean, distclean, or mrproper, config, menuconfig, or xconfig, dep,and linux. If you forget for any of them, the i386 build seems tocontaminate the UML build. If this happens, start from scratch with:
host%make mrproper ARCH=umand repeat the build process with ARCH=um on all the steps.
See2. Compiling the kernel and modules for more details.
Another cause of strange compilation errors is building UML in/usr/src/linux. If you do this, the first thing you need to do isclean up the mess you made. The /usr/src/linux/asm link will nowpoint to /usr/src/linux/asm-um. Make it point back to/usr/src/linux/asm-i386. Then, move your UML pool someplace else andbuild it there. Also see below, where a more specific set of symptomsis described.
13.3. A variety of panics and hangs with /tmp on a reiserfs filesystem¶
I saw this on reiserfs 3.5.21 and it seems to be fixed in 3.5.27.Panics preceded by:
Detaching pid nnnnare diagnostic of this problem. This is a reiserfs bug which causes athread to occasionally read stale data from a mmapped page shared withanother thread. The fix is to upgrade the filesystem or to have /tmpbe an ext2 filesystem.
13.4. The compile fails with errors about conflicting types for‘open’, ‘dup’, and ‘waitpid’
This happens when you build in /usr/src/linux. The UML build makesthe include/asm link point to include/asm-um. /usr/include/asm pointsto /usr/src/linux/include/asm, so when that link gets moved, fileswhich need to include the asm-i386 versions of headers get theincompatible asm-um versions. The fix is to move the include/asm linkback to include/asm-i386 and to do UML builds someplace else.
13.5. UML doesn’t work when /tmp is an NFS filesystem¶
This seems to be a similar situation with the ReiserFS problem above.Some versions of NFS seems not to handle mmap correctly, which UMLdepends on. The workaround is have /tmp be a non-NFS directory.
13.6. UML hangs on boot when compiled with gprof support¶
If you build UML with gprof support and, early in the boot, it doesthis:
kernel BUG at page_alloc.c:100!you have a buggy gcc. You can work around the problem by removingUM_FASTCALL from CFLAGS in arch/um/Makefile-i386. This will open upanother bug, but that one is fairly hard to reproduce.
13.7. syslogd dies with a SIGTERM on startup¶
The exact boot error depends on the distribution that you’re booting,but Debian produces this:
/etc/rc2.d/S10sysklogd: line 49: 93 Terminatedstart-stop-daemon --start --quiet --exec /sbin/syslogd -- $SYSLOGDThis is a syslogd bug. There’s a race between a parent processinstalling a signal handler and its child sending the signal.
13.8. TUN/TAP networking doesn’t work on a 2.4 host¶
There are a couple of problems which were reported byTim Robinson <timro at trkr dot net>
It doesn’t work on hosts running 2.4.7 (or thereabouts) or earlier.The fix is to upgrade to something more recent and then read thenext item.
If you see:
File descriptor in bad statewhen you bring up the device inside UML, you have a header mismatchbetween the original kernel and the upgraded one. Make /usr/src/linuxpoint at the new headers. This will only be a problem if you builduml_net yourself.
13.9. You can network to the host but not to other machines on the net¶
If you can connect to the host, and the host can connect to UML, butyou cannot connect to any other machines, then you may need to enableIP Masquerading on the host. Usually this is only experienced whenusing private IP addresses (192.168.x.x or 10.x.x.x) for host/UMLnetworking, rather than the public address space that your host isconnected to. UML does not enable IP Masquerading, so you will needto create a static rule to enable it:
host%iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADEReplace eth0 with the interface that you use to talk to the rest ofthe world.
Documentation on IP Masquerading, and SNAT, can be found athttp://www.netfilter.org.
If you can reach the local net, but not the outside Internet, thenthat is usually a routing problem. The UML needs a default route:
UML#route add default gw gateway IPThe gateway IP can be any machine on the local net that knows how toreach the outside world. Usually, this is the host or the local net-work’s gateway.
Occasionally, we hear from someone who can reach some machines, butnot others on the same net, or who can reach some ports on othermachines, but not others. These are usually caused by strangefirewalling somewhere between the UML and the other box. You trackthis down by running tcpdump on every interface the packets travelover and see where they disappear. When you find a machine that takesthe packets in, but does not send them onward, that’s the culprit.
13.10. I have no root and I want to scream¶
Thanks to Birgit Wahlich for telling me about this strange one. Itturns out that there’s a limit of six environment variables on thekernel command line. When that limit is reached or exceeded, argumentprocessing stops, which means that the ‘root=’ argument that UMLusually adds is not seen. So, the filesystem has no idea what theroot device is, so it panics.
The fix is to put less stuff on the command line. Glomming all yoursetup variables into one is probably the best way to go.
13.11. UML build conflict between ptrace.h and ucontext.h¶
On some older systems, /usr/include/asm/ptrace.h and/usr/include/sys/ucontext.h define the same names. So, when they’reincluded together, the defines from one completely mess up the parsingof the other, producing errors like:
/usr/include/sys/ucontext.h:47: parse error before`10`plus a pile of warnings.
This is a libc botch, which has since been fixed, and I don’t see anyway around it besides upgrading.
13.12. The UML BogoMips is exactly half the host’s BogoMips¶
On i386 kernels, there are two ways of running the loop that is usedto calculate the BogoMips rating, using the TSC if it’s there or usinga one-instruction loop. The TSC produces twice the BogoMips as theloop. UML uses the loop, since it has nothing resembling a TSC, andwill get almost exactly the same BogoMips as a host using the loop.However, on a host with a TSC, its BogoMips will be double the loopBogoMips, and therefore double the UML BogoMips.
13.13. When you run UML, it immediately segfaults¶
If the host is configured with the 2G/2G address space split, that’swhy. See ref:UML_on_2G/2G_hosts for the details on getting UML torun on your host.
13.14. xterms appear, then immediately disappear¶
If you’re running an up to date kernel with an old release ofuml_utilities, the port-helper program will not work properly, soxterms will exit straight after they appear. The solution is toupgrade to the latest release of uml_utilities. Usually this problemoccurs when you have installed a packaged release of UML then compiledyour own development kernel without upgrading the uml_utilities fromthe source distribution.
13.15. Any other panic, hang, or strange behavior¶
If you’re seeing truly strange behavior, such as hangs or panics thathappen in random places, or you try running the debugger to see what’shappening and it acts strangely, then it could be a problem in thehost kernel. If you’re not running a stock Linus or -ac kernel, thentry that. An early version of the preemption patch and a 2.4.10 SuSEkernel have caused very strange problems in UML.
Otherwise, let me know about it. Send a message to one of the UMLmailing lists - either the developer list - user-mode-linux-devel atlists dot sourceforge dot net (subscription info) or the user list -user-mode-linux-user at lists dot sourceforge do net (subscriptioninfo), whichever you prefer. Don’t assume that everyone knows aboutit and that a fix is imminent.
If you want to be super-helpful, read14. Diagnosing Problems andfollow the instructions contained therein.
14. Diagnosing Problems¶
If you get UML to crash, hang, or otherwise misbehave, you shouldreport this on one of the project mailing lists, either the developerlist - user-mode-linux-devel at lists dot sourceforge dot net(subscription info) or the user list - user-mode-linux-user at listsdot sourceforge dot net (subscription info). When you do, it islikely that I will want more information. So, it would be helpful toread the stuff below, do whatever is applicable in your case, andreport the results to the list.
For any diagnosis, you’re going to need to build a debugging kernel.The binaries from this site aren’t debuggable. If you haven’t donethis before, read about2. Compiling the kernel and modules and11. Kernel debugging UML first.
14.1. Case 1 : Normal kernel panics¶
The most common case is for a normal thread to panic. To debug this,you will need to run it under the debugger (add ‘debug’ to the commandline). An xterm will start up with gdb running inside it. Continueit when it stops in start_kernel and make it crash. Now
^CgdbandIf the panic was a “Kernel mode fault”, then there will be a segvframe on the stack and I’m going to want some more information. Thestack might look something like this:
(UML gdb) backtrace#0 0x1009bf76 in __sigprocmask (how=1, set=0x5f347940, oset=0x0) at ../sysdeps/unix/sysv/linux/sigprocmask.c:49#1 0x10091411 in change_sig (signal=10, on=1) at process.c:218#2 0x10094785 in timer_handler (sig=26) at time_kern.c:32#3 0x1009bf38 in __restore () at ../sysdeps/unix/sysv/linux/i386/sigaction.c:125#4 0x1009534c in segv (address=8, ip=268849158, is_write=2, is_user=0) at trap_kern.c:66#5 0x10095c04 in segv_handler (sig=11) at trap_user.c:285#6 0x1009bf38 in __restore ()I’m going to want to see the symbol and line information for the valueof ip in the segv frame. In this case, you would do the following:
(UML gdb) i sym 268849158and:
(UML gdb) i line *268849158The reason for this is the __restore frame right above the segv_han-dler frame is hiding the frame that actually segfaulted. So, I haveto get that information from the faulting ip.
14.2. Case 2 : Tracing thread panics¶
The less common and more painful case is when the tracing threadpanics. In this case, the kernel debugger will be useless because itneeds a healthy tracing thread in order to work. The first thing todo is get a backtrace from the tracing thread. This is done byfiguring out what its pid is, firing up gdb, and attaching it to thatpid. You can figure out the tracing thread pid by looking at thefirst line of the console output, which will look like this:
tracing thread pid = 15851or by running ps on the host and finding the line that looks likethis:
jdike 15851 4.5 0.4 132568 1104 pts/0 S 21:34 0:05 ./linux [(tracing thread)]If the panic was ‘segfault in signals’, then follow the instructionsabove for collecting information about the location of the seg fault.
If the tracing thread flaked out all by itself, then send thatbacktrace in and wait for our crack debugging team to fix the problem.
14.3. Case 3 : Tracing thread panics caused by other threads
However, there are cases where the misbehavior of another threadcaused the problem. The most common panic of this type is:
wait_for_stop failed to wait for <pid> to stop with <signal number>In this case, you’ll need to get a backtrace from the process men-tioned in the panic, which is complicated by the fact that the kerneldebugger is defunct and without some fancy footwork, another gdb can’tattach to it. So, this is how the fancy footwork goes:
In a shell:
host% kill -STOP pidRun gdb on the tracing thread as described in case 2 and do:
(host gdb) call detach(pid)If you get a segfault, do it again. It always works the second time.
Detach from the tracing thread and attach to that other thread:
(host gdb) detach(host gdb) attach pidIf gdb hangs when attaching to that process, go back to a shell anddo:
host%kill -CONT pidAnd then get the backtrace:
(host gdb) backtrace
14.4. Case 4 : Hangs¶
Hangs seem to be fairly rare, but they sometimes happen. When a hanghappens, we need a backtrace from the offending process. Run thekernel debugger as described in case 1 and get a backtrace. If thecurrent process is not the idle thread, then send in the backtrace.You can tell that it’s the idle thread if the stack looks like this:
#0 0x100b1401 in __libc_nanosleep ()#1 0x100a2885 in idle_sleep (secs=10) at time.c:122#2 0x100a546f in do_idle () at process_kern.c:445#3 0x100a5508 in cpu_idle () at process_kern.c:471#4 0x100ec18f in start_kernel () at init/main.c:592#5 0x100a3e10 in start_kernel_proc (unused=0x0) at um_arch.c:71#6 0x100a383f in signal_tramp (arg=0x100a3dd8) at trap_user.c:50If this is the case, then some other process is at fault, and went tosleep when it shouldn’t have. Run ps on the host and figure out whichprocess should not have gone to sleep and stayed asleep. Then attachto it with gdb and get a backtrace as described in case 3.
15. Thanks¶
A number of people have helped this project in various ways, and thispage gives recognition where recognition is due.
If you’re listed here and you would prefer a real link on your name,or no link at all, instead of the despammed email address pseudo-link,let me know.
If you’re not listed here and you think maybe you should be, pleaselet me know that as well. I try to get everyone, but sometimes mybookkeeping lapses and I forget about contributions.
15.1. Code and Documentation¶
Rusty Russell <rusty at linuxcare.com.au> -
- wrote the HOWTOhttp://user-mode-linux.sourceforge.net/old/UserModeLinux-HOWTO.html
- prodded me into making this project official and putting it onSourceForge
- came up with the way cool UML logohttp://user-mode-linux.sourceforge.net/uml-small.png
- redid the config process
Peter Moulder <reiter at netspace.net.au> - Fixed my config and buildprocesses, and added some useful code to the block driver
Bill Stearns <wstearns at pobox.com> -
- HOWTO updates
- lots of bug reports
- lots of testing
- dedicated a box (uml.ists.dartmouth.edu) to support UML development
- wrote the mkrootfs script, which allows bootable filesystems ofRPM-based distributions to be cranked out
- cranked out a large number of filesystems with said script
Jim Leu <jleu at mindspring.com> - Wrote the virtual ethernet driverand associated usermode tools
Lars Brinkhoffhttp://lars.nocrew.org/ - Contributed the ptraceproxy from his own project to allow easier kernel debugging
Andrea Arcangeli <andrea at suse.de> - Redid some of the early bootcode so that it would work on machines with Large File Support
Chris Emerson - Did the first UML port to Linux/ppc
Harald Welte <laforge at gnumonks.org> - Wrote the multicasttransport for the network driver
Jorgen Cederlof - Added special file support to hostfs
Greg Lonnon <glonnon at ridgerun dot com> - Changed the ubd driverto allow it to layer a COW file on a shared read-only filesystem andwrote the iomem emulation support
Henrik Nordstromhttp://hem.passagen.se/hno/ - Provided a varietyof patches, fixes, and clues
Lennert Buytenhek - Contributed various patches, a rewrite of thenetwork driver, the first implementation of the mconsole driver, anddid the bulk of the work needed to get SMP working again.
Yon Uriarte - Fixed the TUN/TAP network backend while I slept.
Adam Heath - Made a bunch of nice cleanups to the initialization code,plus various other small patches.
Matt Zimmerman - Matt volunteered to be the UML Debian maintainer andis doing a real nice job of it. He also noticed and fixed a number ofactually and potentially exploitable security holes in uml_net. Plusthe occasional patch. I like patches.
James McMechan - James seems to have taken over maintenance of the ubddriver and is doing a nice job of it.
Chandan Kudige - wrote the umlgdb script which automates the reloadingof module symbols.
Steve Schmidtke - wrote the UML slirp transport and hostaudio drivers,enabling UML processes to access audio devices on the host. He alsosubmitted patches for the slip transport and lots of other things.
David Coulsonhttp://davidcoulson.net -
- Set up thehttp://usermodelinux.org site,which is a great way of keeping the UML user community on top ofUML goings-on.
- Site documentation and updates
- Nifty little UML management daemon UMLd
- Lots of testing and bug reports
15.2. Flushing out bugs¶
- Yuri Pudgorodsky
- Gerald Britton
- Ian Wehrman
- Gord Lamb
- Eugene Koontz
- John H. Hartman
- Anders Karlsson
- Daniel Phillips
- John Fremlin
- Rainer Burgstaller
- James Stevenson
- Matt Clay
- Cliff Jefferies
- Geoff Hoff
- Lennert Buytenhek
- Al Viro
- Frank Klingenhoefer
- Livio Baldini Soares
- Jon Burgess
- Petru Paler
- Paul
- Chris Reahard
- Sverker Nilsson
- Gong Su
- johan verrept
- Bjorn Eriksson
- Lorenzo Allegrucci
- Muli Ben-Yehuda
- David Mansfield
- Howard Goff
- Mike Anderson
- John Byrne
- Sapan J. Batia
- Iris Huang
- Jan Hudec
- Voluspa
15.3. Buglets and clean-ups¶
- Dave Zarzycki
- Adam Lazur
- Boria Feigin
- Brian J. Murrell
- JS
- Roman Zippel
- Wil Cooley
- Ayelet Shemesh
- Will Dyson
- Sverker Nilsson
- dvorak
- v.naga srinivas
- Shlomi Fish
- Roger Binns
- johan verrept
- MrChuoi
- Peter Cleve
- Vincent Guffens
- Nathan Scott
- Patrick Caulfield
- jbearce
- Catalin Marinas
- Shane Spencer
- Zou Min
- Ryan Boder
- Lorenzo Colitti
- Gwendal Grignou
- Andre’ Breiler
- Tsutomu Yasuda
15.4. Case Studies¶
- Jon Wright
- William McEwan
- Michael Richardson
15.5. Other contributions¶
Bill Carr <Bill.Carr at compaq.com> made the Red Hat mkrootfs scriptwork with RH 6.2.
Michael Jennings <mikejen at hevanet.com> sent in some material whichis now gracing the top of the index pagehttp://user-mode-linux.sourceforge.net/ of this site.
SGI (and more specifically Ralf Baechle <ralf atuni-koblenz.de> ) gave me an account on oss.sgi.com.The bandwidth there made it possible toproduce most of the filesystems available on the project downloadpage.
Laurent Bonnaud <Laurent.Bonnaud at inpg.fr> took the old grottyDebian filesystem that I’ve been distributing and updated it to 2.2.It is now available by itself here.
Rik van Riel gave me some ftp space on ftp.nl.linux.org so I can makereleases even when Sourceforge is broken.
Rodrigo de Castro looked at my broken pte code and told me what waswrong with it, letting me fix a long-standing (several weeks) andserious set of bugs.
Chris Reahard built a specialized root filesystem for running a DNSserver jailed inside UML. It’s available from the downloadhttp://user-mode-linux.sourceforge.net/old/dl-sf.html page in the JailFilesystems section.