<<

LINUXCOVER USERSTORY SchlagwortUser-Mode sollte hier stehen

Getting started with User-Mode Linux LINUX IN LINUX User-Mode Linux feels like Linux because it is Linux. You’ll find a hundred uses for this fast and sensible virtual Linux system. BY FABRIZIO CIACCHI www.sxc.hu

he popular Tand versatile User-Mode Linux (UML) [1] creates a fully operatational virtual Linux system about a wants to com- on a Linux host. UML has many uses in new or municate with a the Linux world. Developers rely on untested patch. device (for instance, to UML to test their applications without System administrators display something on a monitor, putting the host system at risk. Linux use UML to test system con- print a document, or copy a file to a users run UML to experiment with ker- figurations. You can even run multiple floppy), the process asks the Linux nel versions without having to worry versions of UML on the same host to kernel to manage the communication simulate a network. with the hardware (Figure 1). SELinux into UML User-Mode Linux is a There is a very interesting document What is User-Mode Linux? that runs in Linux as a process. The dif- that explain how to set up an SELinux- User-Mode Linux is not really an emula- ference between a UML kernel and an enabled UML system at [15]. An tor, nor is it an API. The best way to ordinary kernel is that the UML kernel SELinux-enabled UML can be very use- explain User-Mode Linux is to start with does not communicate directly with the ful for creating more secure servers and a look at the role of the Linux kernel. hardware. Commands pass instead to testing SELinux policy without putting The kernel runs processes and talks the “real” Linux host kernel, which the system at risk. with the hardware. When a process manages the hardware communication.

32 ISSUE 54 MAY 2005 WWW.LINUX- MAGAZINE.COM User-Mode Linux COVER STORY

Figure 1: Normal Linux process structure.

Figure 2: UML runs as a process. In this fig- ure, Proc1 is running on the host Linux sys- tem. Proc2 is running on the User-Mode Linux virtual system. Figure 3: Booting the UML virtual system.

Because the virtual system and the $ bunzip2 linux-2.4.27.tar.bz2 After you sucessfully complete these host system are both Linux systems with $ tar -xvf linux-2.4.27.tar commands, you will have a file called nearly identical structures, the commu- $ bunzip2 uml-patch-2.4.27-1.bz2 “linux” in your original directory. This nication passes very efficiently from the $ patch -p1 -d linux-2.4.27 U file is the User-Mode Linux kernel that virtual system to the host, requiring min- < uml-patch-2.4.27-1 will be used to boot the virtual linux imal overhead for abstraction or transla- $cdlinux-2.4.27 system. tion. $ make menuconfig ARCH=um To make UML work properly, you $ make linux ARCH=um need to include two other pieces of the Setting Up UML $ strip linux puzzle: a root filesystem (a compressed You can install User-Mode Linux with image of a linux partition that contains your package manager. For example, with , you need to give this com- Emulation Choice mand as root: Perhaps the best way of understanding Hardware emulation consists of the the advantages of UML is to consider code built on the native hardware archi- # apt-get install U that software of this type comes in three tecture. A hardware emulator is more user-mode-linux U forms: efficient than the software emulator but U uml-utilities • Software emulation it needs to intercept all the calls to the kernel-patch-uml hardware. This solution has the big dis- •Hardware emulation advantage that the code must be spe- •No emulation This command installs the UML kernel cialized for a particular hardware archi- tecture that is the same for host and and also other utilities. Other package Bochs [4] is one of the most famous soft- ware system emulators. The principal guest environment. An example of this managers are equally simple, but if you activity of Bochs is to supply an emula- type of emulator is VMware [5], a com- have a problem installing from a pack- tion of a particular hardware architecture mercial system emulator. age system, or if you have memory prob- (IA-32, called also x86) on top of a partic- User-Mode Linux fits in the last cate- lems during boot [2], you may wish to ular operating systems, like Windows, gory; it doesn’t need to emulate any spe- download a normal linux kernel (we rec- MacOS and, obviously, Linux. Once the cific hardware, but it instead talks nearly ommend the 2.4.27 version [3]) and the hardware is emulated, it is possible to directly with the real hardware. Instruc- UML kernel patch [4]. You can find other install any x86 operating system on it tions pass efficiently from the UML ker- UML patches at [5]. When you have (Linux, Windows, Dos and so on), but nel through the host kernel. UML can downloaded the patch and kernel files the execution is very slow, because execute native code and can run with, at every computer instruction needs to be (in the same directory, of course), open worst, a 20% slowdown compared to translated from the guest operating sys- running the same code on the host. a terminal window and execute the fol- tem to the host operating system. lowing commands:

WWW.LINUX- MAGAZINE.COM ISSUE 54 MAY 2005 33 COVER STORY User-Mode Linux

start two virtual machines (VM1 and VM2) with the same filesystem, you need to open two terminal sessions and write the following commands:

[xterm 1]$ linux U ubd0=uml_vm1.cow, U root_fs_toms1.7.205 [xterm 2]$ linux U ubd0=uml_vm2.cow, U root_fs_toms1.7.205

All the modifications to the two virtual hosts will be written on the respective COW files. In truth, the filesystem is not shared, but the two executions are inde- pendent of each other. The most impor- tant thing to avoid when the two COW files are created is booting the filesystem directly (with ubd0=root_fs_XXX), because every cow file has registered the size and the timestamp of the root filesystem, and every modification will Figure 4: Booting process of the second UML virtual system. make the COW files unusable. The cor- rect syntax to use for the next reboot, all the programs) and the UML utilities. $ bunzip2 U when we have a COW file, is as follows: For the root filesystem, you can find all root_fs_toms1.7.205.bz2 the available images at [6]. You’ll need $ linux U [xterm 1]$ linux U to download the UML utilities from [7] ubd0=root_fs_toms1.7.205 ubd0=uml_vm1.cow and type the following commands: [xterm 2]$ linux U The ubd0= parameter tells the virtual ubd0=uml_vm2.cow $bunzip2 uml_utilitiesU system to use the file specified as the _XXXXXXXX.tar.gz root filesystem. Virtual and Real $ tar -xvf uml_utilitiesU If all goes well, you’ll see the virtual Networking _XXXXXXXX.tar system booting (Figure 3), and you can UML provides several interesting options $cdtools log in to the virtual system with the for networking virtual Linux systems. $ make all username root and the password root. Once you get your UML virtual system $ make install DESTDIR=/ up and running, you may wish to experi- Sharing the Root File ment with networking the virtual system You now have a directory that contains System with its host or networking it with other the root filesystem. Remember to put It is possible to launch two or more vir- virtual systems. You’ll find a thorough the linux program in a location that tual machines using the same root description of UML networking at [8]. permits you to use it. (If you haven’t filesystem. The udb0 driver uses a mech- The basic idea behind UML network- moved the linux program, it is still in the anism called Copy-On-Write (COW), ing is that several optional transports are linux-2.4.27 directory.) Then enter the which reads the root filesystem as a provided for managing the exchange of following commands to read the root read-only shared device and stores the packets between the virtual system and filesystem: changes in a read/write private file (the the host. Table 1 shows some of the COW file). For example, if you want to transport types available for UML. QEMU: A Good Alternative If your goal is to use UML for testing Table 1: UML Transport Types new Linux distributions, you can opt for Etherap, TUN/TAP Transports used for exchanging packets between the virtual system and QEMU [12] system emulator application. the real host. QEMU (based on Bochs [13]) is very Switch daemon A transport designed for purely virtual networking with other UML sys- simple to install, set up, and use. For tems. more on QEMU, see “Virtual Benefits: Multicast Another transport designed for virtual networking. System Emulation with QEMU” in Linux Slip, slirp Transports used primarily when Ethertap and TUN/TAP are not available or Magazine Issue #52, March 2005; you if you don’t have root access to the networking configuration on the host. can download the article in Pdf format Pcap A transport that provides a read-only network interface and is, therefore, a from our archive [14]. good option for network monitoring.

