9

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.

3
  • It's rather confusing for someone seeking information, to use a directory called "chroot". Suggest "chroot-dir" ?
    – MikeW
    Feb 21, 2019 at 11:42
  • @MikeW in original tutorual it's called "edit" though I will rename it to chroot-dir as you suggest, writing a bash script just like as an author of the question. Don't know whether author already has a script and can share with us. Anyway , I will share Nov 20, 2021 at 23:17
  • Here is what I have done so far. Fully working solution, it might be better, but anyway it saved me tons of time. Making LiveCD takes roughly 15 minutes github.com/nuriyevn/UbuntuLiveCDCustomization Nov 21, 2021 at 6:59

5 Answers 5

16

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
  • Very handy, nice!
    – Joril
    Jul 22, 2016 at 10:04
  • 1
    This is a great solution if you don't want to create more than one script
    – musicin3d
    Dec 16, 2018 at 18:23
15

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.

4

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:

1

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
  • This looks promising, I've edited the question a little to clarify that I wanted a scripted solution. Let me test this and get report back. Nov 19, 2014 at 9:16
-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
  • Yeah, I'm looking to script this so it's a repeatable process, not type things in. Nov 19, 2014 at 9:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .