Create a Debian VM with debootstrap

From OptionC

Table of contents

Introduction

A relatively common question for new Xen users seems to be "how do I create a domU?" (or "virtual machine" or "now that Xen is running, what do I do?") Although the number of prebuilt domUs available for download is proliferating (we have posted a sample domU that results from following the directions below), for the do-it-yourself crowd, here's an introduction to one of the world's most admired bootstrap methods (no, I'm serious). This will give you a bare-bones Debian Sarge domU, configured with a swap partition.

Getting debootstrap

If you already have a functioning Debian system, run the following as root:

apt-get install debootstrap

If you are using something else, and debootstrap is not included in your package tree, we have converted versions of the current (v 0.2.45) Debian packages available for download here (currently untested):

  • rpm (http://www.option-c.com/downloads/debootstrap-0.2.45-1.2.i386.rpm)
  • Slackware tgz (http://www.option-c.com/downloads/debootstrap-0.2.45.tgz)

If those packages become out of date, here are instructions for getting and converting the package yourself:

The step-by-step directions

The following will need to be changed to match your system:

  • PATH ("where your images reside")
  • KERNEL_VERSION ("such as 2.6.11")
  • COUNTRY ("the country code for the debian mirror you have chosen, please pick something appropriate to your needs")

Also, if you don't want to name this domU "my_debian_domU" by all means use another name.

Create image files

This creates a 300M file system for root and a 64M swap partition; adjust as needed. File-back VDBs are sparse - the "full size" of the image is not allocated at time of creation, but is used as files are written to the image. (You can also resize these images using "resize2fs", or in the case of the swap partition, just create a larger one with the same name).

dd if=/dev/zero of=/PATH/my_debian_domU.img bs=1k seek=300k count=1
dd if=/dev/zero of=/PATH/my_debian_domU_sw.img bs=1k seek=64k count=1
mkswap /PATH/my_debian_domU_sw.img
mkfs.ext3 /PATH/my_debian_domU.img

The responsorial is:

> mydebian_domU.img is not a block special device.
> Proceed anyway? (y,n) y

NOTE: You can also install directly to a partition, or use a logical volume


Do the debootstrap

Now is probably a good time to point out that debootstrap is going to download all the requisite packages (around 30 - 40M) into the target file system. If you have a slow connection, you'll probably want to go to lunch. If you have a fast connection and a short attention span, go grab a cup of tea after you run the "debootstrap" command - it's not just the download time, debootstrap needs to extract and configure all the packages. As in other parts of this howto, "COUNTRY" should be replaced with the country code of a Debian mirror. (http://www.debian.org/mirror/list)

mkdir /mnt/my_debian_domU
mount -o loop /PATH/my_debian_domU.img /mnt/my_debian_domU
debootstrap --arch i386 sarge /mnt/my_debian_domU http://ftp.COUNTRY.debian.org/debian

What have we got here?

If you didn't get any error messages, you should have a cute new (and nearly ready-to-go) Debian system. Feel free to browse around. The packages that I mentioned before were downloaded to /mnt/my_debian_domU/var/cache/apt/archives, so you can see exactly what debootstrap was doing while you waited.

apt-move and other such tools

WARNING: This section is being actively edited and is almost assured to be inaccurate

If you have a slow connection and want to be able to run debootstrap again without the download step, it is possible and relatively straightforward. You will have to choose where you want to keep your "local repository." This will assume it is on a file system that is local or locally available, and that for the most part you accept the default behaviors.

First create a location for the mirror.

mkdir -p /mirrors/debian

For starters you need to have apt-move installed.

apt-get install apt-move

The configuration file is /etc/apt-move.conf. For this simple example we are only changing three options, FILECACHE (the directory where your local cache of packages are), LISTSTATE and DIST (we really don't need to change DIST but I like to use the cute names for the debian versions)

 # default location 
 # FILECACHE=/var/cache/apt/archives
 # 
 # location per this wiki example
 FILECACHE=/mnt/my_debian_domU/var/cache/apt/archives
 
 #DIST=stable
 DIST=sarge

 #LISTSTATE=/var/lib/apt/lists
 LISTSTATE=/mnt/my_debian_domU/var/lib/apt/lists


 ## JimD: must set CONTENTS=yes and PKGCOMP=none for debootstrap to find the Packages file
 ##    Otherwise, on etch, you'll get errors like: 
 ##       E: Invalid Release file, no entry for main/binary-*/Packages
 #CONTENTS=no
 #PKGCOMP=gzip
 
 CONTENTS=yes
 PKGCOMP=none

Now we can create the local repository. The will move the files to the location picked above and create the release files.

apt-move update

At this point you can look in /mirrors/debian and you should see the normal repository file structure. It's really pretty boring and I won't give you the details but if you want to look at any other mirror (ftp://ftp.us.debian.org/debian/) you can confirm that your repository has the correct structure.




Other Information

  • I've been spending a lot of time dealing with virtual machines and bandwidth issues, and have been relying more and more on debootstrap for this. If anybody else has experiences with this I'd love to know
  • Partial Debian GNU/Linux Mirror (http://people.debian.org/~debacle/mirror/)
  • Creating a partial Debian mirror (http://willem.engen.nl/debpartmirror/)

WARNING: This section is being actively edited and is almost assured to be inaccurate

Xen-specific modifications

We start with this, because it involves copying files from the dom0 to the (currently mounted) domU. The KERNEL_VERSION of the modules you are copying over should match the KERNEL_VERSION of the kernel you will be booting. It does not need to match the version of the domO. (If you don't have the domU kernels and modules, you're SOL - j/k, this silly comment should soon be replaced with a link on that topic...)

cp -dpR /lib/modules/KERNEL_VERSION-xenU /mnt/my_debian_domU/lib/modules

If you don't follow this step, the system will still boot. The question of how much slower and how much of an issue it is to leave these in place is best left to the Xen gurus (http://lists.xensource.com/archives/html/xen-users/2005-06/msg00004.html) - I just don't like seeing the error message. However, keep in mind that there are various changes you may make over time that will recreate /lib/tls - just because you've done this now doesn't mean you won't have to do it again. In this case, let the error messages (on boot) be your guide.

mv /mnt/my_debian_domU/lib/tls /mnt/my_debian_domU/lib/tls.disabled

General Configuration

For those of you who are familiar with "chroot" I will only point out that the following step is not necessary, but it does help avoid "operator error," since it is not unheard of for somebody to forget the path and change the files on the host system by mistake (if you choose not to chroot into the system you'll need to change paths from those listed). If you aren't familiar with "chroot" and really want to be, I refer you to the repository of all knowledge, Wikipedia, and their explanation of chroot (http://en.wikipedia.org/wiki/Chroot). If you aren't familiar with "chroot" and don't want to be, just type in the commands anyway. It won't hurt anything.

chroot /mnt/my_debian_domU

Name your new baby (the default hostname is debian-sarge).

echo "my_debian_domU" > /etc/hostname

Configure apt

WARNING: Do not just cut and paste this

Debian has download mirrors all over the place (http://www.debian.org/mirror/list), and it's usually best to use one geographically close to you or at least to substitute your country code for "COUNTRY."

cat > /etc/apt/sources.list << "EOF"
# sarge/stable 
deb ftp://ftp.COUNTRY.debian.org/debian/ sarge main
deb-src ftp://ftp.COUNTRY.debian.org/debian/ sarge main
deb http://security.debian.org/ sarge/updates main
EOF


NOTE: If you have downloaded the pre-built images this is the /etc/apt/sources.list file:

# sarge/stable 
deb ftp://ftp.debian.org/debian/ sarge main
deb-src ftp://ftp.debian.org/debian/ sarge main
deb http://security.debian.org/ sarge/updates main

This will work, but please change it to a local mirror if you plan to use the image regularly.



Configure mount points (fstab)

Although Xen isn't normally as picky about device names (for example sda vs. hda) as other systems, try to keep them in sync with the configuration you are using for the domU - after all, you're picky about everything else, right? (We've specifically seen issues with swap when there is not a match with the Xen config and fstab.)

cat > /etc/fstab << "EOF"
# Begin /etc/fstab
# <file system> <mount-point>   <type>   <options>                      <dump> <pass> 
/dev/sda1          /             ext3      defaults,errors=remount-ro    0     0
/dev/sda2          swap          swap      sw                            0     0
proc               /proc         proc      defaults                      0     0
# End /etc/fstab
EOF

Configure network

Option 1: DHCP

 cat > /etc/network/interfaces << "EOF"
 # Begin /etc/network/interfaces

 # The loopback network interface
 auto lo
 iface lo inet loopback

 # The primary network interface
 auto eth0
 iface eth0 inet dhcp

 # End /etc/network/interfaces
 EOF

Option 2: Static

  • Substitute the appropriate ip information
  • MY.NAME.SERVER should be an IP address or an FQDN (http://foldoc.org/?query=fqdn) resolvable in /etc/hosts
 
 cat > /etc/network/interfaces << "EOF"
 # Begin /etc/network/interfaces
 
 # The loopback network interface
 auto lo
 iface lo inet loopback
 
 # The primary network interface
 auto eth0
 iface eth0 inet static
    address 192.168.168.100
    netmask 255.255.255.0
    gateway 192.168.168.1
 
 # End /etc/network/interfaces
 EOF

 cat "nameserver MY.NAME.SERVER" > /etc/resolv.conf

Miscellaneous

You may additionally want/need to edit these files:

/etc/hosts
/etc/host.allow
/etc/host.deny

More advanced configuration (such as adding packages) is possible at this point, but the instructions would interrupt the flow of the narrative.


If you have chrooted into the file system, first

[ctrl]+d

Otherwise simply

umount /mnt/my_debian_domU

If you want to be clean and don't think you'll need to mount this image again:

rmdir /mnt/mydebian_domU

Start the domU

I took it as a given that the base system was already running Xen. However, it never hurts to check. If you are using the default tools (xend and friends), this command...

xm info

Should result in something like...

system                 : Linux
host                   : debian-xen206
release                : 2.6.11.10-xen0
version                : #1 Mon May 30 18:47:04 CDT 2005 
machine                : i686
cores                  : 1
hyperthreads_per_core  : 1
cpu_mhz                : 896 
memory                 : 384
free_memory            : 306

If it doesn't, you should check the basics... (is the computer plugged in? have you tried rebooting? is it a full moon?)

Create the configuration file (this is not all the possible configuration options, but should be enough to start this domU)

cat > /PATH/my_debian_domU.cfg << "EOF"
kernel = "/boot/vmlinuz-KERNEL_VERSION-xenU"
memory = 64
name = "my_debian_domU"
disk = ['file:/PATH/my_debian_domU.img,sda1,w','file:/PATH/my_debian_domU_sw.img,sda2,w']
root = "/dev/sda1 ro"
EOF

Start the domU (the "-c" option starts the console with the domU, so you can watch the boot process)

xm create my_debian_domU.cfg -c

If the-powers-that-be look favourably upon you, you should now be watching what looks more or less like a normal boot process. If you get to the login prompt, log in as root with no password. You should change the root password to something other than "".

passwd

Responsorial:

Enter new UNIX password:{new_password}
Retype new UNIX password:{new_password}

At this point you should probably do a few more basics. The following commands shouldn't result in much (since this is a clean install), but they are the regular commands to make sure your system is up to date, so why not run them -it's never too early to start good habits. Plus, if you get errors, you'll know something went wrong with the configuration.

apt-get update
apt-get upgrade

That's about it. If you didn't get through the boot process, look here for links to help troubleshoot Xen


Stop or exit the domU

To halt the domU (from within the domU)

shutdown -h now

To exit the console and leave the domU running

ctrl + ]

To halt the system from the dom0

xm shutdown my_debian_domU

Troubleshooting

The most common points of failure in this process are:

  • Inability to mount the initial image on the loopback device.
    • Your running kernel needs to support the loopback device
    • The default maximum number of devices is 8 - if you exceed that you'll need to recompile the kernel or change the boot parameters
    • There needs to be enough devices in /dev/loop* - some distributions may only have one or two created. To add more:
mknod /dev/loop0 b 7 0
mknod /dev/loop1 b 7 1
mknod /dev/loop2 b 7 2
mknod /dev/loop3 b 7 3
mknod /dev/loop4 b 7 4
mknod /dev/loop5 b 7 5
mknod /dev/loop6 b 7 6
mknod /dev/loop7 b 7 7
  • Problems connecting to the Debian mirror

Since this howto describes creating a debian domU with debootstrap, if you made it through the process of creating the domU but were not able to successfully start it, more information on troubleshooting that process can be found in other areas of this wiki.

Files used

A preconfigured debian domU is available for download from our xenstuff project on sourceforge (http://sourceforge.net/project/showfiles.php?group_id=141782). The following files should be included (subject to version issues):

  1. stable_sarge.img - the root file system
  2. stable_sarge_sw.img - the swap partition
  3. stable_sarge.cfg - a sample configuration file for "xm create"; need to edit "IMG_PATH" at the top of this
  4. stable_sarge.README - random comments, primarily for people who didn't just read this howto
  5. stable_sarge.selections - output of "dpkg --get-selections" listing all installed packages

Recommended Reading