From Mageia wiki
Jump to: navigation, search


Drakconf multiflag.png
Other languages
English ; Français ;

Why should I use a chroot?

Often when building or rebuilding packages you will need to install various libraries and development headers, many of which you won't want on your regular day-to-day installation.

A self-contained chroot environment which can contain all needed development packages is an excellent solution. It can easily be backed up and used as a test environment as well as a build environment; all without cluttering up your main install.

And of course, if you do manage to kill the chroot environment, it's as easy to fix as restoring the backup from a tarball or even starting it again from scratch.

Creating a chroot

Note: Mageia has a tool, iurt which is able to do auto-builds using a clean chroot environment each time and also enables the ability to cross-compile to other architectures. You will require high bandwidth to use this tool efficiently.

What you need

  • Root access to a running Mageia system
  • urpmi
  • access to a Mageia mirror, either remote (http/ftp/rsync...) or local (nfs/cdrom/file...)

Installing the chroot

You need to create your working directory. (/mnt/chroot/cauldron for eg). This directory will host your chrooted Mageia system

mkdir -p /mnt/chroot/cauldron

Setup urpmi with the urpmi.addmedia command. urpmi allows you to install any version of Mageia in a chroot without modifying the urpmi configuration of your running system. We choose here to install Mageia cauldron system into the chroot. We will use the --distrib option of urpmi.addmedia which automatically configures the repository[1] for urpmi:

  • core
  • Core Updates
  • Nonfree
  • Nonfree Updates

For 32bit systems

urpmi.addmedia --distrib --urpmi-root /mnt/chroot/cauldron ftp://distrib-coffee.ipsl.jussieu.fr/pub/linux/Mageia/distrib/cauldron/i586

For 64bit systems

urpmi.addmedia --distrib --urpmi-root /mnt/chroot/cauldron ftp://distrib-coffee.ipsl.jussieu.fr/pub/linux/Mageia/distrib/cauldron/x86_64

A list of mirrors is available at https://mirrors.mageia.org/

Install a base system

The minimum required packages for an operational chroot are:

  • basesystem-minimal (required)
  • urpmi (required for self contained chroot)
  • locales-<XX> (strongly recommended): where <XX> stands for the ISO code of the locale you usually use within your running system [1] In our example, locales-fr or locales-en.

You can also install additional packages during the initial setup. For example; syslinux

urpmi --urpmi-root /mnt/chroot/cauldron basesystem-minimal urpmi locales-fr syslinux

Note that installing basesystem-minimal instead of basesystem saves space disk & installation time by not installing the kernel, the bootloader, the firmwares…

Accessing the chroot

To access the chroot;

chroot /mnt/chroot/cauldron

Inside the self-contained chroot, you can work with it as a regular system. You can exit the chroot with the exit command.

Minimal configuration

  • DNS Setup: In order to have a working network connection within our chroot, you have to fill in /mnt/chroot/cauldron/etc/resolv.conf, the dns ip address of your provider. You can simply copy your working /etc/resolv.conf into the chroot /mnt/chroot/cauldron/etc/resolv.conf
cp /etc/resolv.conf /mnt/chroot/cauldron/etc/resolv.conf
  • Mount /proc filesystem: outside the chroot, mount the /proc virtual filesystem of your running system inside your chroot
mount -o bind /proc /mnt/chroot/cauldron/proc

Building Packages

In the chroot environment, you should now install the following packages:

  • rpm: our patched version of Red Hat's.
  • rpm-build: scripts used to build packages.
  • spec-helper: a tool to minimalize the spec-files by doing automatic things such as stripping the binaries and compressing the man pages.
  • libtool: used by some configure scripts to build shared libraries.
  • rpmlint: used to check the validity of the generated rpm.

You can now follow the Packagers_RPM_tutorial guide while using the chroot environment.

Extra

Access your chrooted system via SSH

Enter into your chrooted system

chroot /mnt/chroot/cauldron

Install the openssh-server package inside your chroot

urpmi openssh-server

Change the default port (port 22), which will be used by the ssh server of your chrooted system to some other port, 2222 for example, so it doesn't conflict with the real ssh server that might be running on the host machine. You'll have to edit /etc/ssh/sshd_config

# $OpenBSD: sshd_config,v 1.74 2006/07/19 13:07:10 dtucker Exp $ # This is the sshd server system-wide configuration file. See # sshd_config(5) for more information. (...) Port 2222 (...)

Launch the ssh server of your chrooted system

systemctl start sshd

Add a user inside your chroot with useradd

useradd jdoe passwd jdoe

Exit the chroot

exit

Now, as long as the /proc filesystem is mounted inside your chroot and that your chrooted sshd server is running, you should be able to log in your chrooted system via ssh like this

{{pre|ssh jdoe@localhost -p 2222

If you don't get a shell after typing the password, you may need to add this to /etc/fstab in the chroot :

none /dev/pts devpts defaults 0 0

And then, of course, mount it.

mount /dev/pts

Access other partitions

Any other available partitions can also be shared to the chroot by binding them to a mount the same way as we did with /proc.

For example, if your /home is a separate partition;

mount -o bind /home /mnt/chroot/cauldron/home

or for external media;

mount -o bind /path/to/media /mnt/chroot/cauldron/media/external