Shell Script that does chroot and execute commands in chroot
Asked Answered
R

2

21

If in shell script I write

chroot /home/mayank/chroot/codebase
cd SBC

when I run this shell script It does go in the chroot but does not execute the command cd SBC, when I exit chroot then it executes cd SBC.

How can I achieve something that does chroot and execute commands in chroot through shell script ??

Rhapsody answered 12/7, 2018 at 12:28 Comment(5)
try chroot /home/whatever "COMMAND" chroot can have commandPonder
chroot /home/whatever "COMMAND" I even tried that, what actually happened was, it didn't go to chroot, but executed that "COMMAND" command successfully ..Rhapsody
Moreover, I have many commands, that is to be run under chroot There must be some way ???Rhapsody
COMMAND can execute a script in the chroot.Wershba
Possible duplicate of Pass commands as input to another command (su, ssh, sh, etc)Clicker
W
23

When you run chroot without telling it what to do, it will try to start chrooted interactive shell session. So your script would "pause" at that point and when you are done with that interactive shell session, it continues out of chroot again.

One of the quick and dirt options would be to abuse here-document, like this:

chroot /home/mayank/chroot/codebase /bin/bash <<"EOT"
cd /tmp/so
ls -l
echo $$
EOT

Which takes all lines up to EOT and feeds them into bash started through chroot. Those double quotes around "EOT" should ensure bash passes the content not trying to expand variables and such. Hence that echo $$ should be PID of the inner chrooted bash.

Weiland answered 12/7, 2018 at 18:19 Comment(8)
Thereis exist way to expand some variables?Conventionality
@AndreyReeshkov You can use <<EOT without double quotes as mentioned, but then the usual processing (parameter expansion, command substitution, arithmetic expansion) takes place in calling shell and you must escape all that is to be passed literally into the child process.Weiland
Nice post! I also have the same problem and I also found another way with python interaction, FYI. #9674230Whiff
@AndreyReeshkov chroot MYVAR=test bash <<"EOT"\necho $MYVAR\nEOTTheta
@Theta Not working. bash: MYVAR=test: No such file or directory. Please, clarify the command.Waterproof
@OndrejK. and you must escape all that is to be passed literally into the child process - Please, elaborate on that. What does this mean?Waterproof
@AntonSamokat If you don't double qoute "EOT", the calling shell will already perform all the expansions... so in that example echo $$ would not be PID of the "inner" shell, but the calling one (already expanded / substituted before passing it). In that case, if one wanted to prevent such expansion, the sequence would have to be escape, e.g. in that case: echo \$\$. You can also check out the docs esp. the paragraph under the format.Weiland
@AntonSamokat chroot /home/mayank/chroot/codebase /usr/bin/env MYVAR=test bash ... unix.stackexchange.com/questions/224105/…Theta
R
5

somewhat I found a solution,

chroot /work3/tmp_GU/$build_env/sbcbuild/chroot ./test.sh

after chroot giving a script there is working fine for me.

test.sh present in the chroot folder. All commands in test.sh will be executed in chroot folder.

So basically giving a command after chroot

man chroot

chroot [OPTION] NEWROOT [COMMAND [ARG]...]

Rhapsody answered 13/7, 2018 at 7:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.