Hi Folks,
I read this here, while a stumbled on similar questions. As I'm able to look at over 30 year Unix experience, I've written a bigger script to address the problem to manage several chroot containers with different setups and multiple invocations of same chroot, and automatic setup/clean when required (or manual if desired):
It is a script called
chroot-commands with multiple operations:
- default - to set a default chroot to use in other commands
- select - to select the default chroot from a list of known setups
- show - show the current default chroot name
- setup - invoke manual setup of chroot container
- clean - to clean setup of a chroot container
- go - to start a shell or program in a chroot container
The setup for the chroot containers are specified using configuration files in a directory, e.g.
Code: Select all
# *****************************************************************************
# x86_64-uclibc.setup - cClibc based Gentoo container for x86_64
# *****************************************************************************
# The chroot base directory and the mount source (device) names are expanded
# in the host shell environment. This allows tilde and variable expansions.
# set the chroot base directory
base = ~systems/x86_64-uclibc
# set the chroot shell program
shell = /bin/bash
# set the chroot search PATH value
path = /usr/sbin:/sbin:/usr/bin:/bin
# mount the essential virtual file systems
mount proc - - virtual -tproc
mount sys - - virtual -tsysfs
mount dev - - /dev -obind
mount dev/pts - - virtual -tdevpts -ogid=5
# bind tmp and var/tmp to host file system directories
mount tmp - - /tmp -obind
mount var/tmp - - /var/tmp -obind
# setup run directory on a new tmpfs
mount run root:root 0751 virtual -ttmpfs -osize=16M
mkdir run/lock root:root 1777
mkdir run/shm root:root 1777
mkdir run/mnt root:root 0755
# setup other mount points
mkdir mnt/portage root:root 0755
...
(just a partial example as I've setup a chroot container, you are free to specify your own setup).
On first container invocation, the setup configuration is parsed and the required operations are invoked (including error checking). More invocations of the same container just bypass the setup step and go straight to the chroot, but are registered (counted) in a directory (/run/chroot). When the last invocation exits the container, the setup configuration is parsed in reverse order, doing the appropriate reverse operation (umount). In case you don't want automatic setup/clean with single but reoccurring invocations, the setup can be locked manual and unlocked afterwards. Container clean is invoked when there are no more chroot invocations and not manually locked.
The list of setup operations is currently limited to make directory, make mountpoint directory, and mount. The script itself is designed to be easily extendable, so more setup/clean operations may be added on request. It runs with /bin/dash (so shall be compatible to other shells), uses only a small set of standard utilities (all provided by e.g. Busybox), and is not dedicated to Gentoo chroot containers.
If there is interest, i'm willing to provide this script to others. Please tell me, how I can distribute this here.
Harald