scripting chroot, how to?

So I'm looking at:

https://help.ubuntu.com/community/LiveCDCustomizationFromScratch

and trying the following within a bash script:

sudo chroot chroot

mount none -t proc /proc
mount none -t sysfs /sys
mount none -t devpts /dev/pts

Running the script drops to a shell at sudo chroot chroot. When I exit that I get the expected warnings about mount needing root. Is there a way I can keep all this within one shell script?

Edit: I want this to be a repeatable process, which is why I want to script the whole thing rather than type it in time and time again.


Solution 1:

Create a second script (e.g. chroot.sh) and place it in your chroot/ folder.

Now edit the command in your original script to this:

chroot chroot/ ./chroot.sh

Now the script chroot.sh will be executed inside your chroot.

Solution 2:

you should go with simple solution like pipe:

cat << EOF | chroot chroot
#do something
EOF

Anything inside EOF is run inside your chrooted directory, you can also use sudo if you like:

cat << EOF | sudo chroot chroot
ls /
EOF

Solution 3:

The thing about chroots and /proc, /sys and /dev/pts is that these three filesystems are provided by the kernel, so they remain the same whether you mount within the chroot or from without. Indeed, you'll see, earlier on in the instructions:

sudo mount --bind /dev chroot/dev

/dev is populated by the kernel, but is not a kernel-provided filesystem, so it had to be bind-mounted. Therefore, in practice, you'll see that mounting it using bind mounts (or otherwise) before entering the chroot works just as well (assume sudo):

for i in dev proc sys dev/pts
do
    mount -o bind /$i chroot/$i
done
chroot chroot
for i in dev/pts proc sys dev
do
    umount -chroot/$i
done
# or
mount -o bind /dev chroot/dev
mount -t sysfs none chroot/sys
mount -t proc none chroot/proc
mount -t devpts none chroot/dev/pts
chroot chroot
for i in dev/pts proc sys dev
do
    umount -chroot/$i
done

Relevant reading:

  • mount dev, proc, sys in a chroot environment?
  • Which of proc, sys etc. should be bind-mounted (or not) when chrooting into a “replacement” distribution?
  • Automate chroot into broken system

Solution 4:

You could create a .bashrc script or something like it, which is appended to the chroot env's /root/.bashrc, which does all the mounting etc. Aftwerwards you restore the backed up .bashrc in /root and exit the chroot:

Main script:

#!/usr/bin/env bash
cp bashrcscript chroot/root/
if [ -a chroot/root/.bashrc ]; then
    cp chroot/root/.bashrc chroot/root/.bashrc.bak
fi
echo "./bashrcscript" >> chroot/root/.bashrc
chroot chroot/
rm chroot/root/.bashrc
rm chroot/root/bashrcscript
if [ -a chroot/root/.bashrc.bak ]; then
    mv chroot/root/.bashrc.bak chroot/root/.bashrc
fi

bashrcscript:

mount none -t proc /proc
mount none -t sysfs /sys
mount none -t devpts /dev/pts 
# Anything else you like to do

The bashrcscript will then be executed when the root console is started. Ensure it's executable.

You could even put the resolv.conf copying into the main script etc.