Howto: Virtualisierung mit Xen unter Debian Lenny (Linux Kernel 2.6.26)

In diesem Howto geht es darum, die Linux Distribution Debian Lenny um die Xen Virtualisierung zu erweitern. Es werden auch einfache Schritte zum erstellen und verwalten von Images gezeigt.
Xen gehört zur Gruppe der Paravirtualisierung, hierbei wird einem sogennaten Hypervisor (der auf dem Hostsystem läuft) die Aufgabe zugeteilt Resourcen virtuell an Gastsysteme zu verteilen. Das Gastsystem wird dabei direkt auf der CPU ausgeführt, mit Ausnahme von Systemcalls, die an den Hypervisor weitergeleitet werden. Damit kann ohne spezielle Hardwareerweiterung eine erstaunliche Performance erzielt werden, die der nativen sehr Nahe kommt. Der einzige Nachteil ist, dass das Gastsystem angepasst werden muss, um nicht echte Syscalls zu benutzten, sonden die des Hypervisors. Anpassungen für Linux, Freebsd, Solaris und andere freie Betriebsysteme stehen bereits, für Windows gibt es sowas leider nicht.

Aber nun zur Installation, die ich Beispielsweise an einem 64-bit Debian Lenny vormache:

Nachdem man Lenny installiert hat, installiert man den angepassten Xen-Kernel und die Xen-Tools:
apt-get install xen-linux-system-2.6.26-1-xen-amd64 xen-utils-3.2-1 lvm2 python-xml xen-tools

Um die Netzwerk-devices unserer Gastsysteme im folgenden zu bridgen, editieren wir die Datei /etc/xen/xend-config.sxp

Folgender Abschnitt ist relevant:


##
# To bridge network traffic, like this:
#
# dom0: ----------------- bridge -> real eth0 -> the network
# |
# domU: fake eth0 -> vifN.0 -+
#
# use
#
(network-script network-bridge)
#
# Your default ethernet device is used as the outgoing interface, by default.
# To use a different one (e.g. eth1) use
#
# (network-script 'network-bridge netdev=eth1')
#
# The bridge is named xenbr0, by default. To rename the bridge, use
#
# (network-script 'network-bridge bridge=‘)
#
# It is possible to use the network-bridge script in more complicated
# scenarios, such as having two outgoing interfaces, with two bridges, and
# two fake interfaces per guest domain. To do things like this, write
# yourself a wrapper script, and call network-bridge from it, as appropriate.
#
#(network-script network-dummy)

Dabei muss (network-script network-dummy) wie ihm Codelisting auskommentiert werden und für das Bridging (network-script network-bridge) einkommentiert werden.

Nun muss man das System mit dem neuen Kernel booten:
shutdown -r now

Um zu checken, ob alles geklappt hat, führen wir:

xm list aus. Xm steht da bei für Xen Management, womit man seine virtuellen Systeme verwalten kann.

Es sollte dabei etwas ähnliches wie das hier:


olymp:~# xm list
Name ID Mem VCPUs State Time(s)
Domain-0 0 3242 4 r----- 266.8

erscheinen. Domain-0 ist unser Hostsystem, wir haben noch keine Gäste erstellt.

