Emmabuntus, Ubuntu, Derivate, Linux, Open Source BackTrack, Linux, distributions, Ubuntu, derivate, securuty, forensic VirtualBox, Linux, Ubuntu, Raring Ringtail synaptic, Ubuntu, Linux, software packages jwplayer, multimedia, Linux, Ubuntu, flash Meshlab, graphic, software, Ubuntu, open source, Linux Synapse, Linux, Ubuntu, raring, Quantal Gimp, Ubuntu, Linux FreeMind, Linux, open source Linux, infographic, history

DebootStrap is a tool which will install a Debian base system into a subdirectory of another, already installed system.

DebootStrap is a tool which will install a Debian base system into a subdirectory of another, already installed system.

It doesn't require an installation CD, just access to a Debian repository. It can also be installed and run from another operating system, so, for instance, you can use debootstrap to install Debian onto an unused partition from a running Gentoo system. It can also be used to create a rootfs for a machine of a different architecture.

This is "cross-debootstrapping". There is also a largely equivalent version written in C: cdebootstrap, which is smaller, and thus used in debian-installer.

If you are interested in setting up a chroot for building Debian packages, look at pbuilder.
Debootstrap can only use one repository for its packages. If you need to merge packages from different repositories (the way apt does) to make a rootfs, or you need to automatically customise the rootfs, then use Multistrap.

Since DebianTesting and DebianUnstable change rapidly, if you are going to attempt to install DebianTesting or DebianUnstable, it is recommended that you download the latest version of debootstrap from the package page and use that. Normally it would not be wise to install a package from unstable onto stable, but in this case it's safe, since debootstrap is really just a shell script and its dependencies are minimal.

This article shows you how to use debootstrap to build a chroot environment that you can use for various needs, from trying out the latest (or even oldest) Ubuntu releases, or even working with Debian releases, to utilizing the chroot as a package building environment.

You can work anywhere - this Howto will assume you're using /var/chroot and gives a target system of HardyHeron. For other target versions replace hardy below with the first part of the release code name, dapper for DapperDrake or lucid for LucidLynx for example.

Getting and installing debootstrap.

For the least pain and gnashing of teeth, please get the Ubuntu binary packages manually by downloading from the following links with the 'wget' command which is demonstrated below:
Example: Terminal session wget-ing and installing the latest hardy debootstrap:
<span class="anchor" id="line-1"></span><i> wget http://archive.ubuntu.com/ubuntu/pool/main/d/debootstrap/debootstrap_1.0.9~hardy1_all.deb
<span class="anchor" id="line-2"></span> sudo dpkg --install debootstrap_1.0.9~hardy1_all.deb</i>

Installing and configuring schroot.

schroot is a convenient means of managing chroot environments; with this tool you can have both Dapper, Feisty and even Debian Sid chroots in your Ubuntu install, and using a chroot environment is a simple as schroot -c dapper -d Wink ;)
To get schroot working in your system, just do the following in a Terminal:
<span class="anchor" id="line-1-1"></span> <i>sudo apt-get install schroot
<span class="anchor" id="line-2-1"></span> sudo mkdir /var/chroot # Remember, we assume our chroot is here
<span class="anchor" id="line-3"></span> sudo editor /etc/schroot/schroot.conf</i>
Then append this line in /etc/schroot/schroot.conf:
<span class="anchor" id="line-1-2"></span><i>[hardy]
<span class="anchor" id="line-2-2"></span>description=Ubuntu hardy
<span class="anchor" id="line-3-1"></span>location=/var/chroot/hardy
<span class="anchor" id="line-4"></span>priority=3
<span class="anchor" id="line-5"></span>users=doko
<span class="anchor" id="line-6"></span>groups=sbuild
<span class="anchor" id="line-7"></span>root-groups=root</i>

Additional steps for 64-bit systems.

If you are running a 64bit kernel and install a 32bit chroot (architectures i386, lpia on amd64, sparc, powerpc), add the line:
<span class="anchor" id="line-1-3"></span><i> personality=linux32</i>
and install the linux32 package. This avoids prefixing each schroot command with the linux32 command.
<span class="anchor" id="line-1-4"></span> <i>aliases=dokochroot,default</i>
default is very useful as are aliases.

Setting up your chroot with debootstrap.

