You are not logged in.

#1 2020-04-04 16:33:08

scrawler
Member
Registered: 2005-06-07
Posts: 318

[solved] scripting inside a chroot from outside

Hi guys,

Is it possible to write a script that enters a chrooted environment, does something, exits, and continues? How?

I have a script that does most of what it's supposed to, but then I have to chroot manually to finish up. It would be nice to also automate the manual steps.
Thanks.

Last edited by scrawler (2020-04-05 01:43:13)

Offline

#2 2020-04-04 17:31:07

ayekat
Member
Registered: 2011-01-17
Posts: 1,385
Website

Re: [solved] scripting inside a chroot from outside

You'll notice that "entering a chroot" is actually just "running a command inside a chroot".

In many cases, you simply want to run an interactive shell, hence you do something like `chroot /dir /bin/bash`.
But you can also just run something else, e.g. copy/generate a script somewhere within that directory and then invoke it with `chroot /dir /path/to/some/script`.


{,META,RE}PKGBUILDSpacman-hacks (includes makemetapkg and remakepkg) │ dotfiles

Offline

#3 2020-04-04 17:43:45

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 23,441
Website

Re: [solved] scripting inside a chroot from outside

mount /dev/sda1 /mnt
cat <<EOF > /mnt/runme
echo hello world
EOF
chmod 0755 /mnt/runme
chroot /mnt /runme
rm /mnt/runme

This script, living outside the chroot, echos 'hello world' from inside a chroot.

EDIT: see below ... that's much better.  I've never bothered to make an automated installation script.

Last edited by Trilby (2020-04-04 17:49:12)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#4 2020-04-04 17:47:09

Head_on_a_Stick
Member
From: London
Registered: 2014-02-20
Posts: 5,384
Website

Re: [solved] scripting inside a chroot from outside

^ Simpler version:

mount /dev/sda1 /mnt
chroot /mnt /bin/bash <<END
echo hello world
END

Offline

#5 2020-04-04 18:39:42

scrawler
Member
Registered: 2005-06-07
Posts: 318

Re: [solved] scripting inside a chroot from outside

Thank you guys, I'll tell you how it works. :-)

Offline

#6 2020-04-05 01:42:30

scrawler
Member
Registered: 2005-06-07
Posts: 318

Re: [solved] scripting inside a chroot from outside

Worked great, as expected--will mark solved. Thanks again everybody.

Offline

Board footer

Powered by FluxBB