Chroot

From miki
Jump to navigation Jump to search

Boot an existing partition

Example of session

sudo mount /dev/sda6 /mnt/root
sudo mount /dev/sda2 /mnt/root/boot  # Optional - if exists
for a in dev proc sys run; do sudo mount --bind /$a /mnt/root/$a; done
#                     +++ -> run needed to recover /etc/resolv.conf
sudo chroot /mnt/root
#...
#...
#...
exit
sudo umount /mnt/root/{boot,dev,proc,sys,run} /mnt/root

Install another distro inside a distro

For instance, we can use chroot to install a 32-bit system inside a running 64-bit system. We use the schroot and debootstrap package.

Guide below comes from stackexchange.com:

Install the packages
sudo apt install schroot debootstrap
Set up schroot

Create a file etc/schroot/chroot.d/lucid32:

[lucid32]
description=Ubuntu 10.04LTS 32-bit
directory=/32
type=directory
personality=linux32
users=yourusername
groups=users,admin
Install the new distribution
mkdir /32
debootstrap --arch i386 lucid /32 http://archive.ubuntu.com/ubuntu
cp -p /etc/apt/apt.conf /32/etc/apt/      # for proxy settings
cp -p /etc/apt/sources.list /32/etc/apt/  # for universe, security, etc
cp -p /etc/environment /32/etc/           # for proxy and locale settings
cp -p /etc/sudoers /32/etc/               # for custom sudo settings
ln -s /proc/mounts /32/etc/mtab

With the directory type, schroot will perform bind mounts of a number of directories, i.e. those directories will be shared with the parent installation: /proc, /dev, /home, /tmp.

Services in the chroot

We setup a policy to prevent services to start on installation. Create the file /32/usr/sbin/policy-rc.d and make it executable (chmod a+rx /32/usr/sbin/policy-rc.d):

#!/bin/sh
## Don't start any service if running in a chroot.
## See /usr/share/doc/sysv-rc/README.policy-rc.d.gz
if [ "$(stat -c %d:%i /)" != "$(stat -c %d:%i /proc/1/root/.)" ]; then
exit 101
fi
Populate the new system
schroot -c lucid32
sudo apt-get update
apt-get install lsb-core nano
...

Generate a few locale:

locale-gen en_US en_US.utf8

Schroot

An extension of chroot that allows to run as unprivileged users in a chroot. See Schroot on debian.

Example of configuration /etc/schroot/chroot.d/stretch:

# schroot chroot definitions.
# See schroot.conf(5) for complete documentation of the file format.
#
# Please take note that you should not add untrusted users to
# root-groups, because they will essentially have full root access
# to your system.  They will only have root access inside the chroot,
# but that's enough to cause malicious damage.
#
# The following lines are examples only.  Uncomment and alter them to
# customise schroot for your needs, or create a new entry from scratch.
#
[stretch]
description=To run Hadean-lands and test schroot
#aliases=test
type=directory
directory=/srv/chroot/stretch
users=peetersm
root-groups=root
profile=desktop
personality=linux
preserve-environment=true

To install the schroot:

sudo apt-get install binutils debootstrap schroot
sudo mkdir /srv/chroot/stretch/
sudo debootstrap stretch /srv/chroot/stretch/

Then we can enter the schroot system as root with:

sudo schroot -c stretch

# for instance we can install X system (xfce4, gdm3)
apt install gdm3
exit

Now we can enter as normal user:

xhost +
schroot -c stretch
Troubleshoot - cannot write in /dev/shm
I simply applied the fix, ie sudo chmod 1777 dev/shm. See also [1].
[511:1231/162459:FATAL:shared_memory_posix.cc(295)] This is frequently caused by incorrect permissions on /dev/shm.  Try 'sudo chmod 1777 /dev/shm' to fix.

Xchroot

xchroot. Yet another variant. Don't know the difference with schroot / chroot