If you want a 32-bit chroot on amd64 add --arch i386 to this command line. If you use the chroot to build packages add --variant=buildd . Change hardy to according to your needs to dapper, edgy, feisty or leave as is for hardy chroot.
To actually install the base chroot, open a Terminal and do:
<span class="anchor" id="line-1-5"></span><i> sudo debootstrap --variant=buildd --arch i386 hardy /var/chroot/hardy http://archive.ubuntu.com/ubuntu/</i>
debootstrap will then build a HardyHeron chroot in /var/chroot/, getting the base packages in http://archive.ubuntu.com/ubuntu/, and, depending on the given additional options (in square brackets,) debootstrap will build a chroot for the given architecture and variant.
If debootstrap finishes successfully, you'll be left with a base chroot in /var/chroot, which is not suitable for nearly anything. To actually get our chroot to work and be able to, say, grab packages from the network, do the following right after debootstrap:
<span class="anchor" id="line-1-6"></span> sudo cp /etc/resolv.conf /var/chroot/hardy/etc/resolv.conf
<span class="anchor" id="line-2-3"></span> sudo cp /etc/apt/sources.list /var/chroot/hardy/etc/apt/
<span class="anchor" id="line-3-2"></span> sudo editor /var/chroot/hardy/etc/apt/sources.list
If your current distribution is different than your target distribution (i.e. you use Hardy and want a Gutsy chroot), change all of the occurrences of Hardy/Gutsy/Feisty/Edgy etc. to your target distribution.
<span class="anchor" id="line-1-7"></span><i> sudo chroot /var/chroot/hardy
<span class="anchor" id="line-2-4"></span> apt-get update
<span class="anchor" id="line-3-3"></span> apt-get --no-install-recommends install wget debconf devscripts gnupg nano  #For package-building
<span class="anchor" id="line-4-1"></span> apt-get update  #clean the gpg error message
<span class="anchor" id="line-5-1"></span> apt-get install locales dialog  #If you don't talk en_US
<span class="anchor" id="line-6-1"></span> locale-gen en_GB.UTF-8  # or your preferred locale
<span class="anchor" id="line-7-1"></span> tzselect; TZ='Continent/Country'; export TZ  #Configure and use our local time instead of UTC; save in .profile
<span class="anchor" id="line-8"></span> exit</i>
If you dont want the locale warnings in your chroot, add this to your ~/.bashrc file.
<span class="anchor" id="line-1-8"></span><i>export LANG=C </i>
You can stop here if you want a simple chroot that you use as root (sudo chroot /var/chroot). If you want to use your chroot as another user and have access to your normal /home and other directories inside the chroot, continue.

Note for Debian chroot on Ubuntu.

If you want to build a Debian chroot on an Ubuntu system you need to point it at a Debian archive:
<i><span class="anchor" id="line-1-9"></span> sudo debootstrap --arch i386 sid sid/ http://ftp.uk.debian.org/debian/</i>

Getting stuff(X/ssh-agent/ect,dbus,mounting removables,modprobe,err stuff) working automagically.

Append these lines to /etc/fstab:
<span class="anchor" id="line-1-10"></span><i>/proc /var/chroot/hardy/proc none rbind 0 0 # Can just be mounted, comments?
<span class="anchor" id="line-2-5"></span>/dev /var/chroot/hardy/dev none rbind 0 0 # Good thing to do, but not secure.
<span class="anchor" id="line-3-4"></span>/sys /var/chroot/hardy/sys none rbind 0 0 # Same as proc?
<span class="anchor" id="line-4-2"></span>/tmp /var/chroot/hardy/tmp none rbind 0 0 # This opens a lot of doors, namly X sockets are here... DRI should work assuming bits match.
<span class="anchor" id="line-5-2"></span>/home /var/chroot/hardy/home none rbind 0 0 # This is optional.  As are the others, but this is more so.
<span class="anchor" id="line-6-2"></span>/media /var/chroot/hardy/media none rbind 0 0 # Your USB stick.
<span class="anchor" id="line-7-2"></span>/lib/modules /var/chroot/hardy/lib/modules none rbind 0 0 # You may need to load modules??  Think binfmt_misc.
<span class="anchor" id="line-8-1"></span>/var/run/dbus/ /var/chroot/hardy/var/run/dbus/ none rbind 0 0 # Gnome likes this.
<span class="anchor" id="line-9"></span># Others??  /etc?</i>
Note: fstype is none options are rbind.

Loading cron/apache/daemons.

Add this(or something like it) to /etc/rc.local or your startup wherever you like:
<span class="anchor" id="line-1-11"></span><i>schroot --all -- su -c /etc/init.d/rc\ 2 -</i>

