Chroot

From miki
Revision as of 06:30, 30 November 2016 by Mip (talk | contribs) (Created page with "== Boot an existing partition == Example of session <source lang=bash> sudo mount /dev/sda6 /mnt/root sudo mount /dev/sda2 /mnt/root/boot # Optional - if exists for a in dev...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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