34 ISSUE 54 MAY 2005 WWW.LINUX- MAGAZINE.COM COVER STORY User-Mode Linux

INFO

[1] User-Mode Linux Homepage: http://user-mode-linux.sourceforge. net [2] UML on 2G/2G hosts: http://user-mode-linux.sourceforge. net/UserModeLinux-HOWTO-4. html#2G-2G [3] Official Linux kernel 2.4.27: http://ftp.ca.kernel.org/linux/kernel/v2. 4/linux-2.4.27.tar.bz2 Figure 5: The COW files of two UML Virtual Machines. [4] UML patch for kernel 2.4.27: To enable a network device in the The IP address of real tap0 and virtual http://prdownloads.sourceforge.net/ virtual machine, pass a string like the eth0 can be the same for simpler config- user-mode-linux/uml-patch-2.4.27-1. bz2 following to the kernel command line: urations. (See [8] for more complex net- work configurations.) [5] UML Downloads: eth=U You then need to set up the interface http://user-mode-linux.sourceforge. net/dl-sf.html , in the virtual machine (/etc/hosts, /etc/resolv.conf, /etc/network, etc.) to [6] Root filesystem list: where represent the real host have fully operative Internet access in http://user-mode-linux.sourceforge. net/dl-jails-sf.html interface (i.e., eth0) to which the virtual the UML environment. machine will attach. The theoretical [7] UML Utilities: http://prdownloads. sourceforge.net/user-mode-linux/ explanation is that, in the UML virtual Conclusion uml_utilities_20040406.tar.bz2 machine, there is an eth0 device that User-Mode Linux provides a quick and corresponds to a tap0 device on the real convenient means for creating virtual [8] UML Network configuration: http://user-mode-linux.sourceforge. host; this tap0 interface is directly con- Linux systems in Linux. You can use net/networking.html nected to the eth0 interface of the real can UML as a tool for planning, model- host. ing, testing, and troubleshooting Linux [9] Compiling the kernel: http://user-mode-linux.sourceforge. So, we can use the command: systems. UML is also the basis for many net/compile.html other open source projects and expe- linux ubd0=root_fs_slack8.1 U riments, as well as for business appli- [10] Kernel debugging: http://user-mode-linux.sourceforge. eth0=ethertap,tap0,U cations and personalized hosting ser- net/debugging.html fe:fd:0:0:0:1,192.168.0.254 vices. Perhaps User-Mode Linux is not so easy to install and configure, but if [11] UML debugging session: http://user-mode-linux.sourceforge. to permit UML to set up eth0 in the vir- you can get it working, you’ll find many net/debug-session.html tual machine with its own IP address. uses for it. ■ [12] QEMU Homepage: http://fabrice.bellard.free.fr/qemu/ [13] Bochs homepage: http://bochs.sourceforge.net [14] QEMU Article “Virtual Benefits”: http://www.linux-magazine.com/ issue/52/QEMU_System_Emulation. pdf [15] SELinux and UML: http://www.golden-gryphon.com/ software/security/selinux-uml.xhtml

Fabrizio Ciacchi (http://fabrizio. ciacchi.it – [email protected]) is an italian student of Computer Sci- ence at the University of Pisa. His main activities are studying Linux, developing Web Sites in PHP, and programming in Java. He also works as a consultant for several

THE AUTHOR companies and writes articles on Linux. Figure 6: A UML Virtual Machine with network services available.

36 ISSUE 54 MAY 2005 WWW.LINUX- MAGAZINE.COM