Setting up a dchroot (non-root) environment.

dchroot makes it possible to use your newly-built chroot even as a non-root user. Hence, you can configure your chroot environment in such a way that you can even use your existing /home as the chroot's /home, thereby saving you some expensive moving in between homes, as well as making package building/testing a LOT more convenient.
To do this, first fix the user and root password:
<span class="anchor" id="line-1-12"></span><i> sudo cp /etc/passwd /var/chroot/hardy/etc/
<span class="anchor" id="line-2-6"></span> sudo sed 's/\([^:]*\):[^:]*:/\1:*:/' /etc/shadow | sudo tee /var/chroot/hardy/etc/shadow
<span class="anchor" id="line-3-5"></span> sudo cp /etc/group /var/chroot/hardy/etc/
<span class="anchor" id="line-4-3"></span> sudo cp /etc/hosts /var/chroot/hardy/etc/ # avoid sudo warnings when it tries to resolve the chroot's hostname</i>
For a debian chroot, I also had to do:
<span class="anchor" id="line-1-13"></span><i>sudo sed 's/\([^:]*\):[^:]*:/\1:*:/' /etc/gshadow | sudo tee /var/chroot/hardy/etc/gshadow</i>
Then enable sudo and setup your passwords for root and the first sudo user in the admin group:
<span class="anchor" id="line-1-14"></span><i> sudo cp /etc/sudoers /var/chroot/hardy/etc/
<span class="anchor" id="line-2-7"></span> sudo chroot /var/chroot/hardy/
<span class="anchor" id="line-3-6"></span> dpkg-reconfigure passwd
<span class="anchor" id="line-4-4"></span> passwd <username of your first ubuntu user in the admin group></i>
Next, install the sudo package to be able to use it being in chroot:
<span class="anchor" id="line-1-15"></span><i> apt-get install sudo
<span class="anchor" id="line-2-8"></span> exit</i>
Finish things up:
<span class="anchor" id="line-1-16"></span><i> sudo editor / etc/fstab</i>
This is like the previous instructions, but different. Add these lines: (/media/cdrom is optional, of course, and you might have to create the dir in the chroot)
<span class="anchor" id="line-1-17"></span> /home           /var/chroot/hardy/home        none    bind            0       0
<span class="anchor" id="line-2-9"></span> /tmp            /var/chroot/hardy/tmp         none    bind            0       0
<span class="anchor" id="line-3-7"></span> /media/cdrom    /var/chroot/hardy/media/cdrom none    bind            0       0
<span class="anchor" id="line-4-5"></span> /dev            /var/chroot/hardy/dev         none    bind            0       0
<span class="anchor" id="line-5-3"></span> proc-chroot     /var/chroot/hardy/proc        proc    defaults        0       0
<span class="anchor" id="line-6-3"></span> devpts-chroot   /var/chroot/hardy/dev/pts     devpts  defaults        0       0
and delete these lines from before:
<span class="anchor" id="line-1-18"></span> /proc /var/chroot/hardy/proc none rbind 0 0 # Can just be mounted, comments?
<span class="anchor" id="line-2-10"></span> /dev /var/chroot/hardy/dev none rbind 0 0 # Good thing to do, but not secure.
<span class="anchor" id="line-3-8"></span> /sys /var/chroot/hardy/sys none rbind 0 0 # Same as proc?
<span class="anchor" id="line-4-6"></span> /tmp /var/chroot/hardy/tmp none rbind 0 0 # This opens a lot of doors, namly X sockets are here... DRI should work assuming bits match.
<span class="anchor" id="line-5-4"></span> /home /var/chroot/hardy/home none rbind 0 0 # This is optional.  As are the others, but this is more so.
<span class="anchor" id="line-6-4"></span> /media /var/chroot/hardy/media none rbind 0 0 # Your USB stick.
<span class="anchor" id="line-7-3"></span> /lib/modules /var/chroot/hardy/lib/modules none rbind 0 0 # You may need to load modules??  Think binfmt_misc.
<span class="anchor" id="line-8-2"></span> /var/run/dbus/ /var/chroot/hardy/var/run/dbus/ none rbind 0 0 # Gnome likes this.
Mount them:
<span class="anchor" id="line-1-19"></span><i>sudo mount -a</i>
The default bash path includes chroot information. To make this visible:
<span class="anchor" id="line-1-20"></span><i>sudo chroot /var/chroot/hardy/
<span class="anchor" id="line-2-11"></span>echo mychroot > etc/debian_chroot
<span class="anchor" id="line-3-9"></span>exit</i>
Set the chroot you just created in the dchroot.conf file
<span class="anchor" id="line-1-21"></span><i>sudo editor /etc/dchroot.conf</i>
Add the following to this file (if this is your first "dchroot" it will be a new, empty file; if there is more than one, the first item listed will be the default):
<span class="anchor" id="line-1-22"></span><i>mychroot /var/chroot/hardy/</i>
Now when you want to use your chroot (you may omit the -c mychroot if there's only one, or you just want the first one in the file). The -d parameter means that your environment will be preserved, this is generally useful if you want chrooted applications to seamlessly use your X server, your session manager, etc.
<span class="anchor" id="line-1-23"></span><i> dchroot -c mychroot -d</i>
Tada! Now you can switch to and from your main / and /var/chroot/, without even becoming root!

Shortcuts / Usage.

you can type dchroot -d "command" and it executes that command in the chroot.
I have this script do_chroot in /usr/local/bin:
<span class="anchor" id="line-1-24"></span><i>/usr/bin/dchroot -d "`echo $0 | sed 's|^.*/||'` $*"</i>
I had trouble with quoting in the above script. This one works better for me. ~JPKotta
<span class="anchor" id="line-1-25"></span><i>args=""
<span class="anchor" id="line-2-12"></span>for i in "$@" ; do
<span class="anchor" id="line-3-10"></span>    args="$args '$i'"
<span class="anchor" id="line-4-7"></span>    #echo $args
<span class="anchor" id="line-5-5"></span>done
<span class="anchor" id="line-6-5"></span>
<span class="anchor" id="line-7-4"></span>/usr/bin/dchroot -d -- "$0" $args</i>
Then I create a symbolic link from that to the command I want to execute in the chroot, e.g.:
<span class="anchor" id="line-1-26"></span><i>ln -s /usr/local/bin/do_chroot /usr/local/bin/firefox</i>
which will execute firefox in the chroot environment when I launch it in my normal 64 bit environment. To launch my amd64 firefox I can type /usr/bin/firefox.
Instead if you want you can just create a script for launching the 32bit firefox e.g.:
<span class="anchor" id="line-1-27"></span>dchroot -d "firefox"
put it in /usr/local/bin and add it to the gnome menu.
If you're going to start a program that only works in 32bit, first type dchroot -d and you'll be in the 32 bit environment.


Some missing points are covered on this external article: http://ornellas.apanela.com/dokuwiki/pub:multiarch.
From unknown Sun Apr 17 05:43:14 +0100 2005 From: Date: Sun, 17 Apr 2005 05:43:14 +0100 Subject: Using symlinks for passwd, groups, shadow, etc..? Message-ID: <20050417054314+0100@https://www.ubuntulinux.org>
Wouldn't it be possible to use symlinks for the files that get copied into the chroot? Like /etc/hosts? Would it work with /etc/passwd and the like?
<span class="anchor" id="line-1-28"></span>  Re: You can link into, but not outof a chroot.
<span class="anchor" id="line-2-13"></span>mv /etc/hosts /chroot/etc/hosts
<span class="anchor" id="line-3-11"></span>ln -s ../chroot/etc/hosts /etc
<span class="anchor" id="line-4-8"></span>... Using hardlinks is better.
From MichaelShigorin Sun Apr 17 13:42:38 +0100 2005 From: Michael Shigorin Date: Sun, 17 Apr 2005 13:42:38 +0100 Subject: nope Message-ID: <20050417134238+0100@https://www.ubuntulinux.org>
...but you can mount --bind them one be one. Smile :)
From goofrider Thu May 12 19:26:45 +0100 2005 From: goofrider Date: Thu, 12 May 2005 19:26:45 +0100 Subject: chroot and symlinks Message-ID: <20050512192645+0100@https://www.ubuntulinux.org>
You can't symlinks from inside the chroot to somewhere outside of it, because once you chroot into it, the new chroot will becomes /, and all symlinks will be resolved relative to this new /. Use mount --bind instead (though hard links should work too). --GoofRider 2005-05-12
From Sam Fri May 13 09:22:44 +0100 2005 From: Sam Date: Fri, 13 May 2005 09:22:44 +0100 Subject: mount -a Message-ID: <20050513092244+0100@www.ubuntulinux.org>
You can use $ sudo mount -a for mounting all the entries in fstab instead of mounting them one by one.
From LukaszStelmach Sun May 15 00:06:59 +0100 2005 From: Lukasz Stelmach Date: Sun, 15 May 2005 00:06:59 +0100 Subject: Using symlinks Message-ID: <20050515000659+0100@www.ubuntulinux.org>
You can make hardlink to files (but only when your chroot dir is on te same partition):
ln /etc/passwd /var/chroot/etc/
From: Elmo, 21.12.05 Does anyone know howto enable DRI from inside a 32bit chroot, 'cause if I mount --bind /dev/dri chroot/dev/dri I get the following error: "DDX driver parameter mismatch: got 848 bytes, but expected 840 bytes. libGL error: InitDriver failed" (glxinfo) I'd really like to get doom3 working on my amd64 install.
26.12.05, Elmo: I know, it should work natively, but I have problems with other games aswell, so getting dri working from a chroot would be great=)
26.12.05, Elmo: At debian-amd64 list(http://lists.debian.org/debian-amd64/2005/02/msg00807.html), around February 05, is said that it's not possible at the moment. Got to find another way around my problem, will propably post to ubuntu forums.
10.06.06 Just a note from a person who ruined his system: After all this is done do not go and delete things from /var/chroot willy-nilly as it will delete the files from the linked directory as well. I found this out only after my entire /home directory was wiped out when I tried to free up some disk space by deleting the files from the chroot directory. Thanks to my foolishness I emptied root's trash before I realized what I'd done. It's been a while since my last backup so I lost everything from Documents, etc for the last year or so.
From: Murray Cumming 06.10.05: I had to do "apt-get install language-pack-en" to avoid the "Locale not supported by C library." warnings. Even "sudo dpkg-reconfigure locales" gave a "perl: warning: Setting locale failed." error until I did this. And that was even after I did a whole "sudo apt-get ubuntu-desktop" in the chroot.
Almost all the schroot config is unhelpful and irrelevant - Adding three lines to schroot.conf completely removes the need to copy anything from/to /etc:
<span class="anchor" id="line-1-29"></span><i>run-setup-scripts=true
<span class="anchor" id="line-2-14"></span>run-exec-scripts=true
<span class="anchor" id="line-3-12"></span>type=directory</i>
these will cause schroot itself to copy the latest versions of the required files every time, and do all required mounting to get /proc and /home working. Removes a LOT of effort and worry. And removes the risk of deleting your own home area due to stray bind mounts. -- directhex, 2007-09-21
<span class="anchor" id="line-1-30"></span> Re: This is the best method.
<span class="anchor" id="line-2-15"></span>I see there are a few things missing from these scripts, the rbind(bind) stuff, ect.  We should identify what is missing and try and get setup scripts to cover these areas.
The dchroot stuff here is practically obsolete. I found that it is completely possible to create a working schroot environment that does not make an individual root. Also the default setup appears to work. I tried it out when I messed up my ubuntu server install. Now my setup is relatively safe. None of the fstab stuff is required at all. I may actually create a wiki page to help out for schroot in non-root setups.

Installing and configuring dchroot (deprecated).

This section formerly appeared before the debootstrap section above. The following is here merely for reference. This use of dchroot is deprecated (no longer preferred), so you should probably use schroot as described above.
dchroot is a convenient means of managing chroot environments; with this tool you can have both Dapper, Feisty and even Debian Sid chroots in your Ubuntu install, and using a chroot environment is a simple as dchroot -c dapper -d Wink ;)
To get it dchroot working in your system, just do the following in a Terminal:
<span class="anchor" id="line-1-31"></span><i> sudo apt-get install dchroot
<span class="anchor" id="line-2-16"></span> sudo mkdir -p /var/chroot/hardy # Remember, we assume our chroot is here
<span class="anchor" id="line-3-13"></span> sudo editor /etc/dchroot.conf</i>
Then append this line in /etc/dchroot.conf:
mychroot /var/chroot/hardy

Custom Search
Adserver           610x250

If you liked this article, subscribe to the feed by clicking the image below to keep informed about new contents of the blog:
Share on Google Plus

About Hugo Repetto

Ubuntu is a Linux distribution that offers an operating system predominantly focused on desktop computers but also provides support for servers. Based on Debian GNU / Linux, Ubuntu focuses on ease of use, freedom in usage restriction, regular releases (every 6 months) and ease of installation.
    Blogger Comment
    Facebook Comment


Post a Comment