Das geht sehr bequem mit den Xen-Tools, die wir bereits installiert haben. Mit der Datei /etc/xen-tools/xen-tools.conf können die Vorgaben bequem editiert werden. Um standartmäßig Debian Lenny 64bit als Gastsystem zu installieren, kann man folgende Anpassungen verwenden:

  1. ##
  2. #  /etc/xen-tools/xen-tools.conf
  3. ##
  4. #
  5. #  This is the global configuration file for the scripts included
  6. # within the xen-tools package.
  7. #
  8. #  For more details please see:
  9. #
  10. #        http://xen-tools.org/
  11. #
  12. ##
  13.  
  14.  
  15. ##
  16. #
  17. # File Format
  18. # ———–
  19. #
  20. #  Anything following a ‘#’ character is ignored as a comment.
  21. #
  22. #  Otherwise the format of this file "key = value".  The value of
  23. # any keys in this file may be constructed via the output of a command.
  24. #
  25. #  For example:
  26. #
  27. #         kernel = /boot/vmlinuz-`uname -r`
  28. #
  29. ##
  30.  
  31.  
  32.  
  33. #
  34. ##
  35. #  Output directory for storing loopback images.
  36. #
  37. #  If you choose to use loopback images, which are simple to manage but
  38. # slower than LVM partitions, then specify a directory here and uncomment
  39. # the line.
  40. #
  41. #  New instances will be stored in subdirectories named after their
  42. # hostnames.
  43. #
  44. ##
  45. dir = /home/xen
  46. #
  47.  
  48. #
  49. ##
  50. #
  51. # If you don’t wish to use loopback images then you may specify an
  52. # LVM volume group here instead
  53. #
  54. ##
  55. # lvm = skx-vg
  56.  
  57.  
  58. #
  59. ##
  60. #
  61. #  Installation method.
  62. #
  63. #  There are four distinct methods which you may to install a new copy
  64. # of Linux to use in your Xen guest domain:
  65. #
  66. #   - Installation via the debootstrap command.
  67. #   - Installation via the rpmstrap command.
  68. #   - Installation via the rinse command.
  69. #   - Installation by copying a directory containing a previous installation.
  70. #   - Installation by untarring a previously archived image.
  71. #
  72. #  NOTE That if you use the "untar", or "copy" options you should ensure
  73. # that the image you’re left with matches the ‘dist’ setting later in
  74. # this file.
  75. #
  76. #
  77. ##
  78. #
  79. #
  80. # install-method = [ debootstrap | rinse | rpmstrap | copy | tar ]
  81. #
  82. #
  83. install-method = debootstrap
  84.  
  85. #
  86. # If you’re using the "copy", or "tar" installation methods you must
  87. # need to specify the source location to copy from, or the source
  88. # .tar file to unpack.
  89. #
  90. # You may specify that with a line such as:
  91. #
  92. # install-source = /path/to/copy
  93. # install-source = /some/path/img.tar
  94. #
  95. #
  96.  
  97. #
  98. ##
  99. #  Command definitions.
  100. ##
  101. #
  102. # The "rinse", and "rpmstrap" commands are hardwired into
  103. # the script, but if you wish to modify the commands which are executed
  104. # when installing new systems by a "copy", "debootstrap", or "tar" method
  105. # you can do so here:
  106. #
  107. # (This allows you to install from a .tar.bz file, rather than a plain
  108. # tar file, use cdebootstrap, etc.)
  109. #
  110. # install-method=copy:
  111. # copy-cmd = /bin/cp -a $src/* $dest
  112. ##
  113. #install-method=debootstrap:
  114. debootstrap-cmd=/usr/sbin/debootstrap
  115. #
  116. # install-method=tar:
  117. # tar-cmd  = /bin/tar –numeric-owner -xvf $src
  118. #
  119. #
  120.  
  121.  
  122.  
  123. #
  124. ##
  125. #  Disk and Sizing options.
  126. ##
  127. #
  128. size   = 4Gb      # Disk image size.
  129. memory = 256Mb    # Memory size
  130. swap   = 256Mb    # Swap size
  131. # noswap = 1      # Don’t use swap at all for the new system.
  132. fs     = ext3     # use the EXT3 filesystem for the disk image.
  133. dist   = lenny     # Default distribution to install.
  134. image  = sparse   # Specify sparse vs. full disk images.
  135.  
  136. #
  137. #  Currently supported and tested distributions include:
  138. #
  139. # via Debootstrap:
  140. #
  141. #  Debian:
  142. #   sid, sarge, etch, lenny.
  143. #
  144. #  Ubuntu:
  145. #   edgy, feisty, dapper.
  146. #
  147. # via Rinse:
  148. #   centos-4, centos-5.
  149. #   fedora-core-4, fedora-core-5, fedora-core-6, fedora-core-7
  150. #
  151. #
  152.  
  153.  
  154.  
  155. ##
  156. # Networking setup values.
  157. ##
  158.  
  159. #
  160. # Uncomment and adjust these network settings if you wish to give your
  161. # new instances static IP addresses.
  162. #
  163. # gateway   = 192.168.1.1
  164. # netmask   = 255.255.255.0
  165. # broadcast = 192.168.1.255
  166. #
  167. # Uncomment this if you wish the images to use DHCP
  168. #
  169. dhcp = 1
  170.  
  171.  
  172. ##
  173. # Misc options
  174. ##
  175.  
  176. #
  177. # Uncomment the following line if you wish to disable the caching
  178. # of downloaded .deb files when using debootstrap to install images.
  179. #
  180. # cache = no
  181. #
  182.  
  183. #
  184. # Uncomment the following line if you wish to interactively setup
  185. # a new root password for images.
  186. #
  187. passwd = 1
  188.  
  189. #
  190. # If you’d like all accounts on your host system which are not present
  191. # on the guest system to be copied over then uncomment the following line.
  192. #
  193. # accounts = 1
  194. #
  195.  
  196. #
  197. # Default kernel and ramdisk to use for the virtual servers
  198. #
  199. kernel      = /boot/vmlinuz-`uname -r`
  200. initrd      = /boot/initrd.img-`uname -r`
  201.  
  202. #
  203. #  The architecture to use when using debootstrap, rinse, or rpmstrap.
  204. #
  205. #  This is most useful on 64 bit host machines, for other systems it
  206. # doesn’t need to be used.
  207. #
  208. arch=amd64
  209. #
  210.  
  211. #
  212. # The default mirror for debootstrap to install Debian-derived distributions
  213. #
  214. mirror = http://ftp.de.debian.org/debian/
  215.  
  216. #
  217. # A mirror suitable for use when installing the Dapper release of Ubuntu.
  218. #
  219. # mirror = http://gb.archive.ubuntu.com/ubuntu/
  220.  
  221. #
  222. #  If you like you could use per-distribution mirrors, which will
  223. # be more useful if you’re working in an environment where you want
  224. # to regularly use multiple distributions:
  225. #
  226. # mirror_sid=http://ftp.us.debian.org/debian
  227. # mirror_sarge=http://ftp.us.debian.org/debian
  228. # mirror_etch=http://ftp.us.debian.org/debian
  229. # mirror_dapper=http://archive.ubuntu.com/ubuntu
  230. # mirror_edgy=http://archive.ubuntu.com/ubuntu
  231. # mirror_feisty=http://archive.ubuntu.com/ubuntu
  232. # mirror_gutsy=http://archive.ubuntu.com/ubuntu
  233.  
  234.  
  235. #
  236. #  Filesystem options for the different filesystems we support.
  237. #
  238. ext3_options   = noatime,nodiratime,errors=remount-ro
  239. ext2_options   = noatime,nodiratime,errors=remount-ro
  240. xfs_options    = defaults
  241. reiser_options = defaults
  242.  
  243. #
  244. #  Uncomment if you wish newly created images to boot once they’ve been
  245. # created.
  246. #
  247. boot = 1
  248.  
  249.  
  250. #
  251. #  If you’re using the lenny or later version of the Xen guest kernel you will
  252. # need to make sure that you use ‘hvc0′ for the guest serial device,
  253. # and ‘xvdX’ instead of ’sdX’ for serial devices.
  254. #
  255. #  You may specify the things to use here:
  256. #
  257. serial_device = hvc0 #default
  258. # serial_device = tty1
  259. #
  260. disk_device = xvda #default
  261. # disk_device = sda
  262. #
  263.  
  264.  
  265. #
  266. #  Here we specify the output directory which the Xen configuration
  267. # files will be written to, and the suffix to give them.
  268. #
  269. #  Historically xen-tools have created configuration files in /etc/xen,
  270. # and given each file the name $hostname.cfg.  If you want to change
  271. # that behaviour you may do so here.
  272. #
  273. #
  274. # output    = /etc/xen
  275. # extension = .cfg
  276. #

Nun kann mittels

xen-create-image --hostname test1 --role=udev

bequem eine neue Lenny Installation eingerichtet werden. Da wir in xen-tools.conf eingestellt haben, den neuen virtuellen Gast automatisch zu booten, sollten wir ihn auch schon mit “xm list” sehen können. Sollte etwas schief gelaufen sein, erscheint der Gast nicht und man kann mittels “xm create ” versuchen ihn manuell zu erstellen.

–role=udev ist übrigens nötig, um automatisch udev im Gast zu installieren. Ohne udev kann man sich nicht per ssh in den Gast einloggen und erhält diese Fehlermeldung:


PTY allocation request failed on channel 0
stdin: is not a tty

Desweiteren kommt man bereits nach 3-4 Gästen an das Standart-Limit der Loopback-Device von Lenny. Es empfiehlt sich in jedem Fall die Datei /etc/modprobe.d/local-loop zu editieren und folgende Zeile hinzuzufügen:


options loop max_loop=64

Ein paar weitere Basics neben “xm list” zum Management der Gäste: mit “xm top” kann die Auslastung der Gäste betrachtet werden. Es ähnelt dem Tool top, zur Anzeige der Prozessauslastung. Mit “xm start “, “xm reboot ” und “xm shutdown ” kann der Gast gestartet, gerebootet oder gestoppt werden.
Mit “xm console ” bekommt man eine direkte Kosonle um auf dem Gast zu arbeiten.

Für weitere Kommandos empfehle ich einen Blick auf den Hilfetext, “xm help” :)

3 Comments

  1. Marko sagt:

    Klasse Kurztrip zu XEN! Danke :-)

  2. Markus sagt:

    Die Seite rufe ich bei jeder neuen Erstellung einer DomU auf.

    Kurz, knapp, alles Wichtige zusammengefasst. Die beste Kurzdoku die ich bisher gefunden habe.

    Vielen Dank!

  3. Daniel sagt:

    Na bitte, da hat mir deine config doch glatt den Abend gerettet.

    Wie schon gesagt: kurz, knapp, klasse. *Daumen hoch*

Leave a Reply