scripting chroot, how to?

So I'm looking at:

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.

3

5 Answers

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.

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 
2

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:

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.

1

I think it's not saying that you should put those commands into a script, but that you should type them; i.e., type the mount commands into the sudo shell.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like