btoll / chroot

Easily setup a chroot
6 stars 0 forks source link

chroot

What's this all about?

Install a minimal Debian build in a chroot environment!

Here's a fine article and a gripping read! On Running a Tor Onion Service in a Chroot

That's great, but why?

Creating a chroot by hand is cumbersome and error-prone. This tool removes the headache by installing a rootfs for you.

Version support

Dependencies

Usage

Usage: ./install.sh [args]

Args:
-c, --chroot   : The name of the chroot jail.
-d, --dir      : The directory in which to install the chroot (defaults to /srv/chroot).
-t, --type     : The name of the type of the chroot. Defaults to 'plain'.
-u, --user     : The name of the chroot user. Must be a user on the host machine.
-g, --group    : The name of the chroot group. Must be a group on the host machine.
-p, --profile  : The name of the chroot group. Must be a group on the host machine.
-r, --release  : The Debian release that will be bootstrapped in the jail:
      - jessie    (8)
      - stretch   (9)
      - buster   (10)
      - bullseye (11)
      - bookworm (12)
--32           : Set this flag if the chroot is to be 32-bit on a 64-bit system.
--dry-run      : Write the config to STDOUT and exit (will not run the program).
-h, --help     : Show usage.

Q. What happens if the user and/or group provided isn't one on the host system?

A. You'll be locked out!

schroot options

The schroot definitions are placed in /etc/schroot/chroot.d by default. They are simple INI files, and I'll briefly describe only the key/value pairs that the wrapper tool uses (there are others).

Note that not all of the keys in the chroot definition below (i.e., what is written as an INI file to /etc/schroot/chroot.d/$CHROOT_NAME maps to a configurable option in the wrapper tool.

For example, there are currently CLI arguments for personality, root-user and group-user, et al, but values for those keys may be set depending upon other CLI arguments.

Read the docs and view the shell script code, it's easy to follow and understand!

Some definitions are taken directly from the schroot.conf(5) man page.

Please read the schroot.conf(5) man page for complete coverage of all of the schroot options.

The wrapper tool only uses a subset of the options that schroot makes available. If you need more, then your use case perhaps exceeds what this script is trying to accomplish, which is to get a chroot bootstrapped quickly for general use cases.

schroot profiles

schroot profiles are a nice feature that allows chroots to be more or less bootstrapped on creation. It controls the files that are copied from the host, what filesystems are mounted and which system databases (like /etc/passwd) to copy into the chroot from the host.

For example, to have a minimum chroot environment with just the base OS, use the 'plain' type (type=plain), or omit it as it's the default. This value doesn't trigger schroot to copy any files or mount any filesystems into the chroot environment.

Another example is that of type=directory which has schroot copy files and mount filesystems into the host. The only question that remains is which profile will be used as the template.

This is important as it will expose more of the host into the chroot environment. For example, a nice configuration for running a web server is to use type=directory and profile=minimal, which, as its name implies, will only mount two filesystems (one being /proc) and copy a conservative number of files and databases into the chroot.

There are other profiles, such as desktop, which are more liberal in what they copy and mount, and may serve other use cases quite well.

Though perhaps not technically accurate, I think of type=directory as "turning on" the ability to copy files and mount filesystems, and of profiles as a use case, where some may fit your project better than others.

Although schroot supports the use of custom scripts, the wrapper tool (currently) does not.

Create the chroot

Run install.sh.

The script will do the following:

Make sure to read the schroot(1) and schroot.conf(5) man pages! I've left out a lot of detail here!

That's it, you're done! You can now change (root) to your new chroot by issuing the following commmand:

schroot --directory / -u $CHROOT_USER -c $CHROOT